blob: 7e01bacf5cf2f567fc329116006fe1f4620e6285 [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
Alexis Hunt96d5c762009-11-21 08:43:09 +000085AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
86 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000087
Steve Naroffb8371e12007-06-09 03:39:29 +000088 AttributeList *CurrAttr = 0;
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner76c72282007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
99 return CurrAttr;
100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000113
Douglas Gregora2f49452010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner76c72282007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000124 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000125 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000126 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000131 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000132
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 }
Chris Lattner76c72282007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000148 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000149 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size(),
152 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000153 }
154 }
155 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000156 switch (Tok.getKind()) {
157 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000159 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000160 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000161 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000162 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begemanf2758702009-06-26 06:32:41 +0000163 break;
164 case tok::kw_char:
165 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000166 case tok::kw_char16_t:
167 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000168 case tok::kw_bool:
169 case tok::kw_short:
170 case tok::kw_int:
171 case tok::kw_long:
172 case tok::kw_signed:
173 case tok::kw_unsigned:
174 case tok::kw_float:
175 case tok::kw_double:
176 case tok::kw_void:
177 case tok::kw_typeof:
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
180 if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection)
181 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate 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;
188 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000189 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000190 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Steve Naroff0f2fe172007-06-01 17:11:19 +0000193 // now parse the list of expressions
194 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000195 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000196 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000197 ArgExprsOk = false;
198 SkipUntil(tok::r_paren);
199 break;
200 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000201 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000202 }
Chris Lattner76c72282007-10-09 17:33:22 +0000203 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000204 break;
205 ConsumeToken(); // Eat the comma, move to the next argument
206 }
207 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000208 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000209 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000210 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000211 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
212 ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000213 CurrAttr);
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 {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000219 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000220 0, SourceLocation(), 0, 0, CurrAttr);
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 }
229 if (EndLoc)
230 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000231 }
232 return CurrAttr;
233}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000234
Eli Friedman06de2b52009-06-08 07:21:15 +0000235/// ParseMicrosoftDeclSpec - Parse an __declspec construct
236///
237/// [MS] decl-specifier:
238/// __declspec ( extended-decl-modifier-seq )
239///
240/// [MS] extended-decl-modifier-seq:
241/// extended-decl-modifier[opt]
242/// extended-decl-modifier extended-decl-modifier-seq
243
Eli Friedman53339e02009-06-08 23:27:34 +0000244AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000246
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000248 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
249 "declspec")) {
250 SkipUntil(tok::r_paren, true); // skip until ) or ;
251 return CurrAttr;
252 }
Eli Friedman53339e02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000254 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
255 SourceLocation AttrNameLoc = ConsumeToken();
256 if (Tok.is(tok::l_paren)) {
257 ConsumeParen();
258 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
259 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman06de2b52009-06-08 07:21:15 +0000264 SourceLocation(), &ExprList, 1,
265 CurrAttr, true);
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
269 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000270 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
271 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000272 }
273 }
274 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
275 SkipUntil(tok::r_paren, false);
Eli Friedman53339e02009-06-08 23:27:34 +0000276 return CurrAttr;
277}
278
279AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
280 // Treat these like attributes
281 // FIXME: Allow Sema to distinguish between these and real attributes!
282 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000283 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
284 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000285 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
286 SourceLocation AttrNameLoc = ConsumeToken();
287 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
288 // FIXME: Support these properly!
289 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000290 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman53339e02009-06-08 23:27:34 +0000291 SourceLocation(), 0, 0, CurrAttr, true);
292 }
293 return CurrAttr;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000294}
295
Dawn Perchik335e16b2010-09-03 01:29:35 +0000296AttributeList* Parser::ParseBorlandTypeAttributes(AttributeList *CurrAttr) {
297 // Treat these like attributes
298 while (Tok.is(tok::kw___pascal)) {
299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
301 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, CurrAttr, true);
303 }
304 return CurrAttr;
305}
306
Chris Lattner53361ac2006-08-10 05:19:57 +0000307/// ParseDeclaration - Parse a full 'declaration', which consists of
308/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000309/// 'Context' should be a Declarator::TheContext value. This returns the
310/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000311///
312/// declaration: [C99 6.7]
313/// block-declaration ->
314/// simple-declaration
315/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000316/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000317/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000318/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000319/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000320/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000321/// others... [FIXME]
322///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000323Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
324 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000325 SourceLocation &DeclEnd,
326 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000327 ParenBraceBracketBalancer BalancerRAIIObj(*this);
328
John McCall48871652010-08-21 09:40:31 +0000329 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000330 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000331 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000332 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000333 if (Attr.HasAttr)
334 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
335 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000336 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000337 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000338 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000339 // Could be the start of an inline namespace. Allowed as an ext in C++03.
340 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
Sebastian Redl67667942010-08-27 23:12:46 +0000341 if (Attr.HasAttr)
342 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
343 << Attr.Range;
344 SourceLocation InlineLoc = ConsumeToken();
345 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
346 break;
347 }
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000348 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
349 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000350 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000351 if (Attr.HasAttr)
352 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
353 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000354 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000355 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000356 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000357 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000358 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000359 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000360 if (Attr.HasAttr)
361 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
362 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000363 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000364 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000365 default:
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000366 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
367 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000368 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000369
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000370 // This routine returns a DeclGroup, if the thing we parsed only contains a
371 // single decl, convert it now.
372 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000373}
374
375/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
376/// declaration-specifiers init-declarator-list[opt] ';'
377///[C90/C++]init-declarator-list ';' [TODO]
378/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000379///
380/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000381/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000382Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
383 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000384 SourceLocation &DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000385 AttributeList *Attr,
386 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000387 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000388 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000389 if (Attr)
390 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000391 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
392 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000393 StmtResult R = Actions.ActOnVlaStmt(DS);
394 if (R.isUsable())
395 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattner0e894622006-08-13 19:58:17 +0000397 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
398 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000399 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000400 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000401 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000402 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000403 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000404 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattner005fc1b2010-04-05 18:18:31 +0000407 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000408}
Mike Stump11289f42009-09-09 15:08:12 +0000409
John McCalld5a36322009-11-03 19:26:08 +0000410/// ParseDeclGroup - Having concluded that this is either a function
411/// definition or a group of object declarations, actually parse the
412/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000413Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
414 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000415 bool AllowFunctionDefinitions,
416 SourceLocation *DeclEnd) {
417 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000418 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000419 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000420
John McCalld5a36322009-11-03 19:26:08 +0000421 // Bail out if the first declarator didn't seem well-formed.
422 if (!D.hasName() && !D.mayOmitIdentifier()) {
423 // Skip until ; or }.
424 SkipUntil(tok::r_brace, true, true);
425 if (Tok.is(tok::semi))
426 ConsumeToken();
427 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000428 }
Mike Stump11289f42009-09-09 15:08:12 +0000429
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000430 // Check to see if we have a function *definition* which must have a body.
431 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
432 // Look at the next token to make sure that this isn't a function
433 // declaration. We have to check this because __attribute__ might be the
434 // start of a function definition in GCC-extended K&R C.
435 !isDeclarationAfterDeclarator()) {
436
Chris Lattner13901342010-07-11 22:42:07 +0000437 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000438 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
439 Diag(Tok, diag::err_function_declared_typedef);
440
441 // Recover by treating the 'typedef' as spurious.
442 DS.ClearStorageClassSpecs();
443 }
444
John McCall48871652010-08-21 09:40:31 +0000445 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000446 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000447 }
448
449 if (isDeclarationSpecifier()) {
450 // If there is an invalid declaration specifier right after the function
451 // prototype, then we must be in a missing semicolon case where this isn't
452 // actually a body. Just fall through into the code that handles it as a
453 // prototype, and let the top-level code handle the erroneous declspec
454 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000455 } else {
456 Diag(Tok, diag::err_expected_fn_body);
457 SkipUntil(tok::semi);
458 return DeclGroupPtrTy();
459 }
460 }
461
John McCall48871652010-08-21 09:40:31 +0000462 llvm::SmallVector<Decl *, 8> DeclsInGroup;
463 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000464 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000465 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000466 DeclsInGroup.push_back(FirstDecl);
467
468 // If we don't have a comma, it is either the end of the list (a ';') or an
469 // error, bail out.
470 while (Tok.is(tok::comma)) {
471 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000472 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000473
474 // Parse the next declarator.
475 D.clear();
476
477 // Accept attributes in an init-declarator. In the first declarator in a
478 // declaration, these would be part of the declspec. In subsequent
479 // declarators, they become part of the declarator itself, so that they
480 // don't apply to declarators after *this* one. Examples:
481 // short __attribute__((common)) var; -> declspec
482 // short var __attribute__((common)); -> declarator
483 // short x, __attribute__((common)) var; -> declarator
484 if (Tok.is(tok::kw___attribute)) {
485 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000486 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000487 D.AddAttributes(AttrList, Loc);
488 }
489
490 ParseDeclarator(D);
491
John McCall48871652010-08-21 09:40:31 +0000492 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000493 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000494 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000495 DeclsInGroup.push_back(ThisDecl);
496 }
497
498 if (DeclEnd)
499 *DeclEnd = Tok.getLocation();
500
501 if (Context != Declarator::ForContext &&
502 ExpectAndConsume(tok::semi,
503 Context == Declarator::FileContext
504 ? diag::err_invalid_token_after_toplevel_declarator
505 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000506 // Okay, there was no semicolon and one was expected. If we see a
507 // declaration specifier, just assume it was missing and continue parsing.
508 // Otherwise things are very confused and we skip to recover.
509 if (!isDeclarationSpecifier()) {
510 SkipUntil(tok::r_brace, true, true);
511 if (Tok.is(tok::semi))
512 ConsumeToken();
513 }
John McCalld5a36322009-11-03 19:26:08 +0000514 }
515
Douglas Gregor0be31a22010-07-02 17:43:08 +0000516 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000517 DeclsInGroup.data(),
518 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000519}
520
Douglas Gregor23996282009-05-12 21:31:51 +0000521/// \brief Parse 'declaration' after parsing 'declaration-specifiers
522/// declarator'. This method parses the remainder of the declaration
523/// (including any attributes or initializer, among other things) and
524/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000525///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000526/// init-declarator: [C99 6.7]
527/// declarator
528/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000529/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
530/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000531/// [C++] declarator initializer[opt]
532///
533/// [C++] initializer:
534/// [C++] '=' initializer-clause
535/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000536/// [C++0x] '=' 'default' [TODO]
537/// [C++0x] '=' 'delete'
538///
539/// According to the standard grammar, =default and =delete are function
540/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000541///
John McCall48871652010-08-21 09:40:31 +0000542Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000543 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000544 // If a simple-asm-expr is present, parse it.
545 if (Tok.is(tok::kw_asm)) {
546 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000547 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000548 if (AsmLabel.isInvalid()) {
549 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000550 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Douglas Gregor23996282009-05-12 21:31:51 +0000553 D.setAsmLabel(AsmLabel.release());
554 D.SetRangeEnd(Loc);
555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Douglas Gregor23996282009-05-12 21:31:51 +0000557 // If attributes are present, parse them.
558 if (Tok.is(tok::kw___attribute)) {
559 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000560 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000561 D.AddAttributes(AttrList, Loc);
562 }
Mike Stump11289f42009-09-09 15:08:12 +0000563
Douglas Gregor23996282009-05-12 21:31:51 +0000564 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000565 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000566 switch (TemplateInfo.Kind) {
567 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000568 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000569 break;
570
571 case ParsedTemplateInfo::Template:
572 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000573 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000574 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000575 TemplateInfo.TemplateParams->data(),
576 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000577 D);
578 break;
579
580 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000581 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000582 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000583 TemplateInfo.ExternLoc,
584 TemplateInfo.TemplateLoc,
585 D);
586 if (ThisRes.isInvalid()) {
587 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000588 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000589 }
590
591 ThisDecl = ThisRes.get();
592 break;
593 }
594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregor23996282009-05-12 21:31:51 +0000596 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000597 if (isTokenEqualOrMistypedEqualEqual(
598 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000599 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000600 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000601 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000602
603 if (!getLang().CPlusPlus0x)
604 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
605
Douglas Gregor23996282009-05-12 21:31:51 +0000606 Actions.SetDeclDeleted(ThisDecl, DelLoc);
607 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000608 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
609 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000610 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000611 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000612
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000613 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000614 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000615 ConsumeCodeCompletionToken();
616 SkipUntil(tok::comma, true, true);
617 return ThisDecl;
618 }
619
John McCalldadc5752010-08-24 06:29:42 +0000620 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000621
John McCall1f4ee7b2009-12-19 09:28:58 +0000622 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000623 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000624 ExitScope();
625 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000626
Douglas Gregor23996282009-05-12 21:31:51 +0000627 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000628 SkipUntil(tok::comma, true, true);
629 Actions.ActOnInitializerError(ThisDecl);
630 } else
John McCallb268a282010-08-23 23:25:46 +0000631 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor23996282009-05-12 21:31:51 +0000632 }
633 } else if (Tok.is(tok::l_paren)) {
634 // Parse C++ direct initializer: '(' expression-list ')'
635 SourceLocation LParenLoc = ConsumeParen();
636 ExprVector Exprs(Actions);
637 CommaLocsTy CommaLocs;
638
Douglas Gregor613bf102009-12-22 17:47:17 +0000639 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
640 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000641 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000642 }
643
Douglas Gregor23996282009-05-12 21:31:51 +0000644 if (ParseExpressionList(Exprs, CommaLocs)) {
645 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000646
647 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000648 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000649 ExitScope();
650 }
Douglas Gregor23996282009-05-12 21:31:51 +0000651 } else {
652 // Match the ')'.
653 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
654
655 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
656 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000657
658 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000659 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000660 ExitScope();
661 }
662
Douglas Gregor23996282009-05-12 21:31:51 +0000663 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
664 move_arg(Exprs),
Douglas Gregorce5aa332010-09-09 16:33:13 +0000665 RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000666 }
667 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000668 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000669 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
670 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000671 }
672
673 return ThisDecl;
674}
675
Chris Lattner1890ac82006-08-13 01:16:23 +0000676/// ParseSpecifierQualifierList
677/// specifier-qualifier-list:
678/// type-specifier specifier-qualifier-list[opt]
679/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000680/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000681///
682void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
683 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
684 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000685 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000686
Chris Lattner1890ac82006-08-13 01:16:23 +0000687 // Validate declspec for type-name.
688 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000689 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
690 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000691 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000692
Chris Lattner1b22eed2006-11-28 05:12:07 +0000693 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000694 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000695 if (DS.getStorageClassSpecLoc().isValid())
696 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
697 else
698 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000699 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
Chris Lattner1b22eed2006-11-28 05:12:07 +0000702 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000703 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000704 if (DS.isInlineSpecified())
705 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
706 if (DS.isVirtualSpecified())
707 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
708 if (DS.isExplicitSpecified())
709 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000710 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000711 }
712}
Chris Lattner53361ac2006-08-10 05:19:57 +0000713
Chris Lattner6cc055a2009-04-12 20:42:31 +0000714/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
715/// specified token is valid after the identifier in a declarator which
716/// immediately follows the declspec. For example, these things are valid:
717///
718/// int x [ 4]; // direct-declarator
719/// int x ( int y); // direct-declarator
720/// int(int x ) // direct-declarator
721/// int x ; // simple-declaration
722/// int x = 17; // init-declarator-list
723/// int x , y; // init-declarator-list
724/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000725/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000726/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000727///
728/// This is not, because 'x' does not immediately follow the declspec (though
729/// ')' happens to be valid anyway).
730/// int (x)
731///
732static bool isValidAfterIdentifierInDeclarator(const Token &T) {
733 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
734 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000735 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000736}
737
Chris Lattner20a0c612009-04-14 21:34:55 +0000738
739/// ParseImplicitInt - This method is called when we have an non-typename
740/// identifier in a declspec (which normally terminates the decl spec) when
741/// the declspec has no type specifier. In this case, the declspec is either
742/// malformed or is "implicit int" (in K&R and C89).
743///
744/// This method handles diagnosing this prettily and returns false if the
745/// declspec is done being processed. If it recovers and thinks there may be
746/// other pieces of declspec after it, it returns true.
747///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000748bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000749 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000750 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000751 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattner20a0c612009-04-14 21:34:55 +0000753 SourceLocation Loc = Tok.getLocation();
754 // If we see an identifier that is not a type name, we normally would
755 // parse it as the identifer being declared. However, when a typename
756 // is typo'd or the definition is not included, this will incorrectly
757 // parse the typename as the identifier name and fall over misparsing
758 // later parts of the diagnostic.
759 //
760 // As such, we try to do some look-ahead in cases where this would
761 // otherwise be an "implicit-int" case to see if this is invalid. For
762 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
763 // an identifier with implicit int, we'd get a parse error because the
764 // next token is obviously invalid for a type. Parse these as a case
765 // with an invalid type specifier.
766 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattner20a0c612009-04-14 21:34:55 +0000768 // Since we know that this either implicit int (which is rare) or an
769 // error, we'd do lookahead to try to do better recovery.
770 if (isValidAfterIdentifierInDeclarator(NextToken())) {
771 // If this token is valid for implicit int, e.g. "static x = 4", then
772 // we just avoid eating the identifier, so it will be parsed as the
773 // identifier in the declarator.
774 return false;
775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattner20a0c612009-04-14 21:34:55 +0000777 // Otherwise, if we don't consume this token, we are going to emit an
778 // error anyway. Try to recover from various common problems. Check
779 // to see if this was a reference to a tag name without a tag specified.
780 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000781 //
782 // C++ doesn't need this, and isTagName doesn't take SS.
783 if (SS == 0) {
784 const char *TagName = 0;
785 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000786
Douglas Gregor0be31a22010-07-02 17:43:08 +0000787 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000788 default: break;
789 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
790 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
791 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
792 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
793 }
Mike Stump11289f42009-09-09 15:08:12 +0000794
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000795 if (TagName) {
796 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000797 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000798 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000799
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000800 // Parse this as a tag as if the missing tag were present.
801 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000802 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000803 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000804 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000805 return true;
806 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000807 }
Mike Stump11289f42009-09-09 15:08:12 +0000808
Douglas Gregor15e56022009-10-13 23:27:22 +0000809 // This is almost certainly an invalid type name. Let the action emit a
810 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000811 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000812 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000813 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000814 // The action emitted a diagnostic, so we don't have to.
815 if (T) {
816 // The action has suggested that the type T could be used. Set that as
817 // the type in the declaration specifiers, consume the would-be type
818 // name token, and we're done.
819 const char *PrevSpec;
820 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000821 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000822 DS.SetRangeEnd(Tok.getLocation());
823 ConsumeToken();
824
825 // There may be other declaration specifiers after this.
826 return true;
827 }
828
829 // Fall through; the action had no suggestion for us.
830 } else {
831 // The action did not emit a diagnostic, so emit one now.
832 SourceRange R;
833 if (SS) R = SS->getRange();
834 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Douglas Gregor15e56022009-10-13 23:27:22 +0000837 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000838 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000839 unsigned DiagID;
840 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000841 DS.SetRangeEnd(Tok.getLocation());
842 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattner20a0c612009-04-14 21:34:55 +0000844 // TODO: Could inject an invalid typedef decl in an enclosing scope to
845 // avoid rippling error messages on subsequent uses of the same type,
846 // could be useful if #include was forgotten.
847 return false;
848}
849
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000850/// \brief Determine the declaration specifier context from the declarator
851/// context.
852///
853/// \param Context the declarator context, which is one of the
854/// Declarator::TheContext enumerator values.
855Parser::DeclSpecContext
856Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
857 if (Context == Declarator::MemberContext)
858 return DSC_class;
859 if (Context == Declarator::FileContext)
860 return DSC_top_level;
861 return DSC_normal;
862}
863
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000864/// ParseDeclarationSpecifiers
865/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000866/// storage-class-specifier declaration-specifiers[opt]
867/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000868/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000869/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000870///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000871/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000872/// 'typedef'
873/// 'extern'
874/// 'static'
875/// 'auto'
876/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000877/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000878/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000879/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000880/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000881/// [C++] 'virtual'
882/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000883/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000884/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000885
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000886///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000887void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000888 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000889 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000890 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000891 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +0000892 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000893 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000894 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000895 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000896 unsigned DiagID = 0;
897
Chris Lattner4d8f8732006-11-28 05:05:08 +0000898 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000899
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000900 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000901 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000902 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000903 // If this is not a declaration specifier token, we're done reading decl
904 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000905 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000906 return;
Mike Stump11289f42009-09-09 15:08:12 +0000907
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000908 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000909 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000910 if (DS.hasTypeSpecifier()) {
911 bool AllowNonIdentifiers
912 = (getCurScope()->getFlags() & (Scope::ControlScope |
913 Scope::BlockScope |
914 Scope::TemplateParamScope |
915 Scope::FunctionPrototypeScope |
916 Scope::AtCatchScope)) == 0;
917 bool AllowNestedNameSpecifiers
918 = DSContext == DSC_top_level ||
919 (DSContext == DSC_class && DS.isFriendSpecified());
920
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000921 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
922 AllowNonIdentifiers,
923 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000924 ConsumeCodeCompletionToken();
925 return;
926 }
927
928 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000929 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
930 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000931 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000932 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000933 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000934 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000935
936 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
937 ConsumeCodeCompletionToken();
938 return;
939 }
940
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000941 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000942 // C++ scope specifier. Annotate and loop, or bail out on error.
943 if (TryAnnotateCXXScopeToken(true)) {
944 if (!DS.hasTypeSpecifier())
945 DS.SetTypeSpecError();
946 goto DoneWithDeclSpec;
947 }
John McCall8bc2a702010-03-01 18:20:46 +0000948 if (Tok.is(tok::coloncolon)) // ::new or ::delete
949 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000950 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000951
952 case tok::annot_cxxscope: {
953 if (DS.hasTypeSpecifier())
954 goto DoneWithDeclSpec;
955
John McCall9dab4e62009-12-12 11:40:51 +0000956 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000957 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000958 SS.setRange(Tok.getAnnotationRange());
959
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000960 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000961 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000962 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000963 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000964 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000965 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000966
967 // C++ [class.qual]p2:
968 // In a lookup in which the constructor is an acceptable lookup
969 // result and the nested-name-specifier nominates a class C:
970 //
971 // - if the name specified after the
972 // nested-name-specifier, when looked up in C, is the
973 // injected-class-name of C (Clause 9), or
974 //
975 // - if the name specified after the nested-name-specifier
976 // is the same as the identifier or the
977 // simple-template-id's template-name in the last
978 // component of the nested-name-specifier,
979 //
980 // the name is instead considered to name the constructor of
981 // class C.
982 //
983 // Thus, if the template-name is actually the constructor
984 // name, then the code is ill-formed; this interpretation is
985 // reinforced by the NAD status of core issue 635.
986 TemplateIdAnnotation *TemplateId
987 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000988 if ((DSContext == DSC_top_level ||
989 (DSContext == DSC_class && DS.isFriendSpecified())) &&
990 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000991 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000992 if (isConstructorDeclarator()) {
993 // The user meant this to be an out-of-line constructor
994 // definition, but template arguments are not allowed
995 // there. Just allow this as a constructor; we'll
996 // complain about it later.
997 goto DoneWithDeclSpec;
998 }
999
1000 // The user meant this to name a type, but it actually names
1001 // a constructor with some extraneous template
1002 // arguments. Complain, then parse it as a type as the user
1003 // intended.
1004 Diag(TemplateId->TemplateNameLoc,
1005 diag::err_out_of_line_template_id_names_constructor)
1006 << TemplateId->Name;
1007 }
1008
John McCall9dab4e62009-12-12 11:40:51 +00001009 DS.getTypeSpecScope() = SS;
1010 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001011 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001012 "ParseOptionalCXXScopeSpecifier not working");
1013 AnnotateTemplateIdTokenAsType(&SS);
1014 continue;
1015 }
1016
Douglas Gregorc5790df2009-09-28 07:26:33 +00001017 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001018 DS.getTypeSpecScope() = SS;
1019 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001020 if (Tok.getAnnotationValue()) {
1021 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorc5790df2009-09-28 07:26:33 +00001022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallba7bf592010-08-24 05:47:05 +00001023 PrevSpec, DiagID, T);
1024 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001025 else
1026 DS.SetTypeSpecError();
1027 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1028 ConsumeToken(); // The typename
1029 }
1030
Douglas Gregor167fa622009-03-25 15:40:00 +00001031 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001032 goto DoneWithDeclSpec;
1033
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001034 // If we're in a context where the identifier could be a class name,
1035 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001036 if ((DSContext == DSC_top_level ||
1037 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001038 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001039 &SS)) {
1040 if (isConstructorDeclarator())
1041 goto DoneWithDeclSpec;
1042
1043 // As noted in C++ [class.qual]p2 (cited above), when the name
1044 // of the class is qualified in a context where it could name
1045 // a constructor, its a constructor name. However, we've
1046 // looked at the declarator, and the user probably meant this
1047 // to be a type. Complain that it isn't supposed to be treated
1048 // as a type, then proceed to parse it as a type.
1049 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1050 << Next.getIdentifierInfo();
1051 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001052
John McCallba7bf592010-08-24 05:47:05 +00001053 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1054 Next.getLocation(),
1055 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001056
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001057 // If the referenced identifier is not a type, then this declspec is
1058 // erroneous: We already checked about that it has no type specifier, and
1059 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001060 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001061 if (TypeRep == 0) {
1062 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001063 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001064 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001065 }
Mike Stump11289f42009-09-09 15:08:12 +00001066
John McCall9dab4e62009-12-12 11:40:51 +00001067 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001068 ConsumeToken(); // The C++ scope.
1069
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001071 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001072 if (isInvalid)
1073 break;
Mike Stump11289f42009-09-09 15:08:12 +00001074
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001075 DS.SetRangeEnd(Tok.getLocation());
1076 ConsumeToken(); // The typename.
1077
1078 continue;
1079 }
Mike Stump11289f42009-09-09 15:08:12 +00001080
Chris Lattnere387d9e2009-01-21 19:48:37 +00001081 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001082 if (Tok.getAnnotationValue()) {
1083 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001084 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001085 DiagID, T);
1086 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001087 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001088
1089 if (isInvalid)
1090 break;
1091
Chris Lattnere387d9e2009-01-21 19:48:37 +00001092 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1093 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattnere387d9e2009-01-21 19:48:37 +00001095 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1096 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001097 // Objective-C interface.
1098 if (Tok.is(tok::less) && getLang().ObjC1)
1099 ParseObjCProtocolQualifiers(DS);
1100
Chris Lattnere387d9e2009-01-21 19:48:37 +00001101 continue;
1102 }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattner16fac4f2008-07-26 01:18:38 +00001104 // typedef-name
1105 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001106 // In C++, check to see if this is a scope specifier like foo::bar::, if
1107 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001108 if (getLang().CPlusPlus) {
1109 if (TryAnnotateCXXScopeToken(true)) {
1110 if (!DS.hasTypeSpecifier())
1111 DS.SetTypeSpecError();
1112 goto DoneWithDeclSpec;
1113 }
1114 if (!Tok.is(tok::identifier))
1115 continue;
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner16fac4f2008-07-26 01:18:38 +00001118 // This identifier can only be a typedef name if we haven't already seen
1119 // a type-specifier. Without this check we misparse:
1120 // typedef int X; struct Y { short X; }; as 'short int'.
1121 if (DS.hasTypeSpecifier())
1122 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001123
John Thompson22334602010-02-05 00:12:22 +00001124 // Check for need to substitute AltiVec keyword tokens.
1125 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1126 break;
1127
Chris Lattner16fac4f2008-07-26 01:18:38 +00001128 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001129 ParsedType TypeRep =
1130 Actions.getTypeName(*Tok.getIdentifierInfo(),
1131 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001132
Chris Lattner6cc055a2009-04-12 20:42:31 +00001133 // If this is not a typedef name, don't parse it as part of the declspec,
1134 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001135 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001136 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001137 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001138 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001139
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001140 // If we're in a context where the identifier could be a class name,
1141 // check whether this is a constructor declaration.
1142 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001143 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001144 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001145 goto DoneWithDeclSpec;
1146
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001147 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001148 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001149 if (isInvalid)
1150 break;
Mike Stump11289f42009-09-09 15:08:12 +00001151
Chris Lattner16fac4f2008-07-26 01:18:38 +00001152 DS.SetRangeEnd(Tok.getLocation());
1153 ConsumeToken(); // The identifier
1154
1155 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1156 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001157 // Objective-C interface.
1158 if (Tok.is(tok::less) && getLang().ObjC1)
1159 ParseObjCProtocolQualifiers(DS);
1160
Steve Naroffcd5e7822008-09-22 10:28:57 +00001161 // Need to support trailing type qualifiers (e.g. "id<p> const").
1162 // If a type specifier follows, it will be diagnosed elsewhere.
1163 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001164 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001165
1166 // type-name
1167 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001168 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001169 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001170 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001171 // This template-id does not refer to a type name, so we're
1172 // done with the type-specifiers.
1173 goto DoneWithDeclSpec;
1174 }
1175
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001176 // If we're in a context where the template-id could be a
1177 // constructor name or specialization, check whether this is a
1178 // constructor declaration.
1179 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001180 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001181 isConstructorDeclarator())
1182 goto DoneWithDeclSpec;
1183
Douglas Gregor7f741122009-02-25 19:37:18 +00001184 // Turn the template-id annotation token into a type annotation
1185 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001186 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001187 continue;
1188 }
1189
Chris Lattnere37e2332006-08-15 04:50:22 +00001190 // GNU attributes support.
1191 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001192 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001193 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001194
1195 // Microsoft declspec support.
1196 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001197 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001198 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001199
Steve Naroff44ac7772008-12-25 14:16:32 +00001200 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001201 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001202 // FIXME: Add handling here!
1203 break;
1204
1205 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001206 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001207 case tok::kw___cdecl:
1208 case tok::kw___stdcall:
1209 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001210 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001211 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1212 continue;
1213
Dawn Perchik335e16b2010-09-03 01:29:35 +00001214 // Borland single token adornments.
1215 case tok::kw___pascal:
1216 DS.AddAttributes(ParseBorlandTypeAttributes());
1217 continue;
1218
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001219 // storage-class-specifier
1220 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001221 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1222 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001223 break;
1224 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001225 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001226 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001227 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1228 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001229 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001230 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001231 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001232 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001233 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001234 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001235 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001236 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001237 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1238 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001239 break;
1240 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001241 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001242 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1243 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001244 else
John McCall49bfce42009-08-03 20:12:06 +00001245 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1246 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001247 break;
1248 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001249 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1250 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001251 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001252 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001253 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1254 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001255 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001256 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001257 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001258 break;
Mike Stump11289f42009-09-09 15:08:12 +00001259
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001260 // function-specifier
1261 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001262 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001263 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001264 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001265 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001266 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001267 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001268 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001269 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001270
Anders Carlssoncd8db412009-05-06 04:46:28 +00001271 // friend
1272 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001273 if (DSContext == DSC_class)
1274 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1275 else {
1276 PrevSpec = ""; // not actually used by the diagnostic
1277 DiagID = diag::err_friend_invalid_in_context;
1278 isInvalid = true;
1279 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001280 break;
Mike Stump11289f42009-09-09 15:08:12 +00001281
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001282 // constexpr
1283 case tok::kw_constexpr:
1284 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1285 break;
1286
Chris Lattnere387d9e2009-01-21 19:48:37 +00001287 // type-specifier
1288 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1290 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001291 break;
1292 case tok::kw_long:
1293 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001294 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1295 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001296 else
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1298 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001299 break;
1300 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1302 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303 break;
1304 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
1308 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1314 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001315 break;
1316 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1318 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001319 break;
1320 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1322 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001323 break;
1324 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1326 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001327 break;
1328 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1330 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001331 break;
1332 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1334 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1338 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001339 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001340 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1342 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001343 break;
1344 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1346 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001347 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001348 case tok::kw_bool:
1349 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1351 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001352 break;
1353 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1355 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001356 break;
1357 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001358 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1359 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001360 break;
1361 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001362 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1363 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001364 break;
John Thompson22334602010-02-05 00:12:22 +00001365 case tok::kw___vector:
1366 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1367 break;
1368 case tok::kw___pixel:
1369 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1370 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001371
1372 // class-specifier:
1373 case tok::kw_class:
1374 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001375 case tok::kw_union: {
1376 tok::TokenKind Kind = Tok.getKind();
1377 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001378 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001379 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001380 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001381
1382 // enum-specifier:
1383 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001384 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001385 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001386 continue;
1387
1388 // cv-qualifier:
1389 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001390 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1391 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001392 break;
1393 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001394 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1395 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001396 break;
1397 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001398 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1399 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001400 break;
1401
Douglas Gregor333489b2009-03-27 23:10:48 +00001402 // C++ typename-specifier:
1403 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001404 if (TryAnnotateTypeOrScopeToken()) {
1405 DS.SetTypeSpecError();
1406 goto DoneWithDeclSpec;
1407 }
1408 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001409 continue;
1410 break;
1411
Chris Lattnere387d9e2009-01-21 19:48:37 +00001412 // GNU typeof support.
1413 case tok::kw_typeof:
1414 ParseTypeofSpecifier(DS);
1415 continue;
1416
Anders Carlsson74948d02009-06-24 17:47:40 +00001417 case tok::kw_decltype:
1418 ParseDecltypeSpecifier(DS);
1419 continue;
1420
Steve Naroffcfdf6162008-06-05 00:02:44 +00001421 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001422 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001423 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1424 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001425 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001426 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001427
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001428 ParseObjCProtocolQualifiers(DS);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001429
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001430 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1431 << FixItHint::CreateInsertion(Loc, "id")
1432 << SourceRange(Loc, DS.getSourceRange().getEnd());
1433
1434 // Need to support trailing type qualifiers (e.g. "id<p> const").
1435 // If a type specifier follows, it will be diagnosed elsewhere.
1436 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001437 }
John McCall49bfce42009-08-03 20:12:06 +00001438 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001439 if (isInvalid) {
1440 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001441 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001442
1443 if (DiagID == diag::ext_duplicate_declspec)
1444 Diag(Tok, DiagID)
1445 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1446 else
1447 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001448 }
Chris Lattner2e232092008-03-13 06:29:04 +00001449 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001450 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001451 }
1452}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001453
Chris Lattnera448d752009-01-06 06:59:53 +00001454/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001455/// primarily follow the C++ grammar with additions for C99 and GNU,
1456/// which together subsume the C grammar. Note that the C++
1457/// type-specifier also includes the C type-qualifier (for const,
1458/// volatile, and C99 restrict). Returns true if a type-specifier was
1459/// found (and parsed), false otherwise.
1460///
1461/// type-specifier: [C++ 7.1.5]
1462/// simple-type-specifier
1463/// class-specifier
1464/// enum-specifier
1465/// elaborated-type-specifier [TODO]
1466/// cv-qualifier
1467///
1468/// cv-qualifier: [C++ 7.1.5.1]
1469/// 'const'
1470/// 'volatile'
1471/// [C99] 'restrict'
1472///
1473/// simple-type-specifier: [ C++ 7.1.5.2]
1474/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1475/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1476/// 'char'
1477/// 'wchar_t'
1478/// 'bool'
1479/// 'short'
1480/// 'int'
1481/// 'long'
1482/// 'signed'
1483/// 'unsigned'
1484/// 'float'
1485/// 'double'
1486/// 'void'
1487/// [C99] '_Bool'
1488/// [C99] '_Complex'
1489/// [C99] '_Imaginary' // Removed in TC2?
1490/// [GNU] '_Decimal32'
1491/// [GNU] '_Decimal64'
1492/// [GNU] '_Decimal128'
1493/// [GNU] typeof-specifier
1494/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1495/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001496/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001497/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001498bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001499 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001500 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001501 const ParsedTemplateInfo &TemplateInfo,
1502 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001503 SourceLocation Loc = Tok.getLocation();
1504
1505 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001506 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001507 // If we already have a type specifier, this identifier is not a type.
1508 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1509 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1510 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1511 return false;
John Thompson22334602010-02-05 00:12:22 +00001512 // Check for need to substitute AltiVec keyword tokens.
1513 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1514 break;
1515 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001516 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001517 // Annotate typenames and C++ scope specifiers. If we get one, just
1518 // recurse to handle whatever we get.
1519 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001520 return true;
1521 if (Tok.is(tok::identifier))
1522 return false;
1523 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1524 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001525 case tok::coloncolon: // ::foo::bar
1526 if (NextToken().is(tok::kw_new) || // ::new
1527 NextToken().is(tok::kw_delete)) // ::delete
1528 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001529
Chris Lattner020bab92009-01-04 23:41:41 +00001530 // Annotate typenames and C++ scope specifiers. If we get one, just
1531 // recurse to handle whatever we get.
1532 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001533 return true;
1534 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1535 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001536
Douglas Gregor450c75a2008-11-07 15:42:26 +00001537 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001538 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001539 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001540 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001541 DiagID, T);
1542 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001543 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001544 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1545 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001546
Douglas Gregor450c75a2008-11-07 15:42:26 +00001547 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1548 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1549 // Objective-C interface. If we don't have Objective-C or a '<', this is
1550 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001551 if (Tok.is(tok::less) && getLang().ObjC1)
1552 ParseObjCProtocolQualifiers(DS);
1553
Douglas Gregor450c75a2008-11-07 15:42:26 +00001554 return true;
1555 }
1556
1557 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001558 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001559 break;
1560 case tok::kw_long:
1561 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001562 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1563 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001564 else
John McCall49bfce42009-08-03 20:12:06 +00001565 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1566 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001569 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001570 break;
1571 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001572 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1573 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001574 break;
1575 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001576 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1577 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001578 break;
1579 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1581 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001582 break;
1583 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001584 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001585 break;
1586 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001587 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001588 break;
1589 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001590 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001591 break;
1592 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001597 break;
1598 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001599 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001600 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001601 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001603 break;
1604 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001606 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001607 case tok::kw_bool:
1608 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001610 break;
1611 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001612 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1613 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001614 break;
1615 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001616 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1617 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001618 break;
1619 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1621 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001622 break;
John Thompson22334602010-02-05 00:12:22 +00001623 case tok::kw___vector:
1624 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1625 break;
1626 case tok::kw___pixel:
1627 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1628 break;
1629
Douglas Gregor450c75a2008-11-07 15:42:26 +00001630 // class-specifier:
1631 case tok::kw_class:
1632 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001633 case tok::kw_union: {
1634 tok::TokenKind Kind = Tok.getKind();
1635 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001636 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1637 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001638 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001639 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001640
1641 // enum-specifier:
1642 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001643 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001644 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001645 return true;
1646
1647 // cv-qualifier:
1648 case tok::kw_const:
1649 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001650 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001651 break;
1652 case tok::kw_volatile:
1653 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001654 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001655 break;
1656 case tok::kw_restrict:
1657 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001658 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001659 break;
1660
1661 // GNU typeof support.
1662 case tok::kw_typeof:
1663 ParseTypeofSpecifier(DS);
1664 return true;
1665
Anders Carlsson74948d02009-06-24 17:47:40 +00001666 // C++0x decltype support.
1667 case tok::kw_decltype:
1668 ParseDecltypeSpecifier(DS);
1669 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001670
Anders Carlssonbae27372009-06-26 23:44:14 +00001671 // C++0x auto support.
1672 case tok::kw_auto:
1673 if (!getLang().CPlusPlus0x)
1674 return false;
1675
John McCall49bfce42009-08-03 20:12:06 +00001676 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001677 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001678
Eli Friedman53339e02009-06-08 23:27:34 +00001679 case tok::kw___ptr64:
1680 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001681 case tok::kw___cdecl:
1682 case tok::kw___stdcall:
1683 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001684 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001685 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001686 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001687
Dawn Perchik335e16b2010-09-03 01:29:35 +00001688 case tok::kw___pascal:
1689 DS.AddAttributes(ParseBorlandTypeAttributes());
1690 return true;
1691
Douglas Gregor450c75a2008-11-07 15:42:26 +00001692 default:
1693 // Not a type-specifier; do nothing.
1694 return false;
1695 }
1696
1697 // If the specifier combination wasn't legal, issue a diagnostic.
1698 if (isInvalid) {
1699 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001700 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001701 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001702 }
1703 DS.SetRangeEnd(Tok.getLocation());
1704 ConsumeToken(); // whatever we parsed above.
1705 return true;
1706}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001707
Chris Lattner70ae4912007-10-29 04:42:53 +00001708/// ParseStructDeclaration - Parse a struct declaration without the terminating
1709/// semicolon.
1710///
Chris Lattner90a26b02007-01-23 04:38:16 +00001711/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001712/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001713/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001714/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001715/// struct-declarator-list:
1716/// struct-declarator
1717/// struct-declarator-list ',' struct-declarator
1718/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1719/// struct-declarator:
1720/// declarator
1721/// [GNU] declarator attributes[opt]
1722/// declarator[opt] ':' constant-expression
1723/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1724///
Chris Lattnera12405b2008-04-10 06:46:29 +00001725void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001726ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001727 if (Tok.is(tok::kw___extension__)) {
1728 // __extension__ silences extension warnings in the subexpression.
1729 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001730 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001731 return ParseStructDeclaration(DS, Fields);
1732 }
Mike Stump11289f42009-09-09 15:08:12 +00001733
Steve Naroff97170802007-08-20 22:28:22 +00001734 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001735 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001736 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001737
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001738 // If there are no declarators, this is a free-standing declaration
1739 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001740 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001741 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001742 return;
1743 }
1744
1745 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001746 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001747 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001748 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001749 FieldDeclarator DeclaratorInfo(DS);
1750
1751 // Attributes are only allowed here on successive declarators.
1752 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1753 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001754 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001755 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1756 }
Mike Stump11289f42009-09-09 15:08:12 +00001757
Steve Naroff97170802007-08-20 22:28:22 +00001758 /// struct-declarator: declarator
1759 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001760 if (Tok.isNot(tok::colon)) {
1761 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1762 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001763 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001764 }
Mike Stump11289f42009-09-09 15:08:12 +00001765
Chris Lattner76c72282007-10-09 17:33:22 +00001766 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001767 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001768 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001769 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001770 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001771 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001772 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001773 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001774
Steve Naroff97170802007-08-20 22:28:22 +00001775 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001776 if (Tok.is(tok::kw___attribute)) {
1777 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001778 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001779 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1780 }
1781
John McCallcfefb6d2009-11-03 02:38:08 +00001782 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001783 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001784 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001785
Steve Naroff97170802007-08-20 22:28:22 +00001786 // If we don't have a comma, it is either the end of the list (a ';')
1787 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001788 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001789 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001790
Steve Naroff97170802007-08-20 22:28:22 +00001791 // Consume the comma.
1792 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001793
John McCallcfefb6d2009-11-03 02:38:08 +00001794 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001795 }
Steve Naroff97170802007-08-20 22:28:22 +00001796}
1797
1798/// ParseStructUnionBody
1799/// struct-contents:
1800/// struct-declaration-list
1801/// [EXT] empty
1802/// [GNU] "struct-declaration-list" without terminatoring ';'
1803/// struct-declaration-list:
1804/// struct-declaration
1805/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001806/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001807///
Chris Lattner1300fb92007-01-23 23:42:53 +00001808void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001809 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001810 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1811 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001812
Chris Lattner90a26b02007-01-23 04:38:16 +00001813 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001814
Douglas Gregor658b9552009-01-09 22:42:13 +00001815 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001816 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001817
Chris Lattner7b9ace62007-01-23 20:11:08 +00001818 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1819 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001820 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001821 Diag(Tok, diag::ext_empty_struct_union)
1822 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001823
John McCall48871652010-08-21 09:40:31 +00001824 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001825
Chris Lattner7b9ace62007-01-23 20:11:08 +00001826 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001827 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001828 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001829
Chris Lattner736ed5d2007-06-09 05:59:07 +00001830 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001831 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001832 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001833 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001834 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001835 ConsumeToken();
1836 continue;
1837 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001838
1839 // Parse all the comma separated declarators.
1840 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001841
John McCallcfefb6d2009-11-03 02:38:08 +00001842 if (!Tok.is(tok::at)) {
1843 struct CFieldCallback : FieldCallback {
1844 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001845 Decl *TagDecl;
1846 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001847
John McCall48871652010-08-21 09:40:31 +00001848 CFieldCallback(Parser &P, Decl *TagDecl,
1849 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001850 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1851
John McCall48871652010-08-21 09:40:31 +00001852 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001853 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001854 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001855 FD.D.getDeclSpec().getSourceRange().getBegin(),
1856 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001857 FieldDecls.push_back(Field);
1858 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001859 }
John McCallcfefb6d2009-11-03 02:38:08 +00001860 } Callback(*this, TagDecl, FieldDecls);
1861
1862 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001863 } else { // Handle @defs
1864 ConsumeToken();
1865 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1866 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001867 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001868 continue;
1869 }
1870 ConsumeToken();
1871 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1872 if (!Tok.is(tok::identifier)) {
1873 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001874 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001875 continue;
1876 }
John McCall48871652010-08-21 09:40:31 +00001877 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001878 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001879 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001880 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1881 ConsumeToken();
1882 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001883 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001884
Chris Lattner76c72282007-10-09 17:33:22 +00001885 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001886 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001887 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001888 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001889 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001890 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001891 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1892 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001893 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001894 // If we stopped at a ';', eat it.
1895 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001896 }
1897 }
Mike Stump11289f42009-09-09 15:08:12 +00001898
Steve Naroff33a1e802007-10-29 21:38:07 +00001899 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001900
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001901 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001902 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001903 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001904 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001905
Douglas Gregor0be31a22010-07-02 17:43:08 +00001906 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001907 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001908 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001909 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001910 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001911 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001912}
1913
1914
Chris Lattner3b561a32006-08-13 00:12:11 +00001915/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001916/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001917/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001918///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001919/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1920/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001921/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001922/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001923///
Douglas Gregor0bf31402010-10-08 23:50:27 +00001924/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1925/// [C++0x] enum-head '{' enumerator-list ',' '}'
1926///
1927/// enum-head: [C++0x]
1928/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1929/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1930///
1931/// enum-key: [C++0x]
1932/// 'enum'
1933/// 'enum' 'class'
1934/// 'enum' 'struct'
1935///
1936/// enum-base: [C++0x]
1937/// ':' type-specifier-seq
1938///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001939/// [C++] elaborated-type-specifier:
1940/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1941///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001942void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001943 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001944 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001945 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001946 if (Tok.is(tok::code_completion)) {
1947 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001948 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001949 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001950 }
1951
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001952 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001953 // If attributes exist after tag, parse them.
1954 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001955 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001956
Abramo Bagnarad7548482010-05-19 21:37:53 +00001957 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001958 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001959 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001960 return;
1961
1962 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001963 Diag(Tok, diag::err_expected_ident);
1964 if (Tok.isNot(tok::l_brace)) {
1965 // Has no name and is not a definition.
1966 // Skip the rest of this declarator, up until the comma or semicolon.
1967 SkipUntil(tok::comma, true);
1968 return;
1969 }
1970 }
1971 }
Mike Stump11289f42009-09-09 15:08:12 +00001972
Douglas Gregor0bf31402010-10-08 23:50:27 +00001973 bool IsScopedEnum = false;
1974
1975 if (getLang().CPlusPlus0x && (Tok.is(tok::kw_class)
1976 || Tok.is(tok::kw_struct))) {
1977 ConsumeToken();
1978 IsScopedEnum = true;
1979 }
1980
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001981 // Must have either 'enum name' or 'enum {...}'.
1982 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1983 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001984
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001985 // Skip the rest of this declarator, up until the comma or semicolon.
1986 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001987 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001988 }
Mike Stump11289f42009-09-09 15:08:12 +00001989
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001990 // If an identifier is present, consume and remember it.
1991 IdentifierInfo *Name = 0;
1992 SourceLocation NameLoc;
1993 if (Tok.is(tok::identifier)) {
1994 Name = Tok.getIdentifierInfo();
1995 NameLoc = ConsumeToken();
1996 }
Mike Stump11289f42009-09-09 15:08:12 +00001997
Douglas Gregor0bf31402010-10-08 23:50:27 +00001998 if (!Name && IsScopedEnum) {
1999 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2000 // declaration of a scoped enumeration.
2001 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2002 IsScopedEnum = false;
2003 }
2004
2005 TypeResult BaseType;
2006
2007 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
2008 ConsumeToken();
2009 SourceRange Range;
2010 BaseType = ParseTypeName(&Range);
2011 }
2012
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002013 // There are three options here. If we have 'enum foo;', then this is a
2014 // forward declaration. If we have 'enum foo {...' then this is a
2015 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2016 //
2017 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2018 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2019 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2020 //
John McCallfaf5fb42010-08-26 23:41:50 +00002021 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002022 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002023 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002024 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002025 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002026 else
John McCallfaf5fb42010-08-26 23:41:50 +00002027 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002028
2029 // enums cannot be templates, although they can be referenced from a
2030 // template.
2031 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002032 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002033 Diag(Tok, diag::err_enum_template);
2034
2035 // Skip the rest of this declarator, up until the comma or semicolon.
2036 SkipUntil(tok::comma, true);
2037 return;
2038 }
2039
Douglas Gregord6ab8742009-05-28 23:31:59 +00002040 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002041 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002042 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2043 const char *PrevSpec = 0;
2044 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002045 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2046 StartLoc, SS, Name, NameLoc, Attr.get(),
2047 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002048 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002049 Owned, IsDependent, IsScopedEnum,
2050 BaseType);
2051
Douglas Gregorba41d012010-04-24 16:38:41 +00002052 if (IsDependent) {
2053 // This enum has a dependent nested-name-specifier. Handle it as a
2054 // dependent tag.
2055 if (!Name) {
2056 DS.SetTypeSpecError();
2057 Diag(Tok, diag::err_expected_type_name_after_typename);
2058 return;
2059 }
2060
Douglas Gregor0be31a22010-07-02 17:43:08 +00002061 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002062 TUK, SS, Name, StartLoc,
2063 NameLoc);
2064 if (Type.isInvalid()) {
2065 DS.SetTypeSpecError();
2066 return;
2067 }
2068
2069 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002070 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002071 Diag(StartLoc, DiagID) << PrevSpec;
2072
2073 return;
2074 }
Mike Stump11289f42009-09-09 15:08:12 +00002075
John McCall48871652010-08-21 09:40:31 +00002076 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002077 // The action failed to produce an enumeration tag. If this is a
2078 // definition, consume the entire definition.
2079 if (Tok.is(tok::l_brace)) {
2080 ConsumeBrace();
2081 SkipUntil(tok::r_brace);
2082 }
2083
2084 DS.SetTypeSpecError();
2085 return;
2086 }
2087
Chris Lattner76c72282007-10-09 17:33:22 +00002088 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002089 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002090
John McCallba7bf592010-08-24 05:47:05 +00002091 // FIXME: The DeclSpec should keep the locations of both the keyword
2092 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002093 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002094 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002095 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002096}
2097
Chris Lattnerc1915e22007-01-25 07:29:02 +00002098/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2099/// enumerator-list:
2100/// enumerator
2101/// enumerator-list ',' enumerator
2102/// enumerator:
2103/// enumeration-constant
2104/// enumeration-constant '=' constant-expression
2105/// enumeration-constant:
2106/// identifier
2107///
John McCall48871652010-08-21 09:40:31 +00002108void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002109 // Enter the scope of the enum body and start the definition.
2110 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002111 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002112
Chris Lattnerc1915e22007-01-25 07:29:02 +00002113 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002114
Chris Lattner37256fb2007-08-27 17:24:30 +00002115 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002116 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002117 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002118
John McCall48871652010-08-21 09:40:31 +00002119 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002120
John McCall48871652010-08-21 09:40:31 +00002121 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002122
Chris Lattnerc1915e22007-01-25 07:29:02 +00002123 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002124 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002125 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2126 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002127
John McCall811a0f52010-10-22 23:36:17 +00002128 // If attributes exist after the enumerator, parse them.
2129 llvm::OwningPtr<AttributeList> Attr;
2130 if (Tok.is(tok::kw___attribute))
2131 Attr.reset(ParseGNUAttributes());
2132
Chris Lattnerc1915e22007-01-25 07:29:02 +00002133 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002134 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002135 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002136 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002137 AssignedVal = ParseConstantExpression();
2138 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002139 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002140 }
Mike Stump11289f42009-09-09 15:08:12 +00002141
Chris Lattnerc1915e22007-01-25 07:29:02 +00002142 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002143 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2144 LastEnumConstDecl,
2145 IdentLoc, Ident,
John McCall811a0f52010-10-22 23:36:17 +00002146 Attr.get(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002147 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002148 EnumConstantDecls.push_back(EnumConstDecl);
2149 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002150
Douglas Gregorce66d022010-09-07 14:51:08 +00002151 if (Tok.is(tok::identifier)) {
2152 // We're missing a comma between enumerators.
2153 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2154 Diag(Loc, diag::err_enumerator_list_missing_comma)
2155 << FixItHint::CreateInsertion(Loc, ", ");
2156 continue;
2157 }
2158
Chris Lattner76c72282007-10-09 17:33:22 +00002159 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002160 break;
2161 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002162
2163 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002164 !(getLang().C99 || getLang().CPlusPlus0x))
2165 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2166 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002167 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002168 }
Mike Stump11289f42009-09-09 15:08:12 +00002169
Chris Lattnerc1915e22007-01-25 07:29:02 +00002170 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002171 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002172
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002173 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002174 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002175 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002176 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002177
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002178 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2179 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00002180 getCurScope(), Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002181
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002182 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002183 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002184}
Chris Lattner3b561a32006-08-13 00:12:11 +00002185
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002186/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002187/// start of a type-qualifier-list.
2188bool Parser::isTypeQualifier() const {
2189 switch (Tok.getKind()) {
2190 default: return false;
2191 // type-qualifier
2192 case tok::kw_const:
2193 case tok::kw_volatile:
2194 case tok::kw_restrict:
2195 return true;
2196 }
2197}
2198
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002199/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2200/// is definitely a type-specifier. Return false if it isn't part of a type
2201/// specifier or if we're not sure.
2202bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2203 switch (Tok.getKind()) {
2204 default: return false;
2205 // type-specifiers
2206 case tok::kw_short:
2207 case tok::kw_long:
2208 case tok::kw_signed:
2209 case tok::kw_unsigned:
2210 case tok::kw__Complex:
2211 case tok::kw__Imaginary:
2212 case tok::kw_void:
2213 case tok::kw_char:
2214 case tok::kw_wchar_t:
2215 case tok::kw_char16_t:
2216 case tok::kw_char32_t:
2217 case tok::kw_int:
2218 case tok::kw_float:
2219 case tok::kw_double:
2220 case tok::kw_bool:
2221 case tok::kw__Bool:
2222 case tok::kw__Decimal32:
2223 case tok::kw__Decimal64:
2224 case tok::kw__Decimal128:
2225 case tok::kw___vector:
2226
2227 // struct-or-union-specifier (C99) or class-specifier (C++)
2228 case tok::kw_class:
2229 case tok::kw_struct:
2230 case tok::kw_union:
2231 // enum-specifier
2232 case tok::kw_enum:
2233
2234 // typedef-name
2235 case tok::annot_typename:
2236 return true;
2237 }
2238}
2239
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002240/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002241/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002242bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002243 switch (Tok.getKind()) {
2244 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002245
Chris Lattner020bab92009-01-04 23:41:41 +00002246 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002247 if (TryAltiVecVectorToken())
2248 return true;
2249 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002250 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002251 // Annotate typenames and C++ scope specifiers. If we get one, just
2252 // recurse to handle whatever we get.
2253 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002254 return true;
2255 if (Tok.is(tok::identifier))
2256 return false;
2257 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002258
Chris Lattner020bab92009-01-04 23:41:41 +00002259 case tok::coloncolon: // ::foo::bar
2260 if (NextToken().is(tok::kw_new) || // ::new
2261 NextToken().is(tok::kw_delete)) // ::delete
2262 return false;
2263
Chris Lattner020bab92009-01-04 23:41:41 +00002264 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002265 return true;
2266 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002267
Chris Lattnere37e2332006-08-15 04:50:22 +00002268 // GNU attributes support.
2269 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002270 // GNU typeof support.
2271 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002272
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002273 // type-specifiers
2274 case tok::kw_short:
2275 case tok::kw_long:
2276 case tok::kw_signed:
2277 case tok::kw_unsigned:
2278 case tok::kw__Complex:
2279 case tok::kw__Imaginary:
2280 case tok::kw_void:
2281 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002282 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002283 case tok::kw_char16_t:
2284 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002285 case tok::kw_int:
2286 case tok::kw_float:
2287 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002288 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002289 case tok::kw__Bool:
2290 case tok::kw__Decimal32:
2291 case tok::kw__Decimal64:
2292 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002293 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002294
Chris Lattner861a2262008-04-13 18:59:07 +00002295 // struct-or-union-specifier (C99) or class-specifier (C++)
2296 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002297 case tok::kw_struct:
2298 case tok::kw_union:
2299 // enum-specifier
2300 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002301
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002302 // type-qualifier
2303 case tok::kw_const:
2304 case tok::kw_volatile:
2305 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002306
2307 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002308 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002309 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002310
Chris Lattner409bf7d2008-10-20 00:25:30 +00002311 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2312 case tok::less:
2313 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002314
Steve Naroff44ac7772008-12-25 14:16:32 +00002315 case tok::kw___cdecl:
2316 case tok::kw___stdcall:
2317 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002318 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002319 case tok::kw___w64:
2320 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002321 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002322 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002323 }
2324}
2325
Chris Lattneracd58a32006-08-06 17:24:14 +00002326/// isDeclarationSpecifier() - Return true if the current token is part of a
2327/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002328///
2329/// \param DisambiguatingWithExpression True to indicate that the purpose of
2330/// this check is to disambiguate between an expression and a declaration.
2331bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002332 switch (Tok.getKind()) {
2333 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002334
Chris Lattner020bab92009-01-04 23:41:41 +00002335 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002336 // Unfortunate hack to support "Class.factoryMethod" notation.
2337 if (getLang().ObjC1 && NextToken().is(tok::period))
2338 return false;
John Thompson22334602010-02-05 00:12:22 +00002339 if (TryAltiVecVectorToken())
2340 return true;
2341 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002342 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002343 // Annotate typenames and C++ scope specifiers. If we get one, just
2344 // recurse to handle whatever we get.
2345 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002346 return true;
2347 if (Tok.is(tok::identifier))
2348 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002349
2350 // If we're in Objective-C and we have an Objective-C class type followed
2351 // by an identifier and then either ':' or ']', in a place where an
2352 // expression is permitted, then this is probably a class message send
2353 // missing the initial '['. In this case, we won't consider this to be
2354 // the start of a declaration.
2355 if (DisambiguatingWithExpression &&
2356 isStartOfObjCClassMessageMissingOpenBracket())
2357 return false;
2358
John McCall1f476a12010-02-26 08:45:28 +00002359 return isDeclarationSpecifier();
2360
Chris Lattner020bab92009-01-04 23:41:41 +00002361 case tok::coloncolon: // ::foo::bar
2362 if (NextToken().is(tok::kw_new) || // ::new
2363 NextToken().is(tok::kw_delete)) // ::delete
2364 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002365
Chris Lattner020bab92009-01-04 23:41:41 +00002366 // Annotate typenames and C++ scope specifiers. If we get one, just
2367 // recurse to handle whatever we get.
2368 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002369 return true;
2370 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002371
Chris Lattneracd58a32006-08-06 17:24:14 +00002372 // storage-class-specifier
2373 case tok::kw_typedef:
2374 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002375 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002376 case tok::kw_static:
2377 case tok::kw_auto:
2378 case tok::kw_register:
2379 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002380
Chris Lattneracd58a32006-08-06 17:24:14 +00002381 // type-specifiers
2382 case tok::kw_short:
2383 case tok::kw_long:
2384 case tok::kw_signed:
2385 case tok::kw_unsigned:
2386 case tok::kw__Complex:
2387 case tok::kw__Imaginary:
2388 case tok::kw_void:
2389 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002390 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002391 case tok::kw_char16_t:
2392 case tok::kw_char32_t:
2393
Chris Lattneracd58a32006-08-06 17:24:14 +00002394 case tok::kw_int:
2395 case tok::kw_float:
2396 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002397 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002398 case tok::kw__Bool:
2399 case tok::kw__Decimal32:
2400 case tok::kw__Decimal64:
2401 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002402 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002403
Chris Lattner861a2262008-04-13 18:59:07 +00002404 // struct-or-union-specifier (C99) or class-specifier (C++)
2405 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002406 case tok::kw_struct:
2407 case tok::kw_union:
2408 // enum-specifier
2409 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002410
Chris Lattneracd58a32006-08-06 17:24:14 +00002411 // type-qualifier
2412 case tok::kw_const:
2413 case tok::kw_volatile:
2414 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002415
Chris Lattneracd58a32006-08-06 17:24:14 +00002416 // function-specifier
2417 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002418 case tok::kw_virtual:
2419 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002420
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002421 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002422 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002423
Chris Lattner599e47e2007-08-09 17:01:07 +00002424 // GNU typeof support.
2425 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002426
Chris Lattner599e47e2007-08-09 17:01:07 +00002427 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002428 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002429 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002430
Chris Lattner8b2ec162008-07-26 03:38:44 +00002431 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2432 case tok::less:
2433 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002434
Steve Narofff192fab2009-01-06 19:34:12 +00002435 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002436 case tok::kw___cdecl:
2437 case tok::kw___stdcall:
2438 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002439 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002440 case tok::kw___w64:
2441 case tok::kw___ptr64:
2442 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002443 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002444 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002445 }
2446}
2447
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002448bool Parser::isConstructorDeclarator() {
2449 TentativeParsingAction TPA(*this);
2450
2451 // Parse the C++ scope specifier.
2452 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002453 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002454 TPA.Revert();
2455 return false;
2456 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002457
2458 // Parse the constructor name.
2459 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2460 // We already know that we have a constructor name; just consume
2461 // the token.
2462 ConsumeToken();
2463 } else {
2464 TPA.Revert();
2465 return false;
2466 }
2467
2468 // Current class name must be followed by a left parentheses.
2469 if (Tok.isNot(tok::l_paren)) {
2470 TPA.Revert();
2471 return false;
2472 }
2473 ConsumeParen();
2474
2475 // A right parentheses or ellipsis signals that we have a constructor.
2476 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2477 TPA.Revert();
2478 return true;
2479 }
2480
2481 // If we need to, enter the specified scope.
2482 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002483 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002484 DeclScopeObj.EnterDeclaratorScope();
2485
2486 // Check whether the next token(s) are part of a declaration
2487 // specifier, in which case we have the start of a parameter and,
2488 // therefore, we know that this is a constructor.
2489 bool IsConstructor = isDeclarationSpecifier();
2490 TPA.Revert();
2491 return IsConstructor;
2492}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002493
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002494/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002495/// type-qualifier-list: [C99 6.7.5]
2496/// type-qualifier
2497/// [vendor] attributes
2498/// [ only if VendorAttributesAllowed=true ]
2499/// type-qualifier-list type-qualifier
2500/// [vendor] type-qualifier-list attributes
2501/// [ only if VendorAttributesAllowed=true ]
2502/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2503/// [ only if CXX0XAttributesAllowed=true ]
2504/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002505///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002506void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2507 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002508 bool CXX0XAttributesAllowed) {
2509 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2510 SourceLocation Loc = Tok.getLocation();
2511 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2512 if (CXX0XAttributesAllowed)
2513 DS.AddAttributes(Attr.AttrList);
2514 else
2515 Diag(Loc, diag::err_attributes_not_allowed);
2516 }
2517
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002518 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002519 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002520 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002521 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002522 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002523
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002524 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002525 case tok::code_completion:
2526 Actions.CodeCompleteTypeQualifiers(DS);
2527 ConsumeCodeCompletionToken();
2528 break;
2529
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002530 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002531 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2532 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002533 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002534 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002535 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2536 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002537 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002538 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002539 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2540 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002541 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002542 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002543 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002544 case tok::kw___cdecl:
2545 case tok::kw___stdcall:
2546 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002547 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002548 if (VendorAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002549 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2550 continue;
2551 }
2552 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002553 case tok::kw___pascal:
2554 if (VendorAttributesAllowed) {
2555 DS.AddAttributes(ParseBorlandTypeAttributes());
2556 continue;
2557 }
2558 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002559 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002560 if (VendorAttributesAllowed) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002561 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002562 continue; // do *not* consume the next token!
2563 }
2564 // otherwise, FALL THROUGH!
2565 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002566 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002567 // If this is not a type-qualifier token, we're done reading type
2568 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002569 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002570 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002571 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002572
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002573 // If the specifier combination wasn't legal, issue a diagnostic.
2574 if (isInvalid) {
2575 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002576 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002577 }
2578 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002579 }
2580}
2581
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002582
2583/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2584///
2585void Parser::ParseDeclarator(Declarator &D) {
2586 /// This implements the 'declarator' production in the C grammar, then checks
2587 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002588 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002589}
2590
Sebastian Redlbd150f42008-11-21 19:14:01 +00002591/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2592/// is parsed by the function passed to it. Pass null, and the direct-declarator
2593/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002594/// ptr-operator production.
2595///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002596/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2597/// [C] pointer[opt] direct-declarator
2598/// [C++] direct-declarator
2599/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002600///
2601/// pointer: [C99 6.7.5]
2602/// '*' type-qualifier-list[opt]
2603/// '*' type-qualifier-list[opt] pointer
2604///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002605/// ptr-operator:
2606/// '*' cv-qualifier-seq[opt]
2607/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002608/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002609/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002610/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002611/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002612void Parser::ParseDeclaratorInternal(Declarator &D,
2613 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002614 if (Diags.hasAllExtensionsSilenced())
2615 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002616
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002617 // C++ member pointers start with a '::' or a nested-name.
2618 // Member pointers get special handling, since there's no place for the
2619 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002620 if (getLang().CPlusPlus &&
2621 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2622 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002623 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002624 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002625
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002626 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002627 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002628 // The scope spec really belongs to the direct-declarator.
2629 D.getCXXScopeSpec() = SS;
2630 if (DirectDeclParser)
2631 (this->*DirectDeclParser)(D);
2632 return;
2633 }
2634
2635 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002636 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002637 DeclSpec DS;
2638 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002639 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002640
2641 // Recurse to parse whatever is left.
2642 ParseDeclaratorInternal(D, DirectDeclParser);
2643
2644 // Sema will have to catch (syntactically invalid) pointers into global
2645 // scope. It has to catch pointers into namespace scope anyway.
2646 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002647 Loc, DS.TakeAttributes()),
2648 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002649 return;
2650 }
2651 }
2652
2653 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002654 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002655 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002656 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002657 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002658 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002659 if (DirectDeclParser)
2660 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002661 return;
2662 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002663
Sebastian Redled0f3b02009-03-15 22:02:01 +00002664 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2665 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002666 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002667 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002668
Chris Lattner9eac9312009-03-27 04:18:06 +00002669 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002670 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002671 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002672
Bill Wendling3708c182007-05-27 10:15:43 +00002673 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002674 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002675
Bill Wendling3708c182007-05-27 10:15:43 +00002676 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002677 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002678 if (Kind == tok::star)
2679 // Remember that we parsed a pointer type, and remember the type-quals.
2680 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002681 DS.TakeAttributes()),
2682 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002683 else
2684 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002685 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002686 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002687 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002688 } else {
2689 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002690 DeclSpec DS;
2691
Sebastian Redl3b27be62009-03-23 00:00:23 +00002692 // Complain about rvalue references in C++03, but then go on and build
2693 // the declarator.
2694 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2695 Diag(Loc, diag::err_rvalue_reference);
2696
Bill Wendling93efb222007-06-02 23:28:54 +00002697 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2698 // cv-qualifiers are introduced through the use of a typedef or of a
2699 // template type argument, in which case the cv-qualifiers are ignored.
2700 //
2701 // [GNU] Retricted references are allowed.
2702 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002703 // [C++0x] Attributes on references are not allowed.
2704 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002705 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002706
2707 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2708 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2709 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002710 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002711 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2712 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002713 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002714 }
Bill Wendling3708c182007-05-27 10:15:43 +00002715
2716 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002717 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002718
Douglas Gregor66583c52008-11-03 15:51:28 +00002719 if (D.getNumTypeObjects() > 0) {
2720 // C++ [dcl.ref]p4: There shall be no references to references.
2721 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2722 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002723 if (const IdentifierInfo *II = D.getIdentifier())
2724 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2725 << II;
2726 else
2727 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2728 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002729
Sebastian Redlbd150f42008-11-21 19:14:01 +00002730 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002731 // can go ahead and build the (technically ill-formed)
2732 // declarator: reference collapsing will take care of it.
2733 }
2734 }
2735
Bill Wendling3708c182007-05-27 10:15:43 +00002736 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002737 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002738 DS.TakeAttributes(),
2739 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002740 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002741 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002742}
2743
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002744/// ParseDirectDeclarator
2745/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002746/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002747/// '(' declarator ')'
2748/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002749/// [C90] direct-declarator '[' constant-expression[opt] ']'
2750/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2751/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2752/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2753/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002754/// direct-declarator '(' parameter-type-list ')'
2755/// direct-declarator '(' identifier-list[opt] ')'
2756/// [GNU] direct-declarator '(' parameter-forward-declarations
2757/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002758/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2759/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002760/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002761///
2762/// declarator-id: [C++ 8]
2763/// id-expression
2764/// '::'[opt] nested-name-specifier[opt] type-name
2765///
2766/// id-expression: [C++ 5.1]
2767/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002768/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002769///
2770/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002771/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002772/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002773/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002774/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002775/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002776///
Chris Lattneracd58a32006-08-06 17:24:14 +00002777void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002778 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002779
Douglas Gregor7861a802009-11-03 01:35:08 +00002780 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2781 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002782 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002783 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002784 }
2785
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002786 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002787 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002788 // Change the declaration context for name lookup, until this function
2789 // is exited (and the declarator has been parsed).
2790 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002791 }
2792
Douglas Gregor7861a802009-11-03 01:35:08 +00002793 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2794 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2795 // We found something that indicates the start of an unqualified-id.
2796 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002797 bool AllowConstructorName;
2798 if (D.getDeclSpec().hasTypeSpecifier())
2799 AllowConstructorName = false;
2800 else if (D.getCXXScopeSpec().isSet())
2801 AllowConstructorName =
2802 (D.getContext() == Declarator::FileContext ||
2803 (D.getContext() == Declarator::MemberContext &&
2804 D.getDeclSpec().isFriendSpecified()));
2805 else
2806 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2807
Douglas Gregor7861a802009-11-03 01:35:08 +00002808 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2809 /*EnteringContext=*/true,
2810 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002811 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002812 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002813 D.getName()) ||
2814 // Once we're past the identifier, if the scope was bad, mark the
2815 // whole declarator bad.
2816 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002817 D.SetIdentifier(0, Tok.getLocation());
2818 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002819 } else {
2820 // Parsed the unqualified-id; update range information and move along.
2821 if (D.getSourceRange().getBegin().isInvalid())
2822 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2823 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002824 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002825 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002826 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002827 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002828 assert(!getLang().CPlusPlus &&
2829 "There's a C++-specific check for tok::identifier above");
2830 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2831 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2832 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002833 goto PastIdentifier;
2834 }
2835
2836 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002837 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002838 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002839 // Example: 'char (*X)' or 'int (*XX)(void)'
2840 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002841
2842 // If the declarator was parenthesized, we entered the declarator
2843 // scope when parsing the parenthesized declarator, then exited
2844 // the scope already. Re-enter the scope, if we need to.
2845 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002846 // If there was an error parsing parenthesized declarator, declarator
2847 // scope may have been enterred before. Don't do it again.
2848 if (!D.isInvalidType() &&
2849 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002850 // Change the declaration context for name lookup, until this function
2851 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002852 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002853 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002854 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002855 // This could be something simple like "int" (in which case the declarator
2856 // portion is empty), if an abstract-declarator is allowed.
2857 D.SetIdentifier(0, Tok.getLocation());
2858 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002859 if (D.getContext() == Declarator::MemberContext)
2860 Diag(Tok, diag::err_expected_member_name_or_semi)
2861 << D.getDeclSpec().getSourceRange();
2862 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002863 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002864 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002865 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002866 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002867 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002868 }
Mike Stump11289f42009-09-09 15:08:12 +00002869
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002870 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002871 assert(D.isPastIdentifier() &&
2872 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002873
Alexis Hunt96d5c762009-11-21 08:43:09 +00002874 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002875 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002876 && isCXX0XAttributeSpecifier(true)) {
2877 SourceLocation AttrEndLoc;
2878 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2879 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2880 }
2881
Chris Lattneracd58a32006-08-06 17:24:14 +00002882 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002883 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002884 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2885 // In such a case, check if we actually have a function declarator; if it
2886 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002887 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2888 // When not in file scope, warn for ambiguous function declarators, just
2889 // in case the author intended it as a variable definition.
2890 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2891 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2892 break;
2893 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002894 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002895 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002896 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002897 } else {
2898 break;
2899 }
2900 }
2901}
2902
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002903/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2904/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002905/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002906/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2907///
2908/// direct-declarator:
2909/// '(' declarator ')'
2910/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002911/// direct-declarator '(' parameter-type-list ')'
2912/// direct-declarator '(' identifier-list[opt] ')'
2913/// [GNU] direct-declarator '(' parameter-forward-declarations
2914/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002915///
2916void Parser::ParseParenDeclarator(Declarator &D) {
2917 SourceLocation StartLoc = ConsumeParen();
2918 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002919
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002920 // Eat any attributes before we look at whether this is a grouping or function
2921 // declarator paren. If this is a grouping paren, the attribute applies to
2922 // the type being built up, for example:
2923 // int (__attribute__(()) *x)(long y)
2924 // If this ends up not being a grouping paren, the attribute applies to the
2925 // first argument, for example:
2926 // int (__attribute__(()) int x)
2927 // In either case, we need to eat any attributes to be able to determine what
2928 // sort of paren this is.
2929 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002930 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002931 bool RequiresArg = false;
2932 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002933 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002934
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002935 // We require that the argument list (if this is a non-grouping paren) be
2936 // present even if the attribute list was empty.
2937 RequiresArg = true;
2938 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002939 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002940 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002941 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2942 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002943 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002944 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00002945 // Eat any Borland extensions.
2946 if (Tok.is(tok::kw___pascal)) {
2947 AttrList.reset(ParseBorlandTypeAttributes(AttrList.take()));
2948 }
Mike Stump11289f42009-09-09 15:08:12 +00002949
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002950 // If we haven't past the identifier yet (or where the identifier would be
2951 // stored, if this is an abstract declarator), then this is probably just
2952 // grouping parens. However, if this could be an abstract-declarator, then
2953 // this could also be the start of function arguments (consider 'void()').
2954 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002955
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002956 if (!D.mayOmitIdentifier()) {
2957 // If this can't be an abstract-declarator, this *must* be a grouping
2958 // paren, because we haven't seen the identifier yet.
2959 isGrouping = true;
2960 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002961 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002962 isDeclarationSpecifier()) { // 'int(int)' is a function.
2963 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2964 // considered to be a type, not a K&R identifier-list.
2965 isGrouping = false;
2966 } else {
2967 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2968 isGrouping = true;
2969 }
Mike Stump11289f42009-09-09 15:08:12 +00002970
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002971 // If this is a grouping paren, handle:
2972 // direct-declarator: '(' declarator ')'
2973 // direct-declarator: '(' attributes declarator ')'
2974 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002975 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002976 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002977 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002978 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002979
Sebastian Redlbd150f42008-11-21 19:14:01 +00002980 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002981 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002982 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002983
2984 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002985 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002986 return;
2987 }
Mike Stump11289f42009-09-09 15:08:12 +00002988
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002989 // Okay, if this wasn't a grouping paren, it must be the start of a function
2990 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002991 // identifier (and remember where it would have been), then call into
2992 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002993 D.SetIdentifier(0, Tok.getLocation());
2994
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002995 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002996}
2997
2998/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2999/// declarator D up to a paren, which indicates that we are parsing function
3000/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003001///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003002/// If AttrList is non-null, then the caller parsed those arguments immediately
3003/// after the open paren - they should be considered to be the first argument of
3004/// a parameter. If RequiresArg is true, then the first argument of the
3005/// function is required to be present and required to not be an identifier
3006/// list.
3007///
Chris Lattneracd58a32006-08-06 17:24:14 +00003008/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003009/// parameter-type-list: [C99 6.7.5]
3010/// parameter-list
3011/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003012/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003013///
3014/// parameter-list: [C99 6.7.5]
3015/// parameter-declaration
3016/// parameter-list ',' parameter-declaration
3017///
3018/// parameter-declaration: [C99 6.7.5]
3019/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003020/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003021/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003022/// declaration-specifiers abstract-declarator[opt]
3023/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003024/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003025/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003026///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003027/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00003028/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003029///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003030void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3031 AttributeList *AttrList,
3032 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003033 // lparen is already consumed!
3034 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003035
Douglas Gregor7fb25412010-10-01 18:44:50 +00003036 ParsedType TrailingReturnType;
3037
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003038 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003039 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003040 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003041 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003042 delete AttrList;
3043 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003044
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003045 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3046 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003047
3048 // cv-qualifier-seq[opt].
3049 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003050 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003051 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003052 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003053 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003054 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003055 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00003056 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003057 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003058 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003059
3060 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003061 if (Tok.is(tok::kw_throw)) {
3062 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003063 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003064 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003065 hasAnyExceptionSpec);
3066 assert(Exceptions.size() == ExceptionRanges.size() &&
3067 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003068 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003069
3070 // Parse trailing-return-type.
3071 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3072 TrailingReturnType = ParseTrailingReturnType().get();
3073 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003074 }
3075
Chris Lattner371ed4e2008-04-06 06:57:35 +00003076 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003077 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003078 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003079 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003080 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003081 /*arglist*/ 0, 0,
3082 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003083 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003084 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003085 Exceptions.data(),
3086 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003087 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003088 LParenLoc, RParenLoc, D,
3089 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003090 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003091 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003092 }
3093
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003094 // Alternatively, this parameter list may be an identifier list form for a
3095 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003096 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3097 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003098 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003099 // K&R identifier lists can't have typedefs as identifiers, per
3100 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00003101 if (RequiresArg) {
3102 Diag(Tok, diag::err_argument_required_after_attribute);
3103 delete AttrList;
3104 }
Chris Lattner9453ab82010-05-14 17:23:36 +00003105
Steve Naroffb0486722009-01-28 19:16:40 +00003106 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003107 // normal declarators, not for abstract-declarators. Get the first
3108 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003109 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003110 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003111
3112 // Identifier lists follow a really simple grammar: the identifiers can
3113 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3114 // identifier lists are really rare in the brave new modern world, and it
3115 // is very common for someone to typo a type in a non-k&r style list. If
3116 // we are presented with something like: "void foo(intptr x, float y)",
3117 // we don't want to start parsing the function declarator as though it is
3118 // a K&R style declarator just because intptr is an invalid type.
3119 //
3120 // To handle this, we check to see if the token after the first identifier
3121 // is a "," or ")". Only if so, do we parse it as an identifier list.
3122 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3123 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3124 FirstTok.getIdentifierInfo(),
3125 FirstTok.getLocation(), D);
3126
3127 // If we get here, the code is invalid. Push the first identifier back
3128 // into the token stream and parse the first argument as an (invalid)
3129 // normal argument declarator.
3130 PP.EnterToken(Tok);
3131 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003132 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003133 }
Mike Stump11289f42009-09-09 15:08:12 +00003134
Chris Lattner371ed4e2008-04-06 06:57:35 +00003135 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003136
Chris Lattner371ed4e2008-04-06 06:57:35 +00003137 // Build up an array of information about the parsed arguments.
3138 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003139
3140 // Enter function-declaration scope, limiting any declarators to the
3141 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003142 ParseScope PrototypeScope(this,
3143 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003144
Chris Lattner371ed4e2008-04-06 06:57:35 +00003145 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003146 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003147 while (1) {
3148 if (Tok.is(tok::ellipsis)) {
3149 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003150 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003151 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003152 }
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003153
3154 // Skip any Microsoft attributes before a param.
3155 if (getLang().Microsoft && Tok.is(tok::l_square))
3156 ParseMicrosoftAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00003157
Chris Lattner371ed4e2008-04-06 06:57:35 +00003158 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003159
Chris Lattner371ed4e2008-04-06 06:57:35 +00003160 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003161 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003162 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003163
3164 // If the caller parsed attributes for the first argument, add them now.
3165 if (AttrList) {
3166 DS.AddAttributes(AttrList);
3167 AttrList = 0; // Only apply the attributes to the first parameter.
3168 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003169 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003170
Chris Lattner371ed4e2008-04-06 06:57:35 +00003171 // Parse the declarator. This is "PrototypeContext", because we must
3172 // accept either 'declarator' or 'abstract-declarator' here.
3173 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3174 ParseDeclarator(ParmDecl);
3175
3176 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003177 if (Tok.is(tok::kw___attribute)) {
3178 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003179 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003180 ParmDecl.AddAttributes(AttrList, Loc);
3181 }
Mike Stump11289f42009-09-09 15:08:12 +00003182
Chris Lattner371ed4e2008-04-06 06:57:35 +00003183 // Remember this parsed parameter in ParamInfo.
3184 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003185
Douglas Gregor4d87df52008-12-16 21:30:33 +00003186 // DefArgToks is used when the parsing of default arguments needs
3187 // to be delayed.
3188 CachedTokens *DefArgToks = 0;
3189
Chris Lattner371ed4e2008-04-06 06:57:35 +00003190 // If no parameter was specified, verify that *something* was specified,
3191 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003192 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3193 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003194 // Completely missing, emit error.
3195 Diag(DSStart, diag::err_missing_param);
3196 } else {
3197 // Otherwise, we have something. Add it and let semantic analysis try
3198 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003199
Chris Lattner371ed4e2008-04-06 06:57:35 +00003200 // Inform the actions module about the parameter declarator, so it gets
3201 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003202 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003203
3204 // Parse the default argument, if any. We parse the default
3205 // arguments in all dialects; the semantic analysis in
3206 // ActOnParamDefaultArgument will reject the default argument in
3207 // C.
3208 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003209 SourceLocation EqualLoc = Tok.getLocation();
3210
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003211 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003212 if (D.getContext() == Declarator::MemberContext) {
3213 // If we're inside a class definition, cache the tokens
3214 // corresponding to the default argument. We'll actually parse
3215 // them when we see the end of the class definition.
3216 // FIXME: Templates will require something similar.
3217 // FIXME: Can we use a smart pointer for Toks?
3218 DefArgToks = new CachedTokens;
3219
Mike Stump11289f42009-09-09 15:08:12 +00003220 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003221 /*StopAtSemi=*/true,
3222 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003223 delete DefArgToks;
3224 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003225 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003226 } else {
3227 // Mark the end of the default argument so that we know when to
3228 // stop when we parse it later on.
3229 Token DefArgEnd;
3230 DefArgEnd.startToken();
3231 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3232 DefArgEnd.setLocation(Tok.getLocation());
3233 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003234 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003235 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003236 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003237 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003238 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003239 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003240
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003241 // The argument isn't actually potentially evaluated unless it is
3242 // used.
3243 EnterExpressionEvaluationContext Eval(Actions,
3244 Sema::PotentiallyEvaluatedIfUsed);
3245
John McCalldadc5752010-08-24 06:29:42 +00003246 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003247 if (DefArgResult.isInvalid()) {
3248 Actions.ActOnParamDefaultArgumentError(Param);
3249 SkipUntil(tok::comma, tok::r_paren, true, true);
3250 } else {
3251 // Inform the actions module about the default argument
3252 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003253 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003254 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003255 }
3256 }
Mike Stump11289f42009-09-09 15:08:12 +00003257
3258 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3259 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003260 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003261 }
3262
3263 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003264 if (Tok.isNot(tok::comma)) {
3265 if (Tok.is(tok::ellipsis)) {
3266 IsVariadic = true;
3267 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3268
3269 if (!getLang().CPlusPlus) {
3270 // We have ellipsis without a preceding ',', which is ill-formed
3271 // in C. Complain and provide the fix.
3272 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003273 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003274 }
3275 }
3276
3277 break;
3278 }
Mike Stump11289f42009-09-09 15:08:12 +00003279
Chris Lattner371ed4e2008-04-06 06:57:35 +00003280 // Consume the comma.
3281 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003282 }
Mike Stump11289f42009-09-09 15:08:12 +00003283
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003284 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003285 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3286 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003287
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003288 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003289 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003290 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003291 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003292 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003293 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003294
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003295 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003296 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003297 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003298 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003299 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003300
3301 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003302 if (Tok.is(tok::kw_throw)) {
3303 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003304 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003305 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003306 hasAnyExceptionSpec);
3307 assert(Exceptions.size() == ExceptionRanges.size() &&
3308 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003309 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003310
3311 // Parse trailing-return-type.
3312 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3313 TrailingReturnType = ParseTrailingReturnType().get();
3314 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003315 }
3316
Douglas Gregor7fb25412010-10-01 18:44:50 +00003317 // FIXME: We should leave the prototype scope before parsing the exception
3318 // specification, and then reenter it when parsing the trailing return type.
3319
3320 // Leave prototype scope.
3321 PrototypeScope.Exit();
3322
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003323 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003324 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003325 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003326 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003327 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003328 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003329 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003330 Exceptions.data(),
3331 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003332 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003333 LParenLoc, RParenLoc, D,
3334 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003335 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003336}
Chris Lattneracd58a32006-08-06 17:24:14 +00003337
Chris Lattner6c940e62008-04-06 06:34:08 +00003338/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3339/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003340/// first identifier has already been consumed, and the current token is the
3341/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003342///
3343/// identifier-list: [C99 6.7.5]
3344/// identifier
3345/// identifier-list ',' identifier
3346///
3347void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003348 IdentifierInfo *FirstIdent,
3349 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003350 Declarator &D) {
3351 // Build up an array of information about the parsed arguments.
3352 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3353 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003354
Chris Lattner6c940e62008-04-06 06:34:08 +00003355 // If there was no identifier specified for the declarator, either we are in
3356 // an abstract-declarator, or we are in a parameter declarator which was found
3357 // to be abstract. In abstract-declarators, identifier lists are not valid:
3358 // diagnose this.
3359 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003360 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003361
Chris Lattner9453ab82010-05-14 17:23:36 +00003362 // The first identifier was already read, and is known to be the first
3363 // identifier in the list. Remember this identifier in ParamInfo.
3364 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003365 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003366
Chris Lattner6c940e62008-04-06 06:34:08 +00003367 while (Tok.is(tok::comma)) {
3368 // Eat the comma.
3369 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003370
Chris Lattner9186f552008-04-06 06:39:19 +00003371 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003372 if (Tok.isNot(tok::identifier)) {
3373 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003374 SkipUntil(tok::r_paren);
3375 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003376 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003377
Chris Lattner6c940e62008-04-06 06:34:08 +00003378 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003379
3380 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003381 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003382 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003383
Chris Lattner6c940e62008-04-06 06:34:08 +00003384 // Verify that the argument identifier has not already been mentioned.
3385 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003386 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003387 } else {
3388 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003389 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003390 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003391 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003392 }
Mike Stump11289f42009-09-09 15:08:12 +00003393
Chris Lattner6c940e62008-04-06 06:34:08 +00003394 // Eat the identifier.
3395 ConsumeToken();
3396 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003397
3398 // If we have the closing ')', eat it and we're done.
3399 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3400
Chris Lattner9186f552008-04-06 06:39:19 +00003401 // Remember that we parsed a function type, and remember the attributes. This
3402 // function type is always a K&R style function type, which is not varargs and
3403 // has no prototype.
3404 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003405 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003406 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003407 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003408 /*exception*/false,
3409 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003410 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003411 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003412}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003413
Chris Lattnere8074e62006-08-06 18:30:15 +00003414/// [C90] direct-declarator '[' constant-expression[opt] ']'
3415/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3416/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3417/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3418/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3419void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003420 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003421
Chris Lattner84a11622008-12-18 07:27:21 +00003422 // C array syntax has many features, but by-far the most common is [] and [4].
3423 // This code does a fast path to handle some of the most obvious cases.
3424 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003425 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003426 //FIXME: Use these
3427 CXX0XAttributeList Attr;
3428 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3429 Attr = ParseCXX0XAttributes();
3430 }
3431
Chris Lattner84a11622008-12-18 07:27:21 +00003432 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003433 ExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003434 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3435 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003436 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003437 return;
3438 } else if (Tok.getKind() == tok::numeric_constant &&
3439 GetLookAheadToken(1).is(tok::r_square)) {
3440 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003441 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003442 ConsumeToken();
3443
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003444 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003445 //FIXME: Use these
3446 CXX0XAttributeList Attr;
3447 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3448 Attr = ParseCXX0XAttributes();
3449 }
Chris Lattner84a11622008-12-18 07:27:21 +00003450
3451 // If there was an error parsing the assignment-expression, recover.
3452 if (ExprRes.isInvalid())
3453 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003454
Chris Lattner84a11622008-12-18 07:27:21 +00003455 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003456 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3457 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003458 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003459 return;
3460 }
Mike Stump11289f42009-09-09 15:08:12 +00003461
Chris Lattnere8074e62006-08-06 18:30:15 +00003462 // If valid, this location is the position where we read the 'static' keyword.
3463 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003464 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003465 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003466
Chris Lattnere8074e62006-08-06 18:30:15 +00003467 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003468 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003469 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003470 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003471
Chris Lattnere8074e62006-08-06 18:30:15 +00003472 // If we haven't already read 'static', check to see if there is one after the
3473 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003474 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003475 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003476
Chris Lattnere8074e62006-08-06 18:30:15 +00003477 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003478 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003479 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003480
Chris Lattner521ff2b2008-04-06 05:26:30 +00003481 // Handle the case where we have '[*]' as the array size. However, a leading
3482 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3483 // the the token after the star is a ']'. Since stars in arrays are
3484 // infrequent, use of lookahead is not costly here.
3485 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003486 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003487
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003488 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003489 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003490 StaticLoc = SourceLocation(); // Drop the static.
3491 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003492 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003493 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003494 // Note, in C89, this production uses the constant-expr production instead
3495 // of assignment-expr. The only difference is that assignment-expr allows
3496 // things like '=' and '*='. Sema rejects these in C89 mode because they
3497 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003498
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003499 // Parse the constant-expression or assignment-expression now (depending
3500 // on dialect).
3501 if (getLang().CPlusPlus)
3502 NumElements = ParseConstantExpression();
3503 else
3504 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003505 }
Mike Stump11289f42009-09-09 15:08:12 +00003506
Chris Lattner62591722006-08-12 18:40:58 +00003507 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003508 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003509 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003510 // If the expression was invalid, skip it.
3511 SkipUntil(tok::r_square);
3512 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003513 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003514
3515 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3516
Alexis Hunt96d5c762009-11-21 08:43:09 +00003517 //FIXME: Use these
3518 CXX0XAttributeList Attr;
3519 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3520 Attr = ParseCXX0XAttributes();
3521 }
3522
Chris Lattner84a11622008-12-18 07:27:21 +00003523 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003524 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3525 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003526 NumElements.release(),
3527 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003528 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003529}
3530
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003531/// [GNU] typeof-specifier:
3532/// typeof ( expressions )
3533/// typeof ( type-name )
3534/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003535///
3536void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003537 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003538 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003539 SourceLocation StartLoc = ConsumeToken();
3540
John McCalle8595032010-01-13 20:03:27 +00003541 const bool hasParens = Tok.is(tok::l_paren);
3542
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003543 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003544 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003545 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003546 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003547 isCastExpr,
3548 CastTy,
3549 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003550 if (hasParens)
3551 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003552
3553 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003554 // FIXME: Not accurate, the range gets one token more than it should.
3555 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003556 else
3557 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003558
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003559 if (isCastExpr) {
3560 if (!CastTy) {
3561 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003562 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003563 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003564
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003565 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003566 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003567 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3568 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003569 DiagID, CastTy))
3570 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003571 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003572 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003573
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003574 // If we get here, the operand to the typeof was an expresion.
3575 if (Operand.isInvalid()) {
3576 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003577 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003578 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003579
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003580 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003581 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003582 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3583 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003584 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003585 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003586}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003587
3588
3589/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3590/// from TryAltiVecVectorToken.
3591bool Parser::TryAltiVecVectorTokenOutOfLine() {
3592 Token Next = NextToken();
3593 switch (Next.getKind()) {
3594 default: return false;
3595 case tok::kw_short:
3596 case tok::kw_long:
3597 case tok::kw_signed:
3598 case tok::kw_unsigned:
3599 case tok::kw_void:
3600 case tok::kw_char:
3601 case tok::kw_int:
3602 case tok::kw_float:
3603 case tok::kw_double:
3604 case tok::kw_bool:
3605 case tok::kw___pixel:
3606 Tok.setKind(tok::kw___vector);
3607 return true;
3608 case tok::identifier:
3609 if (Next.getIdentifierInfo() == Ident_pixel) {
3610 Tok.setKind(tok::kw___vector);
3611 return true;
3612 }
3613 return false;
3614 }
3615}
3616
3617bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3618 const char *&PrevSpec, unsigned &DiagID,
3619 bool &isInvalid) {
3620 if (Tok.getIdentifierInfo() == Ident_vector) {
3621 Token Next = NextToken();
3622 switch (Next.getKind()) {
3623 case tok::kw_short:
3624 case tok::kw_long:
3625 case tok::kw_signed:
3626 case tok::kw_unsigned:
3627 case tok::kw_void:
3628 case tok::kw_char:
3629 case tok::kw_int:
3630 case tok::kw_float:
3631 case tok::kw_double:
3632 case tok::kw_bool:
3633 case tok::kw___pixel:
3634 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3635 return true;
3636 case tok::identifier:
3637 if (Next.getIdentifierInfo() == Ident_pixel) {
3638 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3639 return true;
3640 }
3641 break;
3642 default:
3643 break;
3644 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003645 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003646 DS.isTypeAltiVecVector()) {
3647 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3648 return true;
3649 }
3650 return false;
3651}
3652