blob: 0334209f5044da42fcf82e9b06e0960779cf6d4b [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
Douglas Gregor0be31a22010-07-02 17:43:08 +000045 return Actions.ActOnTypeName(getCurScope(), 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
Douglas Gregora2f49452010-03-16 19:09:18 +0000113 // check if we have a "parameterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattner76c72282007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000123 ConsumeParen(); // ignore the right paren loc for now
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) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000280 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
281 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000282 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) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000312 ParenBraceBracketBalancer BalancerRAIIObj(*this);
313
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000314 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000315 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000316 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000317 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000318 if (Attr.HasAttr)
319 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
320 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000321 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000322 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000323 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000324 if (Attr.HasAttr)
325 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
326 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000327 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000328 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000329 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000330 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000331 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000332 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000333 if (Attr.HasAttr)
334 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
335 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000336 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000337 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000338 default:
Chris Lattner005fc1b2010-04-05 18:18:31 +0000339 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000340 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000341
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000342 // This routine returns a DeclGroup, if the thing we parsed only contains a
343 // single decl, convert it now.
344 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000345}
346
347/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
348/// declaration-specifiers init-declarator-list[opt] ';'
349///[C90/C++]init-declarator-list ';' [TODO]
350/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000351///
352/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000353/// declaration. If it is true, it checks for and eats it.
Chris Lattner32dc41c2009-03-29 17:27:48 +0000354Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000355 SourceLocation &DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000356 AttributeList *Attr,
357 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000358 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000359 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000360 if (Attr)
361 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000362 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
363 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattner0e894622006-08-13 19:58:17 +0000365 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
366 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000367 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000368 if (RequireSemi) ConsumeToken();
Douglas Gregor0be31a22010-07-02 17:43:08 +0000369 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000370 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000371 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000372 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattner005fc1b2010-04-05 18:18:31 +0000375 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000376}
Mike Stump11289f42009-09-09 15:08:12 +0000377
John McCalld5a36322009-11-03 19:26:08 +0000378/// ParseDeclGroup - Having concluded that this is either a function
379/// definition or a group of object declarations, actually parse the
380/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000381Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
382 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000383 bool AllowFunctionDefinitions,
384 SourceLocation *DeclEnd) {
385 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000386 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000387 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000388
John McCalld5a36322009-11-03 19:26:08 +0000389 // Bail out if the first declarator didn't seem well-formed.
390 if (!D.hasName() && !D.mayOmitIdentifier()) {
391 // Skip until ; or }.
392 SkipUntil(tok::r_brace, true, true);
393 if (Tok.is(tok::semi))
394 ConsumeToken();
395 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000398 // Check to see if we have a function *definition* which must have a body.
399 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
400 // Look at the next token to make sure that this isn't a function
401 // declaration. We have to check this because __attribute__ might be the
402 // start of a function definition in GCC-extended K&R C.
403 !isDeclarationAfterDeclarator()) {
404
405 if (isStartOfFunctionDefinition()) {
John McCalld5a36322009-11-03 19:26:08 +0000406 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
407 Diag(Tok, diag::err_function_declared_typedef);
408
409 // Recover by treating the 'typedef' as spurious.
410 DS.ClearStorageClassSpecs();
411 }
412
413 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
414 return Actions.ConvertDeclToDeclGroup(TheDecl);
415 } else {
416 Diag(Tok, diag::err_expected_fn_body);
417 SkipUntil(tok::semi);
418 return DeclGroupPtrTy();
419 }
420 }
421
422 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
423 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000424 D.complete(FirstDecl);
John McCalld5a36322009-11-03 19:26:08 +0000425 if (FirstDecl.get())
426 DeclsInGroup.push_back(FirstDecl);
427
428 // If we don't have a comma, it is either the end of the list (a ';') or an
429 // error, bail out.
430 while (Tok.is(tok::comma)) {
431 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000432 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000433
434 // Parse the next declarator.
435 D.clear();
436
437 // Accept attributes in an init-declarator. In the first declarator in a
438 // declaration, these would be part of the declspec. In subsequent
439 // declarators, they become part of the declarator itself, so that they
440 // don't apply to declarators after *this* one. Examples:
441 // short __attribute__((common)) var; -> declspec
442 // short var __attribute__((common)); -> declarator
443 // short x, __attribute__((common)) var; -> declarator
444 if (Tok.is(tok::kw___attribute)) {
445 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000446 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000447 D.AddAttributes(AttrList, Loc);
448 }
449
450 ParseDeclarator(D);
451
452 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000453 D.complete(ThisDecl);
John McCalld5a36322009-11-03 19:26:08 +0000454 if (ThisDecl.get())
455 DeclsInGroup.push_back(ThisDecl);
456 }
457
458 if (DeclEnd)
459 *DeclEnd = Tok.getLocation();
460
461 if (Context != Declarator::ForContext &&
462 ExpectAndConsume(tok::semi,
463 Context == Declarator::FileContext
464 ? diag::err_invalid_token_after_toplevel_declarator
465 : diag::err_expected_semi_declaration)) {
466 SkipUntil(tok::r_brace, true, true);
467 if (Tok.is(tok::semi))
468 ConsumeToken();
469 }
470
Douglas Gregor0be31a22010-07-02 17:43:08 +0000471 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000472 DeclsInGroup.data(),
473 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000474}
475
Douglas Gregor23996282009-05-12 21:31:51 +0000476/// \brief Parse 'declaration' after parsing 'declaration-specifiers
477/// declarator'. This method parses the remainder of the declaration
478/// (including any attributes or initializer, among other things) and
479/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000480///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000481/// init-declarator: [C99 6.7]
482/// declarator
483/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000484/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
485/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000486/// [C++] declarator initializer[opt]
487///
488/// [C++] initializer:
489/// [C++] '=' initializer-clause
490/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000491/// [C++0x] '=' 'default' [TODO]
492/// [C++0x] '=' 'delete'
493///
494/// According to the standard grammar, =default and =delete are function
495/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000496///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000497Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
498 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000499 // If a simple-asm-expr is present, parse it.
500 if (Tok.is(tok::kw_asm)) {
501 SourceLocation Loc;
502 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
503 if (AsmLabel.isInvalid()) {
504 SkipUntil(tok::semi, true, true);
505 return DeclPtrTy();
506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregor23996282009-05-12 21:31:51 +0000508 D.setAsmLabel(AsmLabel.release());
509 D.SetRangeEnd(Loc);
510 }
Mike Stump11289f42009-09-09 15:08:12 +0000511
Douglas Gregor23996282009-05-12 21:31:51 +0000512 // If attributes are present, parse them.
513 if (Tok.is(tok::kw___attribute)) {
514 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000515 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000516 D.AddAttributes(AttrList, Loc);
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Douglas Gregor23996282009-05-12 21:31:51 +0000519 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000520 DeclPtrTy ThisDecl;
521 switch (TemplateInfo.Kind) {
522 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000523 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000524 break;
525
526 case ParsedTemplateInfo::Template:
527 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000528 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000529 Action::MultiTemplateParamsArg(Actions,
530 TemplateInfo.TemplateParams->data(),
531 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000532 D);
533 break;
534
535 case ParsedTemplateInfo::ExplicitInstantiation: {
536 Action::DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000537 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000538 TemplateInfo.ExternLoc,
539 TemplateInfo.TemplateLoc,
540 D);
541 if (ThisRes.isInvalid()) {
542 SkipUntil(tok::semi, true, true);
543 return DeclPtrTy();
544 }
545
546 ThisDecl = ThisRes.get();
547 break;
548 }
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Douglas Gregor23996282009-05-12 21:31:51 +0000551 // Parse declarator '=' initializer.
552 if (Tok.is(tok::equal)) {
553 ConsumeToken();
554 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
555 SourceLocation DelLoc = ConsumeToken();
556 Actions.SetDeclDeleted(ThisDecl, DelLoc);
557 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000558 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
559 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000560 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000561 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000562
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000563 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000564 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000565 ConsumeCodeCompletionToken();
566 SkipUntil(tok::comma, true, true);
567 return ThisDecl;
568 }
569
Douglas Gregor23996282009-05-12 21:31:51 +0000570 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000571
John McCall1f4ee7b2009-12-19 09:28:58 +0000572 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000573 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000574 ExitScope();
575 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000576
Douglas Gregor23996282009-05-12 21:31:51 +0000577 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000578 SkipUntil(tok::comma, true, true);
579 Actions.ActOnInitializerError(ThisDecl);
580 } else
581 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000582 }
583 } else if (Tok.is(tok::l_paren)) {
584 // Parse C++ direct initializer: '(' expression-list ')'
585 SourceLocation LParenLoc = ConsumeParen();
586 ExprVector Exprs(Actions);
587 CommaLocsTy CommaLocs;
588
Douglas Gregor613bf102009-12-22 17:47:17 +0000589 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
590 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000591 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000592 }
593
Douglas Gregor23996282009-05-12 21:31:51 +0000594 if (ParseExpressionList(Exprs, CommaLocs)) {
595 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000596
597 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000598 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000599 ExitScope();
600 }
Douglas Gregor23996282009-05-12 21:31:51 +0000601 } else {
602 // Match the ')'.
603 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
604
605 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
606 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000607
608 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000609 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000610 ExitScope();
611 }
612
Douglas Gregor23996282009-05-12 21:31:51 +0000613 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
614 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000615 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000616 }
617 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000618 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000619 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
620 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000621 }
622
623 return ThisDecl;
624}
625
Chris Lattner1890ac82006-08-13 01:16:23 +0000626/// ParseSpecifierQualifierList
627/// specifier-qualifier-list:
628/// type-specifier specifier-qualifier-list[opt]
629/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000630/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000631///
632void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
633 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
634 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000635 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000636
Chris Lattner1890ac82006-08-13 01:16:23 +0000637 // Validate declspec for type-name.
638 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000639 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
640 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000641 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000642
Chris Lattner1b22eed2006-11-28 05:12:07 +0000643 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000644 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000645 if (DS.getStorageClassSpecLoc().isValid())
646 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
647 else
648 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000649 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000650 }
Mike Stump11289f42009-09-09 15:08:12 +0000651
Chris Lattner1b22eed2006-11-28 05:12:07 +0000652 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000653 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000654 if (DS.isInlineSpecified())
655 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
656 if (DS.isVirtualSpecified())
657 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
658 if (DS.isExplicitSpecified())
659 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000660 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000661 }
662}
Chris Lattner53361ac2006-08-10 05:19:57 +0000663
Chris Lattner6cc055a2009-04-12 20:42:31 +0000664/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
665/// specified token is valid after the identifier in a declarator which
666/// immediately follows the declspec. For example, these things are valid:
667///
668/// int x [ 4]; // direct-declarator
669/// int x ( int y); // direct-declarator
670/// int(int x ) // direct-declarator
671/// int x ; // simple-declaration
672/// int x = 17; // init-declarator-list
673/// int x , y; // init-declarator-list
674/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000675/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000676/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000677///
678/// This is not, because 'x' does not immediately follow the declspec (though
679/// ')' happens to be valid anyway).
680/// int (x)
681///
682static bool isValidAfterIdentifierInDeclarator(const Token &T) {
683 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
684 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000685 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000686}
687
Chris Lattner20a0c612009-04-14 21:34:55 +0000688
689/// ParseImplicitInt - This method is called when we have an non-typename
690/// identifier in a declspec (which normally terminates the decl spec) when
691/// the declspec has no type specifier. In this case, the declspec is either
692/// malformed or is "implicit int" (in K&R and C89).
693///
694/// This method handles diagnosing this prettily and returns false if the
695/// declspec is done being processed. If it recovers and thinks there may be
696/// other pieces of declspec after it, it returns true.
697///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000698bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000699 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000700 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000701 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattner20a0c612009-04-14 21:34:55 +0000703 SourceLocation Loc = Tok.getLocation();
704 // If we see an identifier that is not a type name, we normally would
705 // parse it as the identifer being declared. However, when a typename
706 // is typo'd or the definition is not included, this will incorrectly
707 // parse the typename as the identifier name and fall over misparsing
708 // later parts of the diagnostic.
709 //
710 // As such, we try to do some look-ahead in cases where this would
711 // otherwise be an "implicit-int" case to see if this is invalid. For
712 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
713 // an identifier with implicit int, we'd get a parse error because the
714 // next token is obviously invalid for a type. Parse these as a case
715 // with an invalid type specifier.
716 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattner20a0c612009-04-14 21:34:55 +0000718 // Since we know that this either implicit int (which is rare) or an
719 // error, we'd do lookahead to try to do better recovery.
720 if (isValidAfterIdentifierInDeclarator(NextToken())) {
721 // If this token is valid for implicit int, e.g. "static x = 4", then
722 // we just avoid eating the identifier, so it will be parsed as the
723 // identifier in the declarator.
724 return false;
725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner20a0c612009-04-14 21:34:55 +0000727 // Otherwise, if we don't consume this token, we are going to emit an
728 // error anyway. Try to recover from various common problems. Check
729 // to see if this was a reference to a tag name without a tag specified.
730 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000731 //
732 // C++ doesn't need this, and isTagName doesn't take SS.
733 if (SS == 0) {
734 const char *TagName = 0;
735 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000736
Douglas Gregor0be31a22010-07-02 17:43:08 +0000737 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000738 default: break;
739 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
740 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
741 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
742 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
743 }
Mike Stump11289f42009-09-09 15:08:12 +0000744
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000745 if (TagName) {
746 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000747 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000748 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000749
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000750 // Parse this as a tag as if the missing tag were present.
751 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000752 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000753 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000754 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000755 return true;
756 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000757 }
Mike Stump11289f42009-09-09 15:08:12 +0000758
Douglas Gregor15e56022009-10-13 23:27:22 +0000759 // This is almost certainly an invalid type name. Let the action emit a
760 // diagnostic and attempt to recover.
761 Action::TypeTy *T = 0;
762 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000763 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000764 // The action emitted a diagnostic, so we don't have to.
765 if (T) {
766 // The action has suggested that the type T could be used. Set that as
767 // the type in the declaration specifiers, consume the would-be type
768 // name token, and we're done.
769 const char *PrevSpec;
770 unsigned DiagID;
771 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
772 false);
773 DS.SetRangeEnd(Tok.getLocation());
774 ConsumeToken();
775
776 // There may be other declaration specifiers after this.
777 return true;
778 }
779
780 // Fall through; the action had no suggestion for us.
781 } else {
782 // The action did not emit a diagnostic, so emit one now.
783 SourceRange R;
784 if (SS) R = SS->getRange();
785 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
786 }
Mike Stump11289f42009-09-09 15:08:12 +0000787
Douglas Gregor15e56022009-10-13 23:27:22 +0000788 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000789 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000790 unsigned DiagID;
791 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000792 DS.SetRangeEnd(Tok.getLocation());
793 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000794
Chris Lattner20a0c612009-04-14 21:34:55 +0000795 // TODO: Could inject an invalid typedef decl in an enclosing scope to
796 // avoid rippling error messages on subsequent uses of the same type,
797 // could be useful if #include was forgotten.
798 return false;
799}
800
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000801/// \brief Determine the declaration specifier context from the declarator
802/// context.
803///
804/// \param Context the declarator context, which is one of the
805/// Declarator::TheContext enumerator values.
806Parser::DeclSpecContext
807Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
808 if (Context == Declarator::MemberContext)
809 return DSC_class;
810 if (Context == Declarator::FileContext)
811 return DSC_top_level;
812 return DSC_normal;
813}
814
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000815/// ParseDeclarationSpecifiers
816/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000817/// storage-class-specifier declaration-specifiers[opt]
818/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000819/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000820/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000821///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000822/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000823/// 'typedef'
824/// 'extern'
825/// 'static'
826/// 'auto'
827/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000828/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000829/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000830/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000831/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000832/// [C++] 'virtual'
833/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000834/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000835/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000836
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000837///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000838void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000839 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000840 AccessSpecifier AS,
841 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000842 if (Tok.is(tok::code_completion)) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000843 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
844 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
845 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
846 : Action::CCC_Template;
847 else if (DSContext == DSC_class)
848 CCC = Action::CCC_Class;
Douglas Gregorf1934162010-01-13 21:24:21 +0000849 else if (ObjCImpDecl)
850 CCC = Action::CCC_ObjCImplementation;
851
Douglas Gregor0be31a22010-07-02 17:43:08 +0000852 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000853 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000854 }
855
Chris Lattner2e232092008-03-13 06:29:04 +0000856 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000857 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000858 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000859 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000860 unsigned DiagID = 0;
861
Chris Lattner4d8f8732006-11-28 05:05:08 +0000862 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000863
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000864 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000865 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000866 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000867 // If this is not a declaration specifier token, we're done reading decl
868 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000869 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000870 return;
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000872 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000873 // C++ scope specifier. Annotate and loop, or bail out on error.
874 if (TryAnnotateCXXScopeToken(true)) {
875 if (!DS.hasTypeSpecifier())
876 DS.SetTypeSpecError();
877 goto DoneWithDeclSpec;
878 }
John McCall8bc2a702010-03-01 18:20:46 +0000879 if (Tok.is(tok::coloncolon)) // ::new or ::delete
880 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000881 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000882
883 case tok::annot_cxxscope: {
884 if (DS.hasTypeSpecifier())
885 goto DoneWithDeclSpec;
886
John McCall9dab4e62009-12-12 11:40:51 +0000887 CXXScopeSpec SS;
888 SS.setScopeRep(Tok.getAnnotationValue());
889 SS.setRange(Tok.getAnnotationRange());
890
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000891 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000892 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000893 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000894 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000895 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000896 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000897
898 // C++ [class.qual]p2:
899 // In a lookup in which the constructor is an acceptable lookup
900 // result and the nested-name-specifier nominates a class C:
901 //
902 // - if the name specified after the
903 // nested-name-specifier, when looked up in C, is the
904 // injected-class-name of C (Clause 9), or
905 //
906 // - if the name specified after the nested-name-specifier
907 // is the same as the identifier or the
908 // simple-template-id's template-name in the last
909 // component of the nested-name-specifier,
910 //
911 // the name is instead considered to name the constructor of
912 // class C.
913 //
914 // Thus, if the template-name is actually the constructor
915 // name, then the code is ill-formed; this interpretation is
916 // reinforced by the NAD status of core issue 635.
917 TemplateIdAnnotation *TemplateId
918 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000919 if ((DSContext == DSC_top_level ||
920 (DSContext == DSC_class && DS.isFriendSpecified())) &&
921 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000922 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000923 if (isConstructorDeclarator()) {
924 // The user meant this to be an out-of-line constructor
925 // definition, but template arguments are not allowed
926 // there. Just allow this as a constructor; we'll
927 // complain about it later.
928 goto DoneWithDeclSpec;
929 }
930
931 // The user meant this to name a type, but it actually names
932 // a constructor with some extraneous template
933 // arguments. Complain, then parse it as a type as the user
934 // intended.
935 Diag(TemplateId->TemplateNameLoc,
936 diag::err_out_of_line_template_id_names_constructor)
937 << TemplateId->Name;
938 }
939
John McCall9dab4e62009-12-12 11:40:51 +0000940 DS.getTypeSpecScope() = SS;
941 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000942 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000943 "ParseOptionalCXXScopeSpecifier not working");
944 AnnotateTemplateIdTokenAsType(&SS);
945 continue;
946 }
947
Douglas Gregorc5790df2009-09-28 07:26:33 +0000948 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000949 DS.getTypeSpecScope() = SS;
950 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000951 if (Tok.getAnnotationValue())
952 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
953 PrevSpec, DiagID,
954 Tok.getAnnotationValue());
955 else
956 DS.SetTypeSpecError();
957 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
958 ConsumeToken(); // The typename
959 }
960
Douglas Gregor167fa622009-03-25 15:40:00 +0000961 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000962 goto DoneWithDeclSpec;
963
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000964 // If we're in a context where the identifier could be a class name,
965 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +0000966 if ((DSContext == DSC_top_level ||
967 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000968 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000969 &SS)) {
970 if (isConstructorDeclarator())
971 goto DoneWithDeclSpec;
972
973 // As noted in C++ [class.qual]p2 (cited above), when the name
974 // of the class is qualified in a context where it could name
975 // a constructor, its a constructor name. However, we've
976 // looked at the declarator, and the user probably meant this
977 // to be a type. Complain that it isn't supposed to be treated
978 // as a type, then proceed to parse it as a type.
979 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
980 << Next.getIdentifierInfo();
981 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000982
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000983 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Douglas Gregor0be31a22010-07-02 17:43:08 +0000984 Next.getLocation(), getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000985
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000986 // If the referenced identifier is not a type, then this declspec is
987 // erroneous: We already checked about that it has no type specifier, and
988 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000989 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000990 if (TypeRep == 0) {
991 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000992 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000993 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000994 }
Mike Stump11289f42009-09-09 15:08:12 +0000995
John McCall9dab4e62009-12-12 11:40:51 +0000996 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000997 ConsumeToken(); // The C++ scope.
998
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000999 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001000 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001001 if (isInvalid)
1002 break;
Mike Stump11289f42009-09-09 15:08:12 +00001003
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001004 DS.SetRangeEnd(Tok.getLocation());
1005 ConsumeToken(); // The typename.
1006
1007 continue;
1008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattnere387d9e2009-01-21 19:48:37 +00001010 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001011 if (Tok.getAnnotationValue())
1012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001013 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001014 else
1015 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001016
1017 if (isInvalid)
1018 break;
1019
Chris Lattnere387d9e2009-01-21 19:48:37 +00001020 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1021 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001022
Chris Lattnere387d9e2009-01-21 19:48:37 +00001023 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1024 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1025 // Objective-C interface. If we don't have Objective-C or a '<', this is
1026 // just a normal reference to a typedef name.
1027 if (!Tok.is(tok::less) || !getLang().ObjC1)
1028 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001029
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001030 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001031 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001032 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1033 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1034 LAngleLoc, EndProtoLoc);
1035 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1036 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001037
Chris Lattnere387d9e2009-01-21 19:48:37 +00001038 DS.SetRangeEnd(EndProtoLoc);
1039 continue;
1040 }
Mike Stump11289f42009-09-09 15:08:12 +00001041
Chris Lattner16fac4f2008-07-26 01:18:38 +00001042 // typedef-name
1043 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001044 // In C++, check to see if this is a scope specifier like foo::bar::, if
1045 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001046 if (getLang().CPlusPlus) {
1047 if (TryAnnotateCXXScopeToken(true)) {
1048 if (!DS.hasTypeSpecifier())
1049 DS.SetTypeSpecError();
1050 goto DoneWithDeclSpec;
1051 }
1052 if (!Tok.is(tok::identifier))
1053 continue;
1054 }
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattner16fac4f2008-07-26 01:18:38 +00001056 // This identifier can only be a typedef name if we haven't already seen
1057 // a type-specifier. Without this check we misparse:
1058 // typedef int X; struct Y { short X; }; as 'short int'.
1059 if (DS.hasTypeSpecifier())
1060 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001061
John Thompson22334602010-02-05 00:12:22 +00001062 // Check for need to substitute AltiVec keyword tokens.
1063 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1064 break;
1065
Chris Lattner16fac4f2008-07-26 01:18:38 +00001066 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +00001067 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00001068 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001069
Chris Lattner6cc055a2009-04-12 20:42:31 +00001070 // If this is not a typedef name, don't parse it as part of the declspec,
1071 // it must be an implicit int or an error.
1072 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001073 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001074 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001075 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001076
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001077 // If we're in a context where the identifier could be a class name,
1078 // check whether this is a constructor declaration.
1079 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001080 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001081 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001082 goto DoneWithDeclSpec;
1083
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001084 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001085 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001086 if (isInvalid)
1087 break;
Mike Stump11289f42009-09-09 15:08:12 +00001088
Chris Lattner16fac4f2008-07-26 01:18:38 +00001089 DS.SetRangeEnd(Tok.getLocation());
1090 ConsumeToken(); // The identifier
1091
1092 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1093 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1094 // Objective-C interface. If we don't have Objective-C or a '<', this is
1095 // just a normal reference to a typedef name.
1096 if (!Tok.is(tok::less) || !getLang().ObjC1)
1097 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001098
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001099 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001100 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001101 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1102 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1103 LAngleLoc, EndProtoLoc);
1104 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1105 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001106
Chris Lattner16fac4f2008-07-26 01:18:38 +00001107 DS.SetRangeEnd(EndProtoLoc);
1108
Steve Naroffcd5e7822008-09-22 10:28:57 +00001109 // Need to support trailing type qualifiers (e.g. "id<p> const").
1110 // If a type specifier follows, it will be diagnosed elsewhere.
1111 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001112 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001113
1114 // type-name
1115 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001116 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001117 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001118 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001119 // This template-id does not refer to a type name, so we're
1120 // done with the type-specifiers.
1121 goto DoneWithDeclSpec;
1122 }
1123
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001124 // If we're in a context where the template-id could be a
1125 // constructor name or specialization, check whether this is a
1126 // constructor declaration.
1127 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001128 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001129 isConstructorDeclarator())
1130 goto DoneWithDeclSpec;
1131
Douglas Gregor7f741122009-02-25 19:37:18 +00001132 // Turn the template-id annotation token into a type annotation
1133 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001134 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001135 continue;
1136 }
1137
Chris Lattnere37e2332006-08-15 04:50:22 +00001138 // GNU attributes support.
1139 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001140 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001141 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001142
1143 // Microsoft declspec support.
1144 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001145 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001146 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001147
Steve Naroff44ac7772008-12-25 14:16:32 +00001148 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001149 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001150 // FIXME: Add handling here!
1151 break;
1152
1153 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001154 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001155 case tok::kw___cdecl:
1156 case tok::kw___stdcall:
1157 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001158 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001159 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1160 continue;
1161
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001162 // storage-class-specifier
1163 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001164 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1165 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001166 break;
1167 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001168 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001169 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001170 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1171 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001172 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001173 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001174 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001175 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001176 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001177 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001178 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001179 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001180 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1181 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001182 break;
1183 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001184 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001185 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1186 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001187 else
John McCall49bfce42009-08-03 20:12:06 +00001188 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1189 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001190 break;
1191 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001192 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1193 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001194 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001195 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001196 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1197 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001198 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001199 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001200 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001201 break;
Mike Stump11289f42009-09-09 15:08:12 +00001202
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001203 // function-specifier
1204 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001205 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001206 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001207 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001208 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001209 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001210 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001211 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001212 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001213
Anders Carlssoncd8db412009-05-06 04:46:28 +00001214 // friend
1215 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001216 if (DSContext == DSC_class)
1217 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1218 else {
1219 PrevSpec = ""; // not actually used by the diagnostic
1220 DiagID = diag::err_friend_invalid_in_context;
1221 isInvalid = true;
1222 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001223 break;
Mike Stump11289f42009-09-09 15:08:12 +00001224
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001225 // constexpr
1226 case tok::kw_constexpr:
1227 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1228 break;
1229
Chris Lattnere387d9e2009-01-21 19:48:37 +00001230 // type-specifier
1231 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001232 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1233 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001234 break;
1235 case tok::kw_long:
1236 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001237 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1238 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001239 else
John McCall49bfce42009-08-03 20:12:06 +00001240 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1241 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001242 break;
1243 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1245 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001246 break;
1247 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001248 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1249 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001250 break;
1251 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001252 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1253 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001254 break;
1255 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001256 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1257 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001258 break;
1259 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001260 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1261 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001262 break;
1263 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001264 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1265 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001266 break;
1267 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001268 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1269 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001270 break;
1271 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1273 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001274 break;
1275 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1277 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001278 break;
1279 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1281 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001282 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001283 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1285 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001286 break;
1287 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1289 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001290 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001291 case tok::kw_bool:
1292 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1294 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 break;
1296 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1298 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001299 break;
1300 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1302 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303 break;
1304 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
John Thompson22334602010-02-05 00:12:22 +00001308 case tok::kw___vector:
1309 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1310 break;
1311 case tok::kw___pixel:
1312 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1313 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001314
1315 // class-specifier:
1316 case tok::kw_class:
1317 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001318 case tok::kw_union: {
1319 tok::TokenKind Kind = Tok.getKind();
1320 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001321 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001322 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001323 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001324
1325 // enum-specifier:
1326 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001327 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001328 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001329 continue;
1330
1331 // cv-qualifier:
1332 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1334 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1338 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001339 break;
1340 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1342 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001343 break;
1344
Douglas Gregor333489b2009-03-27 23:10:48 +00001345 // C++ typename-specifier:
1346 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001347 if (TryAnnotateTypeOrScopeToken()) {
1348 DS.SetTypeSpecError();
1349 goto DoneWithDeclSpec;
1350 }
1351 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001352 continue;
1353 break;
1354
Chris Lattnere387d9e2009-01-21 19:48:37 +00001355 // GNU typeof support.
1356 case tok::kw_typeof:
1357 ParseTypeofSpecifier(DS);
1358 continue;
1359
Anders Carlsson74948d02009-06-24 17:47:40 +00001360 case tok::kw_decltype:
1361 ParseDecltypeSpecifier(DS);
1362 continue;
1363
Steve Naroffcfdf6162008-06-05 00:02:44 +00001364 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001365 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001366 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1367 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001368 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001369 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001370
Chris Lattner0974b232008-07-26 00:20:22 +00001371 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001372 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001373 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001374 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1375 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1376 LAngleLoc, EndProtoLoc);
1377 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1378 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001379 DS.SetRangeEnd(EndProtoLoc);
1380
Chris Lattner6d29c102008-11-18 07:48:38 +00001381 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001382 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001383 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001384 // Need to support trailing type qualifiers (e.g. "id<p> const").
1385 // If a type specifier follows, it will be diagnosed elsewhere.
1386 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001387 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001388 }
John McCall49bfce42009-08-03 20:12:06 +00001389 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001390 if (isInvalid) {
1391 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001392 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001393 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001394 }
Chris Lattner2e232092008-03-13 06:29:04 +00001395 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001396 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001397 }
1398}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001399
Chris Lattnera448d752009-01-06 06:59:53 +00001400/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001401/// primarily follow the C++ grammar with additions for C99 and GNU,
1402/// which together subsume the C grammar. Note that the C++
1403/// type-specifier also includes the C type-qualifier (for const,
1404/// volatile, and C99 restrict). Returns true if a type-specifier was
1405/// found (and parsed), false otherwise.
1406///
1407/// type-specifier: [C++ 7.1.5]
1408/// simple-type-specifier
1409/// class-specifier
1410/// enum-specifier
1411/// elaborated-type-specifier [TODO]
1412/// cv-qualifier
1413///
1414/// cv-qualifier: [C++ 7.1.5.1]
1415/// 'const'
1416/// 'volatile'
1417/// [C99] 'restrict'
1418///
1419/// simple-type-specifier: [ C++ 7.1.5.2]
1420/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1421/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1422/// 'char'
1423/// 'wchar_t'
1424/// 'bool'
1425/// 'short'
1426/// 'int'
1427/// 'long'
1428/// 'signed'
1429/// 'unsigned'
1430/// 'float'
1431/// 'double'
1432/// 'void'
1433/// [C99] '_Bool'
1434/// [C99] '_Complex'
1435/// [C99] '_Imaginary' // Removed in TC2?
1436/// [GNU] '_Decimal32'
1437/// [GNU] '_Decimal64'
1438/// [GNU] '_Decimal128'
1439/// [GNU] typeof-specifier
1440/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1441/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001442/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001443/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001444bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001445 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001446 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001447 const ParsedTemplateInfo &TemplateInfo,
1448 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001449 SourceLocation Loc = Tok.getLocation();
1450
1451 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001452 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001453 // If we already have a type specifier, this identifier is not a type.
1454 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1455 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1456 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1457 return false;
John Thompson22334602010-02-05 00:12:22 +00001458 // Check for need to substitute AltiVec keyword tokens.
1459 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1460 break;
1461 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001462 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001463 // Annotate typenames and C++ scope specifiers. If we get one, just
1464 // recurse to handle whatever we get.
1465 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001466 return true;
1467 if (Tok.is(tok::identifier))
1468 return false;
1469 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1470 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001471 case tok::coloncolon: // ::foo::bar
1472 if (NextToken().is(tok::kw_new) || // ::new
1473 NextToken().is(tok::kw_delete)) // ::delete
1474 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001475
Chris Lattner020bab92009-01-04 23:41:41 +00001476 // Annotate typenames and C++ scope specifiers. If we get one, just
1477 // recurse to handle whatever we get.
1478 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001479 return true;
1480 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1481 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001482
Douglas Gregor450c75a2008-11-07 15:42:26 +00001483 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001484 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001485 if (Tok.getAnnotationValue())
1486 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001487 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001488 else
1489 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001490 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1491 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001492
Douglas Gregor450c75a2008-11-07 15:42:26 +00001493 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1494 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1495 // Objective-C interface. If we don't have Objective-C or a '<', this is
1496 // just a normal reference to a typedef name.
1497 if (!Tok.is(tok::less) || !getLang().ObjC1)
1498 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001499
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001500 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001501 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001502 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1503 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1504 LAngleLoc, EndProtoLoc);
1505 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1506 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001507
Douglas Gregor450c75a2008-11-07 15:42:26 +00001508 DS.SetRangeEnd(EndProtoLoc);
1509 return true;
1510 }
1511
1512 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001513 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001514 break;
1515 case tok::kw_long:
1516 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001517 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1518 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001519 else
John McCall49bfce42009-08-03 20:12:06 +00001520 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1521 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001522 break;
1523 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001524 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001525 break;
1526 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001527 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1528 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001529 break;
1530 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001531 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1532 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001533 break;
1534 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001535 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1536 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001537 break;
1538 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001540 break;
1541 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001543 break;
1544 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001546 break;
1547 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001549 break;
1550 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001552 break;
1553 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001555 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001556 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001558 break;
1559 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001560 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001561 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001562 case tok::kw_bool:
1563 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001565 break;
1566 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1568 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001569 break;
1570 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001571 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1572 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001573 break;
1574 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001575 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1576 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001577 break;
John Thompson22334602010-02-05 00:12:22 +00001578 case tok::kw___vector:
1579 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1580 break;
1581 case tok::kw___pixel:
1582 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1583 break;
1584
Douglas Gregor450c75a2008-11-07 15:42:26 +00001585 // class-specifier:
1586 case tok::kw_class:
1587 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001588 case tok::kw_union: {
1589 tok::TokenKind Kind = Tok.getKind();
1590 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001591 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1592 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001593 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001594 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001595
1596 // enum-specifier:
1597 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001598 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001599 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001600 return true;
1601
1602 // cv-qualifier:
1603 case tok::kw_const:
1604 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001605 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw_volatile:
1608 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001609 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001610 break;
1611 case tok::kw_restrict:
1612 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001613 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001614 break;
1615
1616 // GNU typeof support.
1617 case tok::kw_typeof:
1618 ParseTypeofSpecifier(DS);
1619 return true;
1620
Anders Carlsson74948d02009-06-24 17:47:40 +00001621 // C++0x decltype support.
1622 case tok::kw_decltype:
1623 ParseDecltypeSpecifier(DS);
1624 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001625
Anders Carlssonbae27372009-06-26 23:44:14 +00001626 // C++0x auto support.
1627 case tok::kw_auto:
1628 if (!getLang().CPlusPlus0x)
1629 return false;
1630
John McCall49bfce42009-08-03 20:12:06 +00001631 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001632 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001633 case tok::kw___ptr64:
1634 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001635 case tok::kw___cdecl:
1636 case tok::kw___stdcall:
1637 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001638 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001639 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001640 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001641
Douglas Gregor450c75a2008-11-07 15:42:26 +00001642 default:
1643 // Not a type-specifier; do nothing.
1644 return false;
1645 }
1646
1647 // If the specifier combination wasn't legal, issue a diagnostic.
1648 if (isInvalid) {
1649 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001650 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001651 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001652 }
1653 DS.SetRangeEnd(Tok.getLocation());
1654 ConsumeToken(); // whatever we parsed above.
1655 return true;
1656}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001657
Chris Lattner70ae4912007-10-29 04:42:53 +00001658/// ParseStructDeclaration - Parse a struct declaration without the terminating
1659/// semicolon.
1660///
Chris Lattner90a26b02007-01-23 04:38:16 +00001661/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001662/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001663/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001664/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001665/// struct-declarator-list:
1666/// struct-declarator
1667/// struct-declarator-list ',' struct-declarator
1668/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1669/// struct-declarator:
1670/// declarator
1671/// [GNU] declarator attributes[opt]
1672/// declarator[opt] ':' constant-expression
1673/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1674///
Chris Lattnera12405b2008-04-10 06:46:29 +00001675void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001676ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001677 if (Tok.is(tok::kw___extension__)) {
1678 // __extension__ silences extension warnings in the subexpression.
1679 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001680 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001681 return ParseStructDeclaration(DS, Fields);
1682 }
Mike Stump11289f42009-09-09 15:08:12 +00001683
Steve Naroff97170802007-08-20 22:28:22 +00001684 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001685 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001686 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001687
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001688 // If there are no declarators, this is a free-standing declaration
1689 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001690 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001691 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001692 return;
1693 }
1694
1695 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001696 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001697 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001698 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001699 FieldDeclarator DeclaratorInfo(DS);
1700
1701 // Attributes are only allowed here on successive declarators.
1702 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1703 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001704 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001705 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1706 }
Mike Stump11289f42009-09-09 15:08:12 +00001707
Steve Naroff97170802007-08-20 22:28:22 +00001708 /// struct-declarator: declarator
1709 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001710 if (Tok.isNot(tok::colon)) {
1711 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1712 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001713 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001714 }
Mike Stump11289f42009-09-09 15:08:12 +00001715
Chris Lattner76c72282007-10-09 17:33:22 +00001716 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001717 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001718 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001719 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001720 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001721 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001722 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001723 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001724
Steve Naroff97170802007-08-20 22:28:22 +00001725 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001726 if (Tok.is(tok::kw___attribute)) {
1727 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001728 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001729 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1730 }
1731
John McCallcfefb6d2009-11-03 02:38:08 +00001732 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001733 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1734 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001735
Steve Naroff97170802007-08-20 22:28:22 +00001736 // If we don't have a comma, it is either the end of the list (a ';')
1737 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001738 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001739 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001740
Steve Naroff97170802007-08-20 22:28:22 +00001741 // Consume the comma.
1742 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001743
John McCallcfefb6d2009-11-03 02:38:08 +00001744 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001745 }
Steve Naroff97170802007-08-20 22:28:22 +00001746}
1747
1748/// ParseStructUnionBody
1749/// struct-contents:
1750/// struct-declaration-list
1751/// [EXT] empty
1752/// [GNU] "struct-declaration-list" without terminatoring ';'
1753/// struct-declaration-list:
1754/// struct-declaration
1755/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001756/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001757///
Chris Lattner1300fb92007-01-23 23:42:53 +00001758void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001759 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001760 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1761 PP.getSourceManager(),
1762 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001763
Chris Lattner90a26b02007-01-23 04:38:16 +00001764 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001765
Douglas Gregor658b9552009-01-09 22:42:13 +00001766 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001767 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001768
Chris Lattner7b9ace62007-01-23 20:11:08 +00001769 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1770 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001771 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001772 Diag(Tok, diag::ext_empty_struct_union_enum)
1773 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001774
Chris Lattner83f095c2009-03-28 19:18:32 +00001775 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001776
Chris Lattner7b9ace62007-01-23 20:11:08 +00001777 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001778 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001779 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001780
Chris Lattner736ed5d2007-06-09 05:59:07 +00001781 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001782 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001783 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001784 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001785 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001786 ConsumeToken();
1787 continue;
1788 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001789
1790 // Parse all the comma separated declarators.
1791 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001792
John McCallcfefb6d2009-11-03 02:38:08 +00001793 if (!Tok.is(tok::at)) {
1794 struct CFieldCallback : FieldCallback {
1795 Parser &P;
1796 DeclPtrTy TagDecl;
1797 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1798
1799 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1800 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1801 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1802
1803 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001804 // Install the declarator into the current TagDecl.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001805 DeclPtrTy Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001806 FD.D.getDeclSpec().getSourceRange().getBegin(),
1807 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001808 FieldDecls.push_back(Field);
1809 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001810 }
John McCallcfefb6d2009-11-03 02:38:08 +00001811 } Callback(*this, TagDecl, FieldDecls);
1812
1813 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001814 } else { // Handle @defs
1815 ConsumeToken();
1816 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1817 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001818 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001819 continue;
1820 }
1821 ConsumeToken();
1822 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1823 if (!Tok.is(tok::identifier)) {
1824 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001825 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001826 continue;
1827 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001828 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001829 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001830 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001831 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1832 ConsumeToken();
1833 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001834 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001835
Chris Lattner76c72282007-10-09 17:33:22 +00001836 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001837 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001838 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001839 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001840 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001841 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001842 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1843 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001844 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001845 // If we stopped at a ';', eat it.
1846 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001847 }
1848 }
Mike Stump11289f42009-09-09 15:08:12 +00001849
Steve Naroff33a1e802007-10-29 21:38:07 +00001850 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001851
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001852 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001853 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001854 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001855 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001856
Douglas Gregor0be31a22010-07-02 17:43:08 +00001857 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001858 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001859 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001860 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001861 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001862 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001863}
1864
1865
Chris Lattner3b561a32006-08-13 00:12:11 +00001866/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001867/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001868/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001869///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001870/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1871/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001872/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001873/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001874///
1875/// [C++] elaborated-type-specifier:
1876/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1877///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001878void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001879 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001880 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001881 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001882 if (Tok.is(tok::code_completion)) {
1883 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001884 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001885 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001886 }
1887
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001888 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001889 // If attributes exist after tag, parse them.
1890 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001891 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001892
Abramo Bagnarad7548482010-05-19 21:37:53 +00001893 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001894 if (getLang().CPlusPlus) {
1895 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1896 return;
1897
1898 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001899 Diag(Tok, diag::err_expected_ident);
1900 if (Tok.isNot(tok::l_brace)) {
1901 // Has no name and is not a definition.
1902 // Skip the rest of this declarator, up until the comma or semicolon.
1903 SkipUntil(tok::comma, true);
1904 return;
1905 }
1906 }
1907 }
Mike Stump11289f42009-09-09 15:08:12 +00001908
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001909 // Must have either 'enum name' or 'enum {...}'.
1910 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1911 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001912
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001913 // Skip the rest of this declarator, up until the comma or semicolon.
1914 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001915 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001916 }
Mike Stump11289f42009-09-09 15:08:12 +00001917
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001918 // If an identifier is present, consume and remember it.
1919 IdentifierInfo *Name = 0;
1920 SourceLocation NameLoc;
1921 if (Tok.is(tok::identifier)) {
1922 Name = Tok.getIdentifierInfo();
1923 NameLoc = ConsumeToken();
1924 }
Mike Stump11289f42009-09-09 15:08:12 +00001925
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001926 // There are three options here. If we have 'enum foo;', then this is a
1927 // forward declaration. If we have 'enum foo {...' then this is a
1928 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1929 //
1930 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1931 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1932 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1933 //
John McCall9bb74a52009-07-31 02:45:11 +00001934 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001935 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001936 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001937 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001938 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001939 else
John McCall9bb74a52009-07-31 02:45:11 +00001940 TUK = Action::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00001941
1942 // enums cannot be templates, although they can be referenced from a
1943 // template.
1944 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1945 TUK != Action::TUK_Reference) {
1946 Diag(Tok, diag::err_enum_template);
1947
1948 // Skip the rest of this declarator, up until the comma or semicolon.
1949 SkipUntil(tok::comma, true);
1950 return;
1951 }
1952
Douglas Gregord6ab8742009-05-28 23:31:59 +00001953 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001954 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00001955 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1956 const char *PrevSpec = 0;
1957 unsigned DiagID;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001958 DeclPtrTy TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001959 StartLoc, SS, Name, NameLoc, Attr.get(),
1960 AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001961 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001962 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00001963 if (IsDependent) {
1964 // This enum has a dependent nested-name-specifier. Handle it as a
1965 // dependent tag.
1966 if (!Name) {
1967 DS.SetTypeSpecError();
1968 Diag(Tok, diag::err_expected_type_name_after_typename);
1969 return;
1970 }
1971
Douglas Gregor0be31a22010-07-02 17:43:08 +00001972 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00001973 TUK, SS, Name, StartLoc,
1974 NameLoc);
1975 if (Type.isInvalid()) {
1976 DS.SetTypeSpecError();
1977 return;
1978 }
1979
1980 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
1981 Type.get(), false))
1982 Diag(StartLoc, DiagID) << PrevSpec;
1983
1984 return;
1985 }
Mike Stump11289f42009-09-09 15:08:12 +00001986
Douglas Gregorba41d012010-04-24 16:38:41 +00001987 if (!TagDecl.get()) {
1988 // The action failed to produce an enumeration tag. If this is a
1989 // definition, consume the entire definition.
1990 if (Tok.is(tok::l_brace)) {
1991 ConsumeBrace();
1992 SkipUntil(tok::r_brace);
1993 }
1994
1995 DS.SetTypeSpecError();
1996 return;
1997 }
1998
Chris Lattner76c72282007-10-09 17:33:22 +00001999 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002000 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002001
Douglas Gregor72100632010-01-25 16:33:23 +00002002 // FIXME: The DeclSpec should keep the locations of both the keyword and the
2003 // name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002004 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00002005 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00002006 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002007}
2008
Chris Lattnerc1915e22007-01-25 07:29:02 +00002009/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2010/// enumerator-list:
2011/// enumerator
2012/// enumerator-list ',' enumerator
2013/// enumerator:
2014/// enumeration-constant
2015/// enumeration-constant '=' constant-expression
2016/// enumeration-constant:
2017/// identifier
2018///
Chris Lattner83f095c2009-03-28 19:18:32 +00002019void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002020 // Enter the scope of the enum body and start the definition.
2021 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002022 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002023
Chris Lattnerc1915e22007-01-25 07:29:02 +00002024 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002025
Chris Lattner37256fb2007-08-27 17:24:30 +00002026 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002027 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002028 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002029
Chris Lattner83f095c2009-03-28 19:18:32 +00002030 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002031
Chris Lattner83f095c2009-03-28 19:18:32 +00002032 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002033
Chris Lattnerc1915e22007-01-25 07:29:02 +00002034 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002035 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002036 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2037 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002038
Chris Lattnerc1915e22007-01-25 07:29:02 +00002039 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002040 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00002041 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002042 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002043 AssignedVal = ParseConstantExpression();
2044 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002045 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002046 }
Mike Stump11289f42009-09-09 15:08:12 +00002047
Chris Lattnerc1915e22007-01-25 07:29:02 +00002048 // Install the enumerator constant into EnumDecl.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002049 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +00002050 LastEnumConstDecl,
2051 IdentLoc, Ident,
2052 EqualLoc,
2053 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002054 EnumConstantDecls.push_back(EnumConstDecl);
2055 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002056
Chris Lattner76c72282007-10-09 17:33:22 +00002057 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002058 break;
2059 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002060
2061 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002062 !(getLang().C99 || getLang().CPlusPlus0x))
2063 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2064 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002065 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002066 }
Mike Stump11289f42009-09-09 15:08:12 +00002067
Chris Lattnerc1915e22007-01-25 07:29:02 +00002068 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002069 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002070
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002071 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002072 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002073 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002074 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002075
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002076 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2077 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00002078 getCurScope(), Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002079
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002080 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002081 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002082}
Chris Lattner3b561a32006-08-13 00:12:11 +00002083
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002084/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002085/// start of a type-qualifier-list.
2086bool Parser::isTypeQualifier() const {
2087 switch (Tok.getKind()) {
2088 default: return false;
2089 // type-qualifier
2090 case tok::kw_const:
2091 case tok::kw_volatile:
2092 case tok::kw_restrict:
2093 return true;
2094 }
2095}
2096
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002097/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2098/// is definitely a type-specifier. Return false if it isn't part of a type
2099/// specifier or if we're not sure.
2100bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2101 switch (Tok.getKind()) {
2102 default: return false;
2103 // type-specifiers
2104 case tok::kw_short:
2105 case tok::kw_long:
2106 case tok::kw_signed:
2107 case tok::kw_unsigned:
2108 case tok::kw__Complex:
2109 case tok::kw__Imaginary:
2110 case tok::kw_void:
2111 case tok::kw_char:
2112 case tok::kw_wchar_t:
2113 case tok::kw_char16_t:
2114 case tok::kw_char32_t:
2115 case tok::kw_int:
2116 case tok::kw_float:
2117 case tok::kw_double:
2118 case tok::kw_bool:
2119 case tok::kw__Bool:
2120 case tok::kw__Decimal32:
2121 case tok::kw__Decimal64:
2122 case tok::kw__Decimal128:
2123 case tok::kw___vector:
2124
2125 // struct-or-union-specifier (C99) or class-specifier (C++)
2126 case tok::kw_class:
2127 case tok::kw_struct:
2128 case tok::kw_union:
2129 // enum-specifier
2130 case tok::kw_enum:
2131
2132 // typedef-name
2133 case tok::annot_typename:
2134 return true;
2135 }
2136}
2137
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002138/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002139/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002140bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002141 switch (Tok.getKind()) {
2142 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002143
Chris Lattner020bab92009-01-04 23:41:41 +00002144 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002145 if (TryAltiVecVectorToken())
2146 return true;
2147 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002148 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002149 // Annotate typenames and C++ scope specifiers. If we get one, just
2150 // recurse to handle whatever we get.
2151 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002152 return true;
2153 if (Tok.is(tok::identifier))
2154 return false;
2155 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002156
Chris Lattner020bab92009-01-04 23:41:41 +00002157 case tok::coloncolon: // ::foo::bar
2158 if (NextToken().is(tok::kw_new) || // ::new
2159 NextToken().is(tok::kw_delete)) // ::delete
2160 return false;
2161
Chris Lattner020bab92009-01-04 23:41:41 +00002162 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002163 return true;
2164 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002165
Chris Lattnere37e2332006-08-15 04:50:22 +00002166 // GNU attributes support.
2167 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002168 // GNU typeof support.
2169 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002170
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002171 // type-specifiers
2172 case tok::kw_short:
2173 case tok::kw_long:
2174 case tok::kw_signed:
2175 case tok::kw_unsigned:
2176 case tok::kw__Complex:
2177 case tok::kw__Imaginary:
2178 case tok::kw_void:
2179 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002180 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002181 case tok::kw_char16_t:
2182 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002183 case tok::kw_int:
2184 case tok::kw_float:
2185 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002186 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002187 case tok::kw__Bool:
2188 case tok::kw__Decimal32:
2189 case tok::kw__Decimal64:
2190 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002191 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002192
Chris Lattner861a2262008-04-13 18:59:07 +00002193 // struct-or-union-specifier (C99) or class-specifier (C++)
2194 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002195 case tok::kw_struct:
2196 case tok::kw_union:
2197 // enum-specifier
2198 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002199
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002200 // type-qualifier
2201 case tok::kw_const:
2202 case tok::kw_volatile:
2203 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002204
2205 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002206 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002207 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002208
Chris Lattner409bf7d2008-10-20 00:25:30 +00002209 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2210 case tok::less:
2211 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002212
Steve Naroff44ac7772008-12-25 14:16:32 +00002213 case tok::kw___cdecl:
2214 case tok::kw___stdcall:
2215 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002216 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002217 case tok::kw___w64:
2218 case tok::kw___ptr64:
2219 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002220 }
2221}
2222
Chris Lattneracd58a32006-08-06 17:24:14 +00002223/// isDeclarationSpecifier() - Return true if the current token is part of a
2224/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002225bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002226 switch (Tok.getKind()) {
2227 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002228
Chris Lattner020bab92009-01-04 23:41:41 +00002229 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002230 // Unfortunate hack to support "Class.factoryMethod" notation.
2231 if (getLang().ObjC1 && NextToken().is(tok::period))
2232 return false;
John Thompson22334602010-02-05 00:12:22 +00002233 if (TryAltiVecVectorToken())
2234 return true;
2235 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002236 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002237 // Annotate typenames and C++ scope specifiers. If we get one, just
2238 // recurse to handle whatever we get.
2239 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002240 return true;
2241 if (Tok.is(tok::identifier))
2242 return false;
2243 return isDeclarationSpecifier();
2244
Chris Lattner020bab92009-01-04 23:41:41 +00002245 case tok::coloncolon: // ::foo::bar
2246 if (NextToken().is(tok::kw_new) || // ::new
2247 NextToken().is(tok::kw_delete)) // ::delete
2248 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002249
Chris Lattner020bab92009-01-04 23:41:41 +00002250 // Annotate typenames and C++ scope specifiers. If we get one, just
2251 // recurse to handle whatever we get.
2252 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002253 return true;
2254 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002255
Chris Lattneracd58a32006-08-06 17:24:14 +00002256 // storage-class-specifier
2257 case tok::kw_typedef:
2258 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002259 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002260 case tok::kw_static:
2261 case tok::kw_auto:
2262 case tok::kw_register:
2263 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002264
Chris Lattneracd58a32006-08-06 17:24:14 +00002265 // type-specifiers
2266 case tok::kw_short:
2267 case tok::kw_long:
2268 case tok::kw_signed:
2269 case tok::kw_unsigned:
2270 case tok::kw__Complex:
2271 case tok::kw__Imaginary:
2272 case tok::kw_void:
2273 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002274 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002275 case tok::kw_char16_t:
2276 case tok::kw_char32_t:
2277
Chris Lattneracd58a32006-08-06 17:24:14 +00002278 case tok::kw_int:
2279 case tok::kw_float:
2280 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002281 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002282 case tok::kw__Bool:
2283 case tok::kw__Decimal32:
2284 case tok::kw__Decimal64:
2285 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002286 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002287
Chris Lattner861a2262008-04-13 18:59:07 +00002288 // struct-or-union-specifier (C99) or class-specifier (C++)
2289 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002290 case tok::kw_struct:
2291 case tok::kw_union:
2292 // enum-specifier
2293 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002294
Chris Lattneracd58a32006-08-06 17:24:14 +00002295 // type-qualifier
2296 case tok::kw_const:
2297 case tok::kw_volatile:
2298 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002299
Chris Lattneracd58a32006-08-06 17:24:14 +00002300 // function-specifier
2301 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002302 case tok::kw_virtual:
2303 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002304
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002305 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002306 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002307
Chris Lattner599e47e2007-08-09 17:01:07 +00002308 // GNU typeof support.
2309 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002310
Chris Lattner599e47e2007-08-09 17:01:07 +00002311 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002312 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002313 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002314
Chris Lattner8b2ec162008-07-26 03:38:44 +00002315 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2316 case tok::less:
2317 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002318
Steve Narofff192fab2009-01-06 19:34:12 +00002319 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002320 case tok::kw___cdecl:
2321 case tok::kw___stdcall:
2322 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002323 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002324 case tok::kw___w64:
2325 case tok::kw___ptr64:
2326 case tok::kw___forceinline:
2327 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002328 }
2329}
2330
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002331bool Parser::isConstructorDeclarator() {
2332 TentativeParsingAction TPA(*this);
2333
2334 // Parse the C++ scope specifier.
2335 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002336 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2337 TPA.Revert();
2338 return false;
2339 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002340
2341 // Parse the constructor name.
2342 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2343 // We already know that we have a constructor name; just consume
2344 // the token.
2345 ConsumeToken();
2346 } else {
2347 TPA.Revert();
2348 return false;
2349 }
2350
2351 // Current class name must be followed by a left parentheses.
2352 if (Tok.isNot(tok::l_paren)) {
2353 TPA.Revert();
2354 return false;
2355 }
2356 ConsumeParen();
2357
2358 // A right parentheses or ellipsis signals that we have a constructor.
2359 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2360 TPA.Revert();
2361 return true;
2362 }
2363
2364 // If we need to, enter the specified scope.
2365 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002366 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002367 DeclScopeObj.EnterDeclaratorScope();
2368
2369 // Check whether the next token(s) are part of a declaration
2370 // specifier, in which case we have the start of a parameter and,
2371 // therefore, we know that this is a constructor.
2372 bool IsConstructor = isDeclarationSpecifier();
2373 TPA.Revert();
2374 return IsConstructor;
2375}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002376
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002377/// ParseTypeQualifierListOpt
2378/// type-qualifier-list: [C99 6.7.5]
2379/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002380/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002381/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002382/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002383/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2384/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002385///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002386void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2387 bool CXX0XAttributesAllowed) {
2388 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2389 SourceLocation Loc = Tok.getLocation();
2390 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2391 if (CXX0XAttributesAllowed)
2392 DS.AddAttributes(Attr.AttrList);
2393 else
2394 Diag(Loc, diag::err_attributes_not_allowed);
2395 }
2396
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002397 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002398 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002399 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002400 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002401 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002402
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002403 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002404 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002405 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2406 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002407 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002408 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002409 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2410 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002411 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002412 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002413 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2414 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002415 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002416 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002417 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002418 case tok::kw___cdecl:
2419 case tok::kw___stdcall:
2420 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002421 case tok::kw___thiscall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002422 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002423 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2424 continue;
2425 }
2426 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002427 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002428 if (GNUAttributesAllowed) {
2429 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002430 continue; // do *not* consume the next token!
2431 }
2432 // otherwise, FALL THROUGH!
2433 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002434 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002435 // If this is not a type-qualifier token, we're done reading type
2436 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002437 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002438 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002439 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002440
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002441 // If the specifier combination wasn't legal, issue a diagnostic.
2442 if (isInvalid) {
2443 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002444 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002445 }
2446 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002447 }
2448}
2449
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002450
2451/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2452///
2453void Parser::ParseDeclarator(Declarator &D) {
2454 /// This implements the 'declarator' production in the C grammar, then checks
2455 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002456 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002457}
2458
Sebastian Redlbd150f42008-11-21 19:14:01 +00002459/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2460/// is parsed by the function passed to it. Pass null, and the direct-declarator
2461/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002462/// ptr-operator production.
2463///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002464/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2465/// [C] pointer[opt] direct-declarator
2466/// [C++] direct-declarator
2467/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002468///
2469/// pointer: [C99 6.7.5]
2470/// '*' type-qualifier-list[opt]
2471/// '*' type-qualifier-list[opt] pointer
2472///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002473/// ptr-operator:
2474/// '*' cv-qualifier-seq[opt]
2475/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002476/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002477/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002478/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002479/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002480void Parser::ParseDeclaratorInternal(Declarator &D,
2481 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002482 if (Diags.hasAllExtensionsSilenced())
2483 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002484 // C++ member pointers start with a '::' or a nested-name.
2485 // Member pointers get special handling, since there's no place for the
2486 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002487 if (getLang().CPlusPlus &&
2488 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2489 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002490 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002491 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2492
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002493 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002494 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002495 // The scope spec really belongs to the direct-declarator.
2496 D.getCXXScopeSpec() = SS;
2497 if (DirectDeclParser)
2498 (this->*DirectDeclParser)(D);
2499 return;
2500 }
2501
2502 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002503 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002504 DeclSpec DS;
2505 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002506 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002507
2508 // Recurse to parse whatever is left.
2509 ParseDeclaratorInternal(D, DirectDeclParser);
2510
2511 // Sema will have to catch (syntactically invalid) pointers into global
2512 // scope. It has to catch pointers into namespace scope anyway.
2513 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002514 Loc, DS.TakeAttributes()),
2515 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002516 return;
2517 }
2518 }
2519
2520 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002521 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002522 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002523 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002524 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002525 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002526 if (DirectDeclParser)
2527 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002528 return;
2529 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002530
Sebastian Redled0f3b02009-03-15 22:02:01 +00002531 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2532 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002533 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002534 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002535
Chris Lattner9eac9312009-03-27 04:18:06 +00002536 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002537 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002538 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002539
Bill Wendling3708c182007-05-27 10:15:43 +00002540 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002541 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002542
Bill Wendling3708c182007-05-27 10:15:43 +00002543 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002544 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002545 if (Kind == tok::star)
2546 // Remember that we parsed a pointer type, and remember the type-quals.
2547 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002548 DS.TakeAttributes()),
2549 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002550 else
2551 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002552 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002553 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002554 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002555 } else {
2556 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002557 DeclSpec DS;
2558
Sebastian Redl3b27be62009-03-23 00:00:23 +00002559 // Complain about rvalue references in C++03, but then go on and build
2560 // the declarator.
2561 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2562 Diag(Loc, diag::err_rvalue_reference);
2563
Bill Wendling93efb222007-06-02 23:28:54 +00002564 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2565 // cv-qualifiers are introduced through the use of a typedef or of a
2566 // template type argument, in which case the cv-qualifiers are ignored.
2567 //
2568 // [GNU] Retricted references are allowed.
2569 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002570 // [C++0x] Attributes on references are not allowed.
2571 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002572 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002573
2574 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2575 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2576 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002577 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002578 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2579 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002580 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002581 }
Bill Wendling3708c182007-05-27 10:15:43 +00002582
2583 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002584 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002585
Douglas Gregor66583c52008-11-03 15:51:28 +00002586 if (D.getNumTypeObjects() > 0) {
2587 // C++ [dcl.ref]p4: There shall be no references to references.
2588 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2589 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002590 if (const IdentifierInfo *II = D.getIdentifier())
2591 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2592 << II;
2593 else
2594 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2595 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002596
Sebastian Redlbd150f42008-11-21 19:14:01 +00002597 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002598 // can go ahead and build the (technically ill-formed)
2599 // declarator: reference collapsing will take care of it.
2600 }
2601 }
2602
Bill Wendling3708c182007-05-27 10:15:43 +00002603 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002604 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002605 DS.TakeAttributes(),
2606 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002607 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002608 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002609}
2610
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002611/// ParseDirectDeclarator
2612/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002613/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002614/// '(' declarator ')'
2615/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002616/// [C90] direct-declarator '[' constant-expression[opt] ']'
2617/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2618/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2619/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2620/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002621/// direct-declarator '(' parameter-type-list ')'
2622/// direct-declarator '(' identifier-list[opt] ')'
2623/// [GNU] direct-declarator '(' parameter-forward-declarations
2624/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002625/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2626/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002627/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002628///
2629/// declarator-id: [C++ 8]
2630/// id-expression
2631/// '::'[opt] nested-name-specifier[opt] type-name
2632///
2633/// id-expression: [C++ 5.1]
2634/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002635/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002636///
2637/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002638/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002639/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002640/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002641/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002642/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002643///
Chris Lattneracd58a32006-08-06 17:24:14 +00002644void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002645 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002646
Douglas Gregor7861a802009-11-03 01:35:08 +00002647 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2648 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002649 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002650 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2651 true);
John McCall1f476a12010-02-26 08:45:28 +00002652 }
2653
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002654 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002655 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002656 // Change the declaration context for name lookup, until this function
2657 // is exited (and the declarator has been parsed).
2658 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002659 }
2660
Douglas Gregor7861a802009-11-03 01:35:08 +00002661 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2662 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2663 // We found something that indicates the start of an unqualified-id.
2664 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002665 bool AllowConstructorName;
2666 if (D.getDeclSpec().hasTypeSpecifier())
2667 AllowConstructorName = false;
2668 else if (D.getCXXScopeSpec().isSet())
2669 AllowConstructorName =
2670 (D.getContext() == Declarator::FileContext ||
2671 (D.getContext() == Declarator::MemberContext &&
2672 D.getDeclSpec().isFriendSpecified()));
2673 else
2674 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2675
Douglas Gregor7861a802009-11-03 01:35:08 +00002676 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2677 /*EnteringContext=*/true,
2678 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002679 AllowConstructorName,
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002680 /*ObjectType=*/0,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002681 D.getName()) ||
2682 // Once we're past the identifier, if the scope was bad, mark the
2683 // whole declarator bad.
2684 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002685 D.SetIdentifier(0, Tok.getLocation());
2686 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002687 } else {
2688 // Parsed the unqualified-id; update range information and move along.
2689 if (D.getSourceRange().getBegin().isInvalid())
2690 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2691 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002692 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002693 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002694 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002695 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002696 assert(!getLang().CPlusPlus &&
2697 "There's a C++-specific check for tok::identifier above");
2698 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2699 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2700 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002701 goto PastIdentifier;
2702 }
2703
2704 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002705 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002706 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002707 // Example: 'char (*X)' or 'int (*XX)(void)'
2708 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002709
2710 // If the declarator was parenthesized, we entered the declarator
2711 // scope when parsing the parenthesized declarator, then exited
2712 // the scope already. Re-enter the scope, if we need to.
2713 if (D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002714 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002715 // Change the declaration context for name lookup, until this function
2716 // is exited (and the declarator has been parsed).
2717 DeclScopeObj.EnterDeclaratorScope();
2718 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002719 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002720 // This could be something simple like "int" (in which case the declarator
2721 // portion is empty), if an abstract-declarator is allowed.
2722 D.SetIdentifier(0, Tok.getLocation());
2723 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002724 if (D.getContext() == Declarator::MemberContext)
2725 Diag(Tok, diag::err_expected_member_name_or_semi)
2726 << D.getDeclSpec().getSourceRange();
2727 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002728 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002729 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002730 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002731 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002732 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002733 }
Mike Stump11289f42009-09-09 15:08:12 +00002734
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002735 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002736 assert(D.isPastIdentifier() &&
2737 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002738
Alexis Hunt96d5c762009-11-21 08:43:09 +00002739 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002740 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002741 && isCXX0XAttributeSpecifier(true)) {
2742 SourceLocation AttrEndLoc;
2743 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2744 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2745 }
2746
Chris Lattneracd58a32006-08-06 17:24:14 +00002747 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002748 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002749 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2750 // In such a case, check if we actually have a function declarator; if it
2751 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002752 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2753 // When not in file scope, warn for ambiguous function declarators, just
2754 // in case the author intended it as a variable definition.
2755 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2756 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2757 break;
2758 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002759 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002760 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002761 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002762 } else {
2763 break;
2764 }
2765 }
2766}
2767
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002768/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2769/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002770/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002771/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2772///
2773/// direct-declarator:
2774/// '(' declarator ')'
2775/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002776/// direct-declarator '(' parameter-type-list ')'
2777/// direct-declarator '(' identifier-list[opt] ')'
2778/// [GNU] direct-declarator '(' parameter-forward-declarations
2779/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002780///
2781void Parser::ParseParenDeclarator(Declarator &D) {
2782 SourceLocation StartLoc = ConsumeParen();
2783 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002784
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002785 // Eat any attributes before we look at whether this is a grouping or function
2786 // declarator paren. If this is a grouping paren, the attribute applies to
2787 // the type being built up, for example:
2788 // int (__attribute__(()) *x)(long y)
2789 // If this ends up not being a grouping paren, the attribute applies to the
2790 // first argument, for example:
2791 // int (__attribute__(()) int x)
2792 // In either case, we need to eat any attributes to be able to determine what
2793 // sort of paren this is.
2794 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002795 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002796 bool RequiresArg = false;
2797 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002798 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002799
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002800 // We require that the argument list (if this is a non-grouping paren) be
2801 // present even if the attribute list was empty.
2802 RequiresArg = true;
2803 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002804 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002805 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002806 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2807 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002808 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002809 }
Mike Stump11289f42009-09-09 15:08:12 +00002810
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002811 // If we haven't past the identifier yet (or where the identifier would be
2812 // stored, if this is an abstract declarator), then this is probably just
2813 // grouping parens. However, if this could be an abstract-declarator, then
2814 // this could also be the start of function arguments (consider 'void()').
2815 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002816
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002817 if (!D.mayOmitIdentifier()) {
2818 // If this can't be an abstract-declarator, this *must* be a grouping
2819 // paren, because we haven't seen the identifier yet.
2820 isGrouping = true;
2821 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002822 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002823 isDeclarationSpecifier()) { // 'int(int)' is a function.
2824 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2825 // considered to be a type, not a K&R identifier-list.
2826 isGrouping = false;
2827 } else {
2828 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2829 isGrouping = true;
2830 }
Mike Stump11289f42009-09-09 15:08:12 +00002831
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002832 // If this is a grouping paren, handle:
2833 // direct-declarator: '(' declarator ')'
2834 // direct-declarator: '(' attributes declarator ')'
2835 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002836 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002837 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002838 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002839 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002840
Sebastian Redlbd150f42008-11-21 19:14:01 +00002841 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002842 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002843 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002844
2845 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002846 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002847 return;
2848 }
Mike Stump11289f42009-09-09 15:08:12 +00002849
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002850 // Okay, if this wasn't a grouping paren, it must be the start of a function
2851 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002852 // identifier (and remember where it would have been), then call into
2853 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002854 D.SetIdentifier(0, Tok.getLocation());
2855
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002856 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002857}
2858
2859/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2860/// declarator D up to a paren, which indicates that we are parsing function
2861/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002862///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002863/// If AttrList is non-null, then the caller parsed those arguments immediately
2864/// after the open paren - they should be considered to be the first argument of
2865/// a parameter. If RequiresArg is true, then the first argument of the
2866/// function is required to be present and required to not be an identifier
2867/// list.
2868///
Chris Lattneracd58a32006-08-06 17:24:14 +00002869/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002870/// parameter-type-list: [C99 6.7.5]
2871/// parameter-list
2872/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002873/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002874///
2875/// parameter-list: [C99 6.7.5]
2876/// parameter-declaration
2877/// parameter-list ',' parameter-declaration
2878///
2879/// parameter-declaration: [C99 6.7.5]
2880/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002881/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002882/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002883/// declaration-specifiers abstract-declarator[opt]
2884/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002885/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002886/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002887///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002888/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002889/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002890///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002891void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2892 AttributeList *AttrList,
2893 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002894 // lparen is already consumed!
2895 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002896
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002897 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002898 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002899 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002900 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002901 delete AttrList;
2902 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002903
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002904 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2905 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002906
2907 // cv-qualifier-seq[opt].
2908 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002909 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002910 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002911 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002912 llvm::SmallVector<TypeTy*, 2> Exceptions;
2913 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002914 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002915 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002916 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002917 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002918
2919 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002920 if (Tok.is(tok::kw_throw)) {
2921 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002922 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002923 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002924 hasAnyExceptionSpec);
2925 assert(Exceptions.size() == ExceptionRanges.size() &&
2926 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002927 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002928 }
2929
Chris Lattner371ed4e2008-04-06 06:57:35 +00002930 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002931 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002932 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002933 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002934 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002935 /*arglist*/ 0, 0,
2936 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002937 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002938 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002939 Exceptions.data(),
2940 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002941 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002942 LParenLoc, RParenLoc, D),
2943 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002944 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002945 }
2946
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002947 // Alternatively, this parameter list may be an identifier list form for a
2948 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00002949 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2950 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00002951 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002952 // K&R identifier lists can't have typedefs as identifiers, per
2953 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002954 if (RequiresArg) {
2955 Diag(Tok, diag::err_argument_required_after_attribute);
2956 delete AttrList;
2957 }
Chris Lattner9453ab82010-05-14 17:23:36 +00002958
Steve Naroffb0486722009-01-28 19:16:40 +00002959 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00002960 // normal declarators, not for abstract-declarators. Get the first
2961 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002962 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00002963 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002964
2965 // Identifier lists follow a really simple grammar: the identifiers can
2966 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2967 // identifier lists are really rare in the brave new modern world, and it
2968 // is very common for someone to typo a type in a non-k&r style list. If
2969 // we are presented with something like: "void foo(intptr x, float y)",
2970 // we don't want to start parsing the function declarator as though it is
2971 // a K&R style declarator just because intptr is an invalid type.
2972 //
2973 // To handle this, we check to see if the token after the first identifier
2974 // is a "," or ")". Only if so, do we parse it as an identifier list.
2975 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2976 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
2977 FirstTok.getIdentifierInfo(),
2978 FirstTok.getLocation(), D);
2979
2980 // If we get here, the code is invalid. Push the first identifier back
2981 // into the token stream and parse the first argument as an (invalid)
2982 // normal argument declarator.
2983 PP.EnterToken(Tok);
2984 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002985 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002986 }
Mike Stump11289f42009-09-09 15:08:12 +00002987
Chris Lattner371ed4e2008-04-06 06:57:35 +00002988 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002989
Chris Lattner371ed4e2008-04-06 06:57:35 +00002990 // Build up an array of information about the parsed arguments.
2991 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002992
2993 // Enter function-declaration scope, limiting any declarators to the
2994 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002995 ParseScope PrototypeScope(this,
2996 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002997
Chris Lattner371ed4e2008-04-06 06:57:35 +00002998 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002999 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003000 while (1) {
3001 if (Tok.is(tok::ellipsis)) {
3002 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003003 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003004 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003005 }
Mike Stump11289f42009-09-09 15:08:12 +00003006
Chris Lattner371ed4e2008-04-06 06:57:35 +00003007 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003008
Chris Lattner371ed4e2008-04-06 06:57:35 +00003009 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003010 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003011 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003012
3013 // If the caller parsed attributes for the first argument, add them now.
3014 if (AttrList) {
3015 DS.AddAttributes(AttrList);
3016 AttrList = 0; // Only apply the attributes to the first parameter.
3017 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003018 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003019
Chris Lattner371ed4e2008-04-06 06:57:35 +00003020 // Parse the declarator. This is "PrototypeContext", because we must
3021 // accept either 'declarator' or 'abstract-declarator' here.
3022 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3023 ParseDeclarator(ParmDecl);
3024
3025 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003026 if (Tok.is(tok::kw___attribute)) {
3027 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003028 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003029 ParmDecl.AddAttributes(AttrList, Loc);
3030 }
Mike Stump11289f42009-09-09 15:08:12 +00003031
Chris Lattner371ed4e2008-04-06 06:57:35 +00003032 // Remember this parsed parameter in ParamInfo.
3033 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003034
Douglas Gregor4d87df52008-12-16 21:30:33 +00003035 // DefArgToks is used when the parsing of default arguments needs
3036 // to be delayed.
3037 CachedTokens *DefArgToks = 0;
3038
Chris Lattner371ed4e2008-04-06 06:57:35 +00003039 // If no parameter was specified, verify that *something* was specified,
3040 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003041 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3042 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003043 // Completely missing, emit error.
3044 Diag(DSStart, diag::err_missing_param);
3045 } else {
3046 // Otherwise, we have something. Add it and let semantic analysis try
3047 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003048
Chris Lattner371ed4e2008-04-06 06:57:35 +00003049 // Inform the actions module about the parameter declarator, so it gets
3050 // added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003051 DeclPtrTy Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003052
3053 // Parse the default argument, if any. We parse the default
3054 // arguments in all dialects; the semantic analysis in
3055 // ActOnParamDefaultArgument will reject the default argument in
3056 // C.
3057 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003058 SourceLocation EqualLoc = Tok.getLocation();
3059
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003060 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003061 if (D.getContext() == Declarator::MemberContext) {
3062 // If we're inside a class definition, cache the tokens
3063 // corresponding to the default argument. We'll actually parse
3064 // them when we see the end of the class definition.
3065 // FIXME: Templates will require something similar.
3066 // FIXME: Can we use a smart pointer for Toks?
3067 DefArgToks = new CachedTokens;
3068
Mike Stump11289f42009-09-09 15:08:12 +00003069 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003070 /*StopAtSemi=*/true,
3071 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003072 delete DefArgToks;
3073 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003074 Actions.ActOnParamDefaultArgumentError(Param);
3075 } else
Mike Stump11289f42009-09-09 15:08:12 +00003076 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003077 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003078 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003079 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003080 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003081
Douglas Gregor4d87df52008-12-16 21:30:33 +00003082 OwningExprResult DefArgResult(ParseAssignmentExpression());
3083 if (DefArgResult.isInvalid()) {
3084 Actions.ActOnParamDefaultArgumentError(Param);
3085 SkipUntil(tok::comma, tok::r_paren, true, true);
3086 } else {
3087 // Inform the actions module about the default argument
3088 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00003089 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00003090 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003091 }
3092 }
Mike Stump11289f42009-09-09 15:08:12 +00003093
3094 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3095 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003096 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003097 }
3098
3099 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003100 if (Tok.isNot(tok::comma)) {
3101 if (Tok.is(tok::ellipsis)) {
3102 IsVariadic = true;
3103 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3104
3105 if (!getLang().CPlusPlus) {
3106 // We have ellipsis without a preceding ',', which is ill-formed
3107 // in C. Complain and provide the fix.
3108 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003109 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003110 }
3111 }
3112
3113 break;
3114 }
Mike Stump11289f42009-09-09 15:08:12 +00003115
Chris Lattner371ed4e2008-04-06 06:57:35 +00003116 // Consume the comma.
3117 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003118 }
Mike Stump11289f42009-09-09 15:08:12 +00003119
Chris Lattner371ed4e2008-04-06 06:57:35 +00003120 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003121 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003122
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003123 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003124 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3125 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003126
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003127 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003128 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003129 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003130 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00003131 llvm::SmallVector<TypeTy*, 2> Exceptions;
3132 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003133
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003134 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003135 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003136 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003137 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003138 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003139
3140 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003141 if (Tok.is(tok::kw_throw)) {
3142 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003143 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003144 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003145 hasAnyExceptionSpec);
3146 assert(Exceptions.size() == ExceptionRanges.size() &&
3147 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003148 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003149 }
3150
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003151 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003152 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003153 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003154 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003155 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003156 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003157 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003158 Exceptions.data(),
3159 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003160 Exceptions.size(),
3161 LParenLoc, RParenLoc, D),
3162 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003163}
Chris Lattneracd58a32006-08-06 17:24:14 +00003164
Chris Lattner6c940e62008-04-06 06:34:08 +00003165/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3166/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003167/// first identifier has already been consumed, and the current token is the
3168/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003169///
3170/// identifier-list: [C99 6.7.5]
3171/// identifier
3172/// identifier-list ',' identifier
3173///
3174void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003175 IdentifierInfo *FirstIdent,
3176 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003177 Declarator &D) {
3178 // Build up an array of information about the parsed arguments.
3179 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3180 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003181
Chris Lattner6c940e62008-04-06 06:34:08 +00003182 // If there was no identifier specified for the declarator, either we are in
3183 // an abstract-declarator, or we are in a parameter declarator which was found
3184 // to be abstract. In abstract-declarators, identifier lists are not valid:
3185 // diagnose this.
3186 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003187 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003188
Chris Lattner9453ab82010-05-14 17:23:36 +00003189 // The first identifier was already read, and is known to be the first
3190 // identifier in the list. Remember this identifier in ParamInfo.
3191 ParamsSoFar.insert(FirstIdent);
3192 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00003193 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00003194
Chris Lattner6c940e62008-04-06 06:34:08 +00003195 while (Tok.is(tok::comma)) {
3196 // Eat the comma.
3197 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003198
Chris Lattner9186f552008-04-06 06:39:19 +00003199 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003200 if (Tok.isNot(tok::identifier)) {
3201 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003202 SkipUntil(tok::r_paren);
3203 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003204 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003205
Chris Lattner6c940e62008-04-06 06:34:08 +00003206 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003207
3208 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003209 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003210 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003211
Chris Lattner6c940e62008-04-06 06:34:08 +00003212 // Verify that the argument identifier has not already been mentioned.
3213 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003214 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003215 } else {
3216 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003217 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003218 Tok.getLocation(),
3219 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00003220 }
Mike Stump11289f42009-09-09 15:08:12 +00003221
Chris Lattner6c940e62008-04-06 06:34:08 +00003222 // Eat the identifier.
3223 ConsumeToken();
3224 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003225
3226 // If we have the closing ')', eat it and we're done.
3227 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3228
Chris Lattner9186f552008-04-06 06:39:19 +00003229 // Remember that we parsed a function type, and remember the attributes. This
3230 // function type is always a K&R style function type, which is not varargs and
3231 // has no prototype.
3232 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003233 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003234 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003235 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003236 /*exception*/false,
3237 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003238 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003239 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003240}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003241
Chris Lattnere8074e62006-08-06 18:30:15 +00003242/// [C90] direct-declarator '[' constant-expression[opt] ']'
3243/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3244/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3245/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3246/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3247void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003248 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003249
Chris Lattner84a11622008-12-18 07:27:21 +00003250 // C array syntax has many features, but by-far the most common is [] and [4].
3251 // This code does a fast path to handle some of the most obvious cases.
3252 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003253 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003254 //FIXME: Use these
3255 CXX0XAttributeList Attr;
3256 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3257 Attr = ParseCXX0XAttributes();
3258 }
3259
Chris Lattner84a11622008-12-18 07:27:21 +00003260 // Remember that we parsed the empty array type.
3261 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00003262 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3263 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003264 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003265 return;
3266 } else if (Tok.getKind() == tok::numeric_constant &&
3267 GetLookAheadToken(1).is(tok::r_square)) {
3268 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00003269 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003270 ConsumeToken();
3271
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003272 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003273 //FIXME: Use these
3274 CXX0XAttributeList Attr;
3275 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3276 Attr = ParseCXX0XAttributes();
3277 }
Chris Lattner84a11622008-12-18 07:27:21 +00003278
3279 // If there was an error parsing the assignment-expression, recover.
3280 if (ExprRes.isInvalid())
3281 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003282
Chris Lattner84a11622008-12-18 07:27:21 +00003283 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003284 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3285 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003286 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003287 return;
3288 }
Mike Stump11289f42009-09-09 15:08:12 +00003289
Chris Lattnere8074e62006-08-06 18:30:15 +00003290 // If valid, this location is the position where we read the 'static' keyword.
3291 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003292 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003293 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003294
Chris Lattnere8074e62006-08-06 18:30:15 +00003295 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003296 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003297 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003298 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003299
Chris Lattnere8074e62006-08-06 18:30:15 +00003300 // If we haven't already read 'static', check to see if there is one after the
3301 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003302 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003303 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003304
Chris Lattnere8074e62006-08-06 18:30:15 +00003305 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003306 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00003307 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00003308
Chris Lattner521ff2b2008-04-06 05:26:30 +00003309 // Handle the case where we have '[*]' as the array size. However, a leading
3310 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3311 // the the token after the star is a ']'. Since stars in arrays are
3312 // infrequent, use of lookahead is not costly here.
3313 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003314 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003315
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003316 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003317 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003318 StaticLoc = SourceLocation(); // Drop the static.
3319 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003320 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003321 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003322 // Note, in C89, this production uses the constant-expr production instead
3323 // of assignment-expr. The only difference is that assignment-expr allows
3324 // things like '=' and '*='. Sema rejects these in C89 mode because they
3325 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003326
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003327 // Parse the constant-expression or assignment-expression now (depending
3328 // on dialect).
3329 if (getLang().CPlusPlus)
3330 NumElements = ParseConstantExpression();
3331 else
3332 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003333 }
Mike Stump11289f42009-09-09 15:08:12 +00003334
Chris Lattner62591722006-08-12 18:40:58 +00003335 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003336 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003337 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003338 // If the expression was invalid, skip it.
3339 SkipUntil(tok::r_square);
3340 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003341 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003342
3343 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3344
Alexis Hunt96d5c762009-11-21 08:43:09 +00003345 //FIXME: Use these
3346 CXX0XAttributeList Attr;
3347 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3348 Attr = ParseCXX0XAttributes();
3349 }
3350
Chris Lattner84a11622008-12-18 07:27:21 +00003351 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003352 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3353 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003354 NumElements.release(),
3355 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003356 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003357}
3358
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003359/// [GNU] typeof-specifier:
3360/// typeof ( expressions )
3361/// typeof ( type-name )
3362/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003363///
3364void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003365 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003366 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003367 SourceLocation StartLoc = ConsumeToken();
3368
John McCalle8595032010-01-13 20:03:27 +00003369 const bool hasParens = Tok.is(tok::l_paren);
3370
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003371 bool isCastExpr;
3372 TypeTy *CastTy;
3373 SourceRange CastRange;
3374 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3375 isCastExpr,
3376 CastTy,
3377 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003378 if (hasParens)
3379 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003380
3381 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003382 // FIXME: Not accurate, the range gets one token more than it should.
3383 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003384 else
3385 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003386
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003387 if (isCastExpr) {
3388 if (!CastTy) {
3389 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003390 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003391 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003392
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003393 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003394 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003395 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3396 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003397 DiagID, CastTy))
3398 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003399 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003400 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003401
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003402 // If we get here, the operand to the typeof was an expresion.
3403 if (Operand.isInvalid()) {
3404 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003405 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003406 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003407
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003408 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003409 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003410 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3411 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003412 DiagID, Operand.release()))
3413 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003414}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003415
3416
3417/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3418/// from TryAltiVecVectorToken.
3419bool Parser::TryAltiVecVectorTokenOutOfLine() {
3420 Token Next = NextToken();
3421 switch (Next.getKind()) {
3422 default: return false;
3423 case tok::kw_short:
3424 case tok::kw_long:
3425 case tok::kw_signed:
3426 case tok::kw_unsigned:
3427 case tok::kw_void:
3428 case tok::kw_char:
3429 case tok::kw_int:
3430 case tok::kw_float:
3431 case tok::kw_double:
3432 case tok::kw_bool:
3433 case tok::kw___pixel:
3434 Tok.setKind(tok::kw___vector);
3435 return true;
3436 case tok::identifier:
3437 if (Next.getIdentifierInfo() == Ident_pixel) {
3438 Tok.setKind(tok::kw___vector);
3439 return true;
3440 }
3441 return false;
3442 }
3443}
3444
3445bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3446 const char *&PrevSpec, unsigned &DiagID,
3447 bool &isInvalid) {
3448 if (Tok.getIdentifierInfo() == Ident_vector) {
3449 Token Next = NextToken();
3450 switch (Next.getKind()) {
3451 case tok::kw_short:
3452 case tok::kw_long:
3453 case tok::kw_signed:
3454 case tok::kw_unsigned:
3455 case tok::kw_void:
3456 case tok::kw_char:
3457 case tok::kw_int:
3458 case tok::kw_float:
3459 case tok::kw_double:
3460 case tok::kw_bool:
3461 case tok::kw___pixel:
3462 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3463 return true;
3464 case tok::identifier:
3465 if (Next.getIdentifierInfo() == Ident_pixel) {
3466 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3467 return true;
3468 }
3469 break;
3470 default:
3471 break;
3472 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003473 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003474 DS.isTypeAltiVecVector()) {
3475 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3476 return true;
3477 }
3478 return false;
3479}
3480