blob: 277d69f2cd73293d300faffb04ab05723d0d93ee [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"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000019#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
Chris Lattnerf5fbd792006-08-10 23:56:11 +000026/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redld6434562009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000034 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000035
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046}
47
Alexis Hunt96d5c762009-11-21 08:43:09 +000048/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000049///
50/// [GNU] attributes:
51/// attribute
52/// attributes attribute
53///
54/// [GNU] attribute:
55/// '__attribute__' '(' '(' attribute-list ')' ')'
56///
57/// [GNU] attribute-list:
58/// attrib
59/// attribute_list ',' attrib
60///
61/// [GNU] attrib:
62/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000063/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000067///
Steve Naroff0f2fe172007-06-01 17:11:19 +000068/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000073///
Steve Naroff0f2fe172007-06-01 17:11:19 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000075/// token lookahead. Comment from gcc: "If they start with an identifier
76/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000077/// start with that identifier; otherwise they are an expression list."
78///
79/// At the moment, I am not doing 2 token lookahead. I am also unaware of
80/// any attributes that don't work (based on my limited testing). Most
81/// attributes are very simple in practice. Until we find a bug, I don't see
82/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000083
Alexis Hunt96d5c762009-11-21 08:43:09 +000084AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000086
Steve Naroffb8371e12007-06-09 03:39:29 +000087 AttributeList *CurrAttr = 0;
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner76c72282007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000090 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
94 return CurrAttr;
95 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
98 return CurrAttr;
99 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000112
Steve Naroff0f2fe172007-06-01 17:11:19 +0000113 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattner76c72282007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000123 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000131
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 }
Chris Lattner76c72282007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner76c72282007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000147 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 }
154 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000159 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000161 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begemanf2758702009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
176 case tok::kw_typeof:
177 // If it's a builtin type name, eat it and expect a rparen
178 // __attribute__(( vec_type_hint(char) ))
179 ConsumeToken();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begemanf2758702009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000199 }
Chris Lattner76c72282007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000210 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000211 }
Nate Begemanf2758702009-06-26 06:32:41 +0000212 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000213 }
214 }
215 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000218 }
219 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000221 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
224 SkipUntil(tok::r_paren, false);
225 }
226 if (EndLoc)
227 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000228 }
229 return CurrAttr;
230}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000231
Eli Friedman06de2b52009-06-08 07:21:15 +0000232/// ParseMicrosoftDeclSpec - Parse an __declspec construct
233///
234/// [MS] decl-specifier:
235/// __declspec ( extended-decl-modifier-seq )
236///
237/// [MS] extended-decl-modifier-seq:
238/// extended-decl-modifier[opt]
239/// extended-decl-modifier extended-decl-modifier-seq
240
Eli Friedman53339e02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000243
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000245 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
246 "declspec")) {
247 SkipUntil(tok::r_paren, true); // skip until ) or ;
248 return CurrAttr;
249 }
Eli Friedman53339e02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000251 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
252 SourceLocation AttrNameLoc = ConsumeToken();
253 if (Tok.is(tok::l_paren)) {
254 ConsumeParen();
255 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
256 // correctly.
257 OwningExprResult ArgExpr(ParseAssignmentExpression());
258 if (!ArgExpr.isInvalid()) {
259 ExprTy* ExprList = ArgExpr.take();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman06de2b52009-06-08 07:21:15 +0000261 SourceLocation(), &ExprList, 1,
262 CurrAttr, true);
263 }
264 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
265 SkipUntil(tok::r_paren, false);
266 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman53339e02009-06-08 23:27:34 +0000273 return CurrAttr;
274}
275
276AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
277 // Treat these like attributes
278 // FIXME: Allow Sema to distinguish between these and real attributes!
279 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
280 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
281 Tok.is(tok::kw___w64)) {
282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman53339e02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000291}
292
Chris Lattner53361ac2006-08-10 05:19:57 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner49836b42009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000312 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000313 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000314 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000315 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000320 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000321 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000325 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000326 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000327 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000329 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000330 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000335 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000336 default:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList);
Chris Lattnera5235172007-08-25 06:57:03 +0000338 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000339
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000340 // This routine returns a DeclGroup, if the thing we parsed only contains a
341 // single decl, convert it now.
342 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000343}
344
345/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
346/// declaration-specifiers init-declarator-list[opt] ';'
347///[C90/C++]init-declarator-list ';' [TODO]
348/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000349///
350/// If RequireSemi is false, this does not check for a ';' at the end of the
351/// declaration.
352Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000353 SourceLocation &DeclEnd,
354 AttributeList *Attr) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000355 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000356 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000357 if (Attr)
358 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000359 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
360 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattner0e894622006-08-13 19:58:17 +0000362 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
363 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000364 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000365 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000366 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall28a6aea2009-11-04 02:18:39 +0000367 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000368 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
John McCalld5a36322009-11-03 19:26:08 +0000371 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
372 &DeclEnd);
373 return DG;
374}
Mike Stump11289f42009-09-09 15:08:12 +0000375
John McCalld5a36322009-11-03 19:26:08 +0000376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000385 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000386
John McCalld5a36322009-11-03 19:26:08 +0000387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000394 }
Mike Stump11289f42009-09-09 15:08:12 +0000395
John McCalld5a36322009-11-03 19:26:08 +0000396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000420 D.complete(FirstDecl);
John McCalld5a36322009-11-03 19:26:08 +0000421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000428 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000449 D.complete(ThisDecl);
John McCalld5a36322009-11-03 19:26:08 +0000450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000470}
471
Douglas Gregor23996282009-05-12 21:31:51 +0000472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000476///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000492///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregor23996282009-05-12 21:31:51 +0000504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregor23996282009-05-12 21:31:51 +0000508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000512 D.AddAttributes(AttrList, Loc);
513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregor23996282009-05-12 21:31:51 +0000515 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Douglas Gregor23996282009-05-12 21:31:51 +0000547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000554 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
555 EnterScope(0);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000556 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000557 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000558
Douglas Gregor23996282009-05-12 21:31:51 +0000559 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000560
John McCall1f4ee7b2009-12-19 09:28:58 +0000561 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000562 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000563 ExitScope();
564 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000565
Douglas Gregor23996282009-05-12 21:31:51 +0000566 if (Init.isInvalid()) {
567 SkipUntil(tok::semi, true, true);
568 return DeclPtrTy();
569 }
Anders Carlsson250aada2009-08-16 05:13:48 +0000570 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000571 }
572 } else if (Tok.is(tok::l_paren)) {
573 // Parse C++ direct initializer: '(' expression-list ')'
574 SourceLocation LParenLoc = ConsumeParen();
575 ExprVector Exprs(Actions);
576 CommaLocsTy CommaLocs;
577
Douglas Gregor613bf102009-12-22 17:47:17 +0000578 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
579 EnterScope(0);
580 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
581 }
582
Douglas Gregor23996282009-05-12 21:31:51 +0000583 if (ParseExpressionList(Exprs, CommaLocs)) {
584 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000585
586 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
587 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
588 ExitScope();
589 }
Douglas Gregor23996282009-05-12 21:31:51 +0000590 } else {
591 // Match the ')'.
592 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
593
594 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
595 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000596
597 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
598 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
599 ExitScope();
600 }
601
Douglas Gregor23996282009-05-12 21:31:51 +0000602 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
603 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000604 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000605 }
606 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000607 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000608 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
609 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000610 }
611
612 return ThisDecl;
613}
614
Chris Lattner1890ac82006-08-13 01:16:23 +0000615/// ParseSpecifierQualifierList
616/// specifier-qualifier-list:
617/// type-specifier specifier-qualifier-list[opt]
618/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000619/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000620///
621void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
622 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
623 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000624 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattner1890ac82006-08-13 01:16:23 +0000626 // Validate declspec for type-name.
627 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000628 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
629 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000630 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattner1b22eed2006-11-28 05:12:07 +0000632 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000633 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000634 if (DS.getStorageClassSpecLoc().isValid())
635 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
636 else
637 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000638 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner1b22eed2006-11-28 05:12:07 +0000641 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000642 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000643 if (DS.isInlineSpecified())
644 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
645 if (DS.isVirtualSpecified())
646 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
647 if (DS.isExplicitSpecified())
648 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000649 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000650 }
651}
Chris Lattner53361ac2006-08-10 05:19:57 +0000652
Chris Lattner6cc055a2009-04-12 20:42:31 +0000653/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
654/// specified token is valid after the identifier in a declarator which
655/// immediately follows the declspec. For example, these things are valid:
656///
657/// int x [ 4]; // direct-declarator
658/// int x ( int y); // direct-declarator
659/// int(int x ) // direct-declarator
660/// int x ; // simple-declaration
661/// int x = 17; // init-declarator-list
662/// int x , y; // init-declarator-list
663/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000664/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000665/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000666///
667/// This is not, because 'x' does not immediately follow the declspec (though
668/// ')' happens to be valid anyway).
669/// int (x)
670///
671static bool isValidAfterIdentifierInDeclarator(const Token &T) {
672 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
673 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000674 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000675}
676
Chris Lattner20a0c612009-04-14 21:34:55 +0000677
678/// ParseImplicitInt - This method is called when we have an non-typename
679/// identifier in a declspec (which normally terminates the decl spec) when
680/// the declspec has no type specifier. In this case, the declspec is either
681/// malformed or is "implicit int" (in K&R and C89).
682///
683/// This method handles diagnosing this prettily and returns false if the
684/// declspec is done being processed. If it recovers and thinks there may be
685/// other pieces of declspec after it, it returns true.
686///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000687bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000688 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000689 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000690 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner20a0c612009-04-14 21:34:55 +0000692 SourceLocation Loc = Tok.getLocation();
693 // If we see an identifier that is not a type name, we normally would
694 // parse it as the identifer being declared. However, when a typename
695 // is typo'd or the definition is not included, this will incorrectly
696 // parse the typename as the identifier name and fall over misparsing
697 // later parts of the diagnostic.
698 //
699 // As such, we try to do some look-ahead in cases where this would
700 // otherwise be an "implicit-int" case to see if this is invalid. For
701 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
702 // an identifier with implicit int, we'd get a parse error because the
703 // next token is obviously invalid for a type. Parse these as a case
704 // with an invalid type specifier.
705 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattner20a0c612009-04-14 21:34:55 +0000707 // Since we know that this either implicit int (which is rare) or an
708 // error, we'd do lookahead to try to do better recovery.
709 if (isValidAfterIdentifierInDeclarator(NextToken())) {
710 // If this token is valid for implicit int, e.g. "static x = 4", then
711 // we just avoid eating the identifier, so it will be parsed as the
712 // identifier in the declarator.
713 return false;
714 }
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner20a0c612009-04-14 21:34:55 +0000716 // Otherwise, if we don't consume this token, we are going to emit an
717 // error anyway. Try to recover from various common problems. Check
718 // to see if this was a reference to a tag name without a tag specified.
719 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000720 //
721 // C++ doesn't need this, and isTagName doesn't take SS.
722 if (SS == 0) {
723 const char *TagName = 0;
724 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000725
Chris Lattner20a0c612009-04-14 21:34:55 +0000726 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
727 default: break;
728 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
729 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
730 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
731 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000734 if (TagName) {
735 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000736 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000737 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000738
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000739 // Parse this as a tag as if the missing tag were present.
740 if (TagKind == tok::kw_enum)
741 ParseEnumSpecifier(Loc, DS, AS);
742 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000743 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000744 return true;
745 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000746 }
Mike Stump11289f42009-09-09 15:08:12 +0000747
Douglas Gregor15e56022009-10-13 23:27:22 +0000748 // This is almost certainly an invalid type name. Let the action emit a
749 // diagnostic and attempt to recover.
750 Action::TypeTy *T = 0;
751 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
752 CurScope, SS, T)) {
753 // The action emitted a diagnostic, so we don't have to.
754 if (T) {
755 // The action has suggested that the type T could be used. Set that as
756 // the type in the declaration specifiers, consume the would-be type
757 // name token, and we're done.
758 const char *PrevSpec;
759 unsigned DiagID;
760 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
761 false);
762 DS.SetRangeEnd(Tok.getLocation());
763 ConsumeToken();
764
765 // There may be other declaration specifiers after this.
766 return true;
767 }
768
769 // Fall through; the action had no suggestion for us.
770 } else {
771 // The action did not emit a diagnostic, so emit one now.
772 SourceRange R;
773 if (SS) R = SS->getRange();
774 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Douglas Gregor15e56022009-10-13 23:27:22 +0000777 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000778 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000779 unsigned DiagID;
780 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000781 DS.SetRangeEnd(Tok.getLocation());
782 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattner20a0c612009-04-14 21:34:55 +0000784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
785 // avoid rippling error messages on subsequent uses of the same type,
786 // could be useful if #include was forgotten.
787 return false;
788}
789
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000790/// \brief Determine the declaration specifier context from the declarator
791/// context.
792///
793/// \param Context the declarator context, which is one of the
794/// Declarator::TheContext enumerator values.
795Parser::DeclSpecContext
796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
797 if (Context == Declarator::MemberContext)
798 return DSC_class;
799 if (Context == Declarator::FileContext)
800 return DSC_top_level;
801 return DSC_normal;
802}
803
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000804/// ParseDeclarationSpecifiers
805/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000806/// storage-class-specifier declaration-specifiers[opt]
807/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000808/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000809/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000810///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000811/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000812/// 'typedef'
813/// 'extern'
814/// 'static'
815/// 'auto'
816/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000817/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000818/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000819/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000820/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000821/// [C++] 'virtual'
822/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000823/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000824/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000825
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000826///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000827void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000828 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000829 AccessSpecifier AS,
830 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000831 if (Tok.is(tok::code_completion)) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000832 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
833 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
834 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
835 : Action::CCC_Template;
836 else if (DSContext == DSC_class)
837 CCC = Action::CCC_Class;
Douglas Gregorf1934162010-01-13 21:24:21 +0000838 else if (ObjCImpDecl)
839 CCC = Action::CCC_ObjCImplementation;
840
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000841 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000842 ConsumeToken();
843 }
844
Chris Lattner2e232092008-03-13 06:29:04 +0000845 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000846 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000847 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000848 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000849 unsigned DiagID = 0;
850
Chris Lattner4d8f8732006-11-28 05:05:08 +0000851 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000852
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000853 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000854 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000855 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000856 // If this is not a declaration specifier token, we're done reading decl
857 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000858 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000859 return;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000861 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000862 // C++ scope specifier. Annotate and loop, or bail out on error.
863 if (TryAnnotateCXXScopeToken(true)) {
864 if (!DS.hasTypeSpecifier())
865 DS.SetTypeSpecError();
866 goto DoneWithDeclSpec;
867 }
868 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000869
870 case tok::annot_cxxscope: {
871 if (DS.hasTypeSpecifier())
872 goto DoneWithDeclSpec;
873
John McCall9dab4e62009-12-12 11:40:51 +0000874 CXXScopeSpec SS;
875 SS.setScopeRep(Tok.getAnnotationValue());
876 SS.setRange(Tok.getAnnotationRange());
877
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000878 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000879 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000880 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000881 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000882 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000883 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000884
885 // C++ [class.qual]p2:
886 // In a lookup in which the constructor is an acceptable lookup
887 // result and the nested-name-specifier nominates a class C:
888 //
889 // - if the name specified after the
890 // nested-name-specifier, when looked up in C, is the
891 // injected-class-name of C (Clause 9), or
892 //
893 // - if the name specified after the nested-name-specifier
894 // is the same as the identifier or the
895 // simple-template-id's template-name in the last
896 // component of the nested-name-specifier,
897 //
898 // the name is instead considered to name the constructor of
899 // class C.
900 //
901 // Thus, if the template-name is actually the constructor
902 // name, then the code is ill-formed; this interpretation is
903 // reinforced by the NAD status of core issue 635.
904 TemplateIdAnnotation *TemplateId
905 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
906 if (DSContext == DSC_top_level && TemplateId->Name &&
907 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
908 if (isConstructorDeclarator()) {
909 // The user meant this to be an out-of-line constructor
910 // definition, but template arguments are not allowed
911 // there. Just allow this as a constructor; we'll
912 // complain about it later.
913 goto DoneWithDeclSpec;
914 }
915
916 // The user meant this to name a type, but it actually names
917 // a constructor with some extraneous template
918 // arguments. Complain, then parse it as a type as the user
919 // intended.
920 Diag(TemplateId->TemplateNameLoc,
921 diag::err_out_of_line_template_id_names_constructor)
922 << TemplateId->Name;
923 }
924
John McCall9dab4e62009-12-12 11:40:51 +0000925 DS.getTypeSpecScope() = SS;
926 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000927 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000928 "ParseOptionalCXXScopeSpecifier not working");
929 AnnotateTemplateIdTokenAsType(&SS);
930 continue;
931 }
932
Douglas Gregorc5790df2009-09-28 07:26:33 +0000933 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000934 DS.getTypeSpecScope() = SS;
935 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000936 if (Tok.getAnnotationValue())
937 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
938 PrevSpec, DiagID,
939 Tok.getAnnotationValue());
940 else
941 DS.SetTypeSpecError();
942 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
943 ConsumeToken(); // The typename
944 }
945
Douglas Gregor167fa622009-03-25 15:40:00 +0000946 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000947 goto DoneWithDeclSpec;
948
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000949 // If we're in a context where the identifier could be a class name,
950 // check whether this is a constructor declaration.
951 if (DSContext == DSC_top_level &&
952 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
953 &SS)) {
954 if (isConstructorDeclarator())
955 goto DoneWithDeclSpec;
956
957 // As noted in C++ [class.qual]p2 (cited above), when the name
958 // of the class is qualified in a context where it could name
959 // a constructor, its a constructor name. However, we've
960 // looked at the declarator, and the user probably meant this
961 // to be a type. Complain that it isn't supposed to be treated
962 // as a type, then proceed to parse it as a type.
963 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
964 << Next.getIdentifierInfo();
965 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000966
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000967 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
968 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000969
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000970 // If the referenced identifier is not a type, then this declspec is
971 // erroneous: We already checked about that it has no type specifier, and
972 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000973 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000974 if (TypeRep == 0) {
975 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000976 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000977 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000978 }
Mike Stump11289f42009-09-09 15:08:12 +0000979
John McCall9dab4e62009-12-12 11:40:51 +0000980 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000981 ConsumeToken(); // The C++ scope.
982
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000984 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000985 if (isInvalid)
986 break;
Mike Stump11289f42009-09-09 15:08:12 +0000987
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000988 DS.SetRangeEnd(Tok.getLocation());
989 ConsumeToken(); // The typename.
990
991 continue;
992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Chris Lattnere387d9e2009-01-21 19:48:37 +0000994 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000995 if (Tok.getAnnotationValue())
996 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000997 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000998 else
999 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +00001000 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1001 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001002
Chris Lattnere387d9e2009-01-21 19:48:37 +00001003 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1004 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1005 // Objective-C interface. If we don't have Objective-C or a '<', this is
1006 // just a normal reference to a typedef name.
1007 if (!Tok.is(tok::less) || !getLang().ObjC1)
1008 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001009
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001010 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001011 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001012 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1013 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1014 LAngleLoc, EndProtoLoc);
1015 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1016 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001017
Chris Lattnere387d9e2009-01-21 19:48:37 +00001018 DS.SetRangeEnd(EndProtoLoc);
1019 continue;
1020 }
Mike Stump11289f42009-09-09 15:08:12 +00001021
Chris Lattner16fac4f2008-07-26 01:18:38 +00001022 // typedef-name
1023 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001024 // In C++, check to see if this is a scope specifier like foo::bar::, if
1025 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001026 if (getLang().CPlusPlus) {
1027 if (TryAnnotateCXXScopeToken(true)) {
1028 if (!DS.hasTypeSpecifier())
1029 DS.SetTypeSpecError();
1030 goto DoneWithDeclSpec;
1031 }
1032 if (!Tok.is(tok::identifier))
1033 continue;
1034 }
Mike Stump11289f42009-09-09 15:08:12 +00001035
Chris Lattner16fac4f2008-07-26 01:18:38 +00001036 // This identifier can only be a typedef name if we haven't already seen
1037 // a type-specifier. Without this check we misparse:
1038 // typedef int X; struct Y { short X; }; as 'short int'.
1039 if (DS.hasTypeSpecifier())
1040 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001041
John Thompson22334602010-02-05 00:12:22 +00001042 // Check for need to substitute AltiVec keyword tokens.
1043 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1044 break;
1045
Chris Lattner16fac4f2008-07-26 01:18:38 +00001046 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +00001047 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00001048 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001049
Chris Lattner6cc055a2009-04-12 20:42:31 +00001050 // If this is not a typedef name, don't parse it as part of the declspec,
1051 // it must be an implicit int or an error.
1052 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001053 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001054 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001055 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001056
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001057 // If we're in a context where the identifier could be a class name,
1058 // check whether this is a constructor declaration.
1059 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump11289f42009-09-09 15:08:12 +00001060 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001061 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001062 goto DoneWithDeclSpec;
1063
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001064 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001065 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001066 if (isInvalid)
1067 break;
Mike Stump11289f42009-09-09 15:08:12 +00001068
Chris Lattner16fac4f2008-07-26 01:18:38 +00001069 DS.SetRangeEnd(Tok.getLocation());
1070 ConsumeToken(); // The identifier
1071
1072 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1073 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1074 // Objective-C interface. If we don't have Objective-C or a '<', this is
1075 // just a normal reference to a typedef name.
1076 if (!Tok.is(tok::less) || !getLang().ObjC1)
1077 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001078
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001079 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001080 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001081 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1082 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1083 LAngleLoc, EndProtoLoc);
1084 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1085 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattner16fac4f2008-07-26 01:18:38 +00001087 DS.SetRangeEnd(EndProtoLoc);
1088
Steve Naroffcd5e7822008-09-22 10:28:57 +00001089 // Need to support trailing type qualifiers (e.g. "id<p> const").
1090 // If a type specifier follows, it will be diagnosed elsewhere.
1091 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001092 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001093
1094 // type-name
1095 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001096 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001097 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001098 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001099 // This template-id does not refer to a type name, so we're
1100 // done with the type-specifiers.
1101 goto DoneWithDeclSpec;
1102 }
1103
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001104 // If we're in a context where the template-id could be a
1105 // constructor name or specialization, check whether this is a
1106 // constructor declaration.
1107 if (getLang().CPlusPlus && DSContext == DSC_class &&
1108 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1109 isConstructorDeclarator())
1110 goto DoneWithDeclSpec;
1111
Douglas Gregor7f741122009-02-25 19:37:18 +00001112 // Turn the template-id annotation token into a type annotation
1113 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001114 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001115 continue;
1116 }
1117
Chris Lattnere37e2332006-08-15 04:50:22 +00001118 // GNU attributes support.
1119 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001120 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001121 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001122
1123 // Microsoft declspec support.
1124 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001125 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001126 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001127
Steve Naroff44ac7772008-12-25 14:16:32 +00001128 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001129 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001130 // FIXME: Add handling here!
1131 break;
1132
1133 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001134 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001135 case tok::kw___cdecl:
1136 case tok::kw___stdcall:
1137 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001138 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1139 continue;
1140
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001141 // storage-class-specifier
1142 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001143 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1144 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001145 break;
1146 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001147 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001148 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001149 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1150 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001151 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001152 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001153 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001154 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001155 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001156 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001157 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001158 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001159 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1160 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001161 break;
1162 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001163 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1165 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001166 else
John McCall49bfce42009-08-03 20:12:06 +00001167 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1168 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001169 break;
1170 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001171 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1172 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001173 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001174 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001175 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1176 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001177 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001178 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001179 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001180 break;
Mike Stump11289f42009-09-09 15:08:12 +00001181
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001182 // function-specifier
1183 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001184 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001185 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001186 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001187 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001188 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001189 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001190 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001191 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001192
Anders Carlssoncd8db412009-05-06 04:46:28 +00001193 // friend
1194 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001195 if (DSContext == DSC_class)
1196 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1197 else {
1198 PrevSpec = ""; // not actually used by the diagnostic
1199 DiagID = diag::err_friend_invalid_in_context;
1200 isInvalid = true;
1201 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001202 break;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001204 // constexpr
1205 case tok::kw_constexpr:
1206 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1207 break;
1208
Chris Lattnere387d9e2009-01-21 19:48:37 +00001209 // type-specifier
1210 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001211 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1212 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001213 break;
1214 case tok::kw_long:
1215 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001216 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1217 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001218 else
John McCall49bfce42009-08-03 20:12:06 +00001219 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1220 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001221 break;
1222 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001223 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1224 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001225 break;
1226 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001227 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1228 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001229 break;
1230 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001231 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1232 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001233 break;
1234 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001235 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1236 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001237 break;
1238 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001239 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1240 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001241 break;
1242 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001243 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1244 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001245 break;
1246 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001247 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1248 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001249 break;
1250 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001251 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1252 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001253 break;
1254 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001255 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1256 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001257 break;
1258 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1260 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001261 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001262 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1264 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001265 break;
1266 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001267 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1268 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001269 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001270 case tok::kw_bool:
1271 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1273 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001274 break;
1275 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1277 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001278 break;
1279 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1281 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001282 break;
1283 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1285 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001286 break;
John Thompson22334602010-02-05 00:12:22 +00001287 case tok::kw___vector:
1288 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1289 break;
1290 case tok::kw___pixel:
1291 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1292 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001293
1294 // class-specifier:
1295 case tok::kw_class:
1296 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001297 case tok::kw_union: {
1298 tok::TokenKind Kind = Tok.getKind();
1299 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001300 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001301 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001302 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303
1304 // enum-specifier:
1305 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001306 ConsumeToken();
1307 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001308 continue;
1309
1310 // cv-qualifier:
1311 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1313 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001314 break;
1315 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1317 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001318 break;
1319 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1321 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001322 break;
1323
Douglas Gregor333489b2009-03-27 23:10:48 +00001324 // C++ typename-specifier:
1325 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001326 if (TryAnnotateTypeOrScopeToken()) {
1327 DS.SetTypeSpecError();
1328 goto DoneWithDeclSpec;
1329 }
1330 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001331 continue;
1332 break;
1333
Chris Lattnere387d9e2009-01-21 19:48:37 +00001334 // GNU typeof support.
1335 case tok::kw_typeof:
1336 ParseTypeofSpecifier(DS);
1337 continue;
1338
Anders Carlsson74948d02009-06-24 17:47:40 +00001339 case tok::kw_decltype:
1340 ParseDecltypeSpecifier(DS);
1341 continue;
1342
Steve Naroffcfdf6162008-06-05 00:02:44 +00001343 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001344 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001345 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1346 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001347 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001348 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001349
Chris Lattner0974b232008-07-26 00:20:22 +00001350 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001351 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001352 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001353 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1354 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1355 LAngleLoc, EndProtoLoc);
1356 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1357 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001358 DS.SetRangeEnd(EndProtoLoc);
1359
Chris Lattner6d29c102008-11-18 07:48:38 +00001360 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001361 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001362 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001363 // Need to support trailing type qualifiers (e.g. "id<p> const").
1364 // If a type specifier follows, it will be diagnosed elsewhere.
1365 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001366 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001367 }
John McCall49bfce42009-08-03 20:12:06 +00001368 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001369 if (isInvalid) {
1370 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001371 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001372 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001373 }
Chris Lattner2e232092008-03-13 06:29:04 +00001374 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001375 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001376 }
1377}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001378
Chris Lattnera448d752009-01-06 06:59:53 +00001379/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001380/// primarily follow the C++ grammar with additions for C99 and GNU,
1381/// which together subsume the C grammar. Note that the C++
1382/// type-specifier also includes the C type-qualifier (for const,
1383/// volatile, and C99 restrict). Returns true if a type-specifier was
1384/// found (and parsed), false otherwise.
1385///
1386/// type-specifier: [C++ 7.1.5]
1387/// simple-type-specifier
1388/// class-specifier
1389/// enum-specifier
1390/// elaborated-type-specifier [TODO]
1391/// cv-qualifier
1392///
1393/// cv-qualifier: [C++ 7.1.5.1]
1394/// 'const'
1395/// 'volatile'
1396/// [C99] 'restrict'
1397///
1398/// simple-type-specifier: [ C++ 7.1.5.2]
1399/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1400/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1401/// 'char'
1402/// 'wchar_t'
1403/// 'bool'
1404/// 'short'
1405/// 'int'
1406/// 'long'
1407/// 'signed'
1408/// 'unsigned'
1409/// 'float'
1410/// 'double'
1411/// 'void'
1412/// [C99] '_Bool'
1413/// [C99] '_Complex'
1414/// [C99] '_Imaginary' // Removed in TC2?
1415/// [GNU] '_Decimal32'
1416/// [GNU] '_Decimal64'
1417/// [GNU] '_Decimal128'
1418/// [GNU] typeof-specifier
1419/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1420/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001421/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001422/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001423bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001424 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001425 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001426 const ParsedTemplateInfo &TemplateInfo,
1427 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001428 SourceLocation Loc = Tok.getLocation();
1429
1430 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001431 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00001432 // Check for need to substitute AltiVec keyword tokens.
1433 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1434 break;
1435 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001436 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001437 // Annotate typenames and C++ scope specifiers. If we get one, just
1438 // recurse to handle whatever we get.
1439 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001440 return true;
1441 if (Tok.is(tok::identifier))
1442 return false;
1443 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1444 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001445 case tok::coloncolon: // ::foo::bar
1446 if (NextToken().is(tok::kw_new) || // ::new
1447 NextToken().is(tok::kw_delete)) // ::delete
1448 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001449
Chris Lattner020bab92009-01-04 23:41:41 +00001450 // Annotate typenames and C++ scope specifiers. If we get one, just
1451 // recurse to handle whatever we get.
1452 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001453 return true;
1454 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1455 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001456
Douglas Gregor450c75a2008-11-07 15:42:26 +00001457 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001458 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001459 if (Tok.getAnnotationValue())
1460 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001461 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001462 else
1463 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001464 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1465 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001466
Douglas Gregor450c75a2008-11-07 15:42:26 +00001467 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1468 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1469 // Objective-C interface. If we don't have Objective-C or a '<', this is
1470 // just a normal reference to a typedef name.
1471 if (!Tok.is(tok::less) || !getLang().ObjC1)
1472 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001473
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001474 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001475 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001476 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1477 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1478 LAngleLoc, EndProtoLoc);
1479 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1480 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001481
Douglas Gregor450c75a2008-11-07 15:42:26 +00001482 DS.SetRangeEnd(EndProtoLoc);
1483 return true;
1484 }
1485
1486 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001487 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001488 break;
1489 case tok::kw_long:
1490 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001491 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1492 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001493 else
John McCall49bfce42009-08-03 20:12:06 +00001494 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1495 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001496 break;
1497 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001498 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001499 break;
1500 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001501 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1502 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001503 break;
1504 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001505 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1506 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001507 break;
1508 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001509 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1510 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001511 break;
1512 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001514 break;
1515 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001517 break;
1518 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001520 break;
1521 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001522 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001523 break;
1524 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001526 break;
1527 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001528 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001529 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001530 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001532 break;
1533 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001534 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001535 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001536 case tok::kw_bool:
1537 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001539 break;
1540 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001541 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1542 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001543 break;
1544 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1546 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001547 break;
1548 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1550 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001551 break;
John Thompson22334602010-02-05 00:12:22 +00001552 case tok::kw___vector:
1553 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1554 break;
1555 case tok::kw___pixel:
1556 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1557 break;
1558
Douglas Gregor450c75a2008-11-07 15:42:26 +00001559 // class-specifier:
1560 case tok::kw_class:
1561 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001562 case tok::kw_union: {
1563 tok::TokenKind Kind = Tok.getKind();
1564 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001565 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1566 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001567 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001568 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001569
1570 // enum-specifier:
1571 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001572 ConsumeToken();
1573 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001574 return true;
1575
1576 // cv-qualifier:
1577 case tok::kw_const:
1578 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001579 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001580 break;
1581 case tok::kw_volatile:
1582 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001583 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw_restrict:
1586 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001587 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001588 break;
1589
1590 // GNU typeof support.
1591 case tok::kw_typeof:
1592 ParseTypeofSpecifier(DS);
1593 return true;
1594
Anders Carlsson74948d02009-06-24 17:47:40 +00001595 // C++0x decltype support.
1596 case tok::kw_decltype:
1597 ParseDecltypeSpecifier(DS);
1598 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001599
Anders Carlssonbae27372009-06-26 23:44:14 +00001600 // C++0x auto support.
1601 case tok::kw_auto:
1602 if (!getLang().CPlusPlus0x)
1603 return false;
1604
John McCall49bfce42009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001606 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001607 case tok::kw___ptr64:
1608 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001609 case tok::kw___cdecl:
1610 case tok::kw___stdcall:
1611 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001612 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001613 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001614
Douglas Gregor450c75a2008-11-07 15:42:26 +00001615 default:
1616 // Not a type-specifier; do nothing.
1617 return false;
1618 }
1619
1620 // If the specifier combination wasn't legal, issue a diagnostic.
1621 if (isInvalid) {
1622 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001623 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001624 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001625 }
1626 DS.SetRangeEnd(Tok.getLocation());
1627 ConsumeToken(); // whatever we parsed above.
1628 return true;
1629}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001630
Chris Lattner70ae4912007-10-29 04:42:53 +00001631/// ParseStructDeclaration - Parse a struct declaration without the terminating
1632/// semicolon.
1633///
Chris Lattner90a26b02007-01-23 04:38:16 +00001634/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001635/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001636/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001637/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001638/// struct-declarator-list:
1639/// struct-declarator
1640/// struct-declarator-list ',' struct-declarator
1641/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1642/// struct-declarator:
1643/// declarator
1644/// [GNU] declarator attributes[opt]
1645/// declarator[opt] ':' constant-expression
1646/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1647///
Chris Lattnera12405b2008-04-10 06:46:29 +00001648void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001649ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001650 if (Tok.is(tok::kw___extension__)) {
1651 // __extension__ silences extension warnings in the subexpression.
1652 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001653 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001654 return ParseStructDeclaration(DS, Fields);
1655 }
Mike Stump11289f42009-09-09 15:08:12 +00001656
Steve Naroff97170802007-08-20 22:28:22 +00001657 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001658 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001659 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001660
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001661 // If there are no declarators, this is a free-standing declaration
1662 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001663 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001664 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001665 return;
1666 }
1667
1668 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001669 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001670 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001671 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001672 FieldDeclarator DeclaratorInfo(DS);
1673
1674 // Attributes are only allowed here on successive declarators.
1675 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1676 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001677 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001678 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1679 }
Mike Stump11289f42009-09-09 15:08:12 +00001680
Steve Naroff97170802007-08-20 22:28:22 +00001681 /// struct-declarator: declarator
1682 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001683 if (Tok.isNot(tok::colon)) {
1684 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1685 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001686 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001687 }
Mike Stump11289f42009-09-09 15:08:12 +00001688
Chris Lattner76c72282007-10-09 17:33:22 +00001689 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001690 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001691 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001692 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001693 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001694 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001695 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001696 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001697
Steve Naroff97170802007-08-20 22:28:22 +00001698 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001699 if (Tok.is(tok::kw___attribute)) {
1700 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001701 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001702 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1703 }
1704
John McCallcfefb6d2009-11-03 02:38:08 +00001705 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001706 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1707 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001708
Steve Naroff97170802007-08-20 22:28:22 +00001709 // If we don't have a comma, it is either the end of the list (a ';')
1710 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001711 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001712 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001713
Steve Naroff97170802007-08-20 22:28:22 +00001714 // Consume the comma.
1715 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001716
John McCallcfefb6d2009-11-03 02:38:08 +00001717 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001718 }
Steve Naroff97170802007-08-20 22:28:22 +00001719}
1720
1721/// ParseStructUnionBody
1722/// struct-contents:
1723/// struct-declaration-list
1724/// [EXT] empty
1725/// [GNU] "struct-declaration-list" without terminatoring ';'
1726/// struct-declaration-list:
1727/// struct-declaration
1728/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001729/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001730///
Chris Lattner1300fb92007-01-23 23:42:53 +00001731void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001732 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001733 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1734 PP.getSourceManager(),
1735 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001736
Chris Lattner90a26b02007-01-23 04:38:16 +00001737 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001738
Douglas Gregor658b9552009-01-09 22:42:13 +00001739 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001740 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1741
Chris Lattner7b9ace62007-01-23 20:11:08 +00001742 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1743 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001744 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001745 Diag(Tok, diag::ext_empty_struct_union_enum)
1746 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001747
Chris Lattner83f095c2009-03-28 19:18:32 +00001748 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001749
Chris Lattner7b9ace62007-01-23 20:11:08 +00001750 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001751 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001752 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattner736ed5d2007-06-09 05:59:07 +00001754 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001755 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001756 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001757 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001758 ConsumeToken();
1759 continue;
1760 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001761
1762 // Parse all the comma separated declarators.
1763 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001764
John McCallcfefb6d2009-11-03 02:38:08 +00001765 if (!Tok.is(tok::at)) {
1766 struct CFieldCallback : FieldCallback {
1767 Parser &P;
1768 DeclPtrTy TagDecl;
1769 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1770
1771 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1772 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1773 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1774
1775 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001776 // Install the declarator into the current TagDecl.
John McCall5e6253b2009-11-03 21:13:47 +00001777 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1778 FD.D.getDeclSpec().getSourceRange().getBegin(),
1779 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001780 FieldDecls.push_back(Field);
1781 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001782 }
John McCallcfefb6d2009-11-03 02:38:08 +00001783 } Callback(*this, TagDecl, FieldDecls);
1784
1785 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001786 } else { // Handle @defs
1787 ConsumeToken();
1788 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1789 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001790 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001791 continue;
1792 }
1793 ConsumeToken();
1794 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1795 if (!Tok.is(tok::identifier)) {
1796 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001797 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001798 continue;
1799 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001800 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001801 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001802 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001803 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1804 ConsumeToken();
1805 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001806 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001807
Chris Lattner76c72282007-10-09 17:33:22 +00001808 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001809 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001810 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001811 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001812 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001813 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001814 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1815 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001816 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001817 // If we stopped at a ';', eat it.
1818 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001819 }
1820 }
Mike Stump11289f42009-09-09 15:08:12 +00001821
Steve Naroff33a1e802007-10-29 21:38:07 +00001822 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001823
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001824 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001825 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001826 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001827 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001828
1829 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001830 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001831 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001832 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001833 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001834 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001835}
1836
1837
Chris Lattner3b561a32006-08-13 00:12:11 +00001838/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001839/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001840/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001841///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001842/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1843/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001844/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001845/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001846///
1847/// [C++] elaborated-type-specifier:
1848/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1849///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001850void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1851 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001852 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001853 if (Tok.is(tok::code_completion)) {
1854 // Code completion for an enum name.
1855 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1856 ConsumeToken();
1857 }
1858
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001859 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001860 // If attributes exist after tag, parse them.
1861 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001862 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001863
1864 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00001865 if (getLang().CPlusPlus) {
1866 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1867 return;
1868
1869 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001870 Diag(Tok, diag::err_expected_ident);
1871 if (Tok.isNot(tok::l_brace)) {
1872 // Has no name and is not a definition.
1873 // Skip the rest of this declarator, up until the comma or semicolon.
1874 SkipUntil(tok::comma, true);
1875 return;
1876 }
1877 }
1878 }
Mike Stump11289f42009-09-09 15:08:12 +00001879
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001880 // Must have either 'enum name' or 'enum {...}'.
1881 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1882 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001883
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001884 // Skip the rest of this declarator, up until the comma or semicolon.
1885 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001886 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001887 }
Mike Stump11289f42009-09-09 15:08:12 +00001888
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001889 // If an identifier is present, consume and remember it.
1890 IdentifierInfo *Name = 0;
1891 SourceLocation NameLoc;
1892 if (Tok.is(tok::identifier)) {
1893 Name = Tok.getIdentifierInfo();
1894 NameLoc = ConsumeToken();
1895 }
Mike Stump11289f42009-09-09 15:08:12 +00001896
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001897 // There are three options here. If we have 'enum foo;', then this is a
1898 // forward declaration. If we have 'enum foo {...' then this is a
1899 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1900 //
1901 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1902 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1903 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1904 //
John McCall9bb74a52009-07-31 02:45:11 +00001905 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001906 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001907 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001908 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001909 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001910 else
John McCall9bb74a52009-07-31 02:45:11 +00001911 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001912 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001913 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001914 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001915 StartLoc, SS, Name, NameLoc, Attr.get(),
1916 AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001917 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001918 Owned, IsDependent);
1919 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001920
Chris Lattner76c72282007-10-09 17:33:22 +00001921 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001922 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001923
Douglas Gregor72100632010-01-25 16:33:23 +00001924 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1925 // name (if there is one).
1926 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Chris Lattnerda72c822006-08-13 22:16:42 +00001927 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001928 unsigned DiagID;
Douglas Gregor72100632010-01-25 16:33:23 +00001929 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001930 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001931 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001932}
1933
Chris Lattnerc1915e22007-01-25 07:29:02 +00001934/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1935/// enumerator-list:
1936/// enumerator
1937/// enumerator-list ',' enumerator
1938/// enumerator:
1939/// enumeration-constant
1940/// enumeration-constant '=' constant-expression
1941/// enumeration-constant:
1942/// identifier
1943///
Chris Lattner83f095c2009-03-28 19:18:32 +00001944void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001945 // Enter the scope of the enum body and start the definition.
1946 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001947 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001948
Chris Lattnerc1915e22007-01-25 07:29:02 +00001949 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001950
Chris Lattner37256fb2007-08-27 17:24:30 +00001951 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001952 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001953 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001954
Chris Lattner83f095c2009-03-28 19:18:32 +00001955 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001956
Chris Lattner83f095c2009-03-28 19:18:32 +00001957 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001958
Chris Lattnerc1915e22007-01-25 07:29:02 +00001959 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001960 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001961 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1962 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001963
Chris Lattnerc1915e22007-01-25 07:29:02 +00001964 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001965 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001966 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001967 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001968 AssignedVal = ParseConstantExpression();
1969 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001970 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001971 }
Mike Stump11289f42009-09-09 15:08:12 +00001972
Chris Lattnerc1915e22007-01-25 07:29:02 +00001973 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001974 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1975 LastEnumConstDecl,
1976 IdentLoc, Ident,
1977 EqualLoc,
1978 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001979 EnumConstantDecls.push_back(EnumConstDecl);
1980 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001981
Chris Lattner76c72282007-10-09 17:33:22 +00001982 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001983 break;
1984 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001985
1986 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001987 !(getLang().C99 || getLang().CPlusPlus0x))
1988 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1989 << getLang().CPlusPlus
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001990 << CodeModificationHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001991 }
Mike Stump11289f42009-09-09 15:08:12 +00001992
Chris Lattnerc1915e22007-01-25 07:29:02 +00001993 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001994 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001995
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001996 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001997 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001998 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001999 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002000
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002001 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2002 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002003 CurScope, Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002004
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002005 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00002006 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002007}
Chris Lattner3b561a32006-08-13 00:12:11 +00002008
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002009/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002010/// start of a type-qualifier-list.
2011bool Parser::isTypeQualifier() const {
2012 switch (Tok.getKind()) {
2013 default: return false;
2014 // type-qualifier
2015 case tok::kw_const:
2016 case tok::kw_volatile:
2017 case tok::kw_restrict:
2018 return true;
2019 }
2020}
2021
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002022/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2023/// is definitely a type-specifier. Return false if it isn't part of a type
2024/// specifier or if we're not sure.
2025bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2026 switch (Tok.getKind()) {
2027 default: return false;
2028 // type-specifiers
2029 case tok::kw_short:
2030 case tok::kw_long:
2031 case tok::kw_signed:
2032 case tok::kw_unsigned:
2033 case tok::kw__Complex:
2034 case tok::kw__Imaginary:
2035 case tok::kw_void:
2036 case tok::kw_char:
2037 case tok::kw_wchar_t:
2038 case tok::kw_char16_t:
2039 case tok::kw_char32_t:
2040 case tok::kw_int:
2041 case tok::kw_float:
2042 case tok::kw_double:
2043 case tok::kw_bool:
2044 case tok::kw__Bool:
2045 case tok::kw__Decimal32:
2046 case tok::kw__Decimal64:
2047 case tok::kw__Decimal128:
2048 case tok::kw___vector:
2049
2050 // struct-or-union-specifier (C99) or class-specifier (C++)
2051 case tok::kw_class:
2052 case tok::kw_struct:
2053 case tok::kw_union:
2054 // enum-specifier
2055 case tok::kw_enum:
2056
2057 // typedef-name
2058 case tok::annot_typename:
2059 return true;
2060 }
2061}
2062
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002063/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002064/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002065bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002066 switch (Tok.getKind()) {
2067 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002068
Chris Lattner020bab92009-01-04 23:41:41 +00002069 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002070 if (TryAltiVecVectorToken())
2071 return true;
2072 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002073 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002074 // Annotate typenames and C++ scope specifiers. If we get one, just
2075 // recurse to handle whatever we get.
2076 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002077 return true;
2078 if (Tok.is(tok::identifier))
2079 return false;
2080 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002081
Chris Lattner020bab92009-01-04 23:41:41 +00002082 case tok::coloncolon: // ::foo::bar
2083 if (NextToken().is(tok::kw_new) || // ::new
2084 NextToken().is(tok::kw_delete)) // ::delete
2085 return false;
2086
Chris Lattner020bab92009-01-04 23:41:41 +00002087 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002088 return true;
2089 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002090
Chris Lattnere37e2332006-08-15 04:50:22 +00002091 // GNU attributes support.
2092 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002093 // GNU typeof support.
2094 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002095
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002096 // type-specifiers
2097 case tok::kw_short:
2098 case tok::kw_long:
2099 case tok::kw_signed:
2100 case tok::kw_unsigned:
2101 case tok::kw__Complex:
2102 case tok::kw__Imaginary:
2103 case tok::kw_void:
2104 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002105 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002106 case tok::kw_char16_t:
2107 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002108 case tok::kw_int:
2109 case tok::kw_float:
2110 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002111 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002112 case tok::kw__Bool:
2113 case tok::kw__Decimal32:
2114 case tok::kw__Decimal64:
2115 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002116 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002117
Chris Lattner861a2262008-04-13 18:59:07 +00002118 // struct-or-union-specifier (C99) or class-specifier (C++)
2119 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002120 case tok::kw_struct:
2121 case tok::kw_union:
2122 // enum-specifier
2123 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002124
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002125 // type-qualifier
2126 case tok::kw_const:
2127 case tok::kw_volatile:
2128 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002129
2130 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002131 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002132 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002133
Chris Lattner409bf7d2008-10-20 00:25:30 +00002134 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2135 case tok::less:
2136 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002137
Steve Naroff44ac7772008-12-25 14:16:32 +00002138 case tok::kw___cdecl:
2139 case tok::kw___stdcall:
2140 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002141 case tok::kw___w64:
2142 case tok::kw___ptr64:
2143 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002144 }
2145}
2146
Chris Lattneracd58a32006-08-06 17:24:14 +00002147/// isDeclarationSpecifier() - Return true if the current token is part of a
2148/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002149bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002150 switch (Tok.getKind()) {
2151 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002152
Chris Lattner020bab92009-01-04 23:41:41 +00002153 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002154 // Unfortunate hack to support "Class.factoryMethod" notation.
2155 if (getLang().ObjC1 && NextToken().is(tok::period))
2156 return false;
John Thompson22334602010-02-05 00:12:22 +00002157 if (TryAltiVecVectorToken())
2158 return true;
2159 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002160 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002161 // Annotate typenames and C++ scope specifiers. If we get one, just
2162 // recurse to handle whatever we get.
2163 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002164 return true;
2165 if (Tok.is(tok::identifier))
2166 return false;
2167 return isDeclarationSpecifier();
2168
Chris Lattner020bab92009-01-04 23:41:41 +00002169 case tok::coloncolon: // ::foo::bar
2170 if (NextToken().is(tok::kw_new) || // ::new
2171 NextToken().is(tok::kw_delete)) // ::delete
2172 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002173
Chris Lattner020bab92009-01-04 23:41:41 +00002174 // Annotate typenames and C++ scope specifiers. If we get one, just
2175 // recurse to handle whatever we get.
2176 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002177 return true;
2178 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002179
Chris Lattneracd58a32006-08-06 17:24:14 +00002180 // storage-class-specifier
2181 case tok::kw_typedef:
2182 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002183 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002184 case tok::kw_static:
2185 case tok::kw_auto:
2186 case tok::kw_register:
2187 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002188
Chris Lattneracd58a32006-08-06 17:24:14 +00002189 // type-specifiers
2190 case tok::kw_short:
2191 case tok::kw_long:
2192 case tok::kw_signed:
2193 case tok::kw_unsigned:
2194 case tok::kw__Complex:
2195 case tok::kw__Imaginary:
2196 case tok::kw_void:
2197 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002198 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002199 case tok::kw_char16_t:
2200 case tok::kw_char32_t:
2201
Chris Lattneracd58a32006-08-06 17:24:14 +00002202 case tok::kw_int:
2203 case tok::kw_float:
2204 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002205 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002206 case tok::kw__Bool:
2207 case tok::kw__Decimal32:
2208 case tok::kw__Decimal64:
2209 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002210 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002211
Chris Lattner861a2262008-04-13 18:59:07 +00002212 // struct-or-union-specifier (C99) or class-specifier (C++)
2213 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002214 case tok::kw_struct:
2215 case tok::kw_union:
2216 // enum-specifier
2217 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002218
Chris Lattneracd58a32006-08-06 17:24:14 +00002219 // type-qualifier
2220 case tok::kw_const:
2221 case tok::kw_volatile:
2222 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002223
Chris Lattneracd58a32006-08-06 17:24:14 +00002224 // function-specifier
2225 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002226 case tok::kw_virtual:
2227 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002228
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002229 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002230 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002231
Chris Lattner599e47e2007-08-09 17:01:07 +00002232 // GNU typeof support.
2233 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002234
Chris Lattner599e47e2007-08-09 17:01:07 +00002235 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002236 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002237 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002238
Chris Lattner8b2ec162008-07-26 03:38:44 +00002239 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2240 case tok::less:
2241 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002242
Steve Narofff192fab2009-01-06 19:34:12 +00002243 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002244 case tok::kw___cdecl:
2245 case tok::kw___stdcall:
2246 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002247 case tok::kw___w64:
2248 case tok::kw___ptr64:
2249 case tok::kw___forceinline:
2250 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002251 }
2252}
2253
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002254bool Parser::isConstructorDeclarator() {
2255 TentativeParsingAction TPA(*this);
2256
2257 // Parse the C++ scope specifier.
2258 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002259 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2260 TPA.Revert();
2261 return false;
2262 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002263
2264 // Parse the constructor name.
2265 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2266 // We already know that we have a constructor name; just consume
2267 // the token.
2268 ConsumeToken();
2269 } else {
2270 TPA.Revert();
2271 return false;
2272 }
2273
2274 // Current class name must be followed by a left parentheses.
2275 if (Tok.isNot(tok::l_paren)) {
2276 TPA.Revert();
2277 return false;
2278 }
2279 ConsumeParen();
2280
2281 // A right parentheses or ellipsis signals that we have a constructor.
2282 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2283 TPA.Revert();
2284 return true;
2285 }
2286
2287 // If we need to, enter the specified scope.
2288 DeclaratorScopeObj DeclScopeObj(*this, SS);
2289 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2290 DeclScopeObj.EnterDeclaratorScope();
2291
2292 // Check whether the next token(s) are part of a declaration
2293 // specifier, in which case we have the start of a parameter and,
2294 // therefore, we know that this is a constructor.
2295 bool IsConstructor = isDeclarationSpecifier();
2296 TPA.Revert();
2297 return IsConstructor;
2298}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002299
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002300/// ParseTypeQualifierListOpt
2301/// type-qualifier-list: [C99 6.7.5]
2302/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002303/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002304/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002305/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002306/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2307/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002308///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002309void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2310 bool CXX0XAttributesAllowed) {
2311 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2312 SourceLocation Loc = Tok.getLocation();
2313 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2314 if (CXX0XAttributesAllowed)
2315 DS.AddAttributes(Attr.AttrList);
2316 else
2317 Diag(Loc, diag::err_attributes_not_allowed);
2318 }
2319
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002320 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002321 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002322 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002323 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002324 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002325
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002326 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002327 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002328 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2329 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002330 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002331 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002332 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2333 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002334 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002335 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002336 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2337 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002338 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002339 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002340 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002341 case tok::kw___cdecl:
2342 case tok::kw___stdcall:
2343 case tok::kw___fastcall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002344 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002345 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2346 continue;
2347 }
2348 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002349 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002350 if (GNUAttributesAllowed) {
2351 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002352 continue; // do *not* consume the next token!
2353 }
2354 // otherwise, FALL THROUGH!
2355 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002356 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002357 // If this is not a type-qualifier token, we're done reading type
2358 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002359 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002360 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002361 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002362
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002363 // If the specifier combination wasn't legal, issue a diagnostic.
2364 if (isInvalid) {
2365 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002366 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002367 }
2368 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002369 }
2370}
2371
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002372
2373/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2374///
2375void Parser::ParseDeclarator(Declarator &D) {
2376 /// This implements the 'declarator' production in the C grammar, then checks
2377 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002378 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002379}
2380
Sebastian Redlbd150f42008-11-21 19:14:01 +00002381/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2382/// is parsed by the function passed to it. Pass null, and the direct-declarator
2383/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002384/// ptr-operator production.
2385///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002386/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2387/// [C] pointer[opt] direct-declarator
2388/// [C++] direct-declarator
2389/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002390///
2391/// pointer: [C99 6.7.5]
2392/// '*' type-qualifier-list[opt]
2393/// '*' type-qualifier-list[opt] pointer
2394///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002395/// ptr-operator:
2396/// '*' cv-qualifier-seq[opt]
2397/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002398/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002399/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002400/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002401/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002402void Parser::ParseDeclaratorInternal(Declarator &D,
2403 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002404 if (Diags.hasAllExtensionsSilenced())
2405 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002406 // C++ member pointers start with a '::' or a nested-name.
2407 // Member pointers get special handling, since there's no place for the
2408 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002409 if (getLang().CPlusPlus &&
2410 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2411 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002412 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002413 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2414
2415 if (SS.isSet()) {
Mike Stump11289f42009-09-09 15:08:12 +00002416 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002417 // The scope spec really belongs to the direct-declarator.
2418 D.getCXXScopeSpec() = SS;
2419 if (DirectDeclParser)
2420 (this->*DirectDeclParser)(D);
2421 return;
2422 }
2423
2424 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002425 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002426 DeclSpec DS;
2427 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002428 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002429
2430 // Recurse to parse whatever is left.
2431 ParseDeclaratorInternal(D, DirectDeclParser);
2432
2433 // Sema will have to catch (syntactically invalid) pointers into global
2434 // scope. It has to catch pointers into namespace scope anyway.
2435 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002436 Loc, DS.TakeAttributes()),
2437 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002438 return;
2439 }
2440 }
2441
2442 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002443 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002444 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002445 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002446 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002447 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002448 if (DirectDeclParser)
2449 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002450 return;
2451 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002452
Sebastian Redled0f3b02009-03-15 22:02:01 +00002453 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2454 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002455 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002456 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002457
Chris Lattner9eac9312009-03-27 04:18:06 +00002458 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002459 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002460 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002461
Bill Wendling3708c182007-05-27 10:15:43 +00002462 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002463 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002464
Bill Wendling3708c182007-05-27 10:15:43 +00002465 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002466 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002467 if (Kind == tok::star)
2468 // Remember that we parsed a pointer type, and remember the type-quals.
2469 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002470 DS.TakeAttributes()),
2471 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002472 else
2473 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002474 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002475 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002476 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002477 } else {
2478 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002479 DeclSpec DS;
2480
Sebastian Redl3b27be62009-03-23 00:00:23 +00002481 // Complain about rvalue references in C++03, but then go on and build
2482 // the declarator.
2483 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2484 Diag(Loc, diag::err_rvalue_reference);
2485
Bill Wendling93efb222007-06-02 23:28:54 +00002486 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2487 // cv-qualifiers are introduced through the use of a typedef or of a
2488 // template type argument, in which case the cv-qualifiers are ignored.
2489 //
2490 // [GNU] Retricted references are allowed.
2491 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002492 // [C++0x] Attributes on references are not allowed.
2493 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002494 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002495
2496 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2497 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2498 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002499 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002500 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2501 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002502 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002503 }
Bill Wendling3708c182007-05-27 10:15:43 +00002504
2505 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002506 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002507
Douglas Gregor66583c52008-11-03 15:51:28 +00002508 if (D.getNumTypeObjects() > 0) {
2509 // C++ [dcl.ref]p4: There shall be no references to references.
2510 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2511 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002512 if (const IdentifierInfo *II = D.getIdentifier())
2513 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2514 << II;
2515 else
2516 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2517 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002518
Sebastian Redlbd150f42008-11-21 19:14:01 +00002519 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002520 // can go ahead and build the (technically ill-formed)
2521 // declarator: reference collapsing will take care of it.
2522 }
2523 }
2524
Bill Wendling3708c182007-05-27 10:15:43 +00002525 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002526 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002527 DS.TakeAttributes(),
2528 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002529 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002530 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002531}
2532
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002533/// ParseDirectDeclarator
2534/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002535/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002536/// '(' declarator ')'
2537/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002538/// [C90] direct-declarator '[' constant-expression[opt] ']'
2539/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2540/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2541/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2542/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002543/// direct-declarator '(' parameter-type-list ')'
2544/// direct-declarator '(' identifier-list[opt] ')'
2545/// [GNU] direct-declarator '(' parameter-forward-declarations
2546/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002547/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2548/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002549/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002550///
2551/// declarator-id: [C++ 8]
2552/// id-expression
2553/// '::'[opt] nested-name-specifier[opt] type-name
2554///
2555/// id-expression: [C++ 5.1]
2556/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002557/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002558///
2559/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002560/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002561/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002562/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002563/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002564/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002565///
Chris Lattneracd58a32006-08-06 17:24:14 +00002566void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002567 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002568
Douglas Gregor7861a802009-11-03 01:35:08 +00002569 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2570 // ParseDeclaratorInternal might already have parsed the scope.
John McCall1f476a12010-02-26 08:45:28 +00002571 bool afterCXXScope = D.getCXXScopeSpec().isSet();
2572 if (!afterCXXScope) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002573 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2574 true);
John McCall1f476a12010-02-26 08:45:28 +00002575 afterCXXScope = D.getCXXScopeSpec().isSet();
2576 }
2577
Douglas Gregor7861a802009-11-03 01:35:08 +00002578 if (afterCXXScope) {
John McCall2b058ef2009-12-11 20:04:54 +00002579 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2580 // Change the declaration context for name lookup, until this function
2581 // is exited (and the declarator has been parsed).
2582 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor7861a802009-11-03 01:35:08 +00002583 }
2584
2585 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2586 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2587 // We found something that indicates the start of an unqualified-id.
2588 // Parse that unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002589 bool AllowConstructorName
2590 = ((D.getCXXScopeSpec().isSet() &&
2591 D.getContext() == Declarator::FileContext) ||
2592 (!D.getCXXScopeSpec().isSet() &&
2593 D.getContext() == Declarator::MemberContext)) &&
2594 !D.getDeclSpec().hasTypeSpecifier();
Douglas Gregor7861a802009-11-03 01:35:08 +00002595 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2596 /*EnteringContext=*/true,
2597 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002598 AllowConstructorName,
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002599 /*ObjectType=*/0,
Douglas Gregor7861a802009-11-03 01:35:08 +00002600 D.getName())) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002601 D.SetIdentifier(0, Tok.getLocation());
2602 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002603 } else {
2604 // Parsed the unqualified-id; update range information and move along.
2605 if (D.getSourceRange().getBegin().isInvalid())
2606 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2607 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002608 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002609 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002610 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002611 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002612 assert(!getLang().CPlusPlus &&
2613 "There's a C++-specific check for tok::identifier above");
2614 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2615 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2616 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002617 goto PastIdentifier;
2618 }
2619
2620 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002621 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002622 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002623 // Example: 'char (*X)' or 'int (*XX)(void)'
2624 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002625
2626 // If the declarator was parenthesized, we entered the declarator
2627 // scope when parsing the parenthesized declarator, then exited
2628 // the scope already. Re-enter the scope, if we need to.
2629 if (D.getCXXScopeSpec().isSet()) {
2630 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2631 // Change the declaration context for name lookup, until this function
2632 // is exited (and the declarator has been parsed).
2633 DeclScopeObj.EnterDeclaratorScope();
2634 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002635 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002636 // This could be something simple like "int" (in which case the declarator
2637 // portion is empty), if an abstract-declarator is allowed.
2638 D.SetIdentifier(0, Tok.getLocation());
2639 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002640 if (D.getContext() == Declarator::MemberContext)
2641 Diag(Tok, diag::err_expected_member_name_or_semi)
2642 << D.getDeclSpec().getSourceRange();
2643 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002644 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002645 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002646 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002647 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002648 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002649 }
Mike Stump11289f42009-09-09 15:08:12 +00002650
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002651 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002652 assert(D.isPastIdentifier() &&
2653 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002654
Alexis Hunt96d5c762009-11-21 08:43:09 +00002655 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002656 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002657 && isCXX0XAttributeSpecifier(true)) {
2658 SourceLocation AttrEndLoc;
2659 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2660 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2661 }
2662
Chris Lattneracd58a32006-08-06 17:24:14 +00002663 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002664 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002665 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2666 // In such a case, check if we actually have a function declarator; if it
2667 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002668 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2669 // When not in file scope, warn for ambiguous function declarators, just
2670 // in case the author intended it as a variable definition.
2671 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2672 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2673 break;
2674 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002675 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002676 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002677 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002678 } else {
2679 break;
2680 }
2681 }
2682}
2683
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002684/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2685/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002686/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002687/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2688///
2689/// direct-declarator:
2690/// '(' declarator ')'
2691/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002692/// direct-declarator '(' parameter-type-list ')'
2693/// direct-declarator '(' identifier-list[opt] ')'
2694/// [GNU] direct-declarator '(' parameter-forward-declarations
2695/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002696///
2697void Parser::ParseParenDeclarator(Declarator &D) {
2698 SourceLocation StartLoc = ConsumeParen();
2699 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002700
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002701 // Eat any attributes before we look at whether this is a grouping or function
2702 // declarator paren. If this is a grouping paren, the attribute applies to
2703 // the type being built up, for example:
2704 // int (__attribute__(()) *x)(long y)
2705 // If this ends up not being a grouping paren, the attribute applies to the
2706 // first argument, for example:
2707 // int (__attribute__(()) int x)
2708 // In either case, we need to eat any attributes to be able to determine what
2709 // sort of paren this is.
2710 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002711 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002712 bool RequiresArg = false;
2713 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002714 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002715
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002716 // We require that the argument list (if this is a non-grouping paren) be
2717 // present even if the attribute list was empty.
2718 RequiresArg = true;
2719 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002720 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002721 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2722 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2723 Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002724 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002725 }
Mike Stump11289f42009-09-09 15:08:12 +00002726
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002727 // If we haven't past the identifier yet (or where the identifier would be
2728 // stored, if this is an abstract declarator), then this is probably just
2729 // grouping parens. However, if this could be an abstract-declarator, then
2730 // this could also be the start of function arguments (consider 'void()').
2731 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002732
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002733 if (!D.mayOmitIdentifier()) {
2734 // If this can't be an abstract-declarator, this *must* be a grouping
2735 // paren, because we haven't seen the identifier yet.
2736 isGrouping = true;
2737 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002738 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002739 isDeclarationSpecifier()) { // 'int(int)' is a function.
2740 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2741 // considered to be a type, not a K&R identifier-list.
2742 isGrouping = false;
2743 } else {
2744 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2745 isGrouping = true;
2746 }
Mike Stump11289f42009-09-09 15:08:12 +00002747
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002748 // If this is a grouping paren, handle:
2749 // direct-declarator: '(' declarator ')'
2750 // direct-declarator: '(' attributes declarator ')'
2751 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002752 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002753 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002754 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002755 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002756
Sebastian Redlbd150f42008-11-21 19:14:01 +00002757 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002758 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002759 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002760
2761 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002762 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002763 return;
2764 }
Mike Stump11289f42009-09-09 15:08:12 +00002765
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002766 // Okay, if this wasn't a grouping paren, it must be the start of a function
2767 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002768 // identifier (and remember where it would have been), then call into
2769 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002770 D.SetIdentifier(0, Tok.getLocation());
2771
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002772 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002773}
2774
2775/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2776/// declarator D up to a paren, which indicates that we are parsing function
2777/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002778///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002779/// If AttrList is non-null, then the caller parsed those arguments immediately
2780/// after the open paren - they should be considered to be the first argument of
2781/// a parameter. If RequiresArg is true, then the first argument of the
2782/// function is required to be present and required to not be an identifier
2783/// list.
2784///
Chris Lattneracd58a32006-08-06 17:24:14 +00002785/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002786/// parameter-type-list: [C99 6.7.5]
2787/// parameter-list
2788/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002789/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002790///
2791/// parameter-list: [C99 6.7.5]
2792/// parameter-declaration
2793/// parameter-list ',' parameter-declaration
2794///
2795/// parameter-declaration: [C99 6.7.5]
2796/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002797/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002798/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002799/// declaration-specifiers abstract-declarator[opt]
2800/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002801/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002802/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002803///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002804/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002805/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002806///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002807void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2808 AttributeList *AttrList,
2809 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002810 // lparen is already consumed!
2811 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002812
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002813 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002814 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002815 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002816 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002817 delete AttrList;
2818 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002819
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002820 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2821 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002822
2823 // cv-qualifier-seq[opt].
2824 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002825 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002826 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002827 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002828 llvm::SmallVector<TypeTy*, 2> Exceptions;
2829 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002830 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002831 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002832 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002833 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002834
2835 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002836 if (Tok.is(tok::kw_throw)) {
2837 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002838 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002839 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002840 hasAnyExceptionSpec);
2841 assert(Exceptions.size() == ExceptionRanges.size() &&
2842 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002843 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002844 }
2845
Chris Lattner371ed4e2008-04-06 06:57:35 +00002846 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002847 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002848 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002849 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002850 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002851 /*arglist*/ 0, 0,
2852 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002853 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002854 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002855 Exceptions.data(),
2856 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002857 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002858 LParenLoc, RParenLoc, D),
2859 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002860 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002861 }
2862
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002863 // Alternatively, this parameter list may be an identifier list form for a
2864 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00002865 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2866 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00002867 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002868 // K&R identifier lists can't have typedefs as identifiers, per
2869 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002870 if (RequiresArg) {
2871 Diag(Tok, diag::err_argument_required_after_attribute);
2872 delete AttrList;
2873 }
Steve Naroffb0486722009-01-28 19:16:40 +00002874 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2875 // normal declarators, not for abstract-declarators.
2876 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002877 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002878 }
Mike Stump11289f42009-09-09 15:08:12 +00002879
Chris Lattner371ed4e2008-04-06 06:57:35 +00002880 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002881
Chris Lattner371ed4e2008-04-06 06:57:35 +00002882 // Build up an array of information about the parsed arguments.
2883 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002884
2885 // Enter function-declaration scope, limiting any declarators to the
2886 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002887 ParseScope PrototypeScope(this,
2888 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002889
Chris Lattner371ed4e2008-04-06 06:57:35 +00002890 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002891 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002892 while (1) {
2893 if (Tok.is(tok::ellipsis)) {
2894 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002895 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002896 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002897 }
Mike Stump11289f42009-09-09 15:08:12 +00002898
Chris Lattner371ed4e2008-04-06 06:57:35 +00002899 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002900
Chris Lattner371ed4e2008-04-06 06:57:35 +00002901 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00002902 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002903 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002904
2905 // If the caller parsed attributes for the first argument, add them now.
2906 if (AttrList) {
2907 DS.AddAttributes(AttrList);
2908 AttrList = 0; // Only apply the attributes to the first parameter.
2909 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002910 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002911
Chris Lattner371ed4e2008-04-06 06:57:35 +00002912 // Parse the declarator. This is "PrototypeContext", because we must
2913 // accept either 'declarator' or 'abstract-declarator' here.
2914 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2915 ParseDeclarator(ParmDecl);
2916
2917 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002918 if (Tok.is(tok::kw___attribute)) {
2919 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002920 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002921 ParmDecl.AddAttributes(AttrList, Loc);
2922 }
Mike Stump11289f42009-09-09 15:08:12 +00002923
Chris Lattner371ed4e2008-04-06 06:57:35 +00002924 // Remember this parsed parameter in ParamInfo.
2925 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002926
Douglas Gregor4d87df52008-12-16 21:30:33 +00002927 // DefArgToks is used when the parsing of default arguments needs
2928 // to be delayed.
2929 CachedTokens *DefArgToks = 0;
2930
Chris Lattner371ed4e2008-04-06 06:57:35 +00002931 // If no parameter was specified, verify that *something* was specified,
2932 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002933 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2934 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002935 // Completely missing, emit error.
2936 Diag(DSStart, diag::err_missing_param);
2937 } else {
2938 // Otherwise, we have something. Add it and let semantic analysis try
2939 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002940
Chris Lattner371ed4e2008-04-06 06:57:35 +00002941 // Inform the actions module about the parameter declarator, so it gets
2942 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002943 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002944
2945 // Parse the default argument, if any. We parse the default
2946 // arguments in all dialects; the semantic analysis in
2947 // ActOnParamDefaultArgument will reject the default argument in
2948 // C.
2949 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002950 SourceLocation EqualLoc = Tok.getLocation();
2951
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002952 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002953 if (D.getContext() == Declarator::MemberContext) {
2954 // If we're inside a class definition, cache the tokens
2955 // corresponding to the default argument. We'll actually parse
2956 // them when we see the end of the class definition.
2957 // FIXME: Templates will require something similar.
2958 // FIXME: Can we use a smart pointer for Toks?
2959 DefArgToks = new CachedTokens;
2960
Mike Stump11289f42009-09-09 15:08:12 +00002961 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002962 tok::semi, false)) {
2963 delete DefArgToks;
2964 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002965 Actions.ActOnParamDefaultArgumentError(Param);
2966 } else
Mike Stump11289f42009-09-09 15:08:12 +00002967 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002968 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002969 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002970 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002971 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002972
Douglas Gregor4d87df52008-12-16 21:30:33 +00002973 OwningExprResult DefArgResult(ParseAssignmentExpression());
2974 if (DefArgResult.isInvalid()) {
2975 Actions.ActOnParamDefaultArgumentError(Param);
2976 SkipUntil(tok::comma, tok::r_paren, true, true);
2977 } else {
2978 // Inform the actions module about the default argument
2979 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002980 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002981 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002982 }
2983 }
Mike Stump11289f42009-09-09 15:08:12 +00002984
2985 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2986 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002987 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002988 }
2989
2990 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002991 if (Tok.isNot(tok::comma)) {
2992 if (Tok.is(tok::ellipsis)) {
2993 IsVariadic = true;
2994 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2995
2996 if (!getLang().CPlusPlus) {
2997 // We have ellipsis without a preceding ',', which is ill-formed
2998 // in C. Complain and provide the fix.
2999 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
3000 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
3001 }
3002 }
3003
3004 break;
3005 }
Mike Stump11289f42009-09-09 15:08:12 +00003006
Chris Lattner371ed4e2008-04-06 06:57:35 +00003007 // Consume the comma.
3008 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003009 }
Mike Stump11289f42009-09-09 15:08:12 +00003010
Chris Lattner371ed4e2008-04-06 06:57:35 +00003011 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003012 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003013
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003014 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003015 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3016 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003017
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003018 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003019 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003020 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003021 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00003022 llvm::SmallVector<TypeTy*, 2> Exceptions;
3023 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003024
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003025 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003026 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003027 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003028 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003029 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003030
3031 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003032 if (Tok.is(tok::kw_throw)) {
3033 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003034 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003035 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003036 hasAnyExceptionSpec);
3037 assert(Exceptions.size() == ExceptionRanges.size() &&
3038 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003039 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003040 }
3041
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003042 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003043 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003044 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003045 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003046 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003047 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003048 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003049 Exceptions.data(),
3050 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003051 Exceptions.size(),
3052 LParenLoc, RParenLoc, D),
3053 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003054}
Chris Lattneracd58a32006-08-06 17:24:14 +00003055
Chris Lattner6c940e62008-04-06 06:34:08 +00003056/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3057/// we found a K&R-style identifier list instead of a type argument list. The
3058/// current token is known to be the first identifier in the list.
3059///
3060/// identifier-list: [C99 6.7.5]
3061/// identifier
3062/// identifier-list ',' identifier
3063///
3064void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
3065 Declarator &D) {
3066 // Build up an array of information about the parsed arguments.
3067 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3068 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003069
Chris Lattner6c940e62008-04-06 06:34:08 +00003070 // If there was no identifier specified for the declarator, either we are in
3071 // an abstract-declarator, or we are in a parameter declarator which was found
3072 // to be abstract. In abstract-declarators, identifier lists are not valid:
3073 // diagnose this.
3074 if (!D.getIdentifier())
3075 Diag(Tok, diag::ext_ident_list_in_param);
3076
3077 // Tok is known to be the first identifier in the list. Remember this
3078 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00003079 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00003080 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00003081 Tok.getLocation(),
3082 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00003083
Chris Lattner9186f552008-04-06 06:39:19 +00003084 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00003085
Chris Lattner6c940e62008-04-06 06:34:08 +00003086 while (Tok.is(tok::comma)) {
3087 // Eat the comma.
3088 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003089
Chris Lattner9186f552008-04-06 06:39:19 +00003090 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003091 if (Tok.isNot(tok::identifier)) {
3092 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003093 SkipUntil(tok::r_paren);
3094 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003095 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003096
Chris Lattner6c940e62008-04-06 06:34:08 +00003097 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003098
3099 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00003100 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003101 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003102
Chris Lattner6c940e62008-04-06 06:34:08 +00003103 // Verify that the argument identifier has not already been mentioned.
3104 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003105 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003106 } else {
3107 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003108 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003109 Tok.getLocation(),
3110 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00003111 }
Mike Stump11289f42009-09-09 15:08:12 +00003112
Chris Lattner6c940e62008-04-06 06:34:08 +00003113 // Eat the identifier.
3114 ConsumeToken();
3115 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003116
3117 // If we have the closing ')', eat it and we're done.
3118 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3119
Chris Lattner9186f552008-04-06 06:39:19 +00003120 // Remember that we parsed a function type, and remember the attributes. This
3121 // function type is always a K&R style function type, which is not varargs and
3122 // has no prototype.
3123 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003124 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003125 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003126 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003127 /*exception*/false,
3128 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003129 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003130 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003131}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003132
Chris Lattnere8074e62006-08-06 18:30:15 +00003133/// [C90] direct-declarator '[' constant-expression[opt] ']'
3134/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3135/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3136/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3137/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3138void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003139 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003140
Chris Lattner84a11622008-12-18 07:27:21 +00003141 // C array syntax has many features, but by-far the most common is [] and [4].
3142 // This code does a fast path to handle some of the most obvious cases.
3143 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003144 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003145 //FIXME: Use these
3146 CXX0XAttributeList Attr;
3147 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3148 Attr = ParseCXX0XAttributes();
3149 }
3150
Chris Lattner84a11622008-12-18 07:27:21 +00003151 // Remember that we parsed the empty array type.
3152 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00003153 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3154 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003155 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003156 return;
3157 } else if (Tok.getKind() == tok::numeric_constant &&
3158 GetLookAheadToken(1).is(tok::r_square)) {
3159 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00003160 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003161 ConsumeToken();
3162
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003163 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003164 //FIXME: Use these
3165 CXX0XAttributeList Attr;
3166 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3167 Attr = ParseCXX0XAttributes();
3168 }
Chris Lattner84a11622008-12-18 07:27:21 +00003169
3170 // If there was an error parsing the assignment-expression, recover.
3171 if (ExprRes.isInvalid())
3172 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003173
Chris Lattner84a11622008-12-18 07:27:21 +00003174 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003175 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3176 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003177 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003178 return;
3179 }
Mike Stump11289f42009-09-09 15:08:12 +00003180
Chris Lattnere8074e62006-08-06 18:30:15 +00003181 // If valid, this location is the position where we read the 'static' keyword.
3182 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003183 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003184 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003185
Chris Lattnere8074e62006-08-06 18:30:15 +00003186 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003187 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003188 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003189 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003190
Chris Lattnere8074e62006-08-06 18:30:15 +00003191 // If we haven't already read 'static', check to see if there is one after the
3192 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003193 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003194 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003195
Chris Lattnere8074e62006-08-06 18:30:15 +00003196 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003197 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00003198 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00003199
Chris Lattner521ff2b2008-04-06 05:26:30 +00003200 // Handle the case where we have '[*]' as the array size. However, a leading
3201 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3202 // the the token after the star is a ']'. Since stars in arrays are
3203 // infrequent, use of lookahead is not costly here.
3204 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003205 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003206
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003207 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003208 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003209 StaticLoc = SourceLocation(); // Drop the static.
3210 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003211 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003212 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003213 // Note, in C89, this production uses the constant-expr production instead
3214 // of assignment-expr. The only difference is that assignment-expr allows
3215 // things like '=' and '*='. Sema rejects these in C89 mode because they
3216 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003217
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003218 // Parse the constant-expression or assignment-expression now (depending
3219 // on dialect).
3220 if (getLang().CPlusPlus)
3221 NumElements = ParseConstantExpression();
3222 else
3223 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003224 }
Mike Stump11289f42009-09-09 15:08:12 +00003225
Chris Lattner62591722006-08-12 18:40:58 +00003226 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003227 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003228 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003229 // If the expression was invalid, skip it.
3230 SkipUntil(tok::r_square);
3231 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003232 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003233
3234 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3235
Alexis Hunt96d5c762009-11-21 08:43:09 +00003236 //FIXME: Use these
3237 CXX0XAttributeList Attr;
3238 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3239 Attr = ParseCXX0XAttributes();
3240 }
3241
Chris Lattner84a11622008-12-18 07:27:21 +00003242 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003243 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3244 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003245 NumElements.release(),
3246 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003247 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003248}
3249
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003250/// [GNU] typeof-specifier:
3251/// typeof ( expressions )
3252/// typeof ( type-name )
3253/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003254///
3255void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003256 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003257 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003258 SourceLocation StartLoc = ConsumeToken();
3259
John McCalle8595032010-01-13 20:03:27 +00003260 const bool hasParens = Tok.is(tok::l_paren);
3261
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003262 bool isCastExpr;
3263 TypeTy *CastTy;
3264 SourceRange CastRange;
3265 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3266 isCastExpr,
3267 CastTy,
3268 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003269 if (hasParens)
3270 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003271
3272 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003273 // FIXME: Not accurate, the range gets one token more than it should.
3274 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003275 else
3276 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003277
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003278 if (isCastExpr) {
3279 if (!CastTy) {
3280 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003281 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003282 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003283
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003284 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003285 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003286 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3287 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003288 DiagID, CastTy))
3289 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003290 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003291 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003292
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003293 // If we get here, the operand to the typeof was an expresion.
3294 if (Operand.isInvalid()) {
3295 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003296 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003297 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003298
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003299 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003300 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003301 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3302 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003303 DiagID, Operand.release()))
3304 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003305}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003306
3307
3308/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3309/// from TryAltiVecVectorToken.
3310bool Parser::TryAltiVecVectorTokenOutOfLine() {
3311 Token Next = NextToken();
3312 switch (Next.getKind()) {
3313 default: return false;
3314 case tok::kw_short:
3315 case tok::kw_long:
3316 case tok::kw_signed:
3317 case tok::kw_unsigned:
3318 case tok::kw_void:
3319 case tok::kw_char:
3320 case tok::kw_int:
3321 case tok::kw_float:
3322 case tok::kw_double:
3323 case tok::kw_bool:
3324 case tok::kw___pixel:
3325 Tok.setKind(tok::kw___vector);
3326 return true;
3327 case tok::identifier:
3328 if (Next.getIdentifierInfo() == Ident_pixel) {
3329 Tok.setKind(tok::kw___vector);
3330 return true;
3331 }
3332 return false;
3333 }
3334}
3335
3336bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3337 const char *&PrevSpec, unsigned &DiagID,
3338 bool &isInvalid) {
3339 if (Tok.getIdentifierInfo() == Ident_vector) {
3340 Token Next = NextToken();
3341 switch (Next.getKind()) {
3342 case tok::kw_short:
3343 case tok::kw_long:
3344 case tok::kw_signed:
3345 case tok::kw_unsigned:
3346 case tok::kw_void:
3347 case tok::kw_char:
3348 case tok::kw_int:
3349 case tok::kw_float:
3350 case tok::kw_double:
3351 case tok::kw_bool:
3352 case tok::kw___pixel:
3353 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3354 return true;
3355 case tok::identifier:
3356 if (Next.getIdentifierInfo() == Ident_pixel) {
3357 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3358 return true;
3359 }
3360 break;
3361 default:
3362 break;
3363 }
3364 } else if (Tok.getIdentifierInfo() == Ident_pixel &&
3365 DS.isTypeAltiVecVector()) {
3366 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3367 return true;
3368 }
3369 return false;
3370}
3371