blob: 0cc9103c058d59ccb12bd1e18303746091a3280c [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
Chris Lattner53361ac2006-08-10 05:19:57 +0000296/// ParseDeclaration - Parse a full 'declaration', which consists of
297/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000298/// 'Context' should be a Declarator::TheContext value. This returns the
299/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000300///
301/// declaration: [C99 6.7]
302/// block-declaration ->
303/// simple-declaration
304/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000305/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000306/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000307/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000308/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000309/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000310/// others... [FIXME]
311///
Chris Lattner49836b42009-04-02 04:16:50 +0000312Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000313 SourceLocation &DeclEnd,
314 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000315 ParenBraceBracketBalancer BalancerRAIIObj(*this);
316
John McCall48871652010-08-21 09:40:31 +0000317 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000318 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000319 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000320 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000321 if (Attr.HasAttr)
322 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
323 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000324 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000325 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000326 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000327 if (Attr.HasAttr)
328 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
329 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000330 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000331 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000332 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000333 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000334 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000335 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000336 if (Attr.HasAttr)
337 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
338 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000339 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000340 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000341 default:
Chris Lattner005fc1b2010-04-05 18:18:31 +0000342 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000343 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000344
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000345 // This routine returns a DeclGroup, if the thing we parsed only contains a
346 // single decl, convert it now.
347 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000348}
349
350/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
351/// declaration-specifiers init-declarator-list[opt] ';'
352///[C90/C++]init-declarator-list ';' [TODO]
353/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000354///
355/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000356/// declaration. If it is true, it checks for and eats it.
Chris Lattner32dc41c2009-03-29 17:27:48 +0000357Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000358 SourceLocation &DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000359 AttributeList *Attr,
360 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000361 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000362 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000363 if (Attr)
364 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000365 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
366 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner0e894622006-08-13 19:58:17 +0000368 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
369 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000370 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000371 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000372 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000373 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000374 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000375 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Chris Lattner005fc1b2010-04-05 18:18:31 +0000378 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000379}
Mike Stump11289f42009-09-09 15:08:12 +0000380
John McCalld5a36322009-11-03 19:26:08 +0000381/// ParseDeclGroup - Having concluded that this is either a function
382/// definition or a group of object declarations, actually parse the
383/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000384Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
385 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000386 bool AllowFunctionDefinitions,
387 SourceLocation *DeclEnd) {
388 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000389 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000390 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000391
John McCalld5a36322009-11-03 19:26:08 +0000392 // Bail out if the first declarator didn't seem well-formed.
393 if (!D.hasName() && !D.mayOmitIdentifier()) {
394 // Skip until ; or }.
395 SkipUntil(tok::r_brace, true, true);
396 if (Tok.is(tok::semi))
397 ConsumeToken();
398 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000401 // Check to see if we have a function *definition* which must have a body.
402 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
403 // Look at the next token to make sure that this isn't a function
404 // declaration. We have to check this because __attribute__ might be the
405 // start of a function definition in GCC-extended K&R C.
406 !isDeclarationAfterDeclarator()) {
407
Chris Lattner13901342010-07-11 22:42:07 +0000408 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000409 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
410 Diag(Tok, diag::err_function_declared_typedef);
411
412 // Recover by treating the 'typedef' as spurious.
413 DS.ClearStorageClassSpecs();
414 }
415
John McCall48871652010-08-21 09:40:31 +0000416 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000417 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000418 }
419
420 if (isDeclarationSpecifier()) {
421 // If there is an invalid declaration specifier right after the function
422 // prototype, then we must be in a missing semicolon case where this isn't
423 // actually a body. Just fall through into the code that handles it as a
424 // prototype, and let the top-level code handle the erroneous declspec
425 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000426 } else {
427 Diag(Tok, diag::err_expected_fn_body);
428 SkipUntil(tok::semi);
429 return DeclGroupPtrTy();
430 }
431 }
432
John McCall48871652010-08-21 09:40:31 +0000433 llvm::SmallVector<Decl *, 8> DeclsInGroup;
434 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000435 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000436 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000437 DeclsInGroup.push_back(FirstDecl);
438
439 // If we don't have a comma, it is either the end of the list (a ';') or an
440 // error, bail out.
441 while (Tok.is(tok::comma)) {
442 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000443 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000444
445 // Parse the next declarator.
446 D.clear();
447
448 // Accept attributes in an init-declarator. In the first declarator in a
449 // declaration, these would be part of the declspec. In subsequent
450 // declarators, they become part of the declarator itself, so that they
451 // don't apply to declarators after *this* one. Examples:
452 // short __attribute__((common)) var; -> declspec
453 // short var __attribute__((common)); -> declarator
454 // short x, __attribute__((common)) var; -> declarator
455 if (Tok.is(tok::kw___attribute)) {
456 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000457 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000458 D.AddAttributes(AttrList, Loc);
459 }
460
461 ParseDeclarator(D);
462
John McCall48871652010-08-21 09:40:31 +0000463 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000464 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000465 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000466 DeclsInGroup.push_back(ThisDecl);
467 }
468
469 if (DeclEnd)
470 *DeclEnd = Tok.getLocation();
471
472 if (Context != Declarator::ForContext &&
473 ExpectAndConsume(tok::semi,
474 Context == Declarator::FileContext
475 ? diag::err_invalid_token_after_toplevel_declarator
476 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000477 // Okay, there was no semicolon and one was expected. If we see a
478 // declaration specifier, just assume it was missing and continue parsing.
479 // Otherwise things are very confused and we skip to recover.
480 if (!isDeclarationSpecifier()) {
481 SkipUntil(tok::r_brace, true, true);
482 if (Tok.is(tok::semi))
483 ConsumeToken();
484 }
John McCalld5a36322009-11-03 19:26:08 +0000485 }
486
Douglas Gregor0be31a22010-07-02 17:43:08 +0000487 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000488 DeclsInGroup.data(),
489 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000490}
491
Douglas Gregor23996282009-05-12 21:31:51 +0000492/// \brief Parse 'declaration' after parsing 'declaration-specifiers
493/// declarator'. This method parses the remainder of the declaration
494/// (including any attributes or initializer, among other things) and
495/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000496///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000497/// init-declarator: [C99 6.7]
498/// declarator
499/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000500/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
501/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000502/// [C++] declarator initializer[opt]
503///
504/// [C++] initializer:
505/// [C++] '=' initializer-clause
506/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000507/// [C++0x] '=' 'default' [TODO]
508/// [C++0x] '=' 'delete'
509///
510/// According to the standard grammar, =default and =delete are function
511/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000512///
John McCall48871652010-08-21 09:40:31 +0000513Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000514 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000515 // If a simple-asm-expr is present, parse it.
516 if (Tok.is(tok::kw_asm)) {
517 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000518 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000519 if (AsmLabel.isInvalid()) {
520 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000521 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000522 }
Mike Stump11289f42009-09-09 15:08:12 +0000523
Douglas Gregor23996282009-05-12 21:31:51 +0000524 D.setAsmLabel(AsmLabel.release());
525 D.SetRangeEnd(Loc);
526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Douglas Gregor23996282009-05-12 21:31:51 +0000528 // If attributes are present, parse them.
529 if (Tok.is(tok::kw___attribute)) {
530 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000531 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000532 D.AddAttributes(AttrList, Loc);
533 }
Mike Stump11289f42009-09-09 15:08:12 +0000534
Douglas Gregor23996282009-05-12 21:31:51 +0000535 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000536 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000537 switch (TemplateInfo.Kind) {
538 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000539 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000540 break;
541
542 case ParsedTemplateInfo::Template:
543 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000544 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000545 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000546 TemplateInfo.TemplateParams->data(),
547 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000548 D);
549 break;
550
551 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000552 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000553 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000554 TemplateInfo.ExternLoc,
555 TemplateInfo.TemplateLoc,
556 D);
557 if (ThisRes.isInvalid()) {
558 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000559 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000560 }
561
562 ThisDecl = ThisRes.get();
563 break;
564 }
565 }
Mike Stump11289f42009-09-09 15:08:12 +0000566
Douglas Gregor23996282009-05-12 21:31:51 +0000567 // Parse declarator '=' initializer.
568 if (Tok.is(tok::equal)) {
569 ConsumeToken();
570 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
571 SourceLocation DelLoc = ConsumeToken();
572 Actions.SetDeclDeleted(ThisDecl, DelLoc);
573 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000574 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
575 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000576 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000577 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000578
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000579 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000580 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000581 ConsumeCodeCompletionToken();
582 SkipUntil(tok::comma, true, true);
583 return ThisDecl;
584 }
585
John McCalldadc5752010-08-24 06:29:42 +0000586 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000587
John McCall1f4ee7b2009-12-19 09:28:58 +0000588 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000589 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000590 ExitScope();
591 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000592
Douglas Gregor23996282009-05-12 21:31:51 +0000593 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000594 SkipUntil(tok::comma, true, true);
595 Actions.ActOnInitializerError(ThisDecl);
596 } else
John McCallb268a282010-08-23 23:25:46 +0000597 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor23996282009-05-12 21:31:51 +0000598 }
599 } else if (Tok.is(tok::l_paren)) {
600 // Parse C++ direct initializer: '(' expression-list ')'
601 SourceLocation LParenLoc = ConsumeParen();
602 ExprVector Exprs(Actions);
603 CommaLocsTy CommaLocs;
604
Douglas Gregor613bf102009-12-22 17:47:17 +0000605 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
606 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000607 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000608 }
609
Douglas Gregor23996282009-05-12 21:31:51 +0000610 if (ParseExpressionList(Exprs, CommaLocs)) {
611 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000612
613 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000614 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000615 ExitScope();
616 }
Douglas Gregor23996282009-05-12 21:31:51 +0000617 } else {
618 // Match the ')'.
619 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
620
621 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
622 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000623
624 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000625 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000626 ExitScope();
627 }
628
Douglas Gregor23996282009-05-12 21:31:51 +0000629 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
630 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000631 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000632 }
633 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000634 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000635 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
636 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000637 }
638
639 return ThisDecl;
640}
641
Chris Lattner1890ac82006-08-13 01:16:23 +0000642/// ParseSpecifierQualifierList
643/// specifier-qualifier-list:
644/// type-specifier specifier-qualifier-list[opt]
645/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000646/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000647///
648void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
649 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
650 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000651 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner1890ac82006-08-13 01:16:23 +0000653 // Validate declspec for type-name.
654 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000655 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
656 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000657 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner1b22eed2006-11-28 05:12:07 +0000659 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000660 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000661 if (DS.getStorageClassSpecLoc().isValid())
662 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
663 else
664 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000665 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000666 }
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattner1b22eed2006-11-28 05:12:07 +0000668 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000669 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000670 if (DS.isInlineSpecified())
671 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
672 if (DS.isVirtualSpecified())
673 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
674 if (DS.isExplicitSpecified())
675 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000676 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000677 }
678}
Chris Lattner53361ac2006-08-10 05:19:57 +0000679
Chris Lattner6cc055a2009-04-12 20:42:31 +0000680/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
681/// specified token is valid after the identifier in a declarator which
682/// immediately follows the declspec. For example, these things are valid:
683///
684/// int x [ 4]; // direct-declarator
685/// int x ( int y); // direct-declarator
686/// int(int x ) // direct-declarator
687/// int x ; // simple-declaration
688/// int x = 17; // init-declarator-list
689/// int x , y; // init-declarator-list
690/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000691/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000692/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000693///
694/// This is not, because 'x' does not immediately follow the declspec (though
695/// ')' happens to be valid anyway).
696/// int (x)
697///
698static bool isValidAfterIdentifierInDeclarator(const Token &T) {
699 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
700 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000701 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000702}
703
Chris Lattner20a0c612009-04-14 21:34:55 +0000704
705/// ParseImplicitInt - This method is called when we have an non-typename
706/// identifier in a declspec (which normally terminates the decl spec) when
707/// the declspec has no type specifier. In this case, the declspec is either
708/// malformed or is "implicit int" (in K&R and C89).
709///
710/// This method handles diagnosing this prettily and returns false if the
711/// declspec is done being processed. If it recovers and thinks there may be
712/// other pieces of declspec after it, it returns true.
713///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000714bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000715 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000716 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000717 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000718
Chris Lattner20a0c612009-04-14 21:34:55 +0000719 SourceLocation Loc = Tok.getLocation();
720 // If we see an identifier that is not a type name, we normally would
721 // parse it as the identifer being declared. However, when a typename
722 // is typo'd or the definition is not included, this will incorrectly
723 // parse the typename as the identifier name and fall over misparsing
724 // later parts of the diagnostic.
725 //
726 // As such, we try to do some look-ahead in cases where this would
727 // otherwise be an "implicit-int" case to see if this is invalid. For
728 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
729 // an identifier with implicit int, we'd get a parse error because the
730 // next token is obviously invalid for a type. Parse these as a case
731 // with an invalid type specifier.
732 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattner20a0c612009-04-14 21:34:55 +0000734 // Since we know that this either implicit int (which is rare) or an
735 // error, we'd do lookahead to try to do better recovery.
736 if (isValidAfterIdentifierInDeclarator(NextToken())) {
737 // If this token is valid for implicit int, e.g. "static x = 4", then
738 // we just avoid eating the identifier, so it will be parsed as the
739 // identifier in the declarator.
740 return false;
741 }
Mike Stump11289f42009-09-09 15:08:12 +0000742
Chris Lattner20a0c612009-04-14 21:34:55 +0000743 // Otherwise, if we don't consume this token, we are going to emit an
744 // error anyway. Try to recover from various common problems. Check
745 // to see if this was a reference to a tag name without a tag specified.
746 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000747 //
748 // C++ doesn't need this, and isTagName doesn't take SS.
749 if (SS == 0) {
750 const char *TagName = 0;
751 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000752
Douglas Gregor0be31a22010-07-02 17:43:08 +0000753 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000754 default: break;
755 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
756 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
757 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
758 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
759 }
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000761 if (TagName) {
762 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000763 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000764 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000766 // Parse this as a tag as if the missing tag were present.
767 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000768 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000769 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000770 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000771 return true;
772 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Douglas Gregor15e56022009-10-13 23:27:22 +0000775 // This is almost certainly an invalid type name. Let the action emit a
776 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000777 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000778 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000779 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000780 // The action emitted a diagnostic, so we don't have to.
781 if (T) {
782 // The action has suggested that the type T could be used. Set that as
783 // the type in the declaration specifiers, consume the would-be type
784 // name token, and we're done.
785 const char *PrevSpec;
786 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000787 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000788 DS.SetRangeEnd(Tok.getLocation());
789 ConsumeToken();
790
791 // There may be other declaration specifiers after this.
792 return true;
793 }
794
795 // Fall through; the action had no suggestion for us.
796 } else {
797 // The action did not emit a diagnostic, so emit one now.
798 SourceRange R;
799 if (SS) R = SS->getRange();
800 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
801 }
Mike Stump11289f42009-09-09 15:08:12 +0000802
Douglas Gregor15e56022009-10-13 23:27:22 +0000803 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000804 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000805 unsigned DiagID;
806 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000807 DS.SetRangeEnd(Tok.getLocation());
808 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattner20a0c612009-04-14 21:34:55 +0000810 // TODO: Could inject an invalid typedef decl in an enclosing scope to
811 // avoid rippling error messages on subsequent uses of the same type,
812 // could be useful if #include was forgotten.
813 return false;
814}
815
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000816/// \brief Determine the declaration specifier context from the declarator
817/// context.
818///
819/// \param Context the declarator context, which is one of the
820/// Declarator::TheContext enumerator values.
821Parser::DeclSpecContext
822Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
823 if (Context == Declarator::MemberContext)
824 return DSC_class;
825 if (Context == Declarator::FileContext)
826 return DSC_top_level;
827 return DSC_normal;
828}
829
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000830/// ParseDeclarationSpecifiers
831/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000832/// storage-class-specifier declaration-specifiers[opt]
833/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000834/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000835/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000836///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000837/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000838/// 'typedef'
839/// 'extern'
840/// 'static'
841/// 'auto'
842/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000843/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000844/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000845/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000846/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000847/// [C++] 'virtual'
848/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000849/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000850/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000851
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000852///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000853void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000854 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000855 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000856 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000857 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000858 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000859 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000860 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000861 unsigned DiagID = 0;
862
Chris Lattner4d8f8732006-11-28 05:05:08 +0000863 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000864
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000865 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000866 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000867 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000868 // If this is not a declaration specifier token, we're done reading decl
869 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000870 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000871 return;
Mike Stump11289f42009-09-09 15:08:12 +0000872
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000873 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000874 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000875 if (DS.hasTypeSpecifier()) {
876 bool AllowNonIdentifiers
877 = (getCurScope()->getFlags() & (Scope::ControlScope |
878 Scope::BlockScope |
879 Scope::TemplateParamScope |
880 Scope::FunctionPrototypeScope |
881 Scope::AtCatchScope)) == 0;
882 bool AllowNestedNameSpecifiers
883 = DSContext == DSC_top_level ||
884 (DSContext == DSC_class && DS.isFriendSpecified());
885
886 Actions.CodeCompleteDeclarator(getCurScope(), AllowNonIdentifiers,
887 AllowNestedNameSpecifiers);
888 ConsumeCodeCompletionToken();
889 return;
890 }
891
892 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000893 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
894 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000895 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000896 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000897 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000898 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000899
900 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
901 ConsumeCodeCompletionToken();
902 return;
903 }
904
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000905 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000906 // C++ scope specifier. Annotate and loop, or bail out on error.
907 if (TryAnnotateCXXScopeToken(true)) {
908 if (!DS.hasTypeSpecifier())
909 DS.SetTypeSpecError();
910 goto DoneWithDeclSpec;
911 }
John McCall8bc2a702010-03-01 18:20:46 +0000912 if (Tok.is(tok::coloncolon)) // ::new or ::delete
913 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000914 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000915
916 case tok::annot_cxxscope: {
917 if (DS.hasTypeSpecifier())
918 goto DoneWithDeclSpec;
919
John McCall9dab4e62009-12-12 11:40:51 +0000920 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000921 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000922 SS.setRange(Tok.getAnnotationRange());
923
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000924 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000925 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000926 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000927 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000928 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000929 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000930
931 // C++ [class.qual]p2:
932 // In a lookup in which the constructor is an acceptable lookup
933 // result and the nested-name-specifier nominates a class C:
934 //
935 // - if the name specified after the
936 // nested-name-specifier, when looked up in C, is the
937 // injected-class-name of C (Clause 9), or
938 //
939 // - if the name specified after the nested-name-specifier
940 // is the same as the identifier or the
941 // simple-template-id's template-name in the last
942 // component of the nested-name-specifier,
943 //
944 // the name is instead considered to name the constructor of
945 // class C.
946 //
947 // Thus, if the template-name is actually the constructor
948 // name, then the code is ill-formed; this interpretation is
949 // reinforced by the NAD status of core issue 635.
950 TemplateIdAnnotation *TemplateId
951 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000952 if ((DSContext == DSC_top_level ||
953 (DSContext == DSC_class && DS.isFriendSpecified())) &&
954 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000955 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000956 if (isConstructorDeclarator()) {
957 // The user meant this to be an out-of-line constructor
958 // definition, but template arguments are not allowed
959 // there. Just allow this as a constructor; we'll
960 // complain about it later.
961 goto DoneWithDeclSpec;
962 }
963
964 // The user meant this to name a type, but it actually names
965 // a constructor with some extraneous template
966 // arguments. Complain, then parse it as a type as the user
967 // intended.
968 Diag(TemplateId->TemplateNameLoc,
969 diag::err_out_of_line_template_id_names_constructor)
970 << TemplateId->Name;
971 }
972
John McCall9dab4e62009-12-12 11:40:51 +0000973 DS.getTypeSpecScope() = SS;
974 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000975 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000976 "ParseOptionalCXXScopeSpecifier not working");
977 AnnotateTemplateIdTokenAsType(&SS);
978 continue;
979 }
980
Douglas Gregorc5790df2009-09-28 07:26:33 +0000981 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000982 DS.getTypeSpecScope() = SS;
983 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +0000984 if (Tok.getAnnotationValue()) {
985 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorc5790df2009-09-28 07:26:33 +0000986 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallba7bf592010-08-24 05:47:05 +0000987 PrevSpec, DiagID, T);
988 }
Douglas Gregorc5790df2009-09-28 07:26:33 +0000989 else
990 DS.SetTypeSpecError();
991 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
992 ConsumeToken(); // The typename
993 }
994
Douglas Gregor167fa622009-03-25 15:40:00 +0000995 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000996 goto DoneWithDeclSpec;
997
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000998 // If we're in a context where the identifier could be a class name,
999 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001000 if ((DSContext == DSC_top_level ||
1001 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001002 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001003 &SS)) {
1004 if (isConstructorDeclarator())
1005 goto DoneWithDeclSpec;
1006
1007 // As noted in C++ [class.qual]p2 (cited above), when the name
1008 // of the class is qualified in a context where it could name
1009 // a constructor, its a constructor name. However, we've
1010 // looked at the declarator, and the user probably meant this
1011 // to be a type. Complain that it isn't supposed to be treated
1012 // as a type, then proceed to parse it as a type.
1013 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1014 << Next.getIdentifierInfo();
1015 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001016
John McCallba7bf592010-08-24 05:47:05 +00001017 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1018 Next.getLocation(),
1019 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001020
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001021 // If the referenced identifier is not a type, then this declspec is
1022 // erroneous: We already checked about that it has no type specifier, and
1023 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001024 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001025 if (TypeRep == 0) {
1026 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001027 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001028 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001029 }
Mike Stump11289f42009-09-09 15:08:12 +00001030
John McCall9dab4e62009-12-12 11:40:51 +00001031 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001032 ConsumeToken(); // The C++ scope.
1033
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001035 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001036 if (isInvalid)
1037 break;
Mike Stump11289f42009-09-09 15:08:12 +00001038
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001039 DS.SetRangeEnd(Tok.getLocation());
1040 ConsumeToken(); // The typename.
1041
1042 continue;
1043 }
Mike Stump11289f42009-09-09 15:08:12 +00001044
Chris Lattnere387d9e2009-01-21 19:48:37 +00001045 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001046 if (Tok.getAnnotationValue()) {
1047 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001049 DiagID, T);
1050 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001051 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001052
1053 if (isInvalid)
1054 break;
1055
Chris Lattnere387d9e2009-01-21 19:48:37 +00001056 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1057 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001058
Chris Lattnere387d9e2009-01-21 19:48:37 +00001059 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1060 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1061 // Objective-C interface. If we don't have Objective-C or a '<', this is
1062 // just a normal reference to a typedef name.
1063 if (!Tok.is(tok::less) || !getLang().ObjC1)
1064 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001065
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001066 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001067 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001068 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1069 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1070 LAngleLoc, EndProtoLoc);
1071 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1072 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001073
Chris Lattnere387d9e2009-01-21 19:48:37 +00001074 DS.SetRangeEnd(EndProtoLoc);
1075 continue;
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattner16fac4f2008-07-26 01:18:38 +00001078 // typedef-name
1079 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001080 // In C++, check to see if this is a scope specifier like foo::bar::, if
1081 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001082 if (getLang().CPlusPlus) {
1083 if (TryAnnotateCXXScopeToken(true)) {
1084 if (!DS.hasTypeSpecifier())
1085 DS.SetTypeSpecError();
1086 goto DoneWithDeclSpec;
1087 }
1088 if (!Tok.is(tok::identifier))
1089 continue;
1090 }
Mike Stump11289f42009-09-09 15:08:12 +00001091
Chris Lattner16fac4f2008-07-26 01:18:38 +00001092 // This identifier can only be a typedef name if we haven't already seen
1093 // a type-specifier. Without this check we misparse:
1094 // typedef int X; struct Y { short X; }; as 'short int'.
1095 if (DS.hasTypeSpecifier())
1096 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001097
John Thompson22334602010-02-05 00:12:22 +00001098 // Check for need to substitute AltiVec keyword tokens.
1099 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1100 break;
1101
Chris Lattner16fac4f2008-07-26 01:18:38 +00001102 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001103 ParsedType TypeRep =
1104 Actions.getTypeName(*Tok.getIdentifierInfo(),
1105 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001106
Chris Lattner6cc055a2009-04-12 20:42:31 +00001107 // If this is not a typedef name, don't parse it as part of the declspec,
1108 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001109 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001110 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001111 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001112 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001113
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001114 // If we're in a context where the identifier could be a class name,
1115 // check whether this is a constructor declaration.
1116 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001117 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001118 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001119 goto DoneWithDeclSpec;
1120
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001122 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001123 if (isInvalid)
1124 break;
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattner16fac4f2008-07-26 01:18:38 +00001126 DS.SetRangeEnd(Tok.getLocation());
1127 ConsumeToken(); // The identifier
1128
1129 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1130 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1131 // Objective-C interface. If we don't have Objective-C or a '<', this is
1132 // just a normal reference to a typedef name.
1133 if (!Tok.is(tok::less) || !getLang().ObjC1)
1134 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001136 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001137 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001138 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1139 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1140 LAngleLoc, EndProtoLoc);
1141 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1142 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001143
Chris Lattner16fac4f2008-07-26 01:18:38 +00001144 DS.SetRangeEnd(EndProtoLoc);
1145
Steve Naroffcd5e7822008-09-22 10:28:57 +00001146 // Need to support trailing type qualifiers (e.g. "id<p> const").
1147 // If a type specifier follows, it will be diagnosed elsewhere.
1148 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001149 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001150
1151 // type-name
1152 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001153 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001154 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001155 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001156 // This template-id does not refer to a type name, so we're
1157 // done with the type-specifiers.
1158 goto DoneWithDeclSpec;
1159 }
1160
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001161 // If we're in a context where the template-id could be a
1162 // constructor name or specialization, check whether this is a
1163 // constructor declaration.
1164 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001165 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001166 isConstructorDeclarator())
1167 goto DoneWithDeclSpec;
1168
Douglas Gregor7f741122009-02-25 19:37:18 +00001169 // Turn the template-id annotation token into a type annotation
1170 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001171 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001172 continue;
1173 }
1174
Chris Lattnere37e2332006-08-15 04:50:22 +00001175 // GNU attributes support.
1176 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001177 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001178 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001179
1180 // Microsoft declspec support.
1181 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001182 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001183 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001184
Steve Naroff44ac7772008-12-25 14:16:32 +00001185 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001186 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001187 // FIXME: Add handling here!
1188 break;
1189
1190 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001191 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001192 case tok::kw___cdecl:
1193 case tok::kw___stdcall:
1194 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001195 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001196 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1197 continue;
1198
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001199 // storage-class-specifier
1200 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001201 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1202 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001203 break;
1204 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001205 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001206 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001207 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1208 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001209 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001210 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001211 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001212 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001213 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001214 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001215 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001216 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001217 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1218 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001219 break;
1220 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001221 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001222 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1223 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001224 else
John McCall49bfce42009-08-03 20:12:06 +00001225 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1226 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001227 break;
1228 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1230 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001231 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001232 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001233 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1234 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001235 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001236 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001237 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001238 break;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001240 // function-specifier
1241 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001242 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001243 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001244 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001245 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001246 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001247 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001248 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001249 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001250
Anders Carlssoncd8db412009-05-06 04:46:28 +00001251 // friend
1252 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001253 if (DSContext == DSC_class)
1254 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1255 else {
1256 PrevSpec = ""; // not actually used by the diagnostic
1257 DiagID = diag::err_friend_invalid_in_context;
1258 isInvalid = true;
1259 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001260 break;
Mike Stump11289f42009-09-09 15:08:12 +00001261
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001262 // constexpr
1263 case tok::kw_constexpr:
1264 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1265 break;
1266
Chris Lattnere387d9e2009-01-21 19:48:37 +00001267 // type-specifier
1268 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1270 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001271 break;
1272 case tok::kw_long:
1273 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001274 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1275 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001276 else
John McCall49bfce42009-08-03 20:12:06 +00001277 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1278 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001279 break;
1280 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1282 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001283 break;
1284 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1286 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001287 break;
1288 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1290 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001291 break;
1292 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1294 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 break;
1296 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1298 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001299 break;
1300 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1302 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303 break;
1304 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
1308 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1314 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001315 break;
1316 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1318 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001319 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001320 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1322 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001323 break;
1324 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1326 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001327 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001328 case tok::kw_bool:
1329 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1331 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001332 break;
1333 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1335 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001336 break;
1337 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1339 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001340 break;
1341 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1343 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001344 break;
John Thompson22334602010-02-05 00:12:22 +00001345 case tok::kw___vector:
1346 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1347 break;
1348 case tok::kw___pixel:
1349 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1350 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001351
1352 // class-specifier:
1353 case tok::kw_class:
1354 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001355 case tok::kw_union: {
1356 tok::TokenKind Kind = Tok.getKind();
1357 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001358 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001359 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001360 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001361
1362 // enum-specifier:
1363 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001364 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001365 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001366 continue;
1367
1368 // cv-qualifier:
1369 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1371 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001372 break;
1373 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1375 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001376 break;
1377 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001378 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1379 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001380 break;
1381
Douglas Gregor333489b2009-03-27 23:10:48 +00001382 // C++ typename-specifier:
1383 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001384 if (TryAnnotateTypeOrScopeToken()) {
1385 DS.SetTypeSpecError();
1386 goto DoneWithDeclSpec;
1387 }
1388 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001389 continue;
1390 break;
1391
Chris Lattnere387d9e2009-01-21 19:48:37 +00001392 // GNU typeof support.
1393 case tok::kw_typeof:
1394 ParseTypeofSpecifier(DS);
1395 continue;
1396
Anders Carlsson74948d02009-06-24 17:47:40 +00001397 case tok::kw_decltype:
1398 ParseDecltypeSpecifier(DS);
1399 continue;
1400
Steve Naroffcfdf6162008-06-05 00:02:44 +00001401 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001402 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001403 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1404 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001405 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001406 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattner0974b232008-07-26 00:20:22 +00001408 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001409 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001410 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001411 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1412 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1413 LAngleLoc, EndProtoLoc);
1414 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1415 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001416 DS.SetRangeEnd(EndProtoLoc);
1417
Chris Lattner6d29c102008-11-18 07:48:38 +00001418 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001419 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001420 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001421 // Need to support trailing type qualifiers (e.g. "id<p> const").
1422 // If a type specifier follows, it will be diagnosed elsewhere.
1423 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001424 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001425 }
John McCall49bfce42009-08-03 20:12:06 +00001426 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001427 if (isInvalid) {
1428 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001429 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001430
1431 if (DiagID == diag::ext_duplicate_declspec)
1432 Diag(Tok, DiagID)
1433 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1434 else
1435 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001436 }
Chris Lattner2e232092008-03-13 06:29:04 +00001437 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001438 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001439 }
1440}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001441
Chris Lattnera448d752009-01-06 06:59:53 +00001442/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001443/// primarily follow the C++ grammar with additions for C99 and GNU,
1444/// which together subsume the C grammar. Note that the C++
1445/// type-specifier also includes the C type-qualifier (for const,
1446/// volatile, and C99 restrict). Returns true if a type-specifier was
1447/// found (and parsed), false otherwise.
1448///
1449/// type-specifier: [C++ 7.1.5]
1450/// simple-type-specifier
1451/// class-specifier
1452/// enum-specifier
1453/// elaborated-type-specifier [TODO]
1454/// cv-qualifier
1455///
1456/// cv-qualifier: [C++ 7.1.5.1]
1457/// 'const'
1458/// 'volatile'
1459/// [C99] 'restrict'
1460///
1461/// simple-type-specifier: [ C++ 7.1.5.2]
1462/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1463/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1464/// 'char'
1465/// 'wchar_t'
1466/// 'bool'
1467/// 'short'
1468/// 'int'
1469/// 'long'
1470/// 'signed'
1471/// 'unsigned'
1472/// 'float'
1473/// 'double'
1474/// 'void'
1475/// [C99] '_Bool'
1476/// [C99] '_Complex'
1477/// [C99] '_Imaginary' // Removed in TC2?
1478/// [GNU] '_Decimal32'
1479/// [GNU] '_Decimal64'
1480/// [GNU] '_Decimal128'
1481/// [GNU] typeof-specifier
1482/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1483/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001484/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001485/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001486bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001487 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001488 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001489 const ParsedTemplateInfo &TemplateInfo,
1490 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001491 SourceLocation Loc = Tok.getLocation();
1492
1493 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001494 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001495 // If we already have a type specifier, this identifier is not a type.
1496 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1497 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1498 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1499 return false;
John Thompson22334602010-02-05 00:12:22 +00001500 // Check for need to substitute AltiVec keyword tokens.
1501 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1502 break;
1503 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001504 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001505 // Annotate typenames and C++ scope specifiers. If we get one, just
1506 // recurse to handle whatever we get.
1507 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001508 return true;
1509 if (Tok.is(tok::identifier))
1510 return false;
1511 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1512 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001513 case tok::coloncolon: // ::foo::bar
1514 if (NextToken().is(tok::kw_new) || // ::new
1515 NextToken().is(tok::kw_delete)) // ::delete
1516 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001517
Chris Lattner020bab92009-01-04 23:41:41 +00001518 // Annotate typenames and C++ scope specifiers. If we get one, just
1519 // recurse to handle whatever we get.
1520 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001521 return true;
1522 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1523 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001524
Douglas Gregor450c75a2008-11-07 15:42:26 +00001525 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001526 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001527 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001528 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001529 DiagID, T);
1530 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001531 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001532 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1533 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001534
Douglas Gregor450c75a2008-11-07 15:42:26 +00001535 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1536 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1537 // Objective-C interface. If we don't have Objective-C or a '<', this is
1538 // just a normal reference to a typedef name.
1539 if (!Tok.is(tok::less) || !getLang().ObjC1)
1540 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001541
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001542 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001543 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001544 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1545 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1546 LAngleLoc, EndProtoLoc);
1547 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1548 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001549
Douglas Gregor450c75a2008-11-07 15:42:26 +00001550 DS.SetRangeEnd(EndProtoLoc);
1551 return true;
1552 }
1553
1554 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001555 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001556 break;
1557 case tok::kw_long:
1558 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001559 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1560 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001561 else
John McCall49bfce42009-08-03 20:12:06 +00001562 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1563 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001564 break;
1565 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001566 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001569 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1570 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001571 break;
1572 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1574 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001575 break;
1576 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1578 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001579 break;
1580 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001581 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001582 break;
1583 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001584 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001585 break;
1586 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001587 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001588 break;
1589 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001590 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001591 break;
1592 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001597 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001598 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001599 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001600 break;
1601 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001603 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001604 case tok::kw_bool:
1605 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001606 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1610 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001611 break;
1612 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1614 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001615 break;
1616 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001617 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1618 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001619 break;
John Thompson22334602010-02-05 00:12:22 +00001620 case tok::kw___vector:
1621 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1622 break;
1623 case tok::kw___pixel:
1624 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1625 break;
1626
Douglas Gregor450c75a2008-11-07 15:42:26 +00001627 // class-specifier:
1628 case tok::kw_class:
1629 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001630 case tok::kw_union: {
1631 tok::TokenKind Kind = Tok.getKind();
1632 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001633 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1634 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001635 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001636 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001637
1638 // enum-specifier:
1639 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001640 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001641 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001642 return true;
1643
1644 // cv-qualifier:
1645 case tok::kw_const:
1646 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001647 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001648 break;
1649 case tok::kw_volatile:
1650 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001651 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001652 break;
1653 case tok::kw_restrict:
1654 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001655 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001656 break;
1657
1658 // GNU typeof support.
1659 case tok::kw_typeof:
1660 ParseTypeofSpecifier(DS);
1661 return true;
1662
Anders Carlsson74948d02009-06-24 17:47:40 +00001663 // C++0x decltype support.
1664 case tok::kw_decltype:
1665 ParseDecltypeSpecifier(DS);
1666 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001667
Anders Carlssonbae27372009-06-26 23:44:14 +00001668 // C++0x auto support.
1669 case tok::kw_auto:
1670 if (!getLang().CPlusPlus0x)
1671 return false;
1672
John McCall49bfce42009-08-03 20:12:06 +00001673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001674 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001675 case tok::kw___ptr64:
1676 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001677 case tok::kw___cdecl:
1678 case tok::kw___stdcall:
1679 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001680 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001681 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001682 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001683
Douglas Gregor450c75a2008-11-07 15:42:26 +00001684 default:
1685 // Not a type-specifier; do nothing.
1686 return false;
1687 }
1688
1689 // If the specifier combination wasn't legal, issue a diagnostic.
1690 if (isInvalid) {
1691 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001692 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001693 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001694 }
1695 DS.SetRangeEnd(Tok.getLocation());
1696 ConsumeToken(); // whatever we parsed above.
1697 return true;
1698}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001699
Chris Lattner70ae4912007-10-29 04:42:53 +00001700/// ParseStructDeclaration - Parse a struct declaration without the terminating
1701/// semicolon.
1702///
Chris Lattner90a26b02007-01-23 04:38:16 +00001703/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001704/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001705/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001706/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001707/// struct-declarator-list:
1708/// struct-declarator
1709/// struct-declarator-list ',' struct-declarator
1710/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1711/// struct-declarator:
1712/// declarator
1713/// [GNU] declarator attributes[opt]
1714/// declarator[opt] ':' constant-expression
1715/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1716///
Chris Lattnera12405b2008-04-10 06:46:29 +00001717void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001718ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001719 if (Tok.is(tok::kw___extension__)) {
1720 // __extension__ silences extension warnings in the subexpression.
1721 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001722 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001723 return ParseStructDeclaration(DS, Fields);
1724 }
Mike Stump11289f42009-09-09 15:08:12 +00001725
Steve Naroff97170802007-08-20 22:28:22 +00001726 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001727 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001728 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001729
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001730 // If there are no declarators, this is a free-standing declaration
1731 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001732 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001733 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001734 return;
1735 }
1736
1737 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001738 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001739 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001740 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001741 FieldDeclarator DeclaratorInfo(DS);
1742
1743 // Attributes are only allowed here on successive declarators.
1744 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1745 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001746 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001747 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1748 }
Mike Stump11289f42009-09-09 15:08:12 +00001749
Steve Naroff97170802007-08-20 22:28:22 +00001750 /// struct-declarator: declarator
1751 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001752 if (Tok.isNot(tok::colon)) {
1753 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1754 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001755 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001756 }
Mike Stump11289f42009-09-09 15:08:12 +00001757
Chris Lattner76c72282007-10-09 17:33:22 +00001758 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001759 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001760 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001761 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001762 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001763 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001764 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001765 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001766
Steve Naroff97170802007-08-20 22:28:22 +00001767 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001768 if (Tok.is(tok::kw___attribute)) {
1769 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001770 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001771 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1772 }
1773
John McCallcfefb6d2009-11-03 02:38:08 +00001774 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001775 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001776 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001777
Steve Naroff97170802007-08-20 22:28:22 +00001778 // If we don't have a comma, it is either the end of the list (a ';')
1779 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001780 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001781 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001782
Steve Naroff97170802007-08-20 22:28:22 +00001783 // Consume the comma.
1784 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001785
John McCallcfefb6d2009-11-03 02:38:08 +00001786 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001787 }
Steve Naroff97170802007-08-20 22:28:22 +00001788}
1789
1790/// ParseStructUnionBody
1791/// struct-contents:
1792/// struct-declaration-list
1793/// [EXT] empty
1794/// [GNU] "struct-declaration-list" without terminatoring ';'
1795/// struct-declaration-list:
1796/// struct-declaration
1797/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001798/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001799///
Chris Lattner1300fb92007-01-23 23:42:53 +00001800void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001801 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001802 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1803 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001804
Chris Lattner90a26b02007-01-23 04:38:16 +00001805 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001806
Douglas Gregor658b9552009-01-09 22:42:13 +00001807 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001808 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001809
Chris Lattner7b9ace62007-01-23 20:11:08 +00001810 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1811 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001812 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001813 Diag(Tok, diag::ext_empty_struct_union)
1814 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001815
John McCall48871652010-08-21 09:40:31 +00001816 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001817
Chris Lattner7b9ace62007-01-23 20:11:08 +00001818 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001819 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001820 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001821
Chris Lattner736ed5d2007-06-09 05:59:07 +00001822 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001823 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001824 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001825 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001826 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001827 ConsumeToken();
1828 continue;
1829 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001830
1831 // Parse all the comma separated declarators.
1832 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001833
John McCallcfefb6d2009-11-03 02:38:08 +00001834 if (!Tok.is(tok::at)) {
1835 struct CFieldCallback : FieldCallback {
1836 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001837 Decl *TagDecl;
1838 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001839
John McCall48871652010-08-21 09:40:31 +00001840 CFieldCallback(Parser &P, Decl *TagDecl,
1841 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001842 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1843
John McCall48871652010-08-21 09:40:31 +00001844 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001845 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001846 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001847 FD.D.getDeclSpec().getSourceRange().getBegin(),
1848 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001849 FieldDecls.push_back(Field);
1850 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001851 }
John McCallcfefb6d2009-11-03 02:38:08 +00001852 } Callback(*this, TagDecl, FieldDecls);
1853
1854 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001855 } else { // Handle @defs
1856 ConsumeToken();
1857 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1858 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001859 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001860 continue;
1861 }
1862 ConsumeToken();
1863 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1864 if (!Tok.is(tok::identifier)) {
1865 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001866 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001867 continue;
1868 }
John McCall48871652010-08-21 09:40:31 +00001869 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001870 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001871 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001872 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1873 ConsumeToken();
1874 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001875 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001876
Chris Lattner76c72282007-10-09 17:33:22 +00001877 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001878 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001879 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001880 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001881 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001882 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001883 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1884 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001885 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001886 // If we stopped at a ';', eat it.
1887 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001888 }
1889 }
Mike Stump11289f42009-09-09 15:08:12 +00001890
Steve Naroff33a1e802007-10-29 21:38:07 +00001891 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001892
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001893 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001894 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001895 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001896 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001897
Douglas Gregor0be31a22010-07-02 17:43:08 +00001898 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001899 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001900 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001901 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001902 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001903 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001904}
1905
1906
Chris Lattner3b561a32006-08-13 00:12:11 +00001907/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001908/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001909/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001910///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001911/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1912/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001913/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001914/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001915///
1916/// [C++] elaborated-type-specifier:
1917/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1918///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001919void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001920 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001921 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001922 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001923 if (Tok.is(tok::code_completion)) {
1924 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001925 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001926 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001927 }
1928
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001929 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001930 // If attributes exist after tag, parse them.
1931 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001932 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001933
Abramo Bagnarad7548482010-05-19 21:37:53 +00001934 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001935 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001936 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001937 return;
1938
1939 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001940 Diag(Tok, diag::err_expected_ident);
1941 if (Tok.isNot(tok::l_brace)) {
1942 // Has no name and is not a definition.
1943 // Skip the rest of this declarator, up until the comma or semicolon.
1944 SkipUntil(tok::comma, true);
1945 return;
1946 }
1947 }
1948 }
Mike Stump11289f42009-09-09 15:08:12 +00001949
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001950 // Must have either 'enum name' or 'enum {...}'.
1951 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1952 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001953
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001954 // Skip the rest of this declarator, up until the comma or semicolon.
1955 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001956 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001957 }
Mike Stump11289f42009-09-09 15:08:12 +00001958
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001959 // If an identifier is present, consume and remember it.
1960 IdentifierInfo *Name = 0;
1961 SourceLocation NameLoc;
1962 if (Tok.is(tok::identifier)) {
1963 Name = Tok.getIdentifierInfo();
1964 NameLoc = ConsumeToken();
1965 }
Mike Stump11289f42009-09-09 15:08:12 +00001966
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001967 // There are three options here. If we have 'enum foo;', then this is a
1968 // forward declaration. If we have 'enum foo {...' then this is a
1969 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1970 //
1971 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1972 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1973 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1974 //
John McCallfaf5fb42010-08-26 23:41:50 +00001975 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001976 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00001977 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001978 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00001979 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001980 else
John McCallfaf5fb42010-08-26 23:41:50 +00001981 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00001982
1983 // enums cannot be templates, although they can be referenced from a
1984 // template.
1985 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00001986 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00001987 Diag(Tok, diag::err_enum_template);
1988
1989 // Skip the rest of this declarator, up until the comma or semicolon.
1990 SkipUntil(tok::comma, true);
1991 return;
1992 }
1993
Douglas Gregord6ab8742009-05-28 23:31:59 +00001994 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001995 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00001996 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1997 const char *PrevSpec = 0;
1998 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00001999 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2000 StartLoc, SS, Name, NameLoc, Attr.get(),
2001 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002002 MultiTemplateParamsArg(Actions),
John McCall48871652010-08-21 09:40:31 +00002003 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00002004 if (IsDependent) {
2005 // This enum has a dependent nested-name-specifier. Handle it as a
2006 // dependent tag.
2007 if (!Name) {
2008 DS.SetTypeSpecError();
2009 Diag(Tok, diag::err_expected_type_name_after_typename);
2010 return;
2011 }
2012
Douglas Gregor0be31a22010-07-02 17:43:08 +00002013 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002014 TUK, SS, Name, StartLoc,
2015 NameLoc);
2016 if (Type.isInvalid()) {
2017 DS.SetTypeSpecError();
2018 return;
2019 }
2020
2021 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002022 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002023 Diag(StartLoc, DiagID) << PrevSpec;
2024
2025 return;
2026 }
Mike Stump11289f42009-09-09 15:08:12 +00002027
John McCall48871652010-08-21 09:40:31 +00002028 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002029 // The action failed to produce an enumeration tag. If this is a
2030 // definition, consume the entire definition.
2031 if (Tok.is(tok::l_brace)) {
2032 ConsumeBrace();
2033 SkipUntil(tok::r_brace);
2034 }
2035
2036 DS.SetTypeSpecError();
2037 return;
2038 }
2039
Chris Lattner76c72282007-10-09 17:33:22 +00002040 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002041 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002042
John McCallba7bf592010-08-24 05:47:05 +00002043 // FIXME: The DeclSpec should keep the locations of both the keyword
2044 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002045 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002046 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002047 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002048}
2049
Chris Lattnerc1915e22007-01-25 07:29:02 +00002050/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2051/// enumerator-list:
2052/// enumerator
2053/// enumerator-list ',' enumerator
2054/// enumerator:
2055/// enumeration-constant
2056/// enumeration-constant '=' constant-expression
2057/// enumeration-constant:
2058/// identifier
2059///
John McCall48871652010-08-21 09:40:31 +00002060void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002061 // Enter the scope of the enum body and start the definition.
2062 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002063 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002064
Chris Lattnerc1915e22007-01-25 07:29:02 +00002065 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002066
Chris Lattner37256fb2007-08-27 17:24:30 +00002067 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002068 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002069 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002070
John McCall48871652010-08-21 09:40:31 +00002071 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002072
John McCall48871652010-08-21 09:40:31 +00002073 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002074
Chris Lattnerc1915e22007-01-25 07:29:02 +00002075 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002076 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002077 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2078 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002079
Chris Lattnerc1915e22007-01-25 07:29:02 +00002080 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002081 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002082 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002083 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002084 AssignedVal = ParseConstantExpression();
2085 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002086 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002087 }
Mike Stump11289f42009-09-09 15:08:12 +00002088
Chris Lattnerc1915e22007-01-25 07:29:02 +00002089 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002090 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2091 LastEnumConstDecl,
2092 IdentLoc, Ident,
2093 EqualLoc,
2094 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002095 EnumConstantDecls.push_back(EnumConstDecl);
2096 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002097
Chris Lattner76c72282007-10-09 17:33:22 +00002098 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002099 break;
2100 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002101
2102 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002103 !(getLang().C99 || getLang().CPlusPlus0x))
2104 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2105 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002106 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002107 }
Mike Stump11289f42009-09-09 15:08:12 +00002108
Chris Lattnerc1915e22007-01-25 07:29:02 +00002109 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002110 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002111
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002112 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002113 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002114 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002115 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002116
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002117 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2118 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00002119 getCurScope(), Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002120
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002121 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002122 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002123}
Chris Lattner3b561a32006-08-13 00:12:11 +00002124
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002125/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002126/// start of a type-qualifier-list.
2127bool Parser::isTypeQualifier() const {
2128 switch (Tok.getKind()) {
2129 default: return false;
2130 // type-qualifier
2131 case tok::kw_const:
2132 case tok::kw_volatile:
2133 case tok::kw_restrict:
2134 return true;
2135 }
2136}
2137
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002138/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2139/// is definitely a type-specifier. Return false if it isn't part of a type
2140/// specifier or if we're not sure.
2141bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2142 switch (Tok.getKind()) {
2143 default: return false;
2144 // type-specifiers
2145 case tok::kw_short:
2146 case tok::kw_long:
2147 case tok::kw_signed:
2148 case tok::kw_unsigned:
2149 case tok::kw__Complex:
2150 case tok::kw__Imaginary:
2151 case tok::kw_void:
2152 case tok::kw_char:
2153 case tok::kw_wchar_t:
2154 case tok::kw_char16_t:
2155 case tok::kw_char32_t:
2156 case tok::kw_int:
2157 case tok::kw_float:
2158 case tok::kw_double:
2159 case tok::kw_bool:
2160 case tok::kw__Bool:
2161 case tok::kw__Decimal32:
2162 case tok::kw__Decimal64:
2163 case tok::kw__Decimal128:
2164 case tok::kw___vector:
2165
2166 // struct-or-union-specifier (C99) or class-specifier (C++)
2167 case tok::kw_class:
2168 case tok::kw_struct:
2169 case tok::kw_union:
2170 // enum-specifier
2171 case tok::kw_enum:
2172
2173 // typedef-name
2174 case tok::annot_typename:
2175 return true;
2176 }
2177}
2178
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002179/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002180/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002181bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002182 switch (Tok.getKind()) {
2183 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002184
Chris Lattner020bab92009-01-04 23:41:41 +00002185 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002186 if (TryAltiVecVectorToken())
2187 return true;
2188 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002189 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002190 // Annotate typenames and C++ scope specifiers. If we get one, just
2191 // recurse to handle whatever we get.
2192 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002193 return true;
2194 if (Tok.is(tok::identifier))
2195 return false;
2196 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002197
Chris Lattner020bab92009-01-04 23:41:41 +00002198 case tok::coloncolon: // ::foo::bar
2199 if (NextToken().is(tok::kw_new) || // ::new
2200 NextToken().is(tok::kw_delete)) // ::delete
2201 return false;
2202
Chris Lattner020bab92009-01-04 23:41:41 +00002203 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002204 return true;
2205 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002206
Chris Lattnere37e2332006-08-15 04:50:22 +00002207 // GNU attributes support.
2208 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002209 // GNU typeof support.
2210 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002211
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002212 // type-specifiers
2213 case tok::kw_short:
2214 case tok::kw_long:
2215 case tok::kw_signed:
2216 case tok::kw_unsigned:
2217 case tok::kw__Complex:
2218 case tok::kw__Imaginary:
2219 case tok::kw_void:
2220 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002221 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002222 case tok::kw_char16_t:
2223 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002224 case tok::kw_int:
2225 case tok::kw_float:
2226 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002227 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002228 case tok::kw__Bool:
2229 case tok::kw__Decimal32:
2230 case tok::kw__Decimal64:
2231 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002232 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002233
Chris Lattner861a2262008-04-13 18:59:07 +00002234 // struct-or-union-specifier (C99) or class-specifier (C++)
2235 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002236 case tok::kw_struct:
2237 case tok::kw_union:
2238 // enum-specifier
2239 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002240
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002241 // type-qualifier
2242 case tok::kw_const:
2243 case tok::kw_volatile:
2244 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002245
2246 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002247 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002248 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002249
Chris Lattner409bf7d2008-10-20 00:25:30 +00002250 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2251 case tok::less:
2252 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002253
Steve Naroff44ac7772008-12-25 14:16:32 +00002254 case tok::kw___cdecl:
2255 case tok::kw___stdcall:
2256 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002257 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002258 case tok::kw___w64:
2259 case tok::kw___ptr64:
2260 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002261 }
2262}
2263
Chris Lattneracd58a32006-08-06 17:24:14 +00002264/// isDeclarationSpecifier() - Return true if the current token is part of a
2265/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002266bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002267 switch (Tok.getKind()) {
2268 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002269
Chris Lattner020bab92009-01-04 23:41:41 +00002270 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002271 // Unfortunate hack to support "Class.factoryMethod" notation.
2272 if (getLang().ObjC1 && NextToken().is(tok::period))
2273 return false;
John Thompson22334602010-02-05 00:12:22 +00002274 if (TryAltiVecVectorToken())
2275 return true;
2276 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002277 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002278 // Annotate typenames and C++ scope specifiers. If we get one, just
2279 // recurse to handle whatever we get.
2280 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002281 return true;
2282 if (Tok.is(tok::identifier))
2283 return false;
2284 return isDeclarationSpecifier();
2285
Chris Lattner020bab92009-01-04 23:41:41 +00002286 case tok::coloncolon: // ::foo::bar
2287 if (NextToken().is(tok::kw_new) || // ::new
2288 NextToken().is(tok::kw_delete)) // ::delete
2289 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002290
Chris Lattner020bab92009-01-04 23:41:41 +00002291 // Annotate typenames and C++ scope specifiers. If we get one, just
2292 // recurse to handle whatever we get.
2293 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002294 return true;
2295 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002296
Chris Lattneracd58a32006-08-06 17:24:14 +00002297 // storage-class-specifier
2298 case tok::kw_typedef:
2299 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002300 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002301 case tok::kw_static:
2302 case tok::kw_auto:
2303 case tok::kw_register:
2304 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002305
Chris Lattneracd58a32006-08-06 17:24:14 +00002306 // type-specifiers
2307 case tok::kw_short:
2308 case tok::kw_long:
2309 case tok::kw_signed:
2310 case tok::kw_unsigned:
2311 case tok::kw__Complex:
2312 case tok::kw__Imaginary:
2313 case tok::kw_void:
2314 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002315 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002316 case tok::kw_char16_t:
2317 case tok::kw_char32_t:
2318
Chris Lattneracd58a32006-08-06 17:24:14 +00002319 case tok::kw_int:
2320 case tok::kw_float:
2321 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002322 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002323 case tok::kw__Bool:
2324 case tok::kw__Decimal32:
2325 case tok::kw__Decimal64:
2326 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002327 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002328
Chris Lattner861a2262008-04-13 18:59:07 +00002329 // struct-or-union-specifier (C99) or class-specifier (C++)
2330 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002331 case tok::kw_struct:
2332 case tok::kw_union:
2333 // enum-specifier
2334 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002335
Chris Lattneracd58a32006-08-06 17:24:14 +00002336 // type-qualifier
2337 case tok::kw_const:
2338 case tok::kw_volatile:
2339 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002340
Chris Lattneracd58a32006-08-06 17:24:14 +00002341 // function-specifier
2342 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002343 case tok::kw_virtual:
2344 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002345
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002346 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002347 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002348
Chris Lattner599e47e2007-08-09 17:01:07 +00002349 // GNU typeof support.
2350 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002351
Chris Lattner599e47e2007-08-09 17:01:07 +00002352 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002353 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002354 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002355
Chris Lattner8b2ec162008-07-26 03:38:44 +00002356 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2357 case tok::less:
2358 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002359
Steve Narofff192fab2009-01-06 19:34:12 +00002360 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002361 case tok::kw___cdecl:
2362 case tok::kw___stdcall:
2363 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002364 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002365 case tok::kw___w64:
2366 case tok::kw___ptr64:
2367 case tok::kw___forceinline:
2368 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002369 }
2370}
2371
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002372bool Parser::isConstructorDeclarator() {
2373 TentativeParsingAction TPA(*this);
2374
2375 // Parse the C++ scope specifier.
2376 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002377 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002378 TPA.Revert();
2379 return false;
2380 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002381
2382 // Parse the constructor name.
2383 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2384 // We already know that we have a constructor name; just consume
2385 // the token.
2386 ConsumeToken();
2387 } else {
2388 TPA.Revert();
2389 return false;
2390 }
2391
2392 // Current class name must be followed by a left parentheses.
2393 if (Tok.isNot(tok::l_paren)) {
2394 TPA.Revert();
2395 return false;
2396 }
2397 ConsumeParen();
2398
2399 // A right parentheses or ellipsis signals that we have a constructor.
2400 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2401 TPA.Revert();
2402 return true;
2403 }
2404
2405 // If we need to, enter the specified scope.
2406 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002407 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002408 DeclScopeObj.EnterDeclaratorScope();
2409
2410 // Check whether the next token(s) are part of a declaration
2411 // specifier, in which case we have the start of a parameter and,
2412 // therefore, we know that this is a constructor.
2413 bool IsConstructor = isDeclarationSpecifier();
2414 TPA.Revert();
2415 return IsConstructor;
2416}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002417
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002418/// ParseTypeQualifierListOpt
2419/// type-qualifier-list: [C99 6.7.5]
2420/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002421/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002422/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002423/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002424/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2425/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002426///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002427void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2428 bool CXX0XAttributesAllowed) {
2429 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2430 SourceLocation Loc = Tok.getLocation();
2431 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2432 if (CXX0XAttributesAllowed)
2433 DS.AddAttributes(Attr.AttrList);
2434 else
2435 Diag(Loc, diag::err_attributes_not_allowed);
2436 }
2437
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002438 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002439 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002440 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002441 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002442 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002443
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002444 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002445 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002446 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2447 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002448 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002449 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002450 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2451 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002452 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002453 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002454 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2455 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002456 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002457 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002458 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002459 case tok::kw___cdecl:
2460 case tok::kw___stdcall:
2461 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002462 case tok::kw___thiscall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002463 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002464 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2465 continue;
2466 }
2467 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002468 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002469 if (GNUAttributesAllowed) {
2470 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002471 continue; // do *not* consume the next token!
2472 }
2473 // otherwise, FALL THROUGH!
2474 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002475 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002476 // If this is not a type-qualifier token, we're done reading type
2477 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002478 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002479 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002480 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002481
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002482 // If the specifier combination wasn't legal, issue a diagnostic.
2483 if (isInvalid) {
2484 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002485 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002486 }
2487 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002488 }
2489}
2490
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002491
2492/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2493///
2494void Parser::ParseDeclarator(Declarator &D) {
2495 /// This implements the 'declarator' production in the C grammar, then checks
2496 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002497 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002498}
2499
Sebastian Redlbd150f42008-11-21 19:14:01 +00002500/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2501/// is parsed by the function passed to it. Pass null, and the direct-declarator
2502/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002503/// ptr-operator production.
2504///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002505/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2506/// [C] pointer[opt] direct-declarator
2507/// [C++] direct-declarator
2508/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002509///
2510/// pointer: [C99 6.7.5]
2511/// '*' type-qualifier-list[opt]
2512/// '*' type-qualifier-list[opt] pointer
2513///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002514/// ptr-operator:
2515/// '*' cv-qualifier-seq[opt]
2516/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002517/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002518/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002519/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002520/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002521void Parser::ParseDeclaratorInternal(Declarator &D,
2522 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002523 if (Diags.hasAllExtensionsSilenced())
2524 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002525
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002526 // C++ member pointers start with a '::' or a nested-name.
2527 // Member pointers get special handling, since there's no place for the
2528 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002529 if (getLang().CPlusPlus &&
2530 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2531 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002532 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002533 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002534
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002535 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002536 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002537 // The scope spec really belongs to the direct-declarator.
2538 D.getCXXScopeSpec() = SS;
2539 if (DirectDeclParser)
2540 (this->*DirectDeclParser)(D);
2541 return;
2542 }
2543
2544 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002545 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002546 DeclSpec DS;
2547 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002548 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002549
2550 // Recurse to parse whatever is left.
2551 ParseDeclaratorInternal(D, DirectDeclParser);
2552
2553 // Sema will have to catch (syntactically invalid) pointers into global
2554 // scope. It has to catch pointers into namespace scope anyway.
2555 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002556 Loc, DS.TakeAttributes()),
2557 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002558 return;
2559 }
2560 }
2561
2562 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002563 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002564 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002565 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002566 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002567 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002568 if (DirectDeclParser)
2569 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002570 return;
2571 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002572
Sebastian Redled0f3b02009-03-15 22:02:01 +00002573 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2574 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002575 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002576 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002577
Chris Lattner9eac9312009-03-27 04:18:06 +00002578 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002579 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002580 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002581
Bill Wendling3708c182007-05-27 10:15:43 +00002582 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002583 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002584
Bill Wendling3708c182007-05-27 10:15:43 +00002585 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002586 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002587 if (Kind == tok::star)
2588 // Remember that we parsed a pointer type, and remember the type-quals.
2589 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002590 DS.TakeAttributes()),
2591 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002592 else
2593 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002594 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002595 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002596 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002597 } else {
2598 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002599 DeclSpec DS;
2600
Sebastian Redl3b27be62009-03-23 00:00:23 +00002601 // Complain about rvalue references in C++03, but then go on and build
2602 // the declarator.
2603 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2604 Diag(Loc, diag::err_rvalue_reference);
2605
Bill Wendling93efb222007-06-02 23:28:54 +00002606 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2607 // cv-qualifiers are introduced through the use of a typedef or of a
2608 // template type argument, in which case the cv-qualifiers are ignored.
2609 //
2610 // [GNU] Retricted references are allowed.
2611 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002612 // [C++0x] Attributes on references are not allowed.
2613 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002614 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002615
2616 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2617 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2618 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002619 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002620 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2621 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002622 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002623 }
Bill Wendling3708c182007-05-27 10:15:43 +00002624
2625 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002626 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002627
Douglas Gregor66583c52008-11-03 15:51:28 +00002628 if (D.getNumTypeObjects() > 0) {
2629 // C++ [dcl.ref]p4: There shall be no references to references.
2630 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2631 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002632 if (const IdentifierInfo *II = D.getIdentifier())
2633 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2634 << II;
2635 else
2636 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2637 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002638
Sebastian Redlbd150f42008-11-21 19:14:01 +00002639 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002640 // can go ahead and build the (technically ill-formed)
2641 // declarator: reference collapsing will take care of it.
2642 }
2643 }
2644
Bill Wendling3708c182007-05-27 10:15:43 +00002645 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002646 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002647 DS.TakeAttributes(),
2648 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002649 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002650 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002651}
2652
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002653/// ParseDirectDeclarator
2654/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002655/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002656/// '(' declarator ')'
2657/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002658/// [C90] direct-declarator '[' constant-expression[opt] ']'
2659/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2660/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2661/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2662/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002663/// direct-declarator '(' parameter-type-list ')'
2664/// direct-declarator '(' identifier-list[opt] ')'
2665/// [GNU] direct-declarator '(' parameter-forward-declarations
2666/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002667/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2668/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002669/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002670///
2671/// declarator-id: [C++ 8]
2672/// id-expression
2673/// '::'[opt] nested-name-specifier[opt] type-name
2674///
2675/// id-expression: [C++ 5.1]
2676/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002677/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002678///
2679/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002680/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002681/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002682/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002683/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002684/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002685///
Chris Lattneracd58a32006-08-06 17:24:14 +00002686void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002687 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002688
Douglas Gregor7861a802009-11-03 01:35:08 +00002689 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2690 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002691 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002692 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002693 }
2694
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002695 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002696 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002697 // Change the declaration context for name lookup, until this function
2698 // is exited (and the declarator has been parsed).
2699 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002700 }
2701
Douglas Gregor7861a802009-11-03 01:35:08 +00002702 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2703 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2704 // We found something that indicates the start of an unqualified-id.
2705 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002706 bool AllowConstructorName;
2707 if (D.getDeclSpec().hasTypeSpecifier())
2708 AllowConstructorName = false;
2709 else if (D.getCXXScopeSpec().isSet())
2710 AllowConstructorName =
2711 (D.getContext() == Declarator::FileContext ||
2712 (D.getContext() == Declarator::MemberContext &&
2713 D.getDeclSpec().isFriendSpecified()));
2714 else
2715 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2716
Douglas Gregor7861a802009-11-03 01:35:08 +00002717 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2718 /*EnteringContext=*/true,
2719 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002720 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002721 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002722 D.getName()) ||
2723 // Once we're past the identifier, if the scope was bad, mark the
2724 // whole declarator bad.
2725 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002726 D.SetIdentifier(0, Tok.getLocation());
2727 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002728 } else {
2729 // Parsed the unqualified-id; update range information and move along.
2730 if (D.getSourceRange().getBegin().isInvalid())
2731 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2732 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002733 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002734 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002735 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002736 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002737 assert(!getLang().CPlusPlus &&
2738 "There's a C++-specific check for tok::identifier above");
2739 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2740 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2741 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002742 goto PastIdentifier;
2743 }
2744
2745 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002746 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002747 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002748 // Example: 'char (*X)' or 'int (*XX)(void)'
2749 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002750
2751 // If the declarator was parenthesized, we entered the declarator
2752 // scope when parsing the parenthesized declarator, then exited
2753 // the scope already. Re-enter the scope, if we need to.
2754 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002755 // If there was an error parsing parenthesized declarator, declarator
2756 // scope may have been enterred before. Don't do it again.
2757 if (!D.isInvalidType() &&
2758 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002759 // Change the declaration context for name lookup, until this function
2760 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002761 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002762 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002763 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002764 // This could be something simple like "int" (in which case the declarator
2765 // portion is empty), if an abstract-declarator is allowed.
2766 D.SetIdentifier(0, Tok.getLocation());
2767 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002768 if (D.getContext() == Declarator::MemberContext)
2769 Diag(Tok, diag::err_expected_member_name_or_semi)
2770 << D.getDeclSpec().getSourceRange();
2771 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002772 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002773 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002774 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002775 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002776 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002777 }
Mike Stump11289f42009-09-09 15:08:12 +00002778
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002779 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002780 assert(D.isPastIdentifier() &&
2781 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002782
Alexis Hunt96d5c762009-11-21 08:43:09 +00002783 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002784 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002785 && isCXX0XAttributeSpecifier(true)) {
2786 SourceLocation AttrEndLoc;
2787 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2788 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2789 }
2790
Chris Lattneracd58a32006-08-06 17:24:14 +00002791 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002792 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002793 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2794 // In such a case, check if we actually have a function declarator; if it
2795 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002796 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2797 // When not in file scope, warn for ambiguous function declarators, just
2798 // in case the author intended it as a variable definition.
2799 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2800 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2801 break;
2802 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002803 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002804 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002805 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002806 } else {
2807 break;
2808 }
2809 }
2810}
2811
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002812/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2813/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002814/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002815/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2816///
2817/// direct-declarator:
2818/// '(' declarator ')'
2819/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002820/// direct-declarator '(' parameter-type-list ')'
2821/// direct-declarator '(' identifier-list[opt] ')'
2822/// [GNU] direct-declarator '(' parameter-forward-declarations
2823/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002824///
2825void Parser::ParseParenDeclarator(Declarator &D) {
2826 SourceLocation StartLoc = ConsumeParen();
2827 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002828
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002829 // Eat any attributes before we look at whether this is a grouping or function
2830 // declarator paren. If this is a grouping paren, the attribute applies to
2831 // the type being built up, for example:
2832 // int (__attribute__(()) *x)(long y)
2833 // If this ends up not being a grouping paren, the attribute applies to the
2834 // first argument, for example:
2835 // int (__attribute__(()) int x)
2836 // In either case, we need to eat any attributes to be able to determine what
2837 // sort of paren this is.
2838 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002839 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002840 bool RequiresArg = false;
2841 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002842 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002843
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002844 // We require that the argument list (if this is a non-grouping paren) be
2845 // present even if the attribute list was empty.
2846 RequiresArg = true;
2847 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002848 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002849 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002850 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2851 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002852 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002853 }
Mike Stump11289f42009-09-09 15:08:12 +00002854
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002855 // If we haven't past the identifier yet (or where the identifier would be
2856 // stored, if this is an abstract declarator), then this is probably just
2857 // grouping parens. However, if this could be an abstract-declarator, then
2858 // this could also be the start of function arguments (consider 'void()').
2859 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002860
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002861 if (!D.mayOmitIdentifier()) {
2862 // If this can't be an abstract-declarator, this *must* be a grouping
2863 // paren, because we haven't seen the identifier yet.
2864 isGrouping = true;
2865 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002866 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002867 isDeclarationSpecifier()) { // 'int(int)' is a function.
2868 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2869 // considered to be a type, not a K&R identifier-list.
2870 isGrouping = false;
2871 } else {
2872 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2873 isGrouping = true;
2874 }
Mike Stump11289f42009-09-09 15:08:12 +00002875
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002876 // If this is a grouping paren, handle:
2877 // direct-declarator: '(' declarator ')'
2878 // direct-declarator: '(' attributes declarator ')'
2879 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002880 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002881 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002882 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002883 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002884
Sebastian Redlbd150f42008-11-21 19:14:01 +00002885 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002886 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002887 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002888
2889 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002890 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002891 return;
2892 }
Mike Stump11289f42009-09-09 15:08:12 +00002893
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002894 // Okay, if this wasn't a grouping paren, it must be the start of a function
2895 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002896 // identifier (and remember where it would have been), then call into
2897 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002898 D.SetIdentifier(0, Tok.getLocation());
2899
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002900 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002901}
2902
2903/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2904/// declarator D up to a paren, which indicates that we are parsing function
2905/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002906///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002907/// If AttrList is non-null, then the caller parsed those arguments immediately
2908/// after the open paren - they should be considered to be the first argument of
2909/// a parameter. If RequiresArg is true, then the first argument of the
2910/// function is required to be present and required to not be an identifier
2911/// list.
2912///
Chris Lattneracd58a32006-08-06 17:24:14 +00002913/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002914/// parameter-type-list: [C99 6.7.5]
2915/// parameter-list
2916/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002917/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002918///
2919/// parameter-list: [C99 6.7.5]
2920/// parameter-declaration
2921/// parameter-list ',' parameter-declaration
2922///
2923/// parameter-declaration: [C99 6.7.5]
2924/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002925/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002926/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002927/// declaration-specifiers abstract-declarator[opt]
2928/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002929/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002930/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002931///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002932/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002933/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002934///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002935void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2936 AttributeList *AttrList,
2937 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002938 // lparen is already consumed!
2939 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002940
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002941 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002942 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002943 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002944 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002945 delete AttrList;
2946 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002947
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002948 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2949 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002950
2951 // cv-qualifier-seq[opt].
2952 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002953 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002954 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002955 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00002956 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00002957 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002958 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002959 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002960 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002961 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002962
2963 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002964 if (Tok.is(tok::kw_throw)) {
2965 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002966 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002967 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002968 hasAnyExceptionSpec);
2969 assert(Exceptions.size() == ExceptionRanges.size() &&
2970 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002971 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002972 }
2973
Chris Lattner371ed4e2008-04-06 06:57:35 +00002974 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002975 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002976 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002977 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002978 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002979 /*arglist*/ 0, 0,
2980 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002981 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002982 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002983 Exceptions.data(),
2984 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002985 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002986 LParenLoc, RParenLoc, D),
2987 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002988 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002989 }
2990
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002991 // Alternatively, this parameter list may be an identifier list form for a
2992 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00002993 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2994 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00002995 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002996 // K&R identifier lists can't have typedefs as identifiers, per
2997 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002998 if (RequiresArg) {
2999 Diag(Tok, diag::err_argument_required_after_attribute);
3000 delete AttrList;
3001 }
Chris Lattner9453ab82010-05-14 17:23:36 +00003002
Steve Naroffb0486722009-01-28 19:16:40 +00003003 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003004 // normal declarators, not for abstract-declarators. Get the first
3005 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003006 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003007 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003008
3009 // Identifier lists follow a really simple grammar: the identifiers can
3010 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3011 // identifier lists are really rare in the brave new modern world, and it
3012 // is very common for someone to typo a type in a non-k&r style list. If
3013 // we are presented with something like: "void foo(intptr x, float y)",
3014 // we don't want to start parsing the function declarator as though it is
3015 // a K&R style declarator just because intptr is an invalid type.
3016 //
3017 // To handle this, we check to see if the token after the first identifier
3018 // is a "," or ")". Only if so, do we parse it as an identifier list.
3019 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3020 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3021 FirstTok.getIdentifierInfo(),
3022 FirstTok.getLocation(), D);
3023
3024 // If we get here, the code is invalid. Push the first identifier back
3025 // into the token stream and parse the first argument as an (invalid)
3026 // normal argument declarator.
3027 PP.EnterToken(Tok);
3028 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003029 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003030 }
Mike Stump11289f42009-09-09 15:08:12 +00003031
Chris Lattner371ed4e2008-04-06 06:57:35 +00003032 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003033
Chris Lattner371ed4e2008-04-06 06:57:35 +00003034 // Build up an array of information about the parsed arguments.
3035 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003036
3037 // Enter function-declaration scope, limiting any declarators to the
3038 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003039 ParseScope PrototypeScope(this,
3040 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003041
Chris Lattner371ed4e2008-04-06 06:57:35 +00003042 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003043 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003044 while (1) {
3045 if (Tok.is(tok::ellipsis)) {
3046 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003047 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003048 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003049 }
Mike Stump11289f42009-09-09 15:08:12 +00003050
Chris Lattner371ed4e2008-04-06 06:57:35 +00003051 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003052
Chris Lattner371ed4e2008-04-06 06:57:35 +00003053 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003054 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003055 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003056
3057 // If the caller parsed attributes for the first argument, add them now.
3058 if (AttrList) {
3059 DS.AddAttributes(AttrList);
3060 AttrList = 0; // Only apply the attributes to the first parameter.
3061 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003062 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003063
Chris Lattner371ed4e2008-04-06 06:57:35 +00003064 // Parse the declarator. This is "PrototypeContext", because we must
3065 // accept either 'declarator' or 'abstract-declarator' here.
3066 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3067 ParseDeclarator(ParmDecl);
3068
3069 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003070 if (Tok.is(tok::kw___attribute)) {
3071 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003072 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003073 ParmDecl.AddAttributes(AttrList, Loc);
3074 }
Mike Stump11289f42009-09-09 15:08:12 +00003075
Chris Lattner371ed4e2008-04-06 06:57:35 +00003076 // Remember this parsed parameter in ParamInfo.
3077 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003078
Douglas Gregor4d87df52008-12-16 21:30:33 +00003079 // DefArgToks is used when the parsing of default arguments needs
3080 // to be delayed.
3081 CachedTokens *DefArgToks = 0;
3082
Chris Lattner371ed4e2008-04-06 06:57:35 +00003083 // If no parameter was specified, verify that *something* was specified,
3084 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003085 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3086 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003087 // Completely missing, emit error.
3088 Diag(DSStart, diag::err_missing_param);
3089 } else {
3090 // Otherwise, we have something. Add it and let semantic analysis try
3091 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003092
Chris Lattner371ed4e2008-04-06 06:57:35 +00003093 // Inform the actions module about the parameter declarator, so it gets
3094 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003095 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003096
3097 // Parse the default argument, if any. We parse the default
3098 // arguments in all dialects; the semantic analysis in
3099 // ActOnParamDefaultArgument will reject the default argument in
3100 // C.
3101 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003102 SourceLocation EqualLoc = Tok.getLocation();
3103
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003104 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003105 if (D.getContext() == Declarator::MemberContext) {
3106 // If we're inside a class definition, cache the tokens
3107 // corresponding to the default argument. We'll actually parse
3108 // them when we see the end of the class definition.
3109 // FIXME: Templates will require something similar.
3110 // FIXME: Can we use a smart pointer for Toks?
3111 DefArgToks = new CachedTokens;
3112
Mike Stump11289f42009-09-09 15:08:12 +00003113 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003114 /*StopAtSemi=*/true,
3115 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003116 delete DefArgToks;
3117 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003118 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003119 } else {
3120 // Mark the end of the default argument so that we know when to
3121 // stop when we parse it later on.
3122 Token DefArgEnd;
3123 DefArgEnd.startToken();
3124 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3125 DefArgEnd.setLocation(Tok.getLocation());
3126 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003127 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003128 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003129 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003130 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003131 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003132 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003133
John McCalldadc5752010-08-24 06:29:42 +00003134 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003135 if (DefArgResult.isInvalid()) {
3136 Actions.ActOnParamDefaultArgumentError(Param);
3137 SkipUntil(tok::comma, tok::r_paren, true, true);
3138 } else {
3139 // Inform the actions module about the default argument
3140 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003141 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003142 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003143 }
3144 }
Mike Stump11289f42009-09-09 15:08:12 +00003145
3146 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3147 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003148 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003149 }
3150
3151 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003152 if (Tok.isNot(tok::comma)) {
3153 if (Tok.is(tok::ellipsis)) {
3154 IsVariadic = true;
3155 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3156
3157 if (!getLang().CPlusPlus) {
3158 // We have ellipsis without a preceding ',', which is ill-formed
3159 // in C. Complain and provide the fix.
3160 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003161 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003162 }
3163 }
3164
3165 break;
3166 }
Mike Stump11289f42009-09-09 15:08:12 +00003167
Chris Lattner371ed4e2008-04-06 06:57:35 +00003168 // Consume the comma.
3169 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003170 }
Mike Stump11289f42009-09-09 15:08:12 +00003171
Chris Lattner371ed4e2008-04-06 06:57:35 +00003172 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003173 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003174
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003175 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003176 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3177 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003178
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003179 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003180 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003181 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003182 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003183 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003184 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003185
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003186 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003187 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003188 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003189 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003190 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003191
3192 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003193 if (Tok.is(tok::kw_throw)) {
3194 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003195 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003196 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003197 hasAnyExceptionSpec);
3198 assert(Exceptions.size() == ExceptionRanges.size() &&
3199 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003200 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003201 }
3202
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003203 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003204 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003205 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003206 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003207 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003208 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003209 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003210 Exceptions.data(),
3211 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003212 Exceptions.size(),
3213 LParenLoc, RParenLoc, D),
3214 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003215}
Chris Lattneracd58a32006-08-06 17:24:14 +00003216
Chris Lattner6c940e62008-04-06 06:34:08 +00003217/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3218/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003219/// first identifier has already been consumed, and the current token is the
3220/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003221///
3222/// identifier-list: [C99 6.7.5]
3223/// identifier
3224/// identifier-list ',' identifier
3225///
3226void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003227 IdentifierInfo *FirstIdent,
3228 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003229 Declarator &D) {
3230 // Build up an array of information about the parsed arguments.
3231 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3232 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003233
Chris Lattner6c940e62008-04-06 06:34:08 +00003234 // If there was no identifier specified for the declarator, either we are in
3235 // an abstract-declarator, or we are in a parameter declarator which was found
3236 // to be abstract. In abstract-declarators, identifier lists are not valid:
3237 // diagnose this.
3238 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003239 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003240
Chris Lattner9453ab82010-05-14 17:23:36 +00003241 // The first identifier was already read, and is known to be the first
3242 // identifier in the list. Remember this identifier in ParamInfo.
3243 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003244 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003245
Chris Lattner6c940e62008-04-06 06:34:08 +00003246 while (Tok.is(tok::comma)) {
3247 // Eat the comma.
3248 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003249
Chris Lattner9186f552008-04-06 06:39:19 +00003250 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003251 if (Tok.isNot(tok::identifier)) {
3252 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003253 SkipUntil(tok::r_paren);
3254 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003255 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003256
Chris Lattner6c940e62008-04-06 06:34:08 +00003257 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003258
3259 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003260 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003261 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003262
Chris Lattner6c940e62008-04-06 06:34:08 +00003263 // Verify that the argument identifier has not already been mentioned.
3264 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003265 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003266 } else {
3267 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003268 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003269 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003270 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003271 }
Mike Stump11289f42009-09-09 15:08:12 +00003272
Chris Lattner6c940e62008-04-06 06:34:08 +00003273 // Eat the identifier.
3274 ConsumeToken();
3275 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003276
3277 // If we have the closing ')', eat it and we're done.
3278 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3279
Chris Lattner9186f552008-04-06 06:39:19 +00003280 // Remember that we parsed a function type, and remember the attributes. This
3281 // function type is always a K&R style function type, which is not varargs and
3282 // has no prototype.
3283 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003284 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003285 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003286 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003287 /*exception*/false,
3288 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003289 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003290 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003291}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003292
Chris Lattnere8074e62006-08-06 18:30:15 +00003293/// [C90] direct-declarator '[' constant-expression[opt] ']'
3294/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3295/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3296/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3297/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3298void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003299 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003300
Chris Lattner84a11622008-12-18 07:27:21 +00003301 // C array syntax has many features, but by-far the most common is [] and [4].
3302 // This code does a fast path to handle some of the most obvious cases.
3303 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003304 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003305 //FIXME: Use these
3306 CXX0XAttributeList Attr;
3307 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3308 Attr = ParseCXX0XAttributes();
3309 }
3310
Chris Lattner84a11622008-12-18 07:27:21 +00003311 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003312 ExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003313 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3314 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003315 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003316 return;
3317 } else if (Tok.getKind() == tok::numeric_constant &&
3318 GetLookAheadToken(1).is(tok::r_square)) {
3319 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003320 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003321 ConsumeToken();
3322
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003323 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003324 //FIXME: Use these
3325 CXX0XAttributeList Attr;
3326 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3327 Attr = ParseCXX0XAttributes();
3328 }
Chris Lattner84a11622008-12-18 07:27:21 +00003329
3330 // If there was an error parsing the assignment-expression, recover.
3331 if (ExprRes.isInvalid())
3332 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003333
Chris Lattner84a11622008-12-18 07:27:21 +00003334 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003335 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3336 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003337 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003338 return;
3339 }
Mike Stump11289f42009-09-09 15:08:12 +00003340
Chris Lattnere8074e62006-08-06 18:30:15 +00003341 // If valid, this location is the position where we read the 'static' keyword.
3342 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003343 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003344 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003345
Chris Lattnere8074e62006-08-06 18:30:15 +00003346 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003347 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003348 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003349 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003350
Chris Lattnere8074e62006-08-06 18:30:15 +00003351 // If we haven't already read 'static', check to see if there is one after the
3352 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003353 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003354 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003355
Chris Lattnere8074e62006-08-06 18:30:15 +00003356 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003357 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003358 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003359
Chris Lattner521ff2b2008-04-06 05:26:30 +00003360 // Handle the case where we have '[*]' as the array size. However, a leading
3361 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3362 // the the token after the star is a ']'. Since stars in arrays are
3363 // infrequent, use of lookahead is not costly here.
3364 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003365 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003366
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003367 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003368 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003369 StaticLoc = SourceLocation(); // Drop the static.
3370 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003371 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003372 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003373 // Note, in C89, this production uses the constant-expr production instead
3374 // of assignment-expr. The only difference is that assignment-expr allows
3375 // things like '=' and '*='. Sema rejects these in C89 mode because they
3376 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003377
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003378 // Parse the constant-expression or assignment-expression now (depending
3379 // on dialect).
3380 if (getLang().CPlusPlus)
3381 NumElements = ParseConstantExpression();
3382 else
3383 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003384 }
Mike Stump11289f42009-09-09 15:08:12 +00003385
Chris Lattner62591722006-08-12 18:40:58 +00003386 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003387 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003388 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003389 // If the expression was invalid, skip it.
3390 SkipUntil(tok::r_square);
3391 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003392 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003393
3394 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3395
Alexis Hunt96d5c762009-11-21 08:43:09 +00003396 //FIXME: Use these
3397 CXX0XAttributeList Attr;
3398 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3399 Attr = ParseCXX0XAttributes();
3400 }
3401
Chris Lattner84a11622008-12-18 07:27:21 +00003402 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003403 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3404 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003405 NumElements.release(),
3406 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003407 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003408}
3409
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003410/// [GNU] typeof-specifier:
3411/// typeof ( expressions )
3412/// typeof ( type-name )
3413/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003414///
3415void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003416 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003417 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003418 SourceLocation StartLoc = ConsumeToken();
3419
John McCalle8595032010-01-13 20:03:27 +00003420 const bool hasParens = Tok.is(tok::l_paren);
3421
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003422 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003423 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003424 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003425 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003426 isCastExpr,
3427 CastTy,
3428 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003429 if (hasParens)
3430 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003431
3432 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003433 // FIXME: Not accurate, the range gets one token more than it should.
3434 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003435 else
3436 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003437
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003438 if (isCastExpr) {
3439 if (!CastTy) {
3440 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003441 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003442 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003443
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003444 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003445 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003446 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3447 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003448 DiagID, CastTy))
3449 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003450 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003451 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003452
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003453 // If we get here, the operand to the typeof was an expresion.
3454 if (Operand.isInvalid()) {
3455 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003456 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003457 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003458
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003459 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003460 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003461 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3462 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003463 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003464 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003465}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003466
3467
3468/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3469/// from TryAltiVecVectorToken.
3470bool Parser::TryAltiVecVectorTokenOutOfLine() {
3471 Token Next = NextToken();
3472 switch (Next.getKind()) {
3473 default: return false;
3474 case tok::kw_short:
3475 case tok::kw_long:
3476 case tok::kw_signed:
3477 case tok::kw_unsigned:
3478 case tok::kw_void:
3479 case tok::kw_char:
3480 case tok::kw_int:
3481 case tok::kw_float:
3482 case tok::kw_double:
3483 case tok::kw_bool:
3484 case tok::kw___pixel:
3485 Tok.setKind(tok::kw___vector);
3486 return true;
3487 case tok::identifier:
3488 if (Next.getIdentifierInfo() == Ident_pixel) {
3489 Tok.setKind(tok::kw___vector);
3490 return true;
3491 }
3492 return false;
3493 }
3494}
3495
3496bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3497 const char *&PrevSpec, unsigned &DiagID,
3498 bool &isInvalid) {
3499 if (Tok.getIdentifierInfo() == Ident_vector) {
3500 Token Next = NextToken();
3501 switch (Next.getKind()) {
3502 case tok::kw_short:
3503 case tok::kw_long:
3504 case tok::kw_signed:
3505 case tok::kw_unsigned:
3506 case tok::kw_void:
3507 case tok::kw_char:
3508 case tok::kw_int:
3509 case tok::kw_float:
3510 case tok::kw_double:
3511 case tok::kw_bool:
3512 case tok::kw___pixel:
3513 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3514 return true;
3515 case tok::identifier:
3516 if (Next.getIdentifierInfo() == Ident_pixel) {
3517 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3518 return true;
3519 }
3520 break;
3521 default:
3522 break;
3523 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003524 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003525 DS.isTypeAltiVecVector()) {
3526 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3527 return true;
3528 }
3529 return false;
3530}
3531