blob: cff35b72c45a508b398e2f2ea7dcdd543ec31781 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnereaaebc72009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Sean Huntbbd37c62009-11-21 08:43:09 +000048/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +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
63/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
67///
68/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000073///
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-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
Reid Spencer5f016e22007-07-11 17:01:13 +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.
83
Sean Huntbbd37c62009-11-21 08:43:09 +000084AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner04d66662007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000113 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner04d66662007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // __attribute__(( mode(byte) ))
123 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Chris Lattner04d66662007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner04d66662007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 }
154 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // __attribute__(( nonnull() ))
159 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begeman6f3d8382009-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();
Sean Huntbbd37c62009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 }
Chris Lattner04d66662007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 CurrAttr);
211 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000212 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
214 }
215 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
218 }
219 }
220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
229 return CurrAttr;
230}
231
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000243
Steve Narofff59e17e2008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-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();
Sean Huntbbd37c62009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-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 {
Sean Huntbbd37c62009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000273 return CurrAttr;
274}
275
276AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
277 // Treat these like attributes
278 // FIXME: Allow Sema to distinguish between these and real attributes!
279 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
280 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
281 Tok.is(tok::kw___w64)) {
282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner97144fc2009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Chris Lattner682bf922009-03-29 16:50:03 +0000312 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000314 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000315 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000320 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000321 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000325 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000326 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000327 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000329 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000330 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000335 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000336 default:
Sean Huntbbd37c62009-11-21 08:43:09 +0000337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000338 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000339
Chris Lattner682bf922009-03-29 16:50:03 +0000340 // This routine returns a DeclGroup, if the thing we parsed only contains a
341 // single decl, convert it now.
342 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000343}
344
345/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
346/// declaration-specifiers init-declarator-list[opt] ';'
347///[C90/C++]init-declarator-list ';' [TODO]
348/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000349///
350/// If RequireSemi is false, this does not check for a ';' at the end of the
351/// declaration.
352Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000353 SourceLocation &DeclEnd,
354 AttributeList *Attr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000356 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000357 if (Attr)
358 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000359 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
360 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
363 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000364 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000366 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000367 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000368 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
John McCalld8ac0572009-11-03 19:26:08 +0000371 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
372 &DeclEnd);
373 return DG;
374}
Mike Stump1eb44332009-09-09 15:08:12 +0000375
John McCalld8ac0572009-11-03 19:26:08 +0000376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000385 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000386
John McCalld8ac0572009-11-03 19:26:08 +0000387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
John McCalld8ac0572009-11-03 19:26:08 +0000396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000420 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000428 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000449 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000470}
471
Douglas Gregor1426e532009-05-12 21:31:51 +0000472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000476///
Reid Spencer5f016e22007-07-11 17:01:13 +0000477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000492///
Douglas Gregore542c862009-06-23 23:11:28 +0000493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregor1426e532009-05-12 21:31:51 +0000504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor1426e532009-05-12 21:31:51 +0000508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000512 D.AddAttributes(AttrList, Loc);
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregor1426e532009-05-12 21:31:51 +0000515 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor1426e532009-05-12 21:31:51 +0000547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
John McCall731ad842009-12-19 09:28:58 +0000554 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
555 EnterScope(0);
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000556 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000557 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000558
Douglas Gregor1426e532009-05-12 21:31:51 +0000559 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000560
John McCall731ad842009-12-19 09:28:58 +0000561 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000562 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000563 ExitScope();
564 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000565
Douglas Gregor1426e532009-05-12 21:31:51 +0000566 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000567 SkipUntil(tok::comma, true, true);
568 Actions.ActOnInitializerError(ThisDecl);
569 } else
570 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000571 }
572 } else if (Tok.is(tok::l_paren)) {
573 // Parse C++ direct initializer: '(' expression-list ')'
574 SourceLocation LParenLoc = ConsumeParen();
575 ExprVector Exprs(Actions);
576 CommaLocsTy CommaLocs;
577
Douglas Gregorb4debae2009-12-22 17:47:17 +0000578 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
579 EnterScope(0);
580 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
581 }
582
Douglas Gregor1426e532009-05-12 21:31:51 +0000583 if (ParseExpressionList(Exprs, CommaLocs)) {
584 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000585
586 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
587 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
588 ExitScope();
589 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000590 } else {
591 // Match the ')'.
592 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
593
594 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
595 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000596
597 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
598 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
599 ExitScope();
600 }
601
Douglas Gregor1426e532009-05-12 21:31:51 +0000602 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
603 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000604 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000605 }
606 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000607 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000608 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
609 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000610 }
611
612 return ThisDecl;
613}
614
Reid Spencer5f016e22007-07-11 17:01:13 +0000615/// ParseSpecifierQualifierList
616/// specifier-qualifier-list:
617/// type-specifier specifier-qualifier-list[opt]
618/// type-qualifier specifier-qualifier-list[opt]
619/// [GNU] attributes specifier-qualifier-list[opt]
620///
621void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
622 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
623 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // Validate declspec for type-name.
627 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000628 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
629 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 // Issue diagnostic and remove storage class if present.
633 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
634 if (DS.getStorageClassSpecLoc().isValid())
635 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
636 else
637 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
638 DS.ClearStorageClassSpecs();
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // Issue diagnostic and remove function specfier if present.
642 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000643 if (DS.isInlineSpecified())
644 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
645 if (DS.isVirtualSpecified())
646 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
647 if (DS.isExplicitSpecified())
648 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 DS.ClearFunctionSpecs();
650 }
651}
652
Chris Lattnerc199ab32009-04-12 20:42:31 +0000653/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
654/// specified token is valid after the identifier in a declarator which
655/// immediately follows the declspec. For example, these things are valid:
656///
657/// int x [ 4]; // direct-declarator
658/// int x ( int y); // direct-declarator
659/// int(int x ) // direct-declarator
660/// int x ; // simple-declaration
661/// int x = 17; // init-declarator-list
662/// int x , y; // init-declarator-list
663/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000664/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000665/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000666///
667/// This is not, because 'x' does not immediately follow the declspec (though
668/// ')' happens to be valid anyway).
669/// int (x)
670///
671static bool isValidAfterIdentifierInDeclarator(const Token &T) {
672 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
673 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000674 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000675}
676
Chris Lattnere40c2952009-04-14 21:34:55 +0000677
678/// ParseImplicitInt - This method is called when we have an non-typename
679/// identifier in a declspec (which normally terminates the decl spec) when
680/// the declspec has no type specifier. In this case, the declspec is either
681/// malformed or is "implicit int" (in K&R and C89).
682///
683/// This method handles diagnosing this prettily and returns false if the
684/// declspec is done being processed. If it recovers and thinks there may be
685/// other pieces of declspec after it, it returns true.
686///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000687bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000688 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000689 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000690 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattnere40c2952009-04-14 21:34:55 +0000692 SourceLocation Loc = Tok.getLocation();
693 // If we see an identifier that is not a type name, we normally would
694 // parse it as the identifer being declared. However, when a typename
695 // is typo'd or the definition is not included, this will incorrectly
696 // parse the typename as the identifier name and fall over misparsing
697 // later parts of the diagnostic.
698 //
699 // As such, we try to do some look-ahead in cases where this would
700 // otherwise be an "implicit-int" case to see if this is invalid. For
701 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
702 // an identifier with implicit int, we'd get a parse error because the
703 // next token is obviously invalid for a type. Parse these as a case
704 // with an invalid type specifier.
705 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Chris Lattnere40c2952009-04-14 21:34:55 +0000707 // Since we know that this either implicit int (which is rare) or an
708 // error, we'd do lookahead to try to do better recovery.
709 if (isValidAfterIdentifierInDeclarator(NextToken())) {
710 // If this token is valid for implicit int, e.g. "static x = 4", then
711 // we just avoid eating the identifier, so it will be parsed as the
712 // identifier in the declarator.
713 return false;
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattnere40c2952009-04-14 21:34:55 +0000716 // Otherwise, if we don't consume this token, we are going to emit an
717 // error anyway. Try to recover from various common problems. Check
718 // to see if this was a reference to a tag name without a tag specified.
719 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000720 //
721 // C++ doesn't need this, and isTagName doesn't take SS.
722 if (SS == 0) {
723 const char *TagName = 0;
724 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Chris Lattnere40c2952009-04-14 21:34:55 +0000726 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
727 default: break;
728 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
729 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
730 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
731 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattnerf4382f52009-04-14 22:17:06 +0000734 if (TagName) {
735 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000736 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Chris Lattnerf4382f52009-04-14 22:17:06 +0000737 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Chris Lattnerf4382f52009-04-14 22:17:06 +0000739 // Parse this as a tag as if the missing tag were present.
740 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000741 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000742 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000743 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000744 return true;
745 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Douglas Gregora786fdb2009-10-13 23:27:22 +0000748 // This is almost certainly an invalid type name. Let the action emit a
749 // diagnostic and attempt to recover.
750 Action::TypeTy *T = 0;
751 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
752 CurScope, SS, T)) {
753 // The action emitted a diagnostic, so we don't have to.
754 if (T) {
755 // The action has suggested that the type T could be used. Set that as
756 // the type in the declaration specifiers, consume the would-be type
757 // name token, and we're done.
758 const char *PrevSpec;
759 unsigned DiagID;
760 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
761 false);
762 DS.SetRangeEnd(Tok.getLocation());
763 ConsumeToken();
764
765 // There may be other declaration specifiers after this.
766 return true;
767 }
768
769 // Fall through; the action had no suggestion for us.
770 } else {
771 // The action did not emit a diagnostic, so emit one now.
772 SourceRange R;
773 if (SS) R = SS->getRange();
774 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Douglas Gregora786fdb2009-10-13 23:27:22 +0000777 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000778 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000779 unsigned DiagID;
780 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000781 DS.SetRangeEnd(Tok.getLocation());
782 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattnere40c2952009-04-14 21:34:55 +0000784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
785 // avoid rippling error messages on subsequent uses of the same type,
786 // could be useful if #include was forgotten.
787 return false;
788}
789
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000790/// \brief Determine the declaration specifier context from the declarator
791/// context.
792///
793/// \param Context the declarator context, which is one of the
794/// Declarator::TheContext enumerator values.
795Parser::DeclSpecContext
796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
797 if (Context == Declarator::MemberContext)
798 return DSC_class;
799 if (Context == Declarator::FileContext)
800 return DSC_top_level;
801 return DSC_normal;
802}
803
Reid Spencer5f016e22007-07-11 17:01:13 +0000804/// ParseDeclarationSpecifiers
805/// declaration-specifiers: [C99 6.7]
806/// storage-class-specifier declaration-specifiers[opt]
807/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000808/// [C99] function-specifier declaration-specifiers[opt]
809/// [GNU] attributes declaration-specifiers[opt]
810///
811/// storage-class-specifier: [C99 6.7.1]
812/// 'typedef'
813/// 'extern'
814/// 'static'
815/// 'auto'
816/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000817/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000818/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000819/// function-specifier: [C99 6.7.4]
820/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000821/// [C++] 'virtual'
822/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000823/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000824/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000825
Reid Spencer5f016e22007-07-11 17:01:13 +0000826///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000827void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000828 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000829 AccessSpecifier AS,
830 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000831 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000832 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
833 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
834 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
835 : Action::CCC_Template;
836 else if (DSContext == DSC_class)
837 CCC = Action::CCC_Class;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000838 else if (ObjCImpDecl)
839 CCC = Action::CCC_ObjCImplementation;
840
Douglas Gregor01dfea02010-01-10 23:08:15 +0000841 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor791215b2009-09-21 20:51:25 +0000842 ConsumeToken();
843 }
844
Chris Lattner81c018d2008-03-13 06:29:04 +0000845 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000847 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000849 unsigned DiagID = 0;
850
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000854 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000855 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 // If this is not a declaration specifier token, we're done reading decl
857 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000858 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattner5e02c472009-01-05 00:07:25 +0000861 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000862 // C++ scope specifier. Annotate and loop, or bail out on error.
863 if (TryAnnotateCXXScopeToken(true)) {
864 if (!DS.hasTypeSpecifier())
865 DS.SetTypeSpecError();
866 goto DoneWithDeclSpec;
867 }
John McCall2e0a7152010-03-01 18:20:46 +0000868 if (Tok.is(tok::coloncolon)) // ::new or ::delete
869 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000870 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000871
872 case tok::annot_cxxscope: {
873 if (DS.hasTypeSpecifier())
874 goto DoneWithDeclSpec;
875
John McCallaa87d332009-12-12 11:40:51 +0000876 CXXScopeSpec SS;
877 SS.setScopeRep(Tok.getAnnotationValue());
878 SS.setRange(Tok.getAnnotationRange());
879
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000880 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000881 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000882 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000883 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000884 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000885 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000886
887 // C++ [class.qual]p2:
888 // In a lookup in which the constructor is an acceptable lookup
889 // result and the nested-name-specifier nominates a class C:
890 //
891 // - if the name specified after the
892 // nested-name-specifier, when looked up in C, is the
893 // injected-class-name of C (Clause 9), or
894 //
895 // - if the name specified after the nested-name-specifier
896 // is the same as the identifier or the
897 // simple-template-id's template-name in the last
898 // component of the nested-name-specifier,
899 //
900 // the name is instead considered to name the constructor of
901 // class C.
902 //
903 // Thus, if the template-name is actually the constructor
904 // name, then the code is ill-formed; this interpretation is
905 // reinforced by the NAD status of core issue 635.
906 TemplateIdAnnotation *TemplateId
907 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
908 if (DSContext == DSC_top_level && TemplateId->Name &&
909 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
910 if (isConstructorDeclarator()) {
911 // The user meant this to be an out-of-line constructor
912 // definition, but template arguments are not allowed
913 // there. Just allow this as a constructor; we'll
914 // complain about it later.
915 goto DoneWithDeclSpec;
916 }
917
918 // The user meant this to name a type, but it actually names
919 // a constructor with some extraneous template
920 // arguments. Complain, then parse it as a type as the user
921 // intended.
922 Diag(TemplateId->TemplateNameLoc,
923 diag::err_out_of_line_template_id_names_constructor)
924 << TemplateId->Name;
925 }
926
John McCallaa87d332009-12-12 11:40:51 +0000927 DS.getTypeSpecScope() = SS;
928 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000929 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000930 "ParseOptionalCXXScopeSpecifier not working");
931 AnnotateTemplateIdTokenAsType(&SS);
932 continue;
933 }
934
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000935 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000936 DS.getTypeSpecScope() = SS;
937 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000938 if (Tok.getAnnotationValue())
939 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
940 PrevSpec, DiagID,
941 Tok.getAnnotationValue());
942 else
943 DS.SetTypeSpecError();
944 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
945 ConsumeToken(); // The typename
946 }
947
Douglas Gregor9135c722009-03-25 15:40:00 +0000948 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000949 goto DoneWithDeclSpec;
950
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000951 // If we're in a context where the identifier could be a class name,
952 // check whether this is a constructor declaration.
953 if (DSContext == DSC_top_level &&
954 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
955 &SS)) {
956 if (isConstructorDeclarator())
957 goto DoneWithDeclSpec;
958
959 // As noted in C++ [class.qual]p2 (cited above), when the name
960 // of the class is qualified in a context where it could name
961 // a constructor, its a constructor name. However, we've
962 // looked at the declarator, and the user probably meant this
963 // to be a type. Complain that it isn't supposed to be treated
964 // as a type, then proceed to parse it as a type.
965 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
966 << Next.getIdentifierInfo();
967 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000968
Douglas Gregorb696ea32009-02-04 17:00:24 +0000969 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
970 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000971
Chris Lattnerf4382f52009-04-14 22:17:06 +0000972 // If the referenced identifier is not a type, then this declspec is
973 // erroneous: We already checked about that it has no type specifier, and
974 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000975 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000976 if (TypeRep == 0) {
977 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000978 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000979 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
John McCallaa87d332009-12-12 11:40:51 +0000982 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000983 ConsumeToken(); // The C++ scope.
984
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000986 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000987 if (isInvalid)
988 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000990 DS.SetRangeEnd(Tok.getLocation());
991 ConsumeToken(); // The typename.
992
993 continue;
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner80d0c892009-01-21 19:48:37 +0000996 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000997 if (Tok.getAnnotationValue())
998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000999 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001000 else
1001 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +00001002 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1003 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattner80d0c892009-01-21 19:48:37 +00001005 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1006 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1007 // Objective-C interface. If we don't have Objective-C or a '<', this is
1008 // just a normal reference to a typedef name.
1009 if (!Tok.is(tok::less) || !getLang().ObjC1)
1010 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001012 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001013 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001014 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1015 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1016 LAngleLoc, EndProtoLoc);
1017 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1018 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattner80d0c892009-01-21 19:48:37 +00001020 DS.SetRangeEnd(EndProtoLoc);
1021 continue;
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Chris Lattner3bd934a2008-07-26 01:18:38 +00001024 // typedef-name
1025 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001026 // In C++, check to see if this is a scope specifier like foo::bar::, if
1027 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001028 if (getLang().CPlusPlus) {
1029 if (TryAnnotateCXXScopeToken(true)) {
1030 if (!DS.hasTypeSpecifier())
1031 DS.SetTypeSpecError();
1032 goto DoneWithDeclSpec;
1033 }
1034 if (!Tok.is(tok::identifier))
1035 continue;
1036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattner3bd934a2008-07-26 01:18:38 +00001038 // This identifier can only be a typedef name if we haven't already seen
1039 // a type-specifier. Without this check we misparse:
1040 // typedef int X; struct Y { short X; }; as 'short int'.
1041 if (DS.hasTypeSpecifier())
1042 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001043
John Thompson82287d12010-02-05 00:12:22 +00001044 // Check for need to substitute AltiVec keyword tokens.
1045 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1046 break;
1047
Chris Lattner3bd934a2008-07-26 01:18:38 +00001048 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +00001049 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001050 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001051
Chris Lattnerc199ab32009-04-12 20:42:31 +00001052 // If this is not a typedef name, don't parse it as part of the declspec,
1053 // it must be an implicit int or an error.
1054 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001055 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001056 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001057 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001058
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001059 // If we're in a context where the identifier could be a class name,
1060 // check whether this is a constructor declaration.
1061 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump1eb44332009-09-09 15:08:12 +00001062 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001063 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001064 goto DoneWithDeclSpec;
1065
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001067 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001068 if (isInvalid)
1069 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattner3bd934a2008-07-26 01:18:38 +00001071 DS.SetRangeEnd(Tok.getLocation());
1072 ConsumeToken(); // The identifier
1073
1074 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1075 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1076 // Objective-C interface. If we don't have Objective-C or a '<', this is
1077 // just a normal reference to a typedef name.
1078 if (!Tok.is(tok::less) || !getLang().ObjC1)
1079 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001081 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001082 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001083 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1084 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1085 LAngleLoc, EndProtoLoc);
1086 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1087 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner3bd934a2008-07-26 01:18:38 +00001089 DS.SetRangeEnd(EndProtoLoc);
1090
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001091 // Need to support trailing type qualifiers (e.g. "id<p> const").
1092 // If a type specifier follows, it will be diagnosed elsewhere.
1093 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001094 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001095
1096 // type-name
1097 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001098 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001099 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001100 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001101 // This template-id does not refer to a type name, so we're
1102 // done with the type-specifiers.
1103 goto DoneWithDeclSpec;
1104 }
1105
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001106 // If we're in a context where the template-id could be a
1107 // constructor name or specialization, check whether this is a
1108 // constructor declaration.
1109 if (getLang().CPlusPlus && DSContext == DSC_class &&
1110 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1111 isConstructorDeclarator())
1112 goto DoneWithDeclSpec;
1113
Douglas Gregor39a8de12009-02-25 19:37:18 +00001114 // Turn the template-id annotation token into a type annotation
1115 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001116 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001117 continue;
1118 }
1119
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 // GNU attributes support.
1121 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001122 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001124
1125 // Microsoft declspec support.
1126 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001127 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001128 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Steve Naroff239f0732008-12-25 14:16:32 +00001130 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001131 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001132 // FIXME: Add handling here!
1133 break;
1134
1135 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001136 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001137 case tok::kw___cdecl:
1138 case tok::kw___stdcall:
1139 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001140 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1141 continue;
1142
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 // storage-class-specifier
1144 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001145 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1146 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 break;
1148 case tok::kw_extern:
1149 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001150 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001151 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1152 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001154 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001155 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001156 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001157 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 case tok::kw_static:
1159 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001160 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001161 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1162 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 break;
1164 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001165 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1167 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001168 else
John McCallfec54012009-08-03 20:12:06 +00001169 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1170 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 break;
1172 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001173 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1174 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001176 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001177 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1178 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001179 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001181 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 // function-specifier
1185 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001186 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001188 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001189 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001190 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001191 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001192 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001193 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001194
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001195 // friend
1196 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001197 if (DSContext == DSC_class)
1198 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1199 else {
1200 PrevSpec = ""; // not actually used by the diagnostic
1201 DiagID = diag::err_friend_invalid_in_context;
1202 isInvalid = true;
1203 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001204 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Sebastian Redl2ac67232009-11-05 15:47:02 +00001206 // constexpr
1207 case tok::kw_constexpr:
1208 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1209 break;
1210
Chris Lattner80d0c892009-01-21 19:48:37 +00001211 // type-specifier
1212 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001213 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1214 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001215 break;
1216 case tok::kw_long:
1217 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001218 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1219 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001220 else
John McCallfec54012009-08-03 20:12:06 +00001221 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1222 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001223 break;
1224 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001225 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1226 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001227 break;
1228 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001229 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1230 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001231 break;
1232 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001233 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1234 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001235 break;
1236 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001237 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1238 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001239 break;
1240 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001241 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1242 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001243 break;
1244 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001245 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1246 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001247 break;
1248 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1250 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001251 break;
1252 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1254 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001255 break;
1256 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001257 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1258 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001259 break;
1260 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001261 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1262 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001263 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001264 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001265 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1266 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001267 break;
1268 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1270 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001271 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001272 case tok::kw_bool:
1273 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001274 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1275 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001276 break;
1277 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1279 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001280 break;
1281 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001282 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1283 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001284 break;
1285 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001286 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1287 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001288 break;
John Thompson82287d12010-02-05 00:12:22 +00001289 case tok::kw___vector:
1290 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1291 break;
1292 case tok::kw___pixel:
1293 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1294 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001295
1296 // class-specifier:
1297 case tok::kw_class:
1298 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001299 case tok::kw_union: {
1300 tok::TokenKind Kind = Tok.getKind();
1301 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001302 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001303 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001304 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001305
1306 // enum-specifier:
1307 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001308 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001309 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001310 continue;
1311
1312 // cv-qualifier:
1313 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1315 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001316 break;
1317 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001318 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1319 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001320 break;
1321 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1323 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001324 break;
1325
Douglas Gregord57959a2009-03-27 23:10:48 +00001326 // C++ typename-specifier:
1327 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001328 if (TryAnnotateTypeOrScopeToken()) {
1329 DS.SetTypeSpecError();
1330 goto DoneWithDeclSpec;
1331 }
1332 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001333 continue;
1334 break;
1335
Chris Lattner80d0c892009-01-21 19:48:37 +00001336 // GNU typeof support.
1337 case tok::kw_typeof:
1338 ParseTypeofSpecifier(DS);
1339 continue;
1340
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001341 case tok::kw_decltype:
1342 ParseDecltypeSpecifier(DS);
1343 continue;
1344
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001345 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001346 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001347 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1348 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001349 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001350 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Chris Lattnerbce61352008-07-26 00:20:22 +00001352 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001353 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001354 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001355 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1356 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1357 LAngleLoc, EndProtoLoc);
1358 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1359 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001360 DS.SetRangeEnd(EndProtoLoc);
1361
Chris Lattner1ab3b962008-11-18 07:48:38 +00001362 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001363 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001364 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001365 // Need to support trailing type qualifiers (e.g. "id<p> const").
1366 // If a type specifier follows, it will be diagnosed elsewhere.
1367 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001368 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001369 }
John McCallfec54012009-08-03 20:12:06 +00001370 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 if (isInvalid) {
1372 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001373 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001374 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001376 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 ConsumeToken();
1378 }
1379}
Douglas Gregoradcac882008-12-01 23:54:00 +00001380
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001381/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001382/// primarily follow the C++ grammar with additions for C99 and GNU,
1383/// which together subsume the C grammar. Note that the C++
1384/// type-specifier also includes the C type-qualifier (for const,
1385/// volatile, and C99 restrict). Returns true if a type-specifier was
1386/// found (and parsed), false otherwise.
1387///
1388/// type-specifier: [C++ 7.1.5]
1389/// simple-type-specifier
1390/// class-specifier
1391/// enum-specifier
1392/// elaborated-type-specifier [TODO]
1393/// cv-qualifier
1394///
1395/// cv-qualifier: [C++ 7.1.5.1]
1396/// 'const'
1397/// 'volatile'
1398/// [C99] 'restrict'
1399///
1400/// simple-type-specifier: [ C++ 7.1.5.2]
1401/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1402/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1403/// 'char'
1404/// 'wchar_t'
1405/// 'bool'
1406/// 'short'
1407/// 'int'
1408/// 'long'
1409/// 'signed'
1410/// 'unsigned'
1411/// 'float'
1412/// 'double'
1413/// 'void'
1414/// [C99] '_Bool'
1415/// [C99] '_Complex'
1416/// [C99] '_Imaginary' // Removed in TC2?
1417/// [GNU] '_Decimal32'
1418/// [GNU] '_Decimal64'
1419/// [GNU] '_Decimal128'
1420/// [GNU] typeof-specifier
1421/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1422/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001423/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001424/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001425bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001426 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001427 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001428 const ParsedTemplateInfo &TemplateInfo,
1429 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001430 SourceLocation Loc = Tok.getLocation();
1431
1432 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001433 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00001434 // Check for need to substitute AltiVec keyword tokens.
1435 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1436 break;
1437 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001438 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001439 // Annotate typenames and C++ scope specifiers. If we get one, just
1440 // recurse to handle whatever we get.
1441 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001442 return true;
1443 if (Tok.is(tok::identifier))
1444 return false;
1445 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1446 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001447 case tok::coloncolon: // ::foo::bar
1448 if (NextToken().is(tok::kw_new) || // ::new
1449 NextToken().is(tok::kw_delete)) // ::delete
1450 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Chris Lattner166a8fc2009-01-04 23:41:41 +00001452 // Annotate typenames and C++ scope specifiers. If we get one, just
1453 // recurse to handle whatever we get.
1454 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001455 return true;
1456 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1457 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregor12e083c2008-11-07 15:42:26 +00001459 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001460 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001461 if (Tok.getAnnotationValue())
1462 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001463 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001464 else
1465 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001466 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1467 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Douglas Gregor12e083c2008-11-07 15:42:26 +00001469 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1470 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1471 // Objective-C interface. If we don't have Objective-C or a '<', this is
1472 // just a normal reference to a typedef name.
1473 if (!Tok.is(tok::less) || !getLang().ObjC1)
1474 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001476 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001477 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001478 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1479 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1480 LAngleLoc, EndProtoLoc);
1481 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1482 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregor12e083c2008-11-07 15:42:26 +00001484 DS.SetRangeEnd(EndProtoLoc);
1485 return true;
1486 }
1487
1488 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001489 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001490 break;
1491 case tok::kw_long:
1492 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001493 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1494 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001495 else
John McCallfec54012009-08-03 20:12:06 +00001496 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1497 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001498 break;
1499 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001500 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001501 break;
1502 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001503 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1504 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001505 break;
1506 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001507 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1508 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001509 break;
1510 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001511 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1512 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001513 break;
1514 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001516 break;
1517 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001518 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001519 break;
1520 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001521 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001522 break;
1523 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001524 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001525 break;
1526 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001528 break;
1529 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001530 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001531 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001532 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001533 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001534 break;
1535 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001537 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001538 case tok::kw_bool:
1539 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001540 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001541 break;
1542 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1544 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001545 break;
1546 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1548 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001549 break;
1550 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1552 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001553 break;
John Thompson82287d12010-02-05 00:12:22 +00001554 case tok::kw___vector:
1555 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1556 break;
1557 case tok::kw___pixel:
1558 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1559 break;
1560
Douglas Gregor12e083c2008-11-07 15:42:26 +00001561 // class-specifier:
1562 case tok::kw_class:
1563 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001564 case tok::kw_union: {
1565 tok::TokenKind Kind = Tok.getKind();
1566 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001567 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1568 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001569 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001570 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001571
1572 // enum-specifier:
1573 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001574 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001575 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001576 return true;
1577
1578 // cv-qualifier:
1579 case tok::kw_const:
1580 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001581 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001582 break;
1583 case tok::kw_volatile:
1584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001585 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw_restrict:
1588 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001589 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001590 break;
1591
1592 // GNU typeof support.
1593 case tok::kw_typeof:
1594 ParseTypeofSpecifier(DS);
1595 return true;
1596
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001597 // C++0x decltype support.
1598 case tok::kw_decltype:
1599 ParseDecltypeSpecifier(DS);
1600 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001602 // C++0x auto support.
1603 case tok::kw_auto:
1604 if (!getLang().CPlusPlus0x)
1605 return false;
1606
John McCallfec54012009-08-03 20:12:06 +00001607 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001608 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001609 case tok::kw___ptr64:
1610 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001611 case tok::kw___cdecl:
1612 case tok::kw___stdcall:
1613 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001614 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001615 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001616
Douglas Gregor12e083c2008-11-07 15:42:26 +00001617 default:
1618 // Not a type-specifier; do nothing.
1619 return false;
1620 }
1621
1622 // If the specifier combination wasn't legal, issue a diagnostic.
1623 if (isInvalid) {
1624 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001625 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001626 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001627 }
1628 DS.SetRangeEnd(Tok.getLocation());
1629 ConsumeToken(); // whatever we parsed above.
1630 return true;
1631}
Reid Spencer5f016e22007-07-11 17:01:13 +00001632
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001633/// ParseStructDeclaration - Parse a struct declaration without the terminating
1634/// semicolon.
1635///
Reid Spencer5f016e22007-07-11 17:01:13 +00001636/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001637/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001638/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001639/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001640/// struct-declarator-list:
1641/// struct-declarator
1642/// struct-declarator-list ',' struct-declarator
1643/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1644/// struct-declarator:
1645/// declarator
1646/// [GNU] declarator attributes[opt]
1647/// declarator[opt] ':' constant-expression
1648/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1649///
Chris Lattnere1359422008-04-10 06:46:29 +00001650void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001651ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001652 if (Tok.is(tok::kw___extension__)) {
1653 // __extension__ silences extension warnings in the subexpression.
1654 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001655 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001656 return ParseStructDeclaration(DS, Fields);
1657 }
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Steve Naroff28a7ca82007-08-20 22:28:22 +00001659 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001660 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001661 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001663 // If there are no declarators, this is a free-standing declaration
1664 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001665 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001666 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001667 return;
1668 }
1669
1670 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001671 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001672 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001673 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001674 FieldDeclarator DeclaratorInfo(DS);
1675
1676 // Attributes are only allowed here on successive declarators.
1677 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1678 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001679 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001680 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1681 }
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Steve Naroff28a7ca82007-08-20 22:28:22 +00001683 /// struct-declarator: declarator
1684 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001685 if (Tok.isNot(tok::colon)) {
1686 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1687 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001688 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001689 }
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Chris Lattner04d66662007-10-09 17:33:22 +00001691 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001692 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001693 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001694 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001695 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001696 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001697 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001698 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001699
Steve Naroff28a7ca82007-08-20 22:28:22 +00001700 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001701 if (Tok.is(tok::kw___attribute)) {
1702 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001703 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001704 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1705 }
1706
John McCallbdd563e2009-11-03 02:38:08 +00001707 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001708 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1709 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001710
Steve Naroff28a7ca82007-08-20 22:28:22 +00001711 // If we don't have a comma, it is either the end of the list (a ';')
1712 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001713 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001714 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001715
Steve Naroff28a7ca82007-08-20 22:28:22 +00001716 // Consume the comma.
1717 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001718
John McCallbdd563e2009-11-03 02:38:08 +00001719 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001720 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001721}
1722
1723/// ParseStructUnionBody
1724/// struct-contents:
1725/// struct-declaration-list
1726/// [EXT] empty
1727/// [GNU] "struct-declaration-list" without terminatoring ';'
1728/// struct-declaration-list:
1729/// struct-declaration
1730/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001731/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001732///
Reid Spencer5f016e22007-07-11 17:01:13 +00001733void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001734 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001735 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1736 PP.getSourceManager(),
1737 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001741 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001742 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1743
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1745 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001746 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001747 Diag(Tok, diag::ext_empty_struct_union_enum)
1748 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001749
Chris Lattnerb28317a2009-03-28 19:18:32 +00001750 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001751
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001753 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001757 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001758 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001759 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 ConsumeToken();
1761 continue;
1762 }
Chris Lattnere1359422008-04-10 06:46:29 +00001763
1764 // Parse all the comma separated declarators.
1765 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
John McCallbdd563e2009-11-03 02:38:08 +00001767 if (!Tok.is(tok::at)) {
1768 struct CFieldCallback : FieldCallback {
1769 Parser &P;
1770 DeclPtrTy TagDecl;
1771 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1772
1773 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1774 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1775 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1776
1777 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001778 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001779 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1780 FD.D.getDeclSpec().getSourceRange().getBegin(),
1781 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001782 FieldDecls.push_back(Field);
1783 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001784 }
John McCallbdd563e2009-11-03 02:38:08 +00001785 } Callback(*this, TagDecl, FieldDecls);
1786
1787 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001788 } else { // Handle @defs
1789 ConsumeToken();
1790 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1791 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001792 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001793 continue;
1794 }
1795 ConsumeToken();
1796 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1797 if (!Tok.is(tok::identifier)) {
1798 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001799 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001800 continue;
1801 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001802 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001803 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001804 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001805 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1806 ConsumeToken();
1807 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001808 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001809
Chris Lattner04d66662007-10-09 17:33:22 +00001810 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001812 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001813 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 break;
1815 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001816 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1817 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001819 // If we stopped at a ';', eat it.
1820 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 }
1822 }
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Steve Naroff60fccee2007-10-29 21:38:07 +00001824 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Ted Kremenek1e377652010-02-11 02:19:13 +00001826 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001828 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001829 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001830
1831 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001832 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001833 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001834 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001835 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001836 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001837}
1838
1839
1840/// ParseEnumSpecifier
1841/// enum-specifier: [C99 6.7.2.2]
1842/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001843///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001844/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1845/// '}' attributes[opt]
1846/// 'enum' identifier
1847/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001848///
1849/// [C++] elaborated-type-specifier:
1850/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1851///
Chris Lattner4c97d762009-04-12 21:49:30 +00001852void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001853 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001854 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001856 if (Tok.is(tok::code_completion)) {
1857 // Code completion for an enum name.
1858 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1859 ConsumeToken();
1860 }
1861
Ted Kremenek1e377652010-02-11 02:19:13 +00001862 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001863 // If attributes exist after tag, parse them.
1864 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001865 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001866
1867 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00001868 if (getLang().CPlusPlus) {
1869 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1870 return;
1871
1872 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001873 Diag(Tok, diag::err_expected_ident);
1874 if (Tok.isNot(tok::l_brace)) {
1875 // Has no name and is not a definition.
1876 // Skip the rest of this declarator, up until the comma or semicolon.
1877 SkipUntil(tok::comma, true);
1878 return;
1879 }
1880 }
1881 }
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001883 // Must have either 'enum name' or 'enum {...}'.
1884 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1885 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001887 // Skip the rest of this declarator, up until the comma or semicolon.
1888 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001890 }
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001892 // enums cannot be templates.
1893 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1894 Diag(Tok, diag::err_enum_template);
1895
1896 // Skip the rest of this declarator, up until the comma or semicolon.
1897 SkipUntil(tok::comma, true);
1898 return;
1899 }
1900
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001901 // If an identifier is present, consume and remember it.
1902 IdentifierInfo *Name = 0;
1903 SourceLocation NameLoc;
1904 if (Tok.is(tok::identifier)) {
1905 Name = Tok.getIdentifierInfo();
1906 NameLoc = ConsumeToken();
1907 }
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001909 // There are three options here. If we have 'enum foo;', then this is a
1910 // forward declaration. If we have 'enum foo {...' then this is a
1911 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1912 //
1913 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1914 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1915 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1916 //
John McCall0f434ec2009-07-31 02:45:11 +00001917 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001918 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001919 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001920 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001921 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001922 else
John McCall0f434ec2009-07-31 02:45:11 +00001923 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001924 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001925 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001926 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Ted Kremenek1e377652010-02-11 02:19:13 +00001927 StartLoc, SS, Name, NameLoc, Attr.get(),
1928 AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001929 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001930 Owned, IsDependent);
1931 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Chris Lattner04d66662007-10-09 17:33:22 +00001933 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001936 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1937 // name (if there is one).
1938 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +00001939 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001940 unsigned DiagID;
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001941 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001942 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001943 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001944}
1945
1946/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1947/// enumerator-list:
1948/// enumerator
1949/// enumerator-list ',' enumerator
1950/// enumerator:
1951/// enumeration-constant
1952/// enumeration-constant '=' constant-expression
1953/// enumeration-constant:
1954/// identifier
1955///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001956void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001957 // Enter the scope of the enum body and start the definition.
1958 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001959 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001960
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Chris Lattner7946dd32007-08-27 17:24:30 +00001963 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001964 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001965 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Chris Lattnerb28317a2009-03-28 19:18:32 +00001967 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001968
Chris Lattnerb28317a2009-03-28 19:18:32 +00001969 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001972 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001973 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1974 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Reid Spencer5f016e22007-07-11 17:01:13 +00001976 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001977 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001978 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001980 AssignedVal = ParseConstantExpression();
1981 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001982 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001983 }
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001986 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1987 LastEnumConstDecl,
1988 IdentLoc, Ident,
1989 EqualLoc,
1990 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 EnumConstantDecls.push_back(EnumConstDecl);
1992 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Chris Lattner04d66662007-10-09 17:33:22 +00001994 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001995 break;
1996 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001997
1998 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001999 !(getLang().C99 || getLang().CPlusPlus0x))
2000 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2001 << getLang().CPlusPlus
Chris Lattner29d9c1a2009-12-06 17:36:05 +00002002 << CodeModificationHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 }
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002006 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002007
Ted Kremenek1e377652010-02-11 02:19:13 +00002008 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002009 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002010 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002011 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002012
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002013 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2014 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +00002015 CurScope, Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregor72de6672009-01-08 20:45:30 +00002017 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00002018 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002019}
2020
2021/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002022/// start of a type-qualifier-list.
2023bool Parser::isTypeQualifier() const {
2024 switch (Tok.getKind()) {
2025 default: return false;
2026 // type-qualifier
2027 case tok::kw_const:
2028 case tok::kw_volatile:
2029 case tok::kw_restrict:
2030 return true;
2031 }
2032}
2033
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002034/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2035/// is definitely a type-specifier. Return false if it isn't part of a type
2036/// specifier or if we're not sure.
2037bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2038 switch (Tok.getKind()) {
2039 default: return false;
2040 // type-specifiers
2041 case tok::kw_short:
2042 case tok::kw_long:
2043 case tok::kw_signed:
2044 case tok::kw_unsigned:
2045 case tok::kw__Complex:
2046 case tok::kw__Imaginary:
2047 case tok::kw_void:
2048 case tok::kw_char:
2049 case tok::kw_wchar_t:
2050 case tok::kw_char16_t:
2051 case tok::kw_char32_t:
2052 case tok::kw_int:
2053 case tok::kw_float:
2054 case tok::kw_double:
2055 case tok::kw_bool:
2056 case tok::kw__Bool:
2057 case tok::kw__Decimal32:
2058 case tok::kw__Decimal64:
2059 case tok::kw__Decimal128:
2060 case tok::kw___vector:
2061
2062 // struct-or-union-specifier (C99) or class-specifier (C++)
2063 case tok::kw_class:
2064 case tok::kw_struct:
2065 case tok::kw_union:
2066 // enum-specifier
2067 case tok::kw_enum:
2068
2069 // typedef-name
2070 case tok::annot_typename:
2071 return true;
2072 }
2073}
2074
Steve Naroff5f8aa692008-02-11 23:15:56 +00002075/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002076/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002077bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002078 switch (Tok.getKind()) {
2079 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Chris Lattner166a8fc2009-01-04 23:41:41 +00002081 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002082 if (TryAltiVecVectorToken())
2083 return true;
2084 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002085 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002086 // Annotate typenames and C++ scope specifiers. If we get one, just
2087 // recurse to handle whatever we get.
2088 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002089 return true;
2090 if (Tok.is(tok::identifier))
2091 return false;
2092 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002093
Chris Lattner166a8fc2009-01-04 23:41:41 +00002094 case tok::coloncolon: // ::foo::bar
2095 if (NextToken().is(tok::kw_new) || // ::new
2096 NextToken().is(tok::kw_delete)) // ::delete
2097 return false;
2098
Chris Lattner166a8fc2009-01-04 23:41:41 +00002099 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002100 return true;
2101 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Reid Spencer5f016e22007-07-11 17:01:13 +00002103 // GNU attributes support.
2104 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002105 // GNU typeof support.
2106 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 // type-specifiers
2109 case tok::kw_short:
2110 case tok::kw_long:
2111 case tok::kw_signed:
2112 case tok::kw_unsigned:
2113 case tok::kw__Complex:
2114 case tok::kw__Imaginary:
2115 case tok::kw_void:
2116 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002117 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002118 case tok::kw_char16_t:
2119 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 case tok::kw_int:
2121 case tok::kw_float:
2122 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002123 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 case tok::kw__Bool:
2125 case tok::kw__Decimal32:
2126 case tok::kw__Decimal64:
2127 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002128 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Chris Lattner99dc9142008-04-13 18:59:07 +00002130 // struct-or-union-specifier (C99) or class-specifier (C++)
2131 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 case tok::kw_struct:
2133 case tok::kw_union:
2134 // enum-specifier
2135 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002136
Reid Spencer5f016e22007-07-11 17:01:13 +00002137 // type-qualifier
2138 case tok::kw_const:
2139 case tok::kw_volatile:
2140 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002141
2142 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002143 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Chris Lattner7c186be2008-10-20 00:25:30 +00002146 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2147 case tok::less:
2148 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Steve Naroff239f0732008-12-25 14:16:32 +00002150 case tok::kw___cdecl:
2151 case tok::kw___stdcall:
2152 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002153 case tok::kw___w64:
2154 case tok::kw___ptr64:
2155 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 }
2157}
2158
2159/// isDeclarationSpecifier() - Return true if the current token is part of a
2160/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002161bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002162 switch (Tok.getKind()) {
2163 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Chris Lattner166a8fc2009-01-04 23:41:41 +00002165 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002166 // Unfortunate hack to support "Class.factoryMethod" notation.
2167 if (getLang().ObjC1 && NextToken().is(tok::period))
2168 return false;
John Thompson82287d12010-02-05 00:12:22 +00002169 if (TryAltiVecVectorToken())
2170 return true;
2171 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002172 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002173 // Annotate typenames and C++ scope specifiers. If we get one, just
2174 // recurse to handle whatever we get.
2175 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002176 return true;
2177 if (Tok.is(tok::identifier))
2178 return false;
2179 return isDeclarationSpecifier();
2180
Chris Lattner166a8fc2009-01-04 23:41:41 +00002181 case tok::coloncolon: // ::foo::bar
2182 if (NextToken().is(tok::kw_new) || // ::new
2183 NextToken().is(tok::kw_delete)) // ::delete
2184 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Chris Lattner166a8fc2009-01-04 23:41:41 +00002186 // Annotate typenames and C++ scope specifiers. If we get one, just
2187 // recurse to handle whatever we get.
2188 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002189 return true;
2190 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002191
Reid Spencer5f016e22007-07-11 17:01:13 +00002192 // storage-class-specifier
2193 case tok::kw_typedef:
2194 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002195 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 case tok::kw_static:
2197 case tok::kw_auto:
2198 case tok::kw_register:
2199 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 // type-specifiers
2202 case tok::kw_short:
2203 case tok::kw_long:
2204 case tok::kw_signed:
2205 case tok::kw_unsigned:
2206 case tok::kw__Complex:
2207 case tok::kw__Imaginary:
2208 case tok::kw_void:
2209 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002210 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002211 case tok::kw_char16_t:
2212 case tok::kw_char32_t:
2213
Reid Spencer5f016e22007-07-11 17:01:13 +00002214 case tok::kw_int:
2215 case tok::kw_float:
2216 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002217 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002218 case tok::kw__Bool:
2219 case tok::kw__Decimal32:
2220 case tok::kw__Decimal64:
2221 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002222 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Chris Lattner99dc9142008-04-13 18:59:07 +00002224 // struct-or-union-specifier (C99) or class-specifier (C++)
2225 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002226 case tok::kw_struct:
2227 case tok::kw_union:
2228 // enum-specifier
2229 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Reid Spencer5f016e22007-07-11 17:01:13 +00002231 // type-qualifier
2232 case tok::kw_const:
2233 case tok::kw_volatile:
2234 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002235
Reid Spencer5f016e22007-07-11 17:01:13 +00002236 // function-specifier
2237 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002238 case tok::kw_virtual:
2239 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002240
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002241 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002242 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002243
Chris Lattner1ef08762007-08-09 17:01:07 +00002244 // GNU typeof support.
2245 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Chris Lattner1ef08762007-08-09 17:01:07 +00002247 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002248 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002249 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Chris Lattnerf3948c42008-07-26 03:38:44 +00002251 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2252 case tok::less:
2253 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Steve Naroff47f52092009-01-06 19:34:12 +00002255 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002256 case tok::kw___cdecl:
2257 case tok::kw___stdcall:
2258 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002259 case tok::kw___w64:
2260 case tok::kw___ptr64:
2261 case tok::kw___forceinline:
2262 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002263 }
2264}
2265
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002266bool Parser::isConstructorDeclarator() {
2267 TentativeParsingAction TPA(*this);
2268
2269 // Parse the C++ scope specifier.
2270 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002271 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2272 TPA.Revert();
2273 return false;
2274 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002275
2276 // Parse the constructor name.
2277 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2278 // We already know that we have a constructor name; just consume
2279 // the token.
2280 ConsumeToken();
2281 } else {
2282 TPA.Revert();
2283 return false;
2284 }
2285
2286 // Current class name must be followed by a left parentheses.
2287 if (Tok.isNot(tok::l_paren)) {
2288 TPA.Revert();
2289 return false;
2290 }
2291 ConsumeParen();
2292
2293 // A right parentheses or ellipsis signals that we have a constructor.
2294 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2295 TPA.Revert();
2296 return true;
2297 }
2298
2299 // If we need to, enter the specified scope.
2300 DeclaratorScopeObj DeclScopeObj(*this, SS);
2301 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2302 DeclScopeObj.EnterDeclaratorScope();
2303
2304 // Check whether the next token(s) are part of a declaration
2305 // specifier, in which case we have the start of a parameter and,
2306 // therefore, we know that this is a constructor.
2307 bool IsConstructor = isDeclarationSpecifier();
2308 TPA.Revert();
2309 return IsConstructor;
2310}
Reid Spencer5f016e22007-07-11 17:01:13 +00002311
2312/// ParseTypeQualifierListOpt
2313/// type-qualifier-list: [C99 6.7.5]
2314/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002315/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002316/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002317/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002318/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2319/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002320///
Sean Huntbbd37c62009-11-21 08:43:09 +00002321void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2322 bool CXX0XAttributesAllowed) {
2323 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2324 SourceLocation Loc = Tok.getLocation();
2325 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2326 if (CXX0XAttributesAllowed)
2327 DS.AddAttributes(Attr.AttrList);
2328 else
2329 Diag(Loc, diag::err_attributes_not_allowed);
2330 }
2331
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002333 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002334 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002335 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002336 SourceLocation Loc = Tok.getLocation();
2337
2338 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002339 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002340 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2341 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002342 break;
2343 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002344 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2345 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002346 break;
2347 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002348 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2349 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002350 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002351 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002352 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002353 case tok::kw___cdecl:
2354 case tok::kw___stdcall:
2355 case tok::kw___fastcall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002356 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002357 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2358 continue;
2359 }
2360 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002362 if (GNUAttributesAllowed) {
2363 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002364 continue; // do *not* consume the next token!
2365 }
2366 // otherwise, FALL THROUGH!
2367 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002368 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002369 // If this is not a type-qualifier token, we're done reading type
2370 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002371 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002372 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002373 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002374
Reid Spencer5f016e22007-07-11 17:01:13 +00002375 // If the specifier combination wasn't legal, issue a diagnostic.
2376 if (isInvalid) {
2377 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002378 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002379 }
2380 ConsumeToken();
2381 }
2382}
2383
2384
2385/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2386///
2387void Parser::ParseDeclarator(Declarator &D) {
2388 /// This implements the 'declarator' production in the C grammar, then checks
2389 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002390 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002391}
2392
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002393/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2394/// is parsed by the function passed to it. Pass null, and the direct-declarator
2395/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002396/// ptr-operator production.
2397///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002398/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2399/// [C] pointer[opt] direct-declarator
2400/// [C++] direct-declarator
2401/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002402///
2403/// pointer: [C99 6.7.5]
2404/// '*' type-qualifier-list[opt]
2405/// '*' type-qualifier-list[opt] pointer
2406///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002407/// ptr-operator:
2408/// '*' cv-qualifier-seq[opt]
2409/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002410/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002411/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002412/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002413/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002414void Parser::ParseDeclaratorInternal(Declarator &D,
2415 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002416 if (Diags.hasAllExtensionsSilenced())
2417 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002418 // C++ member pointers start with a '::' or a nested-name.
2419 // Member pointers get special handling, since there's no place for the
2420 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002421 if (getLang().CPlusPlus &&
2422 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2423 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002424 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002425 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2426
2427 if (SS.isSet()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002428 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002429 // The scope spec really belongs to the direct-declarator.
2430 D.getCXXScopeSpec() = SS;
2431 if (DirectDeclParser)
2432 (this->*DirectDeclParser)(D);
2433 return;
2434 }
2435
2436 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002437 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002438 DeclSpec DS;
2439 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002440 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002441
2442 // Recurse to parse whatever is left.
2443 ParseDeclaratorInternal(D, DirectDeclParser);
2444
2445 // Sema will have to catch (syntactically invalid) pointers into global
2446 // scope. It has to catch pointers into namespace scope anyway.
2447 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002448 Loc, DS.TakeAttributes()),
2449 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002450 return;
2451 }
2452 }
2453
2454 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002455 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002456 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002457 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002458 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002459 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002460 if (DirectDeclParser)
2461 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002462 return;
2463 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002464
Sebastian Redl05532f22009-03-15 22:02:01 +00002465 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2466 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002467 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002468 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002469
Chris Lattner9af55002009-03-27 04:18:06 +00002470 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002471 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002472 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002473
Reid Spencer5f016e22007-07-11 17:01:13 +00002474 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002475 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002476
Reid Spencer5f016e22007-07-11 17:01:13 +00002477 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002478 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002479 if (Kind == tok::star)
2480 // Remember that we parsed a pointer type, and remember the type-quals.
2481 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002482 DS.TakeAttributes()),
2483 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002484 else
2485 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002486 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002487 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002488 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002489 } else {
2490 // Is a reference
2491 DeclSpec DS;
2492
Sebastian Redl743de1f2009-03-23 00:00:23 +00002493 // Complain about rvalue references in C++03, but then go on and build
2494 // the declarator.
2495 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2496 Diag(Loc, diag::err_rvalue_reference);
2497
Reid Spencer5f016e22007-07-11 17:01:13 +00002498 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2499 // cv-qualifiers are introduced through the use of a typedef or of a
2500 // template type argument, in which case the cv-qualifiers are ignored.
2501 //
2502 // [GNU] Retricted references are allowed.
2503 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002504 // [C++0x] Attributes on references are not allowed.
2505 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002506 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002507
2508 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2509 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2510 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002511 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002512 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2513 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002514 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002515 }
2516
2517 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002518 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002519
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002520 if (D.getNumTypeObjects() > 0) {
2521 // C++ [dcl.ref]p4: There shall be no references to references.
2522 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2523 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002524 if (const IdentifierInfo *II = D.getIdentifier())
2525 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2526 << II;
2527 else
2528 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2529 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002530
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002531 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002532 // can go ahead and build the (technically ill-formed)
2533 // declarator: reference collapsing will take care of it.
2534 }
2535 }
2536
Reid Spencer5f016e22007-07-11 17:01:13 +00002537 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002538 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002539 DS.TakeAttributes(),
2540 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002541 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002542 }
2543}
2544
2545/// ParseDirectDeclarator
2546/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002547/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002548/// '(' declarator ')'
2549/// [GNU] '(' attributes declarator ')'
2550/// [C90] direct-declarator '[' constant-expression[opt] ']'
2551/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2552/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2553/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2554/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2555/// direct-declarator '(' parameter-type-list ')'
2556/// direct-declarator '(' identifier-list[opt] ')'
2557/// [GNU] direct-declarator '(' parameter-forward-declarations
2558/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002559/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2560/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002561/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002562///
2563/// declarator-id: [C++ 8]
2564/// id-expression
2565/// '::'[opt] nested-name-specifier[opt] type-name
2566///
2567/// id-expression: [C++ 5.1]
2568/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002569/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002570///
2571/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002572/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002573/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002574/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002575/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002576/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002577///
Reid Spencer5f016e22007-07-11 17:01:13 +00002578void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002579 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002580
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002581 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2582 // ParseDeclaratorInternal might already have parsed the scope.
John McCall9ba61662010-02-26 08:45:28 +00002583 bool afterCXXScope = D.getCXXScopeSpec().isSet();
2584 if (!afterCXXScope) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002585 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2586 true);
John McCall9ba61662010-02-26 08:45:28 +00002587 afterCXXScope = D.getCXXScopeSpec().isSet();
2588 }
2589
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002590 if (afterCXXScope) {
John McCalle7e278b2009-12-11 20:04:54 +00002591 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2592 // Change the declaration context for name lookup, until this function
2593 // is exited (and the declarator has been parsed).
2594 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002595 }
2596
2597 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2598 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2599 // We found something that indicates the start of an unqualified-id.
2600 // Parse that unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002601 bool AllowConstructorName
2602 = ((D.getCXXScopeSpec().isSet() &&
2603 D.getContext() == Declarator::FileContext) ||
2604 (!D.getCXXScopeSpec().isSet() &&
2605 D.getContext() == Declarator::MemberContext)) &&
2606 !D.getDeclSpec().hasTypeSpecifier();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002607 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2608 /*EnteringContext=*/true,
2609 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002610 AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002611 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002612 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002613 D.SetIdentifier(0, Tok.getLocation());
2614 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002615 } else {
2616 // Parsed the unqualified-id; update range information and move along.
2617 if (D.getSourceRange().getBegin().isInvalid())
2618 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2619 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002620 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002621 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002622 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002623 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002624 assert(!getLang().CPlusPlus &&
2625 "There's a C++-specific check for tok::identifier above");
2626 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2627 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2628 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002629 goto PastIdentifier;
2630 }
2631
2632 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002633 // direct-declarator: '(' declarator ')'
2634 // direct-declarator: '(' attributes declarator ')'
2635 // Example: 'char (*X)' or 'int (*XX)(void)'
2636 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002637
2638 // If the declarator was parenthesized, we entered the declarator
2639 // scope when parsing the parenthesized declarator, then exited
2640 // the scope already. Re-enter the scope, if we need to.
2641 if (D.getCXXScopeSpec().isSet()) {
2642 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2643 // Change the declaration context for name lookup, until this function
2644 // is exited (and the declarator has been parsed).
2645 DeclScopeObj.EnterDeclaratorScope();
2646 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002647 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002648 // This could be something simple like "int" (in which case the declarator
2649 // portion is empty), if an abstract-declarator is allowed.
2650 D.SetIdentifier(0, Tok.getLocation());
2651 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002652 if (D.getContext() == Declarator::MemberContext)
2653 Diag(Tok, diag::err_expected_member_name_or_semi)
2654 << D.getDeclSpec().getSourceRange();
2655 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002656 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002657 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002658 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002659 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002660 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002661 }
Mike Stump1eb44332009-09-09 15:08:12 +00002662
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002663 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002664 assert(D.isPastIdentifier() &&
2665 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002666
Sean Huntbbd37c62009-11-21 08:43:09 +00002667 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002668 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002669 && isCXX0XAttributeSpecifier(true)) {
2670 SourceLocation AttrEndLoc;
2671 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2672 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2673 }
2674
Reid Spencer5f016e22007-07-11 17:01:13 +00002675 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002676 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002677 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2678 // In such a case, check if we actually have a function declarator; if it
2679 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002680 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2681 // When not in file scope, warn for ambiguous function declarators, just
2682 // in case the author intended it as a variable definition.
2683 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2684 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2685 break;
2686 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002687 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002688 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002689 ParseBracketDeclarator(D);
2690 } else {
2691 break;
2692 }
2693 }
2694}
2695
Chris Lattneref4715c2008-04-06 05:45:57 +00002696/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2697/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002698/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002699/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2700///
2701/// direct-declarator:
2702/// '(' declarator ')'
2703/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002704/// direct-declarator '(' parameter-type-list ')'
2705/// direct-declarator '(' identifier-list[opt] ')'
2706/// [GNU] direct-declarator '(' parameter-forward-declarations
2707/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002708///
2709void Parser::ParseParenDeclarator(Declarator &D) {
2710 SourceLocation StartLoc = ConsumeParen();
2711 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Chris Lattner7399ee02008-10-20 02:05:46 +00002713 // Eat any attributes before we look at whether this is a grouping or function
2714 // declarator paren. If this is a grouping paren, the attribute applies to
2715 // the type being built up, for example:
2716 // int (__attribute__(()) *x)(long y)
2717 // If this ends up not being a grouping paren, the attribute applies to the
2718 // first argument, for example:
2719 // int (__attribute__(()) int x)
2720 // In either case, we need to eat any attributes to be able to determine what
2721 // sort of paren this is.
2722 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002723 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002724 bool RequiresArg = false;
2725 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002726 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Chris Lattner7399ee02008-10-20 02:05:46 +00002728 // We require that the argument list (if this is a non-grouping paren) be
2729 // present even if the attribute list was empty.
2730 RequiresArg = true;
2731 }
Steve Naroff239f0732008-12-25 14:16:32 +00002732 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002733 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2734 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2735 Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002736 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002737 }
Mike Stump1eb44332009-09-09 15:08:12 +00002738
Chris Lattneref4715c2008-04-06 05:45:57 +00002739 // If we haven't past the identifier yet (or where the identifier would be
2740 // stored, if this is an abstract declarator), then this is probably just
2741 // grouping parens. However, if this could be an abstract-declarator, then
2742 // this could also be the start of function arguments (consider 'void()').
2743 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Chris Lattneref4715c2008-04-06 05:45:57 +00002745 if (!D.mayOmitIdentifier()) {
2746 // If this can't be an abstract-declarator, this *must* be a grouping
2747 // paren, because we haven't seen the identifier yet.
2748 isGrouping = true;
2749 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002750 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002751 isDeclarationSpecifier()) { // 'int(int)' is a function.
2752 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2753 // considered to be a type, not a K&R identifier-list.
2754 isGrouping = false;
2755 } else {
2756 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2757 isGrouping = true;
2758 }
Mike Stump1eb44332009-09-09 15:08:12 +00002759
Chris Lattneref4715c2008-04-06 05:45:57 +00002760 // If this is a grouping paren, handle:
2761 // direct-declarator: '(' declarator ')'
2762 // direct-declarator: '(' attributes declarator ')'
2763 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002764 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002765 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002766 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002767 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002768
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002769 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002770 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002771 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002772
2773 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002774 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002775 return;
2776 }
Mike Stump1eb44332009-09-09 15:08:12 +00002777
Chris Lattneref4715c2008-04-06 05:45:57 +00002778 // Okay, if this wasn't a grouping paren, it must be the start of a function
2779 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002780 // identifier (and remember where it would have been), then call into
2781 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002782 D.SetIdentifier(0, Tok.getLocation());
2783
Ted Kremenek1e377652010-02-11 02:19:13 +00002784 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002785}
2786
2787/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2788/// declarator D up to a paren, which indicates that we are parsing function
2789/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002790///
Chris Lattner7399ee02008-10-20 02:05:46 +00002791/// If AttrList is non-null, then the caller parsed those arguments immediately
2792/// after the open paren - they should be considered to be the first argument of
2793/// a parameter. If RequiresArg is true, then the first argument of the
2794/// function is required to be present and required to not be an identifier
2795/// list.
2796///
Reid Spencer5f016e22007-07-11 17:01:13 +00002797/// This method also handles this portion of the grammar:
2798/// parameter-type-list: [C99 6.7.5]
2799/// parameter-list
2800/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002801/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002802///
2803/// parameter-list: [C99 6.7.5]
2804/// parameter-declaration
2805/// parameter-list ',' parameter-declaration
2806///
2807/// parameter-declaration: [C99 6.7.5]
2808/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002809/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002810/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002811/// declaration-specifiers abstract-declarator[opt]
2812/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002813/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002814/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2815///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002816/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002817/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002818///
Chris Lattner7399ee02008-10-20 02:05:46 +00002819void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2820 AttributeList *AttrList,
2821 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002822 // lparen is already consumed!
2823 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Chris Lattner7399ee02008-10-20 02:05:46 +00002825 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002826 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002827 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002828 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002829 delete AttrList;
2830 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002831
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002832 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2833 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002834
2835 // cv-qualifier-seq[opt].
2836 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002837 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002838 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002839 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002840 llvm::SmallVector<TypeTy*, 2> Exceptions;
2841 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002842 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002843 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002844 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002845 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002846
2847 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002848 if (Tok.is(tok::kw_throw)) {
2849 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002850 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002851 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002852 hasAnyExceptionSpec);
2853 assert(Exceptions.size() == ExceptionRanges.size() &&
2854 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002855 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002856 }
2857
Chris Lattnerf97409f2008-04-06 06:57:35 +00002858 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002859 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002860 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002861 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002862 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002863 /*arglist*/ 0, 0,
2864 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002865 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002866 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002867 Exceptions.data(),
2868 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002869 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002870 LParenLoc, RParenLoc, D),
2871 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002872 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002873 }
2874
Chris Lattner7399ee02008-10-20 02:05:46 +00002875 // Alternatively, this parameter list may be an identifier list form for a
2876 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00002877 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2878 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00002879 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002880 // K&R identifier lists can't have typedefs as identifiers, per
2881 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002882 if (RequiresArg) {
2883 Diag(Tok, diag::err_argument_required_after_attribute);
2884 delete AttrList;
2885 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002886 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2887 // normal declarators, not for abstract-declarators.
2888 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002889 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002890 }
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Chris Lattnerf97409f2008-04-06 06:57:35 +00002892 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002893
Chris Lattnerf97409f2008-04-06 06:57:35 +00002894 // Build up an array of information about the parsed arguments.
2895 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002896
2897 // Enter function-declaration scope, limiting any declarators to the
2898 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002899 ParseScope PrototypeScope(this,
2900 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Chris Lattnerf97409f2008-04-06 06:57:35 +00002902 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002903 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002904 while (1) {
2905 if (Tok.is(tok::ellipsis)) {
2906 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002907 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002908 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002909 }
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Chris Lattnerf97409f2008-04-06 06:57:35 +00002911 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002912
Chris Lattnerf97409f2008-04-06 06:57:35 +00002913 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002914 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002915 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002916
2917 // If the caller parsed attributes for the first argument, add them now.
2918 if (AttrList) {
2919 DS.AddAttributes(AttrList);
2920 AttrList = 0; // Only apply the attributes to the first parameter.
2921 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002922 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002923
Chris Lattnerf97409f2008-04-06 06:57:35 +00002924 // Parse the declarator. This is "PrototypeContext", because we must
2925 // accept either 'declarator' or 'abstract-declarator' here.
2926 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2927 ParseDeclarator(ParmDecl);
2928
2929 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002930 if (Tok.is(tok::kw___attribute)) {
2931 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002932 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002933 ParmDecl.AddAttributes(AttrList, Loc);
2934 }
Mike Stump1eb44332009-09-09 15:08:12 +00002935
Chris Lattnerf97409f2008-04-06 06:57:35 +00002936 // Remember this parsed parameter in ParamInfo.
2937 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002938
Douglas Gregor72b505b2008-12-16 21:30:33 +00002939 // DefArgToks is used when the parsing of default arguments needs
2940 // to be delayed.
2941 CachedTokens *DefArgToks = 0;
2942
Chris Lattnerf97409f2008-04-06 06:57:35 +00002943 // If no parameter was specified, verify that *something* was specified,
2944 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002945 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2946 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002947 // Completely missing, emit error.
2948 Diag(DSStart, diag::err_missing_param);
2949 } else {
2950 // Otherwise, we have something. Add it and let semantic analysis try
2951 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002952
Chris Lattnerf97409f2008-04-06 06:57:35 +00002953 // Inform the actions module about the parameter declarator, so it gets
2954 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002955 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002956
2957 // Parse the default argument, if any. We parse the default
2958 // arguments in all dialects; the semantic analysis in
2959 // ActOnParamDefaultArgument will reject the default argument in
2960 // C.
2961 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002962 SourceLocation EqualLoc = Tok.getLocation();
2963
Chris Lattner04421082008-04-08 04:40:51 +00002964 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002965 if (D.getContext() == Declarator::MemberContext) {
2966 // If we're inside a class definition, cache the tokens
2967 // corresponding to the default argument. We'll actually parse
2968 // them when we see the end of the class definition.
2969 // FIXME: Templates will require something similar.
2970 // FIXME: Can we use a smart pointer for Toks?
2971 DefArgToks = new CachedTokens;
2972
Mike Stump1eb44332009-09-09 15:08:12 +00002973 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002974 tok::semi, false)) {
2975 delete DefArgToks;
2976 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002977 Actions.ActOnParamDefaultArgumentError(Param);
2978 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002979 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002980 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002981 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002982 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002983 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002984
Douglas Gregor72b505b2008-12-16 21:30:33 +00002985 OwningExprResult DefArgResult(ParseAssignmentExpression());
2986 if (DefArgResult.isInvalid()) {
2987 Actions.ActOnParamDefaultArgumentError(Param);
2988 SkipUntil(tok::comma, tok::r_paren, true, true);
2989 } else {
2990 // Inform the actions module about the default argument
2991 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002992 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002993 }
Chris Lattner04421082008-04-08 04:40:51 +00002994 }
2995 }
Mike Stump1eb44332009-09-09 15:08:12 +00002996
2997 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2998 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002999 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003000 }
3001
3002 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003003 if (Tok.isNot(tok::comma)) {
3004 if (Tok.is(tok::ellipsis)) {
3005 IsVariadic = true;
3006 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3007
3008 if (!getLang().CPlusPlus) {
3009 // We have ellipsis without a preceding ',', which is ill-formed
3010 // in C. Complain and provide the fix.
3011 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
3012 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
3013 }
3014 }
3015
3016 break;
3017 }
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Chris Lattnerf97409f2008-04-06 06:57:35 +00003019 // Consume the comma.
3020 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003021 }
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Chris Lattnerf97409f2008-04-06 06:57:35 +00003023 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003024 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003025
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003026 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003027 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3028 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003029
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003030 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003031 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003032 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003033 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00003034 llvm::SmallVector<TypeTy*, 2> Exceptions;
3035 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003036
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003037 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003038 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003039 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003040 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003041 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003042
3043 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003044 if (Tok.is(tok::kw_throw)) {
3045 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003046 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003047 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003048 hasAnyExceptionSpec);
3049 assert(Exceptions.size() == ExceptionRanges.size() &&
3050 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003051 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003052 }
3053
Reid Spencer5f016e22007-07-11 17:01:13 +00003054 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003055 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003056 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003057 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003058 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003059 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003060 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003061 Exceptions.data(),
3062 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003063 Exceptions.size(),
3064 LParenLoc, RParenLoc, D),
3065 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003066}
3067
Chris Lattner66d28652008-04-06 06:34:08 +00003068/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3069/// we found a K&R-style identifier list instead of a type argument list. The
3070/// current token is known to be the first identifier in the list.
3071///
3072/// identifier-list: [C99 6.7.5]
3073/// identifier
3074/// identifier-list ',' identifier
3075///
3076void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
3077 Declarator &D) {
3078 // Build up an array of information about the parsed arguments.
3079 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3080 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003081
Chris Lattner66d28652008-04-06 06:34:08 +00003082 // If there was no identifier specified for the declarator, either we are in
3083 // an abstract-declarator, or we are in a parameter declarator which was found
3084 // to be abstract. In abstract-declarators, identifier lists are not valid:
3085 // diagnose this.
3086 if (!D.getIdentifier())
3087 Diag(Tok, diag::ext_ident_list_in_param);
3088
3089 // Tok is known to be the first identifier in the list. Remember this
3090 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00003091 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00003092 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00003093 Tok.getLocation(),
3094 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00003095
Chris Lattner50c64772008-04-06 06:39:19 +00003096 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00003097
Chris Lattner66d28652008-04-06 06:34:08 +00003098 while (Tok.is(tok::comma)) {
3099 // Eat the comma.
3100 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003101
Chris Lattner50c64772008-04-06 06:39:19 +00003102 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003103 if (Tok.isNot(tok::identifier)) {
3104 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003105 SkipUntil(tok::r_paren);
3106 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003107 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003108
Chris Lattner66d28652008-04-06 06:34:08 +00003109 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003110
3111 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00003112 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003113 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003114
Chris Lattner66d28652008-04-06 06:34:08 +00003115 // Verify that the argument identifier has not already been mentioned.
3116 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003117 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003118 } else {
3119 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003120 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003121 Tok.getLocation(),
3122 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00003123 }
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Chris Lattner66d28652008-04-06 06:34:08 +00003125 // Eat the identifier.
3126 ConsumeToken();
3127 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003128
3129 // If we have the closing ')', eat it and we're done.
3130 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3131
Chris Lattner50c64772008-04-06 06:39:19 +00003132 // Remember that we parsed a function type, and remember the attributes. This
3133 // function type is always a K&R style function type, which is not varargs and
3134 // has no prototype.
3135 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003136 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003137 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003138 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003139 /*exception*/false,
3140 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003141 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003142 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003143}
Chris Lattneref4715c2008-04-06 05:45:57 +00003144
Reid Spencer5f016e22007-07-11 17:01:13 +00003145/// [C90] direct-declarator '[' constant-expression[opt] ']'
3146/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3147/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3148/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3149/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3150void Parser::ParseBracketDeclarator(Declarator &D) {
3151 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003152
Chris Lattner378c7e42008-12-18 07:27:21 +00003153 // C array syntax has many features, but by-far the most common is [] and [4].
3154 // This code does a fast path to handle some of the most obvious cases.
3155 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003156 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003157 //FIXME: Use these
3158 CXX0XAttributeList Attr;
3159 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3160 Attr = ParseCXX0XAttributes();
3161 }
3162
Chris Lattner378c7e42008-12-18 07:27:21 +00003163 // Remember that we parsed the empty array type.
3164 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003165 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3166 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003167 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003168 return;
3169 } else if (Tok.getKind() == tok::numeric_constant &&
3170 GetLookAheadToken(1).is(tok::r_square)) {
3171 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00003172 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003173 ConsumeToken();
3174
Sebastian Redlab197ba2009-02-09 18:23:29 +00003175 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003176 //FIXME: Use these
3177 CXX0XAttributeList Attr;
3178 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3179 Attr = ParseCXX0XAttributes();
3180 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003181
3182 // If there was an error parsing the assignment-expression, recover.
3183 if (ExprRes.isInvalid())
3184 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Chris Lattner378c7e42008-12-18 07:27:21 +00003186 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003187 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3188 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003189 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003190 return;
3191 }
Mike Stump1eb44332009-09-09 15:08:12 +00003192
Reid Spencer5f016e22007-07-11 17:01:13 +00003193 // If valid, this location is the position where we read the 'static' keyword.
3194 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003195 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003196 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003197
Reid Spencer5f016e22007-07-11 17:01:13 +00003198 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003199 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003200 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003201 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003202
Reid Spencer5f016e22007-07-11 17:01:13 +00003203 // If we haven't already read 'static', check to see if there is one after the
3204 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003205 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003206 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003207
Reid Spencer5f016e22007-07-11 17:01:13 +00003208 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3209 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00003210 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00003211
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003212 // Handle the case where we have '[*]' as the array size. However, a leading
3213 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3214 // the the token after the star is a ']'. Since stars in arrays are
3215 // infrequent, use of lookahead is not costly here.
3216 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003217 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003218
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003219 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003220 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003221 StaticLoc = SourceLocation(); // Drop the static.
3222 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003223 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003224 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003225 // Note, in C89, this production uses the constant-expr production instead
3226 // of assignment-expr. The only difference is that assignment-expr allows
3227 // things like '=' and '*='. Sema rejects these in C89 mode because they
3228 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003229
Douglas Gregore0762c92009-06-19 23:52:42 +00003230 // Parse the constant-expression or assignment-expression now (depending
3231 // on dialect).
3232 if (getLang().CPlusPlus)
3233 NumElements = ParseConstantExpression();
3234 else
3235 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003236 }
Mike Stump1eb44332009-09-09 15:08:12 +00003237
Reid Spencer5f016e22007-07-11 17:01:13 +00003238 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003239 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003240 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003241 // If the expression was invalid, skip it.
3242 SkipUntil(tok::r_square);
3243 return;
3244 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003245
3246 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3247
Sean Huntbbd37c62009-11-21 08:43:09 +00003248 //FIXME: Use these
3249 CXX0XAttributeList Attr;
3250 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3251 Attr = ParseCXX0XAttributes();
3252 }
3253
Chris Lattner378c7e42008-12-18 07:27:21 +00003254 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3256 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003257 NumElements.release(),
3258 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003259 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003260}
3261
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003262/// [GNU] typeof-specifier:
3263/// typeof ( expressions )
3264/// typeof ( type-name )
3265/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003266///
3267void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003268 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003269 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003270 SourceLocation StartLoc = ConsumeToken();
3271
John McCallcfb708c2010-01-13 20:03:27 +00003272 const bool hasParens = Tok.is(tok::l_paren);
3273
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003274 bool isCastExpr;
3275 TypeTy *CastTy;
3276 SourceRange CastRange;
3277 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3278 isCastExpr,
3279 CastTy,
3280 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003281 if (hasParens)
3282 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003283
3284 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003285 // FIXME: Not accurate, the range gets one token more than it should.
3286 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003287 else
3288 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003289
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003290 if (isCastExpr) {
3291 if (!CastTy) {
3292 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003293 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003294 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003295
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003296 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003297 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003298 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3299 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003300 DiagID, CastTy))
3301 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003302 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003303 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003304
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003305 // If we get here, the operand to the typeof was an expresion.
3306 if (Operand.isInvalid()) {
3307 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003308 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003309 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003310
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003311 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003312 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003313 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3314 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003315 DiagID, Operand.release()))
3316 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003317}
Chris Lattner1b492422010-02-28 18:33:55 +00003318
3319
3320/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3321/// from TryAltiVecVectorToken.
3322bool Parser::TryAltiVecVectorTokenOutOfLine() {
3323 Token Next = NextToken();
3324 switch (Next.getKind()) {
3325 default: return false;
3326 case tok::kw_short:
3327 case tok::kw_long:
3328 case tok::kw_signed:
3329 case tok::kw_unsigned:
3330 case tok::kw_void:
3331 case tok::kw_char:
3332 case tok::kw_int:
3333 case tok::kw_float:
3334 case tok::kw_double:
3335 case tok::kw_bool:
3336 case tok::kw___pixel:
3337 Tok.setKind(tok::kw___vector);
3338 return true;
3339 case tok::identifier:
3340 if (Next.getIdentifierInfo() == Ident_pixel) {
3341 Tok.setKind(tok::kw___vector);
3342 return true;
3343 }
3344 return false;
3345 }
3346}
3347
3348bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3349 const char *&PrevSpec, unsigned &DiagID,
3350 bool &isInvalid) {
3351 if (Tok.getIdentifierInfo() == Ident_vector) {
3352 Token Next = NextToken();
3353 switch (Next.getKind()) {
3354 case tok::kw_short:
3355 case tok::kw_long:
3356 case tok::kw_signed:
3357 case tok::kw_unsigned:
3358 case tok::kw_void:
3359 case tok::kw_char:
3360 case tok::kw_int:
3361 case tok::kw_float:
3362 case tok::kw_double:
3363 case tok::kw_bool:
3364 case tok::kw___pixel:
3365 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3366 return true;
3367 case tok::identifier:
3368 if (Next.getIdentifierInfo() == Ident_pixel) {
3369 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3370 return true;
3371 }
3372 break;
3373 default:
3374 break;
3375 }
3376 } else if (Tok.getIdentifierInfo() == Ident_pixel &&
3377 DS.isTypeAltiVecVector()) {
3378 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3379 return true;
3380 }
3381 return false;
3382}
3383