blob: e6a4b91e2d1ce426f706417d099524a5cc3fb000 [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 Lattnerc0acd3d2006-07-31 05:13:43 +0000892 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000893 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000894 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000895 unsigned DiagID = 0;
896
Chris Lattner4d8f8732006-11-28 05:05:08 +0000897 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000898
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000899 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000900 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000901 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000902 // If this is not a declaration specifier token, we're done reading decl
903 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000904 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000905 return;
Mike Stump11289f42009-09-09 15:08:12 +0000906
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000907 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000908 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000909 if (DS.hasTypeSpecifier()) {
910 bool AllowNonIdentifiers
911 = (getCurScope()->getFlags() & (Scope::ControlScope |
912 Scope::BlockScope |
913 Scope::TemplateParamScope |
914 Scope::FunctionPrototypeScope |
915 Scope::AtCatchScope)) == 0;
916 bool AllowNestedNameSpecifiers
917 = DSContext == DSC_top_level ||
918 (DSContext == DSC_class && DS.isFriendSpecified());
919
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000920 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
921 AllowNonIdentifiers,
922 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000923 ConsumeCodeCompletionToken();
924 return;
925 }
926
927 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000928 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
929 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000930 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000931 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000932 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000933 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000934
935 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
936 ConsumeCodeCompletionToken();
937 return;
938 }
939
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000940 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000941 // C++ scope specifier. Annotate and loop, or bail out on error.
942 if (TryAnnotateCXXScopeToken(true)) {
943 if (!DS.hasTypeSpecifier())
944 DS.SetTypeSpecError();
945 goto DoneWithDeclSpec;
946 }
John McCall8bc2a702010-03-01 18:20:46 +0000947 if (Tok.is(tok::coloncolon)) // ::new or ::delete
948 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000949 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000950
951 case tok::annot_cxxscope: {
952 if (DS.hasTypeSpecifier())
953 goto DoneWithDeclSpec;
954
John McCall9dab4e62009-12-12 11:40:51 +0000955 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000956 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000957 SS.setRange(Tok.getAnnotationRange());
958
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000959 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000960 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000961 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000962 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000963 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000964 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000965
966 // C++ [class.qual]p2:
967 // In a lookup in which the constructor is an acceptable lookup
968 // result and the nested-name-specifier nominates a class C:
969 //
970 // - if the name specified after the
971 // nested-name-specifier, when looked up in C, is the
972 // injected-class-name of C (Clause 9), or
973 //
974 // - if the name specified after the nested-name-specifier
975 // is the same as the identifier or the
976 // simple-template-id's template-name in the last
977 // component of the nested-name-specifier,
978 //
979 // the name is instead considered to name the constructor of
980 // class C.
981 //
982 // Thus, if the template-name is actually the constructor
983 // name, then the code is ill-formed; this interpretation is
984 // reinforced by the NAD status of core issue 635.
985 TemplateIdAnnotation *TemplateId
986 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000987 if ((DSContext == DSC_top_level ||
988 (DSContext == DSC_class && DS.isFriendSpecified())) &&
989 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000990 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000991 if (isConstructorDeclarator()) {
992 // The user meant this to be an out-of-line constructor
993 // definition, but template arguments are not allowed
994 // there. Just allow this as a constructor; we'll
995 // complain about it later.
996 goto DoneWithDeclSpec;
997 }
998
999 // The user meant this to name a type, but it actually names
1000 // a constructor with some extraneous template
1001 // arguments. Complain, then parse it as a type as the user
1002 // intended.
1003 Diag(TemplateId->TemplateNameLoc,
1004 diag::err_out_of_line_template_id_names_constructor)
1005 << TemplateId->Name;
1006 }
1007
John McCall9dab4e62009-12-12 11:40:51 +00001008 DS.getTypeSpecScope() = SS;
1009 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001010 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001011 "ParseOptionalCXXScopeSpecifier not working");
1012 AnnotateTemplateIdTokenAsType(&SS);
1013 continue;
1014 }
1015
Douglas Gregorc5790df2009-09-28 07:26:33 +00001016 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001017 DS.getTypeSpecScope() = SS;
1018 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001019 if (Tok.getAnnotationValue()) {
1020 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorc5790df2009-09-28 07:26:33 +00001021 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallba7bf592010-08-24 05:47:05 +00001022 PrevSpec, DiagID, T);
1023 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001024 else
1025 DS.SetTypeSpecError();
1026 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1027 ConsumeToken(); // The typename
1028 }
1029
Douglas Gregor167fa622009-03-25 15:40:00 +00001030 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001031 goto DoneWithDeclSpec;
1032
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001033 // If we're in a context where the identifier could be a class name,
1034 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001035 if ((DSContext == DSC_top_level ||
1036 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001037 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001038 &SS)) {
1039 if (isConstructorDeclarator())
1040 goto DoneWithDeclSpec;
1041
1042 // As noted in C++ [class.qual]p2 (cited above), when the name
1043 // of the class is qualified in a context where it could name
1044 // a constructor, its a constructor name. However, we've
1045 // looked at the declarator, and the user probably meant this
1046 // to be a type. Complain that it isn't supposed to be treated
1047 // as a type, then proceed to parse it as a type.
1048 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1049 << Next.getIdentifierInfo();
1050 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001051
John McCallba7bf592010-08-24 05:47:05 +00001052 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1053 Next.getLocation(),
1054 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001055
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001056 // If the referenced identifier is not a type, then this declspec is
1057 // erroneous: We already checked about that it has no type specifier, and
1058 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001059 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001060 if (TypeRep == 0) {
1061 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001062 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001063 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001064 }
Mike Stump11289f42009-09-09 15:08:12 +00001065
John McCall9dab4e62009-12-12 11:40:51 +00001066 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001067 ConsumeToken(); // The C++ scope.
1068
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001070 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001071 if (isInvalid)
1072 break;
Mike Stump11289f42009-09-09 15:08:12 +00001073
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001074 DS.SetRangeEnd(Tok.getLocation());
1075 ConsumeToken(); // The typename.
1076
1077 continue;
1078 }
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattnere387d9e2009-01-21 19:48:37 +00001080 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001081 if (Tok.getAnnotationValue()) {
1082 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001084 DiagID, T);
1085 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001086 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001087
1088 if (isInvalid)
1089 break;
1090
Chris Lattnere387d9e2009-01-21 19:48:37 +00001091 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1092 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001093
Chris Lattnere387d9e2009-01-21 19:48:37 +00001094 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1095 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1096 // Objective-C interface. If we don't have Objective-C or a '<', this is
1097 // just a normal reference to a typedef name.
1098 if (!Tok.is(tok::less) || !getLang().ObjC1)
1099 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001100
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001101 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001102 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001103 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1104 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1105 LAngleLoc, EndProtoLoc);
1106 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1107 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001108
Chris Lattnere387d9e2009-01-21 19:48:37 +00001109 DS.SetRangeEnd(EndProtoLoc);
1110 continue;
1111 }
Mike Stump11289f42009-09-09 15:08:12 +00001112
Chris Lattner16fac4f2008-07-26 01:18:38 +00001113 // typedef-name
1114 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001115 // In C++, check to see if this is a scope specifier like foo::bar::, if
1116 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001117 if (getLang().CPlusPlus) {
1118 if (TryAnnotateCXXScopeToken(true)) {
1119 if (!DS.hasTypeSpecifier())
1120 DS.SetTypeSpecError();
1121 goto DoneWithDeclSpec;
1122 }
1123 if (!Tok.is(tok::identifier))
1124 continue;
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner16fac4f2008-07-26 01:18:38 +00001127 // This identifier can only be a typedef name if we haven't already seen
1128 // a type-specifier. Without this check we misparse:
1129 // typedef int X; struct Y { short X; }; as 'short int'.
1130 if (DS.hasTypeSpecifier())
1131 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001132
John Thompson22334602010-02-05 00:12:22 +00001133 // Check for need to substitute AltiVec keyword tokens.
1134 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1135 break;
1136
Chris Lattner16fac4f2008-07-26 01:18:38 +00001137 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001138 ParsedType TypeRep =
1139 Actions.getTypeName(*Tok.getIdentifierInfo(),
1140 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001141
Chris Lattner6cc055a2009-04-12 20:42:31 +00001142 // If this is not a typedef name, don't parse it as part of the declspec,
1143 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001144 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001145 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001146 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001147 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001148
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001149 // If we're in a context where the identifier could be a class name,
1150 // check whether this is a constructor declaration.
1151 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001152 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001153 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001154 goto DoneWithDeclSpec;
1155
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001156 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001157 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001158 if (isInvalid)
1159 break;
Mike Stump11289f42009-09-09 15:08:12 +00001160
Chris Lattner16fac4f2008-07-26 01:18:38 +00001161 DS.SetRangeEnd(Tok.getLocation());
1162 ConsumeToken(); // The identifier
1163
1164 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1165 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1166 // Objective-C interface. If we don't have Objective-C or a '<', this is
1167 // just a normal reference to a typedef name.
1168 if (!Tok.is(tok::less) || !getLang().ObjC1)
1169 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001170
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001171 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001172 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001173 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1174 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1175 LAngleLoc, EndProtoLoc);
1176 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1177 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattner16fac4f2008-07-26 01:18:38 +00001179 DS.SetRangeEnd(EndProtoLoc);
1180
Steve Naroffcd5e7822008-09-22 10:28:57 +00001181 // Need to support trailing type qualifiers (e.g. "id<p> const").
1182 // If a type specifier follows, it will be diagnosed elsewhere.
1183 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001184 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001185
1186 // type-name
1187 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001188 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001189 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001190 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001191 // This template-id does not refer to a type name, so we're
1192 // done with the type-specifiers.
1193 goto DoneWithDeclSpec;
1194 }
1195
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001196 // If we're in a context where the template-id could be a
1197 // constructor name or specialization, check whether this is a
1198 // constructor declaration.
1199 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001200 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001201 isConstructorDeclarator())
1202 goto DoneWithDeclSpec;
1203
Douglas Gregor7f741122009-02-25 19:37:18 +00001204 // Turn the template-id annotation token into a type annotation
1205 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001206 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001207 continue;
1208 }
1209
Chris Lattnere37e2332006-08-15 04:50:22 +00001210 // GNU attributes support.
1211 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001212 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001213 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001214
1215 // Microsoft declspec support.
1216 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001217 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001218 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001219
Steve Naroff44ac7772008-12-25 14:16:32 +00001220 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001221 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001222 // FIXME: Add handling here!
1223 break;
1224
1225 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001226 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001227 case tok::kw___cdecl:
1228 case tok::kw___stdcall:
1229 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001230 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001231 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1232 continue;
1233
Dawn Perchik335e16b2010-09-03 01:29:35 +00001234 // Borland single token adornments.
1235 case tok::kw___pascal:
1236 DS.AddAttributes(ParseBorlandTypeAttributes());
1237 continue;
1238
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001239 // storage-class-specifier
1240 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001241 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1242 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001243 break;
1244 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001245 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001246 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001247 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1248 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001249 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001250 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001251 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001252 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001253 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001254 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001255 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001256 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001257 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1258 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001259 break;
1260 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001261 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1263 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001264 else
John McCall49bfce42009-08-03 20:12:06 +00001265 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1266 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001267 break;
1268 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001269 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1270 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001271 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001272 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001273 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1274 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001275 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001276 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001277 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001278 break;
Mike Stump11289f42009-09-09 15:08:12 +00001279
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001280 // function-specifier
1281 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001282 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001283 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001284 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001285 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001286 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001287 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001288 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001289 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001290
Anders Carlssoncd8db412009-05-06 04:46:28 +00001291 // friend
1292 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001293 if (DSContext == DSC_class)
1294 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1295 else {
1296 PrevSpec = ""; // not actually used by the diagnostic
1297 DiagID = diag::err_friend_invalid_in_context;
1298 isInvalid = true;
1299 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001300 break;
Mike Stump11289f42009-09-09 15:08:12 +00001301
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001302 // constexpr
1303 case tok::kw_constexpr:
1304 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1305 break;
1306
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 // type-specifier
1308 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_long:
1313 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1315 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001316 else
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1318 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001319 break;
1320 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1322 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001323 break;
1324 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1326 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001327 break;
1328 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1330 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001331 break;
1332 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1334 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1338 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001339 break;
1340 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1342 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001343 break;
1344 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1346 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001347 break;
1348 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1350 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001351 break;
1352 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1354 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001355 break;
1356 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1358 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001359 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001360 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1362 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001363 break;
1364 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001365 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1366 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001367 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001368 case tok::kw_bool:
1369 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1371 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001372 break;
1373 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1375 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001376 break;
1377 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1379 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001380 break;
1381 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1383 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001384 break;
John Thompson22334602010-02-05 00:12:22 +00001385 case tok::kw___vector:
1386 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1387 break;
1388 case tok::kw___pixel:
1389 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1390 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001391
1392 // class-specifier:
1393 case tok::kw_class:
1394 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001395 case tok::kw_union: {
1396 tok::TokenKind Kind = Tok.getKind();
1397 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001398 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001399 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001400 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001401
1402 // enum-specifier:
1403 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001404 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001405 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001406 continue;
1407
1408 // cv-qualifier:
1409 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001410 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1411 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001412 break;
1413 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001414 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1415 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001416 break;
1417 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001418 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1419 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001420 break;
1421
Douglas Gregor333489b2009-03-27 23:10:48 +00001422 // C++ typename-specifier:
1423 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001424 if (TryAnnotateTypeOrScopeToken()) {
1425 DS.SetTypeSpecError();
1426 goto DoneWithDeclSpec;
1427 }
1428 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001429 continue;
1430 break;
1431
Chris Lattnere387d9e2009-01-21 19:48:37 +00001432 // GNU typeof support.
1433 case tok::kw_typeof:
1434 ParseTypeofSpecifier(DS);
1435 continue;
1436
Anders Carlsson74948d02009-06-24 17:47:40 +00001437 case tok::kw_decltype:
1438 ParseDecltypeSpecifier(DS);
1439 continue;
1440
Steve Naroffcfdf6162008-06-05 00:02:44 +00001441 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001442 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001443 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1444 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001445 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001446 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001447
Chris Lattner0974b232008-07-26 00:20:22 +00001448 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001449 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001450 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001451 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1452 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1453 LAngleLoc, EndProtoLoc);
1454 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1455 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001456 DS.SetRangeEnd(EndProtoLoc);
1457
Chris Lattner6d29c102008-11-18 07:48:38 +00001458 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001459 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001460 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001461 // Need to support trailing type qualifiers (e.g. "id<p> const").
1462 // If a type specifier follows, it will be diagnosed elsewhere.
1463 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001464 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001465 }
John McCall49bfce42009-08-03 20:12:06 +00001466 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001467 if (isInvalid) {
1468 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001469 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001470
1471 if (DiagID == diag::ext_duplicate_declspec)
1472 Diag(Tok, DiagID)
1473 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1474 else
1475 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001476 }
Chris Lattner2e232092008-03-13 06:29:04 +00001477 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001478 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001479 }
1480}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001481
Chris Lattnera448d752009-01-06 06:59:53 +00001482/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001483/// primarily follow the C++ grammar with additions for C99 and GNU,
1484/// which together subsume the C grammar. Note that the C++
1485/// type-specifier also includes the C type-qualifier (for const,
1486/// volatile, and C99 restrict). Returns true if a type-specifier was
1487/// found (and parsed), false otherwise.
1488///
1489/// type-specifier: [C++ 7.1.5]
1490/// simple-type-specifier
1491/// class-specifier
1492/// enum-specifier
1493/// elaborated-type-specifier [TODO]
1494/// cv-qualifier
1495///
1496/// cv-qualifier: [C++ 7.1.5.1]
1497/// 'const'
1498/// 'volatile'
1499/// [C99] 'restrict'
1500///
1501/// simple-type-specifier: [ C++ 7.1.5.2]
1502/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1503/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1504/// 'char'
1505/// 'wchar_t'
1506/// 'bool'
1507/// 'short'
1508/// 'int'
1509/// 'long'
1510/// 'signed'
1511/// 'unsigned'
1512/// 'float'
1513/// 'double'
1514/// 'void'
1515/// [C99] '_Bool'
1516/// [C99] '_Complex'
1517/// [C99] '_Imaginary' // Removed in TC2?
1518/// [GNU] '_Decimal32'
1519/// [GNU] '_Decimal64'
1520/// [GNU] '_Decimal128'
1521/// [GNU] typeof-specifier
1522/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1523/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001524/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001525/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001526bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001527 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001528 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001529 const ParsedTemplateInfo &TemplateInfo,
1530 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001531 SourceLocation Loc = Tok.getLocation();
1532
1533 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001534 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001535 // If we already have a type specifier, this identifier is not a type.
1536 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1537 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1538 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1539 return false;
John Thompson22334602010-02-05 00:12:22 +00001540 // Check for need to substitute AltiVec keyword tokens.
1541 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1542 break;
1543 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001544 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001545 // Annotate typenames and C++ scope specifiers. If we get one, just
1546 // recurse to handle whatever we get.
1547 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001548 return true;
1549 if (Tok.is(tok::identifier))
1550 return false;
1551 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1552 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001553 case tok::coloncolon: // ::foo::bar
1554 if (NextToken().is(tok::kw_new) || // ::new
1555 NextToken().is(tok::kw_delete)) // ::delete
1556 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001557
Chris Lattner020bab92009-01-04 23:41:41 +00001558 // Annotate typenames and C++ scope specifiers. If we get one, just
1559 // recurse to handle whatever we get.
1560 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001561 return true;
1562 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1563 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001564
Douglas Gregor450c75a2008-11-07 15:42:26 +00001565 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001566 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001567 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001568 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001569 DiagID, T);
1570 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001571 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001572 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1573 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001574
Douglas Gregor450c75a2008-11-07 15:42:26 +00001575 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1576 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1577 // Objective-C interface. If we don't have Objective-C or a '<', this is
1578 // just a normal reference to a typedef name.
1579 if (!Tok.is(tok::less) || !getLang().ObjC1)
1580 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001581
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001582 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001583 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001584 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1585 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1586 LAngleLoc, EndProtoLoc);
1587 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1588 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001589
Douglas Gregor450c75a2008-11-07 15:42:26 +00001590 DS.SetRangeEnd(EndProtoLoc);
1591 return true;
1592 }
1593
1594 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_long:
1598 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001599 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1600 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001601 else
John McCall49bfce42009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1603 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001604 break;
1605 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001606 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1610 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001611 break;
1612 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1614 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001615 break;
1616 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001617 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1618 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001619 break;
1620 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001621 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001622 break;
1623 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001625 break;
1626 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001627 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001628 break;
1629 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001630 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001631 break;
1632 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001633 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001634 break;
1635 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001637 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001638 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001639 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001640 break;
1641 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001642 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001643 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001644 case tok::kw_bool:
1645 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001646 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001647 break;
1648 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1650 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001651 break;
1652 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001653 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1654 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001655 break;
1656 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001657 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1658 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001659 break;
John Thompson22334602010-02-05 00:12:22 +00001660 case tok::kw___vector:
1661 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1662 break;
1663 case tok::kw___pixel:
1664 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1665 break;
1666
Douglas Gregor450c75a2008-11-07 15:42:26 +00001667 // class-specifier:
1668 case tok::kw_class:
1669 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001670 case tok::kw_union: {
1671 tok::TokenKind Kind = Tok.getKind();
1672 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001673 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1674 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001675 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001676 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001677
1678 // enum-specifier:
1679 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001680 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001681 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001682 return true;
1683
1684 // cv-qualifier:
1685 case tok::kw_const:
1686 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001687 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001688 break;
1689 case tok::kw_volatile:
1690 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001691 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001692 break;
1693 case tok::kw_restrict:
1694 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001695 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001696 break;
1697
1698 // GNU typeof support.
1699 case tok::kw_typeof:
1700 ParseTypeofSpecifier(DS);
1701 return true;
1702
Anders Carlsson74948d02009-06-24 17:47:40 +00001703 // C++0x decltype support.
1704 case tok::kw_decltype:
1705 ParseDecltypeSpecifier(DS);
1706 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001707
Anders Carlssonbae27372009-06-26 23:44:14 +00001708 // C++0x auto support.
1709 case tok::kw_auto:
1710 if (!getLang().CPlusPlus0x)
1711 return false;
1712
John McCall49bfce42009-08-03 20:12:06 +00001713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001714 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001715
Eli Friedman53339e02009-06-08 23:27:34 +00001716 case tok::kw___ptr64:
1717 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001718 case tok::kw___cdecl:
1719 case tok::kw___stdcall:
1720 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001721 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001722 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001723 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001724
Dawn Perchik335e16b2010-09-03 01:29:35 +00001725 case tok::kw___pascal:
1726 DS.AddAttributes(ParseBorlandTypeAttributes());
1727 return true;
1728
Douglas Gregor450c75a2008-11-07 15:42:26 +00001729 default:
1730 // Not a type-specifier; do nothing.
1731 return false;
1732 }
1733
1734 // If the specifier combination wasn't legal, issue a diagnostic.
1735 if (isInvalid) {
1736 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001737 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001738 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001739 }
1740 DS.SetRangeEnd(Tok.getLocation());
1741 ConsumeToken(); // whatever we parsed above.
1742 return true;
1743}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001744
Chris Lattner70ae4912007-10-29 04:42:53 +00001745/// ParseStructDeclaration - Parse a struct declaration without the terminating
1746/// semicolon.
1747///
Chris Lattner90a26b02007-01-23 04:38:16 +00001748/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001749/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001750/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001751/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001752/// struct-declarator-list:
1753/// struct-declarator
1754/// struct-declarator-list ',' struct-declarator
1755/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1756/// struct-declarator:
1757/// declarator
1758/// [GNU] declarator attributes[opt]
1759/// declarator[opt] ':' constant-expression
1760/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1761///
Chris Lattnera12405b2008-04-10 06:46:29 +00001762void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001763ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001764 if (Tok.is(tok::kw___extension__)) {
1765 // __extension__ silences extension warnings in the subexpression.
1766 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001767 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001768 return ParseStructDeclaration(DS, Fields);
1769 }
Mike Stump11289f42009-09-09 15:08:12 +00001770
Steve Naroff97170802007-08-20 22:28:22 +00001771 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001772 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001773 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001774
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001775 // If there are no declarators, this is a free-standing declaration
1776 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001777 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001778 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001779 return;
1780 }
1781
1782 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001783 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001784 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001785 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001786 FieldDeclarator DeclaratorInfo(DS);
1787
1788 // Attributes are only allowed here on successive declarators.
1789 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1790 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001791 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001792 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1793 }
Mike Stump11289f42009-09-09 15:08:12 +00001794
Steve Naroff97170802007-08-20 22:28:22 +00001795 /// struct-declarator: declarator
1796 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001797 if (Tok.isNot(tok::colon)) {
1798 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1799 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001800 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001801 }
Mike Stump11289f42009-09-09 15:08:12 +00001802
Chris Lattner76c72282007-10-09 17:33:22 +00001803 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001804 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001805 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001806 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001807 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001808 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001809 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001810 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001811
Steve Naroff97170802007-08-20 22:28:22 +00001812 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001813 if (Tok.is(tok::kw___attribute)) {
1814 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001815 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001816 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1817 }
1818
John McCallcfefb6d2009-11-03 02:38:08 +00001819 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001820 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001821 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001822
Steve Naroff97170802007-08-20 22:28:22 +00001823 // If we don't have a comma, it is either the end of the list (a ';')
1824 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001825 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001826 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001827
Steve Naroff97170802007-08-20 22:28:22 +00001828 // Consume the comma.
1829 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001830
John McCallcfefb6d2009-11-03 02:38:08 +00001831 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001832 }
Steve Naroff97170802007-08-20 22:28:22 +00001833}
1834
1835/// ParseStructUnionBody
1836/// struct-contents:
1837/// struct-declaration-list
1838/// [EXT] empty
1839/// [GNU] "struct-declaration-list" without terminatoring ';'
1840/// struct-declaration-list:
1841/// struct-declaration
1842/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001843/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001844///
Chris Lattner1300fb92007-01-23 23:42:53 +00001845void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001846 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001847 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1848 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001849
Chris Lattner90a26b02007-01-23 04:38:16 +00001850 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001851
Douglas Gregor658b9552009-01-09 22:42:13 +00001852 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001853 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001854
Chris Lattner7b9ace62007-01-23 20:11:08 +00001855 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1856 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001857 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001858 Diag(Tok, diag::ext_empty_struct_union)
1859 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001860
John McCall48871652010-08-21 09:40:31 +00001861 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001862
Chris Lattner7b9ace62007-01-23 20:11:08 +00001863 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001864 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001865 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001866
Chris Lattner736ed5d2007-06-09 05:59:07 +00001867 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001868 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001869 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001870 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001871 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001872 ConsumeToken();
1873 continue;
1874 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001875
1876 // Parse all the comma separated declarators.
1877 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001878
John McCallcfefb6d2009-11-03 02:38:08 +00001879 if (!Tok.is(tok::at)) {
1880 struct CFieldCallback : FieldCallback {
1881 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001882 Decl *TagDecl;
1883 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001884
John McCall48871652010-08-21 09:40:31 +00001885 CFieldCallback(Parser &P, Decl *TagDecl,
1886 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001887 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1888
John McCall48871652010-08-21 09:40:31 +00001889 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001890 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001891 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001892 FD.D.getDeclSpec().getSourceRange().getBegin(),
1893 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001894 FieldDecls.push_back(Field);
1895 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001896 }
John McCallcfefb6d2009-11-03 02:38:08 +00001897 } Callback(*this, TagDecl, FieldDecls);
1898
1899 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001900 } else { // Handle @defs
1901 ConsumeToken();
1902 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1903 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001904 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001905 continue;
1906 }
1907 ConsumeToken();
1908 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1909 if (!Tok.is(tok::identifier)) {
1910 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001911 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001912 continue;
1913 }
John McCall48871652010-08-21 09:40:31 +00001914 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001915 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001916 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001917 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1918 ConsumeToken();
1919 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001920 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001921
Chris Lattner76c72282007-10-09 17:33:22 +00001922 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001923 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001924 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001925 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001926 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001927 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001928 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1929 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001930 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001931 // If we stopped at a ';', eat it.
1932 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001933 }
1934 }
Mike Stump11289f42009-09-09 15:08:12 +00001935
Steve Naroff33a1e802007-10-29 21:38:07 +00001936 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001937
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001938 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001939 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001940 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001941 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001942
Douglas Gregor0be31a22010-07-02 17:43:08 +00001943 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001944 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001945 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001946 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001947 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001948 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001949}
1950
1951
Chris Lattner3b561a32006-08-13 00:12:11 +00001952/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001953/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001954/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001955///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001956/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1957/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001958/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001959/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001960///
1961/// [C++] elaborated-type-specifier:
1962/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1963///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001964void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001965 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001966 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001967 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001968 if (Tok.is(tok::code_completion)) {
1969 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001970 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001971 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001972 }
1973
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001974 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001975 // If attributes exist after tag, parse them.
1976 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001977 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001978
Abramo Bagnarad7548482010-05-19 21:37:53 +00001979 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001980 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001981 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001982 return;
1983
1984 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001985 Diag(Tok, diag::err_expected_ident);
1986 if (Tok.isNot(tok::l_brace)) {
1987 // Has no name and is not a definition.
1988 // Skip the rest of this declarator, up until the comma or semicolon.
1989 SkipUntil(tok::comma, true);
1990 return;
1991 }
1992 }
1993 }
Mike Stump11289f42009-09-09 15:08:12 +00001994
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001995 // Must have either 'enum name' or 'enum {...}'.
1996 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1997 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001998
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001999 // Skip the rest of this declarator, up until the comma or semicolon.
2000 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002001 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002002 }
Mike Stump11289f42009-09-09 15:08:12 +00002003
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002004 // If an identifier is present, consume and remember it.
2005 IdentifierInfo *Name = 0;
2006 SourceLocation NameLoc;
2007 if (Tok.is(tok::identifier)) {
2008 Name = Tok.getIdentifierInfo();
2009 NameLoc = ConsumeToken();
2010 }
Mike Stump11289f42009-09-09 15:08:12 +00002011
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002012 // There are three options here. If we have 'enum foo;', then this is a
2013 // forward declaration. If we have 'enum foo {...' then this is a
2014 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2015 //
2016 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2017 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2018 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2019 //
John McCallfaf5fb42010-08-26 23:41:50 +00002020 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002021 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002022 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002023 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002024 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002025 else
John McCallfaf5fb42010-08-26 23:41:50 +00002026 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002027
2028 // enums cannot be templates, although they can be referenced from a
2029 // template.
2030 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002031 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002032 Diag(Tok, diag::err_enum_template);
2033
2034 // Skip the rest of this declarator, up until the comma or semicolon.
2035 SkipUntil(tok::comma, true);
2036 return;
2037 }
2038
Douglas Gregord6ab8742009-05-28 23:31:59 +00002039 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002040 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002041 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2042 const char *PrevSpec = 0;
2043 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002044 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2045 StartLoc, SS, Name, NameLoc, Attr.get(),
2046 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002047 MultiTemplateParamsArg(Actions),
John McCall48871652010-08-21 09:40:31 +00002048 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00002049 if (IsDependent) {
2050 // This enum has a dependent nested-name-specifier. Handle it as a
2051 // dependent tag.
2052 if (!Name) {
2053 DS.SetTypeSpecError();
2054 Diag(Tok, diag::err_expected_type_name_after_typename);
2055 return;
2056 }
2057
Douglas Gregor0be31a22010-07-02 17:43:08 +00002058 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002059 TUK, SS, Name, StartLoc,
2060 NameLoc);
2061 if (Type.isInvalid()) {
2062 DS.SetTypeSpecError();
2063 return;
2064 }
2065
2066 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002067 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002068 Diag(StartLoc, DiagID) << PrevSpec;
2069
2070 return;
2071 }
Mike Stump11289f42009-09-09 15:08:12 +00002072
John McCall48871652010-08-21 09:40:31 +00002073 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002074 // The action failed to produce an enumeration tag. If this is a
2075 // definition, consume the entire definition.
2076 if (Tok.is(tok::l_brace)) {
2077 ConsumeBrace();
2078 SkipUntil(tok::r_brace);
2079 }
2080
2081 DS.SetTypeSpecError();
2082 return;
2083 }
2084
Chris Lattner76c72282007-10-09 17:33:22 +00002085 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002086 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002087
John McCallba7bf592010-08-24 05:47:05 +00002088 // FIXME: The DeclSpec should keep the locations of both the keyword
2089 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002090 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002091 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002092 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002093}
2094
Chris Lattnerc1915e22007-01-25 07:29:02 +00002095/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2096/// enumerator-list:
2097/// enumerator
2098/// enumerator-list ',' enumerator
2099/// enumerator:
2100/// enumeration-constant
2101/// enumeration-constant '=' constant-expression
2102/// enumeration-constant:
2103/// identifier
2104///
John McCall48871652010-08-21 09:40:31 +00002105void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002106 // Enter the scope of the enum body and start the definition.
2107 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002108 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002109
Chris Lattnerc1915e22007-01-25 07:29:02 +00002110 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002111
Chris Lattner37256fb2007-08-27 17:24:30 +00002112 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002113 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002114 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002115
John McCall48871652010-08-21 09:40:31 +00002116 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002117
John McCall48871652010-08-21 09:40:31 +00002118 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002119
Chris Lattnerc1915e22007-01-25 07:29:02 +00002120 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002121 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002122 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2123 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002124
Chris Lattnerc1915e22007-01-25 07:29:02 +00002125 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002126 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002127 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002128 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002129 AssignedVal = ParseConstantExpression();
2130 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002131 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002132 }
Mike Stump11289f42009-09-09 15:08:12 +00002133
Chris Lattnerc1915e22007-01-25 07:29:02 +00002134 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002135 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2136 LastEnumConstDecl,
2137 IdentLoc, Ident,
2138 EqualLoc,
2139 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002140 EnumConstantDecls.push_back(EnumConstDecl);
2141 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002142
Douglas Gregorce66d022010-09-07 14:51:08 +00002143 if (Tok.is(tok::identifier)) {
2144 // We're missing a comma between enumerators.
2145 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2146 Diag(Loc, diag::err_enumerator_list_missing_comma)
2147 << FixItHint::CreateInsertion(Loc, ", ");
2148 continue;
2149 }
2150
Chris Lattner76c72282007-10-09 17:33:22 +00002151 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002152 break;
2153 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002154
2155 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002156 !(getLang().C99 || getLang().CPlusPlus0x))
2157 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2158 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002159 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002160 }
Mike Stump11289f42009-09-09 15:08:12 +00002161
Chris Lattnerc1915e22007-01-25 07:29:02 +00002162 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002163 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002164
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002165 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002166 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002167 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002168 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002169
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002170 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2171 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00002172 getCurScope(), Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002173
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002174 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002175 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002176}
Chris Lattner3b561a32006-08-13 00:12:11 +00002177
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002178/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002179/// start of a type-qualifier-list.
2180bool Parser::isTypeQualifier() const {
2181 switch (Tok.getKind()) {
2182 default: return false;
2183 // type-qualifier
2184 case tok::kw_const:
2185 case tok::kw_volatile:
2186 case tok::kw_restrict:
2187 return true;
2188 }
2189}
2190
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002191/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2192/// is definitely a type-specifier. Return false if it isn't part of a type
2193/// specifier or if we're not sure.
2194bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2195 switch (Tok.getKind()) {
2196 default: return false;
2197 // type-specifiers
2198 case tok::kw_short:
2199 case tok::kw_long:
2200 case tok::kw_signed:
2201 case tok::kw_unsigned:
2202 case tok::kw__Complex:
2203 case tok::kw__Imaginary:
2204 case tok::kw_void:
2205 case tok::kw_char:
2206 case tok::kw_wchar_t:
2207 case tok::kw_char16_t:
2208 case tok::kw_char32_t:
2209 case tok::kw_int:
2210 case tok::kw_float:
2211 case tok::kw_double:
2212 case tok::kw_bool:
2213 case tok::kw__Bool:
2214 case tok::kw__Decimal32:
2215 case tok::kw__Decimal64:
2216 case tok::kw__Decimal128:
2217 case tok::kw___vector:
2218
2219 // struct-or-union-specifier (C99) or class-specifier (C++)
2220 case tok::kw_class:
2221 case tok::kw_struct:
2222 case tok::kw_union:
2223 // enum-specifier
2224 case tok::kw_enum:
2225
2226 // typedef-name
2227 case tok::annot_typename:
2228 return true;
2229 }
2230}
2231
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002232/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002233/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002234bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002235 switch (Tok.getKind()) {
2236 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002237
Chris Lattner020bab92009-01-04 23:41:41 +00002238 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002239 if (TryAltiVecVectorToken())
2240 return true;
2241 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002242 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002243 // Annotate typenames and C++ scope specifiers. If we get one, just
2244 // recurse to handle whatever we get.
2245 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002246 return true;
2247 if (Tok.is(tok::identifier))
2248 return false;
2249 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002250
Chris Lattner020bab92009-01-04 23:41:41 +00002251 case tok::coloncolon: // ::foo::bar
2252 if (NextToken().is(tok::kw_new) || // ::new
2253 NextToken().is(tok::kw_delete)) // ::delete
2254 return false;
2255
Chris Lattner020bab92009-01-04 23:41:41 +00002256 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002257 return true;
2258 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002259
Chris Lattnere37e2332006-08-15 04:50:22 +00002260 // GNU attributes support.
2261 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002262 // GNU typeof support.
2263 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002264
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002265 // type-specifiers
2266 case tok::kw_short:
2267 case tok::kw_long:
2268 case tok::kw_signed:
2269 case tok::kw_unsigned:
2270 case tok::kw__Complex:
2271 case tok::kw__Imaginary:
2272 case tok::kw_void:
2273 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002274 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002275 case tok::kw_char16_t:
2276 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002277 case tok::kw_int:
2278 case tok::kw_float:
2279 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002280 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002281 case tok::kw__Bool:
2282 case tok::kw__Decimal32:
2283 case tok::kw__Decimal64:
2284 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002285 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002286
Chris Lattner861a2262008-04-13 18:59:07 +00002287 // struct-or-union-specifier (C99) or class-specifier (C++)
2288 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002289 case tok::kw_struct:
2290 case tok::kw_union:
2291 // enum-specifier
2292 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002293
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002294 // type-qualifier
2295 case tok::kw_const:
2296 case tok::kw_volatile:
2297 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002298
2299 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002300 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002301 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002302
Chris Lattner409bf7d2008-10-20 00:25:30 +00002303 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2304 case tok::less:
2305 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002306
Steve Naroff44ac7772008-12-25 14:16:32 +00002307 case tok::kw___cdecl:
2308 case tok::kw___stdcall:
2309 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002310 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002311 case tok::kw___w64:
2312 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002313 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002314 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002315 }
2316}
2317
Chris Lattneracd58a32006-08-06 17:24:14 +00002318/// isDeclarationSpecifier() - Return true if the current token is part of a
2319/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002320///
2321/// \param DisambiguatingWithExpression True to indicate that the purpose of
2322/// this check is to disambiguate between an expression and a declaration.
2323bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002324 switch (Tok.getKind()) {
2325 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002326
Chris Lattner020bab92009-01-04 23:41:41 +00002327 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002328 // Unfortunate hack to support "Class.factoryMethod" notation.
2329 if (getLang().ObjC1 && NextToken().is(tok::period))
2330 return false;
John Thompson22334602010-02-05 00:12:22 +00002331 if (TryAltiVecVectorToken())
2332 return true;
2333 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002334 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002335 // Annotate typenames and C++ scope specifiers. If we get one, just
2336 // recurse to handle whatever we get.
2337 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002338 return true;
2339 if (Tok.is(tok::identifier))
2340 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002341
2342 // If we're in Objective-C and we have an Objective-C class type followed
2343 // by an identifier and then either ':' or ']', in a place where an
2344 // expression is permitted, then this is probably a class message send
2345 // missing the initial '['. In this case, we won't consider this to be
2346 // the start of a declaration.
2347 if (DisambiguatingWithExpression &&
2348 isStartOfObjCClassMessageMissingOpenBracket())
2349 return false;
2350
John McCall1f476a12010-02-26 08:45:28 +00002351 return isDeclarationSpecifier();
2352
Chris Lattner020bab92009-01-04 23:41:41 +00002353 case tok::coloncolon: // ::foo::bar
2354 if (NextToken().is(tok::kw_new) || // ::new
2355 NextToken().is(tok::kw_delete)) // ::delete
2356 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002357
Chris Lattner020bab92009-01-04 23:41:41 +00002358 // Annotate typenames and C++ scope specifiers. If we get one, just
2359 // recurse to handle whatever we get.
2360 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002361 return true;
2362 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002363
Chris Lattneracd58a32006-08-06 17:24:14 +00002364 // storage-class-specifier
2365 case tok::kw_typedef:
2366 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002367 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002368 case tok::kw_static:
2369 case tok::kw_auto:
2370 case tok::kw_register:
2371 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002372
Chris Lattneracd58a32006-08-06 17:24:14 +00002373 // type-specifiers
2374 case tok::kw_short:
2375 case tok::kw_long:
2376 case tok::kw_signed:
2377 case tok::kw_unsigned:
2378 case tok::kw__Complex:
2379 case tok::kw__Imaginary:
2380 case tok::kw_void:
2381 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002382 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002383 case tok::kw_char16_t:
2384 case tok::kw_char32_t:
2385
Chris Lattneracd58a32006-08-06 17:24:14 +00002386 case tok::kw_int:
2387 case tok::kw_float:
2388 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002389 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002390 case tok::kw__Bool:
2391 case tok::kw__Decimal32:
2392 case tok::kw__Decimal64:
2393 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002394 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002395
Chris Lattner861a2262008-04-13 18:59:07 +00002396 // struct-or-union-specifier (C99) or class-specifier (C++)
2397 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002398 case tok::kw_struct:
2399 case tok::kw_union:
2400 // enum-specifier
2401 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002402
Chris Lattneracd58a32006-08-06 17:24:14 +00002403 // type-qualifier
2404 case tok::kw_const:
2405 case tok::kw_volatile:
2406 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002407
Chris Lattneracd58a32006-08-06 17:24:14 +00002408 // function-specifier
2409 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002410 case tok::kw_virtual:
2411 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002412
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002413 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002414 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002415
Chris Lattner599e47e2007-08-09 17:01:07 +00002416 // GNU typeof support.
2417 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002418
Chris Lattner599e47e2007-08-09 17:01:07 +00002419 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002420 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002421 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002422
Chris Lattner8b2ec162008-07-26 03:38:44 +00002423 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2424 case tok::less:
2425 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002426
Steve Narofff192fab2009-01-06 19:34:12 +00002427 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002428 case tok::kw___cdecl:
2429 case tok::kw___stdcall:
2430 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002431 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002432 case tok::kw___w64:
2433 case tok::kw___ptr64:
2434 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002435 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002436 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002437 }
2438}
2439
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002440bool Parser::isConstructorDeclarator() {
2441 TentativeParsingAction TPA(*this);
2442
2443 // Parse the C++ scope specifier.
2444 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002445 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002446 TPA.Revert();
2447 return false;
2448 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002449
2450 // Parse the constructor name.
2451 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2452 // We already know that we have a constructor name; just consume
2453 // the token.
2454 ConsumeToken();
2455 } else {
2456 TPA.Revert();
2457 return false;
2458 }
2459
2460 // Current class name must be followed by a left parentheses.
2461 if (Tok.isNot(tok::l_paren)) {
2462 TPA.Revert();
2463 return false;
2464 }
2465 ConsumeParen();
2466
2467 // A right parentheses or ellipsis signals that we have a constructor.
2468 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2469 TPA.Revert();
2470 return true;
2471 }
2472
2473 // If we need to, enter the specified scope.
2474 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002475 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002476 DeclScopeObj.EnterDeclaratorScope();
2477
2478 // Check whether the next token(s) are part of a declaration
2479 // specifier, in which case we have the start of a parameter and,
2480 // therefore, we know that this is a constructor.
2481 bool IsConstructor = isDeclarationSpecifier();
2482 TPA.Revert();
2483 return IsConstructor;
2484}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002485
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002486/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002487/// type-qualifier-list: [C99 6.7.5]
2488/// type-qualifier
2489/// [vendor] attributes
2490/// [ only if VendorAttributesAllowed=true ]
2491/// type-qualifier-list type-qualifier
2492/// [vendor] type-qualifier-list attributes
2493/// [ only if VendorAttributesAllowed=true ]
2494/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2495/// [ only if CXX0XAttributesAllowed=true ]
2496/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002497///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002498void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2499 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002500 bool CXX0XAttributesAllowed) {
2501 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2502 SourceLocation Loc = Tok.getLocation();
2503 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2504 if (CXX0XAttributesAllowed)
2505 DS.AddAttributes(Attr.AttrList);
2506 else
2507 Diag(Loc, diag::err_attributes_not_allowed);
2508 }
2509
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002510 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002511 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002512 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002513 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002514 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002515
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002516 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002517 case tok::code_completion:
2518 Actions.CodeCompleteTypeQualifiers(DS);
2519 ConsumeCodeCompletionToken();
2520 break;
2521
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002522 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002523 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2524 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002525 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002526 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002527 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2528 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002529 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002530 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002531 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2532 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002533 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002534 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002535 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002536 case tok::kw___cdecl:
2537 case tok::kw___stdcall:
2538 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002539 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002540 if (VendorAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002541 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2542 continue;
2543 }
2544 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002545 case tok::kw___pascal:
2546 if (VendorAttributesAllowed) {
2547 DS.AddAttributes(ParseBorlandTypeAttributes());
2548 continue;
2549 }
2550 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002551 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002552 if (VendorAttributesAllowed) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002553 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002554 continue; // do *not* consume the next token!
2555 }
2556 // otherwise, FALL THROUGH!
2557 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002558 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002559 // If this is not a type-qualifier token, we're done reading type
2560 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002561 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002562 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002563 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002564
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002565 // If the specifier combination wasn't legal, issue a diagnostic.
2566 if (isInvalid) {
2567 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002568 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002569 }
2570 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002571 }
2572}
2573
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002574
2575/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2576///
2577void Parser::ParseDeclarator(Declarator &D) {
2578 /// This implements the 'declarator' production in the C grammar, then checks
2579 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002580 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002581}
2582
Sebastian Redlbd150f42008-11-21 19:14:01 +00002583/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2584/// is parsed by the function passed to it. Pass null, and the direct-declarator
2585/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002586/// ptr-operator production.
2587///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002588/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2589/// [C] pointer[opt] direct-declarator
2590/// [C++] direct-declarator
2591/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002592///
2593/// pointer: [C99 6.7.5]
2594/// '*' type-qualifier-list[opt]
2595/// '*' type-qualifier-list[opt] pointer
2596///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002597/// ptr-operator:
2598/// '*' cv-qualifier-seq[opt]
2599/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002600/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002601/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002602/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002603/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002604void Parser::ParseDeclaratorInternal(Declarator &D,
2605 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002606 if (Diags.hasAllExtensionsSilenced())
2607 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002608
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002609 // C++ member pointers start with a '::' or a nested-name.
2610 // Member pointers get special handling, since there's no place for the
2611 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002612 if (getLang().CPlusPlus &&
2613 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2614 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002615 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002616 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002617
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002618 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002619 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002620 // The scope spec really belongs to the direct-declarator.
2621 D.getCXXScopeSpec() = SS;
2622 if (DirectDeclParser)
2623 (this->*DirectDeclParser)(D);
2624 return;
2625 }
2626
2627 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002628 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002629 DeclSpec DS;
2630 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002631 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002632
2633 // Recurse to parse whatever is left.
2634 ParseDeclaratorInternal(D, DirectDeclParser);
2635
2636 // Sema will have to catch (syntactically invalid) pointers into global
2637 // scope. It has to catch pointers into namespace scope anyway.
2638 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002639 Loc, DS.TakeAttributes()),
2640 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002641 return;
2642 }
2643 }
2644
2645 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002646 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002647 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002648 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002649 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002650 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002651 if (DirectDeclParser)
2652 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002653 return;
2654 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002655
Sebastian Redled0f3b02009-03-15 22:02:01 +00002656 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2657 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002658 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002659 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002660
Chris Lattner9eac9312009-03-27 04:18:06 +00002661 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002662 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002663 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002664
Bill Wendling3708c182007-05-27 10:15:43 +00002665 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002666 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002667
Bill Wendling3708c182007-05-27 10:15:43 +00002668 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002669 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002670 if (Kind == tok::star)
2671 // Remember that we parsed a pointer type, and remember the type-quals.
2672 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002673 DS.TakeAttributes()),
2674 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002675 else
2676 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002677 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002678 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002679 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002680 } else {
2681 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002682 DeclSpec DS;
2683
Sebastian Redl3b27be62009-03-23 00:00:23 +00002684 // Complain about rvalue references in C++03, but then go on and build
2685 // the declarator.
2686 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2687 Diag(Loc, diag::err_rvalue_reference);
2688
Bill Wendling93efb222007-06-02 23:28:54 +00002689 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2690 // cv-qualifiers are introduced through the use of a typedef or of a
2691 // template type argument, in which case the cv-qualifiers are ignored.
2692 //
2693 // [GNU] Retricted references are allowed.
2694 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002695 // [C++0x] Attributes on references are not allowed.
2696 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002697 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002698
2699 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2700 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2701 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002702 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002703 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2704 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002705 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002706 }
Bill Wendling3708c182007-05-27 10:15:43 +00002707
2708 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002709 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002710
Douglas Gregor66583c52008-11-03 15:51:28 +00002711 if (D.getNumTypeObjects() > 0) {
2712 // C++ [dcl.ref]p4: There shall be no references to references.
2713 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2714 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002715 if (const IdentifierInfo *II = D.getIdentifier())
2716 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2717 << II;
2718 else
2719 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2720 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002721
Sebastian Redlbd150f42008-11-21 19:14:01 +00002722 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002723 // can go ahead and build the (technically ill-formed)
2724 // declarator: reference collapsing will take care of it.
2725 }
2726 }
2727
Bill Wendling3708c182007-05-27 10:15:43 +00002728 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002729 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002730 DS.TakeAttributes(),
2731 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002732 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002733 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002734}
2735
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002736/// ParseDirectDeclarator
2737/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002738/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002739/// '(' declarator ')'
2740/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002741/// [C90] direct-declarator '[' constant-expression[opt] ']'
2742/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2743/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2744/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2745/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002746/// direct-declarator '(' parameter-type-list ')'
2747/// direct-declarator '(' identifier-list[opt] ')'
2748/// [GNU] direct-declarator '(' parameter-forward-declarations
2749/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002750/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2751/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002752/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002753///
2754/// declarator-id: [C++ 8]
2755/// id-expression
2756/// '::'[opt] nested-name-specifier[opt] type-name
2757///
2758/// id-expression: [C++ 5.1]
2759/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002760/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002761///
2762/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002763/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002764/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002765/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002766/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002767/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002768///
Chris Lattneracd58a32006-08-06 17:24:14 +00002769void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002770 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002771
Douglas Gregor7861a802009-11-03 01:35:08 +00002772 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2773 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002774 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002775 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002776 }
2777
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002778 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002779 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002780 // Change the declaration context for name lookup, until this function
2781 // is exited (and the declarator has been parsed).
2782 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002783 }
2784
Douglas Gregor7861a802009-11-03 01:35:08 +00002785 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2786 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2787 // We found something that indicates the start of an unqualified-id.
2788 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002789 bool AllowConstructorName;
2790 if (D.getDeclSpec().hasTypeSpecifier())
2791 AllowConstructorName = false;
2792 else if (D.getCXXScopeSpec().isSet())
2793 AllowConstructorName =
2794 (D.getContext() == Declarator::FileContext ||
2795 (D.getContext() == Declarator::MemberContext &&
2796 D.getDeclSpec().isFriendSpecified()));
2797 else
2798 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2799
Douglas Gregor7861a802009-11-03 01:35:08 +00002800 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2801 /*EnteringContext=*/true,
2802 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002803 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002804 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002805 D.getName()) ||
2806 // Once we're past the identifier, if the scope was bad, mark the
2807 // whole declarator bad.
2808 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002809 D.SetIdentifier(0, Tok.getLocation());
2810 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002811 } else {
2812 // Parsed the unqualified-id; update range information and move along.
2813 if (D.getSourceRange().getBegin().isInvalid())
2814 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2815 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002816 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002817 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002818 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002819 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002820 assert(!getLang().CPlusPlus &&
2821 "There's a C++-specific check for tok::identifier above");
2822 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2823 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2824 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002825 goto PastIdentifier;
2826 }
2827
2828 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002829 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002830 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002831 // Example: 'char (*X)' or 'int (*XX)(void)'
2832 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002833
2834 // If the declarator was parenthesized, we entered the declarator
2835 // scope when parsing the parenthesized declarator, then exited
2836 // the scope already. Re-enter the scope, if we need to.
2837 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002838 // If there was an error parsing parenthesized declarator, declarator
2839 // scope may have been enterred before. Don't do it again.
2840 if (!D.isInvalidType() &&
2841 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002842 // Change the declaration context for name lookup, until this function
2843 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002844 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002845 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002846 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002847 // This could be something simple like "int" (in which case the declarator
2848 // portion is empty), if an abstract-declarator is allowed.
2849 D.SetIdentifier(0, Tok.getLocation());
2850 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002851 if (D.getContext() == Declarator::MemberContext)
2852 Diag(Tok, diag::err_expected_member_name_or_semi)
2853 << D.getDeclSpec().getSourceRange();
2854 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002855 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002856 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002857 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002858 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002859 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002860 }
Mike Stump11289f42009-09-09 15:08:12 +00002861
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002862 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002863 assert(D.isPastIdentifier() &&
2864 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002865
Alexis Hunt96d5c762009-11-21 08:43:09 +00002866 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002867 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002868 && isCXX0XAttributeSpecifier(true)) {
2869 SourceLocation AttrEndLoc;
2870 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2871 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2872 }
2873
Chris Lattneracd58a32006-08-06 17:24:14 +00002874 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002875 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002876 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2877 // In such a case, check if we actually have a function declarator; if it
2878 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002879 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2880 // When not in file scope, warn for ambiguous function declarators, just
2881 // in case the author intended it as a variable definition.
2882 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2883 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2884 break;
2885 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002886 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002887 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002888 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002889 } else {
2890 break;
2891 }
2892 }
2893}
2894
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002895/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2896/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002897/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002898/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2899///
2900/// direct-declarator:
2901/// '(' declarator ')'
2902/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002903/// direct-declarator '(' parameter-type-list ')'
2904/// direct-declarator '(' identifier-list[opt] ')'
2905/// [GNU] direct-declarator '(' parameter-forward-declarations
2906/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002907///
2908void Parser::ParseParenDeclarator(Declarator &D) {
2909 SourceLocation StartLoc = ConsumeParen();
2910 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002911
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002912 // Eat any attributes before we look at whether this is a grouping or function
2913 // declarator paren. If this is a grouping paren, the attribute applies to
2914 // the type being built up, for example:
2915 // int (__attribute__(()) *x)(long y)
2916 // If this ends up not being a grouping paren, the attribute applies to the
2917 // first argument, for example:
2918 // int (__attribute__(()) int x)
2919 // In either case, we need to eat any attributes to be able to determine what
2920 // sort of paren this is.
2921 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002922 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002923 bool RequiresArg = false;
2924 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002925 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002926
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002927 // We require that the argument list (if this is a non-grouping paren) be
2928 // present even if the attribute list was empty.
2929 RequiresArg = true;
2930 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002931 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002932 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002933 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2934 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002935 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002936 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00002937 // Eat any Borland extensions.
2938 if (Tok.is(tok::kw___pascal)) {
2939 AttrList.reset(ParseBorlandTypeAttributes(AttrList.take()));
2940 }
Mike Stump11289f42009-09-09 15:08:12 +00002941
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002942 // If we haven't past the identifier yet (or where the identifier would be
2943 // stored, if this is an abstract declarator), then this is probably just
2944 // grouping parens. However, if this could be an abstract-declarator, then
2945 // this could also be the start of function arguments (consider 'void()').
2946 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002947
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002948 if (!D.mayOmitIdentifier()) {
2949 // If this can't be an abstract-declarator, this *must* be a grouping
2950 // paren, because we haven't seen the identifier yet.
2951 isGrouping = true;
2952 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002953 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002954 isDeclarationSpecifier()) { // 'int(int)' is a function.
2955 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2956 // considered to be a type, not a K&R identifier-list.
2957 isGrouping = false;
2958 } else {
2959 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2960 isGrouping = true;
2961 }
Mike Stump11289f42009-09-09 15:08:12 +00002962
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002963 // If this is a grouping paren, handle:
2964 // direct-declarator: '(' declarator ')'
2965 // direct-declarator: '(' attributes declarator ')'
2966 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002967 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002968 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002969 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002970 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002971
Sebastian Redlbd150f42008-11-21 19:14:01 +00002972 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002973 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002974 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002975
2976 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002977 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002978 return;
2979 }
Mike Stump11289f42009-09-09 15:08:12 +00002980
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002981 // Okay, if this wasn't a grouping paren, it must be the start of a function
2982 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002983 // identifier (and remember where it would have been), then call into
2984 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002985 D.SetIdentifier(0, Tok.getLocation());
2986
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002987 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002988}
2989
2990/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2991/// declarator D up to a paren, which indicates that we are parsing function
2992/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002993///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002994/// If AttrList is non-null, then the caller parsed those arguments immediately
2995/// after the open paren - they should be considered to be the first argument of
2996/// a parameter. If RequiresArg is true, then the first argument of the
2997/// function is required to be present and required to not be an identifier
2998/// list.
2999///
Chris Lattneracd58a32006-08-06 17:24:14 +00003000/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003001/// parameter-type-list: [C99 6.7.5]
3002/// parameter-list
3003/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003004/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003005///
3006/// parameter-list: [C99 6.7.5]
3007/// parameter-declaration
3008/// parameter-list ',' parameter-declaration
3009///
3010/// parameter-declaration: [C99 6.7.5]
3011/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003012/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003013/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003014/// declaration-specifiers abstract-declarator[opt]
3015/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003016/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003017/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003018///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003019/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00003020/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003021///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003022void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3023 AttributeList *AttrList,
3024 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003025 // lparen is already consumed!
3026 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003027
Douglas Gregor7fb25412010-10-01 18:44:50 +00003028 ParsedType TrailingReturnType;
3029
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003030 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003031 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003032 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003033 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003034 delete AttrList;
3035 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003036
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003037 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3038 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003039
3040 // cv-qualifier-seq[opt].
3041 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003042 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003043 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003044 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003045 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003046 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003047 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00003048 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003049 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003050 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003051
3052 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003053 if (Tok.is(tok::kw_throw)) {
3054 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003055 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003056 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003057 hasAnyExceptionSpec);
3058 assert(Exceptions.size() == ExceptionRanges.size() &&
3059 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003060 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003061
3062 // Parse trailing-return-type.
3063 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3064 TrailingReturnType = ParseTrailingReturnType().get();
3065 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003066 }
3067
Chris Lattner371ed4e2008-04-06 06:57:35 +00003068 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003069 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003070 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003071 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003072 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003073 /*arglist*/ 0, 0,
3074 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003075 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003076 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003077 Exceptions.data(),
3078 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003079 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003080 LParenLoc, RParenLoc, D,
3081 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003082 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003083 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003084 }
3085
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003086 // Alternatively, this parameter list may be an identifier list form for a
3087 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003088 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3089 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003090 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003091 // K&R identifier lists can't have typedefs as identifiers, per
3092 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00003093 if (RequiresArg) {
3094 Diag(Tok, diag::err_argument_required_after_attribute);
3095 delete AttrList;
3096 }
Chris Lattner9453ab82010-05-14 17:23:36 +00003097
Steve Naroffb0486722009-01-28 19:16:40 +00003098 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003099 // normal declarators, not for abstract-declarators. Get the first
3100 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003101 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003102 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003103
3104 // Identifier lists follow a really simple grammar: the identifiers can
3105 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3106 // identifier lists are really rare in the brave new modern world, and it
3107 // is very common for someone to typo a type in a non-k&r style list. If
3108 // we are presented with something like: "void foo(intptr x, float y)",
3109 // we don't want to start parsing the function declarator as though it is
3110 // a K&R style declarator just because intptr is an invalid type.
3111 //
3112 // To handle this, we check to see if the token after the first identifier
3113 // is a "," or ")". Only if so, do we parse it as an identifier list.
3114 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3115 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3116 FirstTok.getIdentifierInfo(),
3117 FirstTok.getLocation(), D);
3118
3119 // If we get here, the code is invalid. Push the first identifier back
3120 // into the token stream and parse the first argument as an (invalid)
3121 // normal argument declarator.
3122 PP.EnterToken(Tok);
3123 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003124 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003125 }
Mike Stump11289f42009-09-09 15:08:12 +00003126
Chris Lattner371ed4e2008-04-06 06:57:35 +00003127 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003128
Chris Lattner371ed4e2008-04-06 06:57:35 +00003129 // Build up an array of information about the parsed arguments.
3130 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003131
3132 // Enter function-declaration scope, limiting any declarators to the
3133 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003134 ParseScope PrototypeScope(this,
3135 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003136
Chris Lattner371ed4e2008-04-06 06:57:35 +00003137 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003138 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003139 while (1) {
3140 if (Tok.is(tok::ellipsis)) {
3141 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003142 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003143 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003144 }
Mike Stump11289f42009-09-09 15:08:12 +00003145
Chris Lattner371ed4e2008-04-06 06:57:35 +00003146 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003147
Chris Lattner371ed4e2008-04-06 06:57:35 +00003148 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003149 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003150 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003151
3152 // If the caller parsed attributes for the first argument, add them now.
3153 if (AttrList) {
3154 DS.AddAttributes(AttrList);
3155 AttrList = 0; // Only apply the attributes to the first parameter.
3156 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003157 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003158
Chris Lattner371ed4e2008-04-06 06:57:35 +00003159 // Parse the declarator. This is "PrototypeContext", because we must
3160 // accept either 'declarator' or 'abstract-declarator' here.
3161 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3162 ParseDeclarator(ParmDecl);
3163
3164 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003165 if (Tok.is(tok::kw___attribute)) {
3166 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003167 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003168 ParmDecl.AddAttributes(AttrList, Loc);
3169 }
Mike Stump11289f42009-09-09 15:08:12 +00003170
Chris Lattner371ed4e2008-04-06 06:57:35 +00003171 // Remember this parsed parameter in ParamInfo.
3172 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003173
Douglas Gregor4d87df52008-12-16 21:30:33 +00003174 // DefArgToks is used when the parsing of default arguments needs
3175 // to be delayed.
3176 CachedTokens *DefArgToks = 0;
3177
Chris Lattner371ed4e2008-04-06 06:57:35 +00003178 // If no parameter was specified, verify that *something* was specified,
3179 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003180 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3181 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003182 // Completely missing, emit error.
3183 Diag(DSStart, diag::err_missing_param);
3184 } else {
3185 // Otherwise, we have something. Add it and let semantic analysis try
3186 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003187
Chris Lattner371ed4e2008-04-06 06:57:35 +00003188 // Inform the actions module about the parameter declarator, so it gets
3189 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003190 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003191
3192 // Parse the default argument, if any. We parse the default
3193 // arguments in all dialects; the semantic analysis in
3194 // ActOnParamDefaultArgument will reject the default argument in
3195 // C.
3196 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003197 SourceLocation EqualLoc = Tok.getLocation();
3198
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003199 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003200 if (D.getContext() == Declarator::MemberContext) {
3201 // If we're inside a class definition, cache the tokens
3202 // corresponding to the default argument. We'll actually parse
3203 // them when we see the end of the class definition.
3204 // FIXME: Templates will require something similar.
3205 // FIXME: Can we use a smart pointer for Toks?
3206 DefArgToks = new CachedTokens;
3207
Mike Stump11289f42009-09-09 15:08:12 +00003208 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003209 /*StopAtSemi=*/true,
3210 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003211 delete DefArgToks;
3212 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003213 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003214 } else {
3215 // Mark the end of the default argument so that we know when to
3216 // stop when we parse it later on.
3217 Token DefArgEnd;
3218 DefArgEnd.startToken();
3219 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3220 DefArgEnd.setLocation(Tok.getLocation());
3221 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003222 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003223 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003224 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003225 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003226 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003227 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003228
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003229 // The argument isn't actually potentially evaluated unless it is
3230 // used.
3231 EnterExpressionEvaluationContext Eval(Actions,
3232 Sema::PotentiallyEvaluatedIfUsed);
3233
John McCalldadc5752010-08-24 06:29:42 +00003234 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003235 if (DefArgResult.isInvalid()) {
3236 Actions.ActOnParamDefaultArgumentError(Param);
3237 SkipUntil(tok::comma, tok::r_paren, true, true);
3238 } else {
3239 // Inform the actions module about the default argument
3240 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003241 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003242 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003243 }
3244 }
Mike Stump11289f42009-09-09 15:08:12 +00003245
3246 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3247 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003248 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003249 }
3250
3251 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003252 if (Tok.isNot(tok::comma)) {
3253 if (Tok.is(tok::ellipsis)) {
3254 IsVariadic = true;
3255 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3256
3257 if (!getLang().CPlusPlus) {
3258 // We have ellipsis without a preceding ',', which is ill-formed
3259 // in C. Complain and provide the fix.
3260 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003261 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003262 }
3263 }
3264
3265 break;
3266 }
Mike Stump11289f42009-09-09 15:08:12 +00003267
Chris Lattner371ed4e2008-04-06 06:57:35 +00003268 // Consume the comma.
3269 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003270 }
Mike Stump11289f42009-09-09 15:08:12 +00003271
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003272 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003273 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3274 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003275
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003276 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003277 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003278 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003279 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003280 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003281 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003282
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003283 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003284 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003285 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003286 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003287 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003288
3289 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003290 if (Tok.is(tok::kw_throw)) {
3291 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003292 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003293 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003294 hasAnyExceptionSpec);
3295 assert(Exceptions.size() == ExceptionRanges.size() &&
3296 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003297 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003298
3299 // Parse trailing-return-type.
3300 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3301 TrailingReturnType = ParseTrailingReturnType().get();
3302 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003303 }
3304
Douglas Gregor7fb25412010-10-01 18:44:50 +00003305 // FIXME: We should leave the prototype scope before parsing the exception
3306 // specification, and then reenter it when parsing the trailing return type.
3307
3308 // Leave prototype scope.
3309 PrototypeScope.Exit();
3310
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003311 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003312 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003313 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003314 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003315 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003316 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003317 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003318 Exceptions.data(),
3319 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003320 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003321 LParenLoc, RParenLoc, D,
3322 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003323 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003324}
Chris Lattneracd58a32006-08-06 17:24:14 +00003325
Chris Lattner6c940e62008-04-06 06:34:08 +00003326/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3327/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003328/// first identifier has already been consumed, and the current token is the
3329/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003330///
3331/// identifier-list: [C99 6.7.5]
3332/// identifier
3333/// identifier-list ',' identifier
3334///
3335void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003336 IdentifierInfo *FirstIdent,
3337 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003338 Declarator &D) {
3339 // Build up an array of information about the parsed arguments.
3340 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3341 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003342
Chris Lattner6c940e62008-04-06 06:34:08 +00003343 // If there was no identifier specified for the declarator, either we are in
3344 // an abstract-declarator, or we are in a parameter declarator which was found
3345 // to be abstract. In abstract-declarators, identifier lists are not valid:
3346 // diagnose this.
3347 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003348 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003349
Chris Lattner9453ab82010-05-14 17:23:36 +00003350 // The first identifier was already read, and is known to be the first
3351 // identifier in the list. Remember this identifier in ParamInfo.
3352 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003353 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003354
Chris Lattner6c940e62008-04-06 06:34:08 +00003355 while (Tok.is(tok::comma)) {
3356 // Eat the comma.
3357 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003358
Chris Lattner9186f552008-04-06 06:39:19 +00003359 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003360 if (Tok.isNot(tok::identifier)) {
3361 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003362 SkipUntil(tok::r_paren);
3363 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003364 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003365
Chris Lattner6c940e62008-04-06 06:34:08 +00003366 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003367
3368 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003369 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003370 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003371
Chris Lattner6c940e62008-04-06 06:34:08 +00003372 // Verify that the argument identifier has not already been mentioned.
3373 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003374 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003375 } else {
3376 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003377 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003378 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003379 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003380 }
Mike Stump11289f42009-09-09 15:08:12 +00003381
Chris Lattner6c940e62008-04-06 06:34:08 +00003382 // Eat the identifier.
3383 ConsumeToken();
3384 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003385
3386 // If we have the closing ')', eat it and we're done.
3387 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3388
Chris Lattner9186f552008-04-06 06:39:19 +00003389 // Remember that we parsed a function type, and remember the attributes. This
3390 // function type is always a K&R style function type, which is not varargs and
3391 // has no prototype.
3392 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003393 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003394 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003395 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003396 /*exception*/false,
3397 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003398 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003399 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003400}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003401
Chris Lattnere8074e62006-08-06 18:30:15 +00003402/// [C90] direct-declarator '[' constant-expression[opt] ']'
3403/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3404/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3405/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3406/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3407void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003408 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003409
Chris Lattner84a11622008-12-18 07:27:21 +00003410 // C array syntax has many features, but by-far the most common is [] and [4].
3411 // This code does a fast path to handle some of the most obvious cases.
3412 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003413 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003414 //FIXME: Use these
3415 CXX0XAttributeList Attr;
3416 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3417 Attr = ParseCXX0XAttributes();
3418 }
3419
Chris Lattner84a11622008-12-18 07:27:21 +00003420 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003421 ExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003422 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3423 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003424 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003425 return;
3426 } else if (Tok.getKind() == tok::numeric_constant &&
3427 GetLookAheadToken(1).is(tok::r_square)) {
3428 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003429 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003430 ConsumeToken();
3431
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003432 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003433 //FIXME: Use these
3434 CXX0XAttributeList Attr;
3435 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3436 Attr = ParseCXX0XAttributes();
3437 }
Chris Lattner84a11622008-12-18 07:27:21 +00003438
3439 // If there was an error parsing the assignment-expression, recover.
3440 if (ExprRes.isInvalid())
3441 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003442
Chris Lattner84a11622008-12-18 07:27:21 +00003443 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003444 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3445 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003446 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003447 return;
3448 }
Mike Stump11289f42009-09-09 15:08:12 +00003449
Chris Lattnere8074e62006-08-06 18:30:15 +00003450 // If valid, this location is the position where we read the 'static' keyword.
3451 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003452 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003453 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003454
Chris Lattnere8074e62006-08-06 18:30:15 +00003455 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003456 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003457 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003458 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003459
Chris Lattnere8074e62006-08-06 18:30:15 +00003460 // If we haven't already read 'static', check to see if there is one after the
3461 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003462 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003463 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003464
Chris Lattnere8074e62006-08-06 18:30:15 +00003465 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003466 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003467 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003468
Chris Lattner521ff2b2008-04-06 05:26:30 +00003469 // Handle the case where we have '[*]' as the array size. However, a leading
3470 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3471 // the the token after the star is a ']'. Since stars in arrays are
3472 // infrequent, use of lookahead is not costly here.
3473 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003474 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003475
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003476 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003477 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003478 StaticLoc = SourceLocation(); // Drop the static.
3479 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003480 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003481 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003482 // Note, in C89, this production uses the constant-expr production instead
3483 // of assignment-expr. The only difference is that assignment-expr allows
3484 // things like '=' and '*='. Sema rejects these in C89 mode because they
3485 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003486
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003487 // Parse the constant-expression or assignment-expression now (depending
3488 // on dialect).
3489 if (getLang().CPlusPlus)
3490 NumElements = ParseConstantExpression();
3491 else
3492 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003493 }
Mike Stump11289f42009-09-09 15:08:12 +00003494
Chris Lattner62591722006-08-12 18:40:58 +00003495 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003496 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003497 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003498 // If the expression was invalid, skip it.
3499 SkipUntil(tok::r_square);
3500 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003501 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003502
3503 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3504
Alexis Hunt96d5c762009-11-21 08:43:09 +00003505 //FIXME: Use these
3506 CXX0XAttributeList Attr;
3507 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3508 Attr = ParseCXX0XAttributes();
3509 }
3510
Chris Lattner84a11622008-12-18 07:27:21 +00003511 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003512 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3513 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003514 NumElements.release(),
3515 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003516 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003517}
3518
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003519/// [GNU] typeof-specifier:
3520/// typeof ( expressions )
3521/// typeof ( type-name )
3522/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003523///
3524void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003525 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003526 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003527 SourceLocation StartLoc = ConsumeToken();
3528
John McCalle8595032010-01-13 20:03:27 +00003529 const bool hasParens = Tok.is(tok::l_paren);
3530
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003531 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003532 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003533 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003534 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003535 isCastExpr,
3536 CastTy,
3537 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003538 if (hasParens)
3539 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003540
3541 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003542 // FIXME: Not accurate, the range gets one token more than it should.
3543 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003544 else
3545 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003546
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003547 if (isCastExpr) {
3548 if (!CastTy) {
3549 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003550 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003551 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003552
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003553 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003554 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003555 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3556 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003557 DiagID, CastTy))
3558 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003559 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003560 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003561
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003562 // If we get here, the operand to the typeof was an expresion.
3563 if (Operand.isInvalid()) {
3564 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003565 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003566 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003567
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003568 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003569 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003570 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3571 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003572 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003573 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003574}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003575
3576
3577/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3578/// from TryAltiVecVectorToken.
3579bool Parser::TryAltiVecVectorTokenOutOfLine() {
3580 Token Next = NextToken();
3581 switch (Next.getKind()) {
3582 default: return false;
3583 case tok::kw_short:
3584 case tok::kw_long:
3585 case tok::kw_signed:
3586 case tok::kw_unsigned:
3587 case tok::kw_void:
3588 case tok::kw_char:
3589 case tok::kw_int:
3590 case tok::kw_float:
3591 case tok::kw_double:
3592 case tok::kw_bool:
3593 case tok::kw___pixel:
3594 Tok.setKind(tok::kw___vector);
3595 return true;
3596 case tok::identifier:
3597 if (Next.getIdentifierInfo() == Ident_pixel) {
3598 Tok.setKind(tok::kw___vector);
3599 return true;
3600 }
3601 return false;
3602 }
3603}
3604
3605bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3606 const char *&PrevSpec, unsigned &DiagID,
3607 bool &isInvalid) {
3608 if (Tok.getIdentifierInfo() == Ident_vector) {
3609 Token Next = NextToken();
3610 switch (Next.getKind()) {
3611 case tok::kw_short:
3612 case tok::kw_long:
3613 case tok::kw_signed:
3614 case tok::kw_unsigned:
3615 case tok::kw_void:
3616 case tok::kw_char:
3617 case tok::kw_int:
3618 case tok::kw_float:
3619 case tok::kw_double:
3620 case tok::kw_bool:
3621 case tok::kw___pixel:
3622 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3623 return true;
3624 case tok::identifier:
3625 if (Next.getIdentifierInfo() == Ident_pixel) {
3626 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3627 return true;
3628 }
3629 break;
3630 default:
3631 break;
3632 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003633 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003634 DS.isTypeAltiVecVector()) {
3635 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3636 return true;
3637 }
3638 return false;
3639}
3640