blob: df02f95d67571ea312aba35d8449e1250c90a429 [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
Douglas Gregor23c94db2010-07-02 17:43:08 +000045 return Actions.ActOnTypeName(getCurScope(), 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) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000280 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
281 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
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) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000312 ParenBraceBracketBalancer BalancerRAIIObj(*this);
313
Chris Lattner682bf922009-03-29 16:50:03 +0000314 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000315 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000316 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000317 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000318 if (Attr.HasAttr)
319 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
320 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000321 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000322 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000323 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000324 if (Attr.HasAttr)
325 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
326 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000327 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000328 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000329 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000330 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000331 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000332 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000333 if (Attr.HasAttr)
334 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
335 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000336 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000337 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000338 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000339 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000340 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000341
Chris Lattner682bf922009-03-29 16:50:03 +0000342 // This routine returns a DeclGroup, if the thing we parsed only contains a
343 // single decl, convert it now.
344 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000345}
346
347/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
348/// declaration-specifiers init-declarator-list[opt] ';'
349///[C90/C++]init-declarator-list ';' [TODO]
350/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000351///
352/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000353/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000354Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000355 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000356 AttributeList *Attr,
357 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000359 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000360 if (Attr)
361 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000362 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
363 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
366 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000367 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000368 if (RequireSemi) ConsumeToken();
Douglas Gregor23c94db2010-07-02 17:43:08 +0000369 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000370 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000371 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000372 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner5c5db552010-04-05 18:18:31 +0000375 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000376}
Mike Stump1eb44332009-09-09 15:08:12 +0000377
John McCalld8ac0572009-11-03 19:26:08 +0000378/// ParseDeclGroup - Having concluded that this is either a function
379/// definition or a group of object declarations, actually parse the
380/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000381Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
382 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000383 bool AllowFunctionDefinitions,
384 SourceLocation *DeclEnd) {
385 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000386 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000387 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000388
John McCalld8ac0572009-11-03 19:26:08 +0000389 // Bail out if the first declarator didn't seem well-formed.
390 if (!D.hasName() && !D.mayOmitIdentifier()) {
391 // Skip until ; or }.
392 SkipUntil(tok::r_brace, true, true);
393 if (Tok.is(tok::semi))
394 ConsumeToken();
395 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
John McCalld8ac0572009-11-03 19:26:08 +0000398 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
399 if (isDeclarationAfterDeclarator()) {
400 // Fall though. We have to check this first, though, because
401 // __attribute__ might be the start of a function definition in
402 // (extended) K&R C.
403 } else if (isStartOfFunctionDefinition()) {
404 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
405 Diag(Tok, diag::err_function_declared_typedef);
406
407 // Recover by treating the 'typedef' as spurious.
408 DS.ClearStorageClassSpecs();
409 }
410
411 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
412 return Actions.ConvertDeclToDeclGroup(TheDecl);
413 } else {
414 Diag(Tok, diag::err_expected_fn_body);
415 SkipUntil(tok::semi);
416 return DeclGroupPtrTy();
417 }
418 }
419
420 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
421 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000422 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000423 if (FirstDecl.get())
424 DeclsInGroup.push_back(FirstDecl);
425
426 // If we don't have a comma, it is either the end of the list (a ';') or an
427 // error, bail out.
428 while (Tok.is(tok::comma)) {
429 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000430 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000431
432 // Parse the next declarator.
433 D.clear();
434
435 // Accept attributes in an init-declarator. In the first declarator in a
436 // declaration, these would be part of the declspec. In subsequent
437 // declarators, they become part of the declarator itself, so that they
438 // don't apply to declarators after *this* one. Examples:
439 // short __attribute__((common)) var; -> declspec
440 // short var __attribute__((common)); -> declarator
441 // short x, __attribute__((common)) var; -> declarator
442 if (Tok.is(tok::kw___attribute)) {
443 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000444 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000445 D.AddAttributes(AttrList, Loc);
446 }
447
448 ParseDeclarator(D);
449
450 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000451 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000452 if (ThisDecl.get())
453 DeclsInGroup.push_back(ThisDecl);
454 }
455
456 if (DeclEnd)
457 *DeclEnd = Tok.getLocation();
458
459 if (Context != Declarator::ForContext &&
460 ExpectAndConsume(tok::semi,
461 Context == Declarator::FileContext
462 ? diag::err_invalid_token_after_toplevel_declarator
463 : diag::err_expected_semi_declaration)) {
464 SkipUntil(tok::r_brace, true, true);
465 if (Tok.is(tok::semi))
466 ConsumeToken();
467 }
468
Douglas Gregor23c94db2010-07-02 17:43:08 +0000469 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000470 DeclsInGroup.data(),
471 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000472}
473
Douglas Gregor1426e532009-05-12 21:31:51 +0000474/// \brief Parse 'declaration' after parsing 'declaration-specifiers
475/// declarator'. This method parses the remainder of the declaration
476/// (including any attributes or initializer, among other things) and
477/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000478///
Reid Spencer5f016e22007-07-11 17:01:13 +0000479/// init-declarator: [C99 6.7]
480/// declarator
481/// declarator '=' initializer
482/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
483/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000484/// [C++] declarator initializer[opt]
485///
486/// [C++] initializer:
487/// [C++] '=' initializer-clause
488/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000489/// [C++0x] '=' 'default' [TODO]
490/// [C++0x] '=' 'delete'
491///
492/// According to the standard grammar, =default and =delete are function
493/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000494///
Douglas Gregore542c862009-06-23 23:11:28 +0000495Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
496 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000497 // If a simple-asm-expr is present, parse it.
498 if (Tok.is(tok::kw_asm)) {
499 SourceLocation Loc;
500 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
501 if (AsmLabel.isInvalid()) {
502 SkipUntil(tok::semi, true, true);
503 return DeclPtrTy();
504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregor1426e532009-05-12 21:31:51 +0000506 D.setAsmLabel(AsmLabel.release());
507 D.SetRangeEnd(Loc);
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregor1426e532009-05-12 21:31:51 +0000510 // If attributes are present, parse them.
511 if (Tok.is(tok::kw___attribute)) {
512 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000513 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000514 D.AddAttributes(AttrList, Loc);
515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Douglas Gregor1426e532009-05-12 21:31:51 +0000517 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000518 DeclPtrTy ThisDecl;
519 switch (TemplateInfo.Kind) {
520 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000521 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000522 break;
523
524 case ParsedTemplateInfo::Template:
525 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000526 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Douglas Gregore542c862009-06-23 23:11:28 +0000527 Action::MultiTemplateParamsArg(Actions,
528 TemplateInfo.TemplateParams->data(),
529 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000530 D);
531 break;
532
533 case ParsedTemplateInfo::ExplicitInstantiation: {
534 Action::DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000535 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000536 TemplateInfo.ExternLoc,
537 TemplateInfo.TemplateLoc,
538 D);
539 if (ThisRes.isInvalid()) {
540 SkipUntil(tok::semi, true, true);
541 return DeclPtrTy();
542 }
543
544 ThisDecl = ThisRes.get();
545 break;
546 }
547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregor1426e532009-05-12 21:31:51 +0000549 // Parse declarator '=' initializer.
550 if (Tok.is(tok::equal)) {
551 ConsumeToken();
552 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
553 SourceLocation DelLoc = ConsumeToken();
554 Actions.SetDeclDeleted(ThisDecl, DelLoc);
555 } else {
John McCall731ad842009-12-19 09:28:58 +0000556 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
557 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000558 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000559 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000560
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000561 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000562 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000563 ConsumeCodeCompletionToken();
564 SkipUntil(tok::comma, true, true);
565 return ThisDecl;
566 }
567
Douglas Gregor1426e532009-05-12 21:31:51 +0000568 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000569
John McCall731ad842009-12-19 09:28:58 +0000570 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000571 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000572 ExitScope();
573 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000574
Douglas Gregor1426e532009-05-12 21:31:51 +0000575 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000576 SkipUntil(tok::comma, true, true);
577 Actions.ActOnInitializerError(ThisDecl);
578 } else
579 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000580 }
581 } else if (Tok.is(tok::l_paren)) {
582 // Parse C++ direct initializer: '(' expression-list ')'
583 SourceLocation LParenLoc = ConsumeParen();
584 ExprVector Exprs(Actions);
585 CommaLocsTy CommaLocs;
586
Douglas Gregorb4debae2009-12-22 17:47:17 +0000587 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
588 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000589 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000590 }
591
Douglas Gregor1426e532009-05-12 21:31:51 +0000592 if (ParseExpressionList(Exprs, CommaLocs)) {
593 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000594
595 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000596 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000597 ExitScope();
598 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000599 } else {
600 // Match the ')'.
601 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
602
603 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
604 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000605
606 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000607 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000608 ExitScope();
609 }
610
Douglas Gregor1426e532009-05-12 21:31:51 +0000611 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
612 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000613 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000614 }
615 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000616 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000617 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
618 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000619 }
620
621 return ThisDecl;
622}
623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624/// ParseSpecifierQualifierList
625/// specifier-qualifier-list:
626/// type-specifier specifier-qualifier-list[opt]
627/// type-qualifier specifier-qualifier-list[opt]
628/// [GNU] attributes specifier-qualifier-list[opt]
629///
630void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
631 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
632 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 // Validate declspec for type-name.
636 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000637 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
638 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // Issue diagnostic and remove storage class if present.
642 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
643 if (DS.getStorageClassSpecLoc().isValid())
644 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
645 else
646 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
647 DS.ClearStorageClassSpecs();
648 }
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 // Issue diagnostic and remove function specfier if present.
651 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000652 if (DS.isInlineSpecified())
653 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
654 if (DS.isVirtualSpecified())
655 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
656 if (DS.isExplicitSpecified())
657 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 DS.ClearFunctionSpecs();
659 }
660}
661
Chris Lattnerc199ab32009-04-12 20:42:31 +0000662/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
663/// specified token is valid after the identifier in a declarator which
664/// immediately follows the declspec. For example, these things are valid:
665///
666/// int x [ 4]; // direct-declarator
667/// int x ( int y); // direct-declarator
668/// int(int x ) // direct-declarator
669/// int x ; // simple-declaration
670/// int x = 17; // init-declarator-list
671/// int x , y; // init-declarator-list
672/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000673/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000674/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000675///
676/// This is not, because 'x' does not immediately follow the declspec (though
677/// ')' happens to be valid anyway).
678/// int (x)
679///
680static bool isValidAfterIdentifierInDeclarator(const Token &T) {
681 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
682 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000683 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000684}
685
Chris Lattnere40c2952009-04-14 21:34:55 +0000686
687/// ParseImplicitInt - This method is called when we have an non-typename
688/// identifier in a declspec (which normally terminates the decl spec) when
689/// the declspec has no type specifier. In this case, the declspec is either
690/// malformed or is "implicit int" (in K&R and C89).
691///
692/// This method handles diagnosing this prettily and returns false if the
693/// declspec is done being processed. If it recovers and thinks there may be
694/// other pieces of declspec after it, it returns true.
695///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000696bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000697 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000698 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000699 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Chris Lattnere40c2952009-04-14 21:34:55 +0000701 SourceLocation Loc = Tok.getLocation();
702 // If we see an identifier that is not a type name, we normally would
703 // parse it as the identifer being declared. However, when a typename
704 // is typo'd or the definition is not included, this will incorrectly
705 // parse the typename as the identifier name and fall over misparsing
706 // later parts of the diagnostic.
707 //
708 // As such, we try to do some look-ahead in cases where this would
709 // otherwise be an "implicit-int" case to see if this is invalid. For
710 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
711 // an identifier with implicit int, we'd get a parse error because the
712 // next token is obviously invalid for a type. Parse these as a case
713 // with an invalid type specifier.
714 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattnere40c2952009-04-14 21:34:55 +0000716 // Since we know that this either implicit int (which is rare) or an
717 // error, we'd do lookahead to try to do better recovery.
718 if (isValidAfterIdentifierInDeclarator(NextToken())) {
719 // If this token is valid for implicit int, e.g. "static x = 4", then
720 // we just avoid eating the identifier, so it will be parsed as the
721 // identifier in the declarator.
722 return false;
723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Chris Lattnere40c2952009-04-14 21:34:55 +0000725 // Otherwise, if we don't consume this token, we are going to emit an
726 // error anyway. Try to recover from various common problems. Check
727 // to see if this was a reference to a tag name without a tag specified.
728 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000729 //
730 // C++ doesn't need this, and isTagName doesn't take SS.
731 if (SS == 0) {
732 const char *TagName = 0;
733 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Douglas Gregor23c94db2010-07-02 17:43:08 +0000735 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000736 default: break;
737 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
738 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
739 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
740 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Chris Lattnerf4382f52009-04-14 22:17:06 +0000743 if (TagName) {
744 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000745 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000746 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattnerf4382f52009-04-14 22:17:06 +0000748 // Parse this as a tag as if the missing tag were present.
749 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000750 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000751 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000752 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000753 return true;
754 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Douglas Gregora786fdb2009-10-13 23:27:22 +0000757 // This is almost certainly an invalid type name. Let the action emit a
758 // diagnostic and attempt to recover.
759 Action::TypeTy *T = 0;
760 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000761 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000762 // The action emitted a diagnostic, so we don't have to.
763 if (T) {
764 // The action has suggested that the type T could be used. Set that as
765 // the type in the declaration specifiers, consume the would-be type
766 // name token, and we're done.
767 const char *PrevSpec;
768 unsigned DiagID;
769 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
770 false);
771 DS.SetRangeEnd(Tok.getLocation());
772 ConsumeToken();
773
774 // There may be other declaration specifiers after this.
775 return true;
776 }
777
778 // Fall through; the action had no suggestion for us.
779 } else {
780 // The action did not emit a diagnostic, so emit one now.
781 SourceRange R;
782 if (SS) R = SS->getRange();
783 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregora786fdb2009-10-13 23:27:22 +0000786 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000787 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000788 unsigned DiagID;
789 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000790 DS.SetRangeEnd(Tok.getLocation());
791 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Chris Lattnere40c2952009-04-14 21:34:55 +0000793 // TODO: Could inject an invalid typedef decl in an enclosing scope to
794 // avoid rippling error messages on subsequent uses of the same type,
795 // could be useful if #include was forgotten.
796 return false;
797}
798
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000799/// \brief Determine the declaration specifier context from the declarator
800/// context.
801///
802/// \param Context the declarator context, which is one of the
803/// Declarator::TheContext enumerator values.
804Parser::DeclSpecContext
805Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
806 if (Context == Declarator::MemberContext)
807 return DSC_class;
808 if (Context == Declarator::FileContext)
809 return DSC_top_level;
810 return DSC_normal;
811}
812
Reid Spencer5f016e22007-07-11 17:01:13 +0000813/// ParseDeclarationSpecifiers
814/// declaration-specifiers: [C99 6.7]
815/// storage-class-specifier declaration-specifiers[opt]
816/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000817/// [C99] function-specifier declaration-specifiers[opt]
818/// [GNU] attributes declaration-specifiers[opt]
819///
820/// storage-class-specifier: [C99 6.7.1]
821/// 'typedef'
822/// 'extern'
823/// 'static'
824/// 'auto'
825/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000826/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000827/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000828/// function-specifier: [C99 6.7.4]
829/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000830/// [C++] 'virtual'
831/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000832/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000833/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000836void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000837 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000838 AccessSpecifier AS,
839 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000840 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000841 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
842 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
843 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
844 : Action::CCC_Template;
845 else if (DSContext == DSC_class)
846 CCC = Action::CCC_Class;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000847 else if (ObjCImpDecl)
848 CCC = Action::CCC_ObjCImplementation;
849
Douglas Gregor23c94db2010-07-02 17:43:08 +0000850 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Douglas Gregordc845342010-05-25 05:58:43 +0000851 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +0000852 }
853
Chris Lattner81c018d2008-03-13 06:29:04 +0000854 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000856 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000858 unsigned DiagID = 0;
859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000861
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000863 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000864 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 // If this is not a declaration specifier token, we're done reading decl
866 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000867 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattner5e02c472009-01-05 00:07:25 +0000870 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000871 // C++ scope specifier. Annotate and loop, or bail out on error.
872 if (TryAnnotateCXXScopeToken(true)) {
873 if (!DS.hasTypeSpecifier())
874 DS.SetTypeSpecError();
875 goto DoneWithDeclSpec;
876 }
John McCall2e0a7152010-03-01 18:20:46 +0000877 if (Tok.is(tok::coloncolon)) // ::new or ::delete
878 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000879 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000880
881 case tok::annot_cxxscope: {
882 if (DS.hasTypeSpecifier())
883 goto DoneWithDeclSpec;
884
John McCallaa87d332009-12-12 11:40:51 +0000885 CXXScopeSpec SS;
886 SS.setScopeRep(Tok.getAnnotationValue());
887 SS.setRange(Tok.getAnnotationRange());
888
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000889 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000890 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000891 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000892 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000893 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000894 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000895
896 // C++ [class.qual]p2:
897 // In a lookup in which the constructor is an acceptable lookup
898 // result and the nested-name-specifier nominates a class C:
899 //
900 // - if the name specified after the
901 // nested-name-specifier, when looked up in C, is the
902 // injected-class-name of C (Clause 9), or
903 //
904 // - if the name specified after the nested-name-specifier
905 // is the same as the identifier or the
906 // simple-template-id's template-name in the last
907 // component of the nested-name-specifier,
908 //
909 // the name is instead considered to name the constructor of
910 // class C.
911 //
912 // Thus, if the template-name is actually the constructor
913 // name, then the code is ill-formed; this interpretation is
914 // reinforced by the NAD status of core issue 635.
915 TemplateIdAnnotation *TemplateId
916 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000917 if ((DSContext == DSC_top_level ||
918 (DSContext == DSC_class && DS.isFriendSpecified())) &&
919 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000920 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000921 if (isConstructorDeclarator()) {
922 // The user meant this to be an out-of-line constructor
923 // definition, but template arguments are not allowed
924 // there. Just allow this as a constructor; we'll
925 // complain about it later.
926 goto DoneWithDeclSpec;
927 }
928
929 // The user meant this to name a type, but it actually names
930 // a constructor with some extraneous template
931 // arguments. Complain, then parse it as a type as the user
932 // intended.
933 Diag(TemplateId->TemplateNameLoc,
934 diag::err_out_of_line_template_id_names_constructor)
935 << TemplateId->Name;
936 }
937
John McCallaa87d332009-12-12 11:40:51 +0000938 DS.getTypeSpecScope() = SS;
939 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000940 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000941 "ParseOptionalCXXScopeSpecifier not working");
942 AnnotateTemplateIdTokenAsType(&SS);
943 continue;
944 }
945
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000946 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000947 DS.getTypeSpecScope() = SS;
948 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000949 if (Tok.getAnnotationValue())
950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
951 PrevSpec, DiagID,
952 Tok.getAnnotationValue());
953 else
954 DS.SetTypeSpecError();
955 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
956 ConsumeToken(); // The typename
957 }
958
Douglas Gregor9135c722009-03-25 15:40:00 +0000959 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000960 goto DoneWithDeclSpec;
961
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000962 // If we're in a context where the identifier could be a class name,
963 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +0000964 if ((DSContext == DSC_top_level ||
965 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000966 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000967 &SS)) {
968 if (isConstructorDeclarator())
969 goto DoneWithDeclSpec;
970
971 // As noted in C++ [class.qual]p2 (cited above), when the name
972 // of the class is qualified in a context where it could name
973 // a constructor, its a constructor name. However, we've
974 // looked at the declarator, and the user probably meant this
975 // to be a type. Complain that it isn't supposed to be treated
976 // as a type, then proceed to parse it as a type.
977 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
978 << Next.getIdentifierInfo();
979 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000980
Douglas Gregorb696ea32009-02-04 17:00:24 +0000981 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Douglas Gregor23c94db2010-07-02 17:43:08 +0000982 Next.getLocation(), getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000983
Chris Lattnerf4382f52009-04-14 22:17:06 +0000984 // If the referenced identifier is not a type, then this declspec is
985 // erroneous: We already checked about that it has no type specifier, and
986 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000987 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000988 if (TypeRep == 0) {
989 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000990 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000991 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
John McCallaa87d332009-12-12 11:40:51 +0000994 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000995 ConsumeToken(); // The C++ scope.
996
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000997 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000998 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000999 if (isInvalid)
1000 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001002 DS.SetRangeEnd(Tok.getLocation());
1003 ConsumeToken(); // The typename.
1004
1005 continue;
1006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Chris Lattner80d0c892009-01-21 19:48:37 +00001008 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001009 if (Tok.getAnnotationValue())
1010 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001011 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001012 else
1013 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001014
1015 if (isInvalid)
1016 break;
1017
Chris Lattner80d0c892009-01-21 19:48:37 +00001018 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1019 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Chris Lattner80d0c892009-01-21 19:48:37 +00001021 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1022 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1023 // Objective-C interface. If we don't have Objective-C or a '<', this is
1024 // just a normal reference to a typedef name.
1025 if (!Tok.is(tok::less) || !getLang().ObjC1)
1026 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001028 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001029 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001030 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1031 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1032 LAngleLoc, EndProtoLoc);
1033 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1034 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Chris Lattner80d0c892009-01-21 19:48:37 +00001036 DS.SetRangeEnd(EndProtoLoc);
1037 continue;
1038 }
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattner3bd934a2008-07-26 01:18:38 +00001040 // typedef-name
1041 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001042 // In C++, check to see if this is a scope specifier like foo::bar::, if
1043 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001044 if (getLang().CPlusPlus) {
1045 if (TryAnnotateCXXScopeToken(true)) {
1046 if (!DS.hasTypeSpecifier())
1047 DS.SetTypeSpecError();
1048 goto DoneWithDeclSpec;
1049 }
1050 if (!Tok.is(tok::identifier))
1051 continue;
1052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Chris Lattner3bd934a2008-07-26 01:18:38 +00001054 // This identifier can only be a typedef name if we haven't already seen
1055 // a type-specifier. Without this check we misparse:
1056 // typedef int X; struct Y { short X; }; as 'short int'.
1057 if (DS.hasTypeSpecifier())
1058 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001059
John Thompson82287d12010-02-05 00:12:22 +00001060 // Check for need to substitute AltiVec keyword tokens.
1061 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1062 break;
1063
Chris Lattner3bd934a2008-07-26 01:18:38 +00001064 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +00001065 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00001066 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001067
Chris Lattnerc199ab32009-04-12 20:42:31 +00001068 // If this is not a typedef name, don't parse it as part of the declspec,
1069 // it must be an implicit int or an error.
1070 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001071 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001072 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001073 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001074
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001075 // If we're in a context where the identifier could be a class name,
1076 // check whether this is a constructor declaration.
1077 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001078 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001079 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001080 goto DoneWithDeclSpec;
1081
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001082 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001083 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001084 if (isInvalid)
1085 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattner3bd934a2008-07-26 01:18:38 +00001087 DS.SetRangeEnd(Tok.getLocation());
1088 ConsumeToken(); // The identifier
1089
1090 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1091 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1092 // Objective-C interface. If we don't have Objective-C or a '<', this is
1093 // just a normal reference to a typedef name.
1094 if (!Tok.is(tok::less) || !getLang().ObjC1)
1095 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001097 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001098 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001099 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1100 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1101 LAngleLoc, EndProtoLoc);
1102 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1103 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner3bd934a2008-07-26 01:18:38 +00001105 DS.SetRangeEnd(EndProtoLoc);
1106
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001107 // Need to support trailing type qualifiers (e.g. "id<p> const").
1108 // If a type specifier follows, it will be diagnosed elsewhere.
1109 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001110 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001111
1112 // type-name
1113 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001114 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001115 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001116 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001117 // This template-id does not refer to a type name, so we're
1118 // done with the type-specifiers.
1119 goto DoneWithDeclSpec;
1120 }
1121
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001122 // If we're in a context where the template-id could be a
1123 // constructor name or specialization, check whether this is a
1124 // constructor declaration.
1125 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001126 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001127 isConstructorDeclarator())
1128 goto DoneWithDeclSpec;
1129
Douglas Gregor39a8de12009-02-25 19:37:18 +00001130 // Turn the template-id annotation token into a type annotation
1131 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001132 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001133 continue;
1134 }
1135
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 // GNU attributes support.
1137 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001138 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001140
1141 // Microsoft declspec support.
1142 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001143 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001144 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Steve Naroff239f0732008-12-25 14:16:32 +00001146 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001147 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001148 // FIXME: Add handling here!
1149 break;
1150
1151 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001152 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001153 case tok::kw___cdecl:
1154 case tok::kw___stdcall:
1155 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001156 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001157 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1158 continue;
1159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 // storage-class-specifier
1161 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001162 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1163 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 break;
1165 case tok::kw_extern:
1166 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001167 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001168 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1169 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001171 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001172 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001173 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001174 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 case tok::kw_static:
1176 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001177 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001178 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1179 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 break;
1181 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001182 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001183 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1184 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001185 else
John McCallfec54012009-08-03 20:12:06 +00001186 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1187 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 break;
1189 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001190 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1191 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001193 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001194 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1195 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001196 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001198 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 // function-specifier
1202 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001203 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001205 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001206 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001207 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001208 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001209 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001210 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001211
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001212 // friend
1213 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001214 if (DSContext == DSC_class)
1215 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1216 else {
1217 PrevSpec = ""; // not actually used by the diagnostic
1218 DiagID = diag::err_friend_invalid_in_context;
1219 isInvalid = true;
1220 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001221 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Sebastian Redl2ac67232009-11-05 15:47:02 +00001223 // constexpr
1224 case tok::kw_constexpr:
1225 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1226 break;
1227
Chris Lattner80d0c892009-01-21 19:48:37 +00001228 // type-specifier
1229 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001230 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1231 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001232 break;
1233 case tok::kw_long:
1234 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001235 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1236 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001237 else
John McCallfec54012009-08-03 20:12:06 +00001238 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1239 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001240 break;
1241 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001242 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1243 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001244 break;
1245 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001246 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1247 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001248 break;
1249 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001250 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1251 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001252 break;
1253 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001254 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1255 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001256 break;
1257 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001258 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1259 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001260 break;
1261 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1263 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001264 break;
1265 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1267 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001268 break;
1269 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001270 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1271 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001272 break;
1273 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001274 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1275 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001276 break;
1277 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1279 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001280 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001281 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001282 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1283 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001284 break;
1285 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001286 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1287 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001288 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001289 case tok::kw_bool:
1290 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1292 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001293 break;
1294 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1296 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001297 break;
1298 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1300 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001301 break;
1302 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1304 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001305 break;
John Thompson82287d12010-02-05 00:12:22 +00001306 case tok::kw___vector:
1307 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1308 break;
1309 case tok::kw___pixel:
1310 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1311 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001312
1313 // class-specifier:
1314 case tok::kw_class:
1315 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001316 case tok::kw_union: {
1317 tok::TokenKind Kind = Tok.getKind();
1318 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001319 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001320 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001321 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001322
1323 // enum-specifier:
1324 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001325 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001326 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001327 continue;
1328
1329 // cv-qualifier:
1330 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001331 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1332 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001333 break;
1334 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001335 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1336 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001337 break;
1338 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001339 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1340 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001341 break;
1342
Douglas Gregord57959a2009-03-27 23:10:48 +00001343 // C++ typename-specifier:
1344 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001345 if (TryAnnotateTypeOrScopeToken()) {
1346 DS.SetTypeSpecError();
1347 goto DoneWithDeclSpec;
1348 }
1349 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001350 continue;
1351 break;
1352
Chris Lattner80d0c892009-01-21 19:48:37 +00001353 // GNU typeof support.
1354 case tok::kw_typeof:
1355 ParseTypeofSpecifier(DS);
1356 continue;
1357
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001358 case tok::kw_decltype:
1359 ParseDecltypeSpecifier(DS);
1360 continue;
1361
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001362 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001363 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001364 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1365 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001366 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001367 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Chris Lattnerbce61352008-07-26 00:20:22 +00001369 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001370 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001371 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001372 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1373 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1374 LAngleLoc, EndProtoLoc);
1375 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1376 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001377 DS.SetRangeEnd(EndProtoLoc);
1378
Chris Lattner1ab3b962008-11-18 07:48:38 +00001379 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001380 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001381 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001382 // Need to support trailing type qualifiers (e.g. "id<p> const").
1383 // If a type specifier follows, it will be diagnosed elsewhere.
1384 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001385 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 }
John McCallfec54012009-08-03 20:12:06 +00001387 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001388 if (isInvalid) {
1389 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001390 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001391 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001393 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 ConsumeToken();
1395 }
1396}
Douglas Gregoradcac882008-12-01 23:54:00 +00001397
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001398/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001399/// primarily follow the C++ grammar with additions for C99 and GNU,
1400/// which together subsume the C grammar. Note that the C++
1401/// type-specifier also includes the C type-qualifier (for const,
1402/// volatile, and C99 restrict). Returns true if a type-specifier was
1403/// found (and parsed), false otherwise.
1404///
1405/// type-specifier: [C++ 7.1.5]
1406/// simple-type-specifier
1407/// class-specifier
1408/// enum-specifier
1409/// elaborated-type-specifier [TODO]
1410/// cv-qualifier
1411///
1412/// cv-qualifier: [C++ 7.1.5.1]
1413/// 'const'
1414/// 'volatile'
1415/// [C99] 'restrict'
1416///
1417/// simple-type-specifier: [ C++ 7.1.5.2]
1418/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1419/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1420/// 'char'
1421/// 'wchar_t'
1422/// 'bool'
1423/// 'short'
1424/// 'int'
1425/// 'long'
1426/// 'signed'
1427/// 'unsigned'
1428/// 'float'
1429/// 'double'
1430/// 'void'
1431/// [C99] '_Bool'
1432/// [C99] '_Complex'
1433/// [C99] '_Imaginary' // Removed in TC2?
1434/// [GNU] '_Decimal32'
1435/// [GNU] '_Decimal64'
1436/// [GNU] '_Decimal128'
1437/// [GNU] typeof-specifier
1438/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1439/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001440/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001441/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001442bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001443 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001444 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001445 const ParsedTemplateInfo &TemplateInfo,
1446 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001447 SourceLocation Loc = Tok.getLocation();
1448
1449 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001450 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001451 // If we already have a type specifier, this identifier is not a type.
1452 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1453 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1454 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1455 return false;
John Thompson82287d12010-02-05 00:12:22 +00001456 // Check for need to substitute AltiVec keyword tokens.
1457 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1458 break;
1459 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001460 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001461 // Annotate typenames and C++ scope specifiers. If we get one, just
1462 // recurse to handle whatever we get.
1463 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001464 return true;
1465 if (Tok.is(tok::identifier))
1466 return false;
1467 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1468 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001469 case tok::coloncolon: // ::foo::bar
1470 if (NextToken().is(tok::kw_new) || // ::new
1471 NextToken().is(tok::kw_delete)) // ::delete
1472 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Chris Lattner166a8fc2009-01-04 23:41:41 +00001474 // Annotate typenames and C++ scope specifiers. If we get one, just
1475 // recurse to handle whatever we get.
1476 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001477 return true;
1478 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1479 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Douglas Gregor12e083c2008-11-07 15:42:26 +00001481 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001482 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001483 if (Tok.getAnnotationValue())
1484 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001485 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001486 else
1487 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001488 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1489 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Douglas Gregor12e083c2008-11-07 15:42:26 +00001491 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1492 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1493 // Objective-C interface. If we don't have Objective-C or a '<', this is
1494 // just a normal reference to a typedef name.
1495 if (!Tok.is(tok::less) || !getLang().ObjC1)
1496 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001498 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001499 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001500 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1501 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1502 LAngleLoc, EndProtoLoc);
1503 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1504 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregor12e083c2008-11-07 15:42:26 +00001506 DS.SetRangeEnd(EndProtoLoc);
1507 return true;
1508 }
1509
1510 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001511 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001512 break;
1513 case tok::kw_long:
1514 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001515 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1516 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001517 else
John McCallfec54012009-08-03 20:12:06 +00001518 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1519 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001520 break;
1521 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001522 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001523 break;
1524 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001525 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1526 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001527 break;
1528 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001529 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1530 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001531 break;
1532 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001533 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1534 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001535 break;
1536 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001537 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001538 break;
1539 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001540 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001541 break;
1542 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001544 break;
1545 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001546 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001547 break;
1548 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001550 break;
1551 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001552 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001553 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001554 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001556 break;
1557 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001559 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001560 case tok::kw_bool:
1561 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001563 break;
1564 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001565 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1566 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001569 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1570 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001571 break;
1572 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1574 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001575 break;
John Thompson82287d12010-02-05 00:12:22 +00001576 case tok::kw___vector:
1577 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1578 break;
1579 case tok::kw___pixel:
1580 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1581 break;
1582
Douglas Gregor12e083c2008-11-07 15:42:26 +00001583 // class-specifier:
1584 case tok::kw_class:
1585 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001586 case tok::kw_union: {
1587 tok::TokenKind Kind = Tok.getKind();
1588 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001589 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1590 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001591 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001592 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593
1594 // enum-specifier:
1595 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001596 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001597 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001598 return true;
1599
1600 // cv-qualifier:
1601 case tok::kw_const:
1602 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001603 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001604 break;
1605 case tok::kw_volatile:
1606 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001607 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001608 break;
1609 case tok::kw_restrict:
1610 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001611 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001612 break;
1613
1614 // GNU typeof support.
1615 case tok::kw_typeof:
1616 ParseTypeofSpecifier(DS);
1617 return true;
1618
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001619 // C++0x decltype support.
1620 case tok::kw_decltype:
1621 ParseDecltypeSpecifier(DS);
1622 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001624 // C++0x auto support.
1625 case tok::kw_auto:
1626 if (!getLang().CPlusPlus0x)
1627 return false;
1628
John McCallfec54012009-08-03 20:12:06 +00001629 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001630 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001631 case tok::kw___ptr64:
1632 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001633 case tok::kw___cdecl:
1634 case tok::kw___stdcall:
1635 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001636 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001637 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001638 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001639
Douglas Gregor12e083c2008-11-07 15:42:26 +00001640 default:
1641 // Not a type-specifier; do nothing.
1642 return false;
1643 }
1644
1645 // If the specifier combination wasn't legal, issue a diagnostic.
1646 if (isInvalid) {
1647 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001648 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001649 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001650 }
1651 DS.SetRangeEnd(Tok.getLocation());
1652 ConsumeToken(); // whatever we parsed above.
1653 return true;
1654}
Reid Spencer5f016e22007-07-11 17:01:13 +00001655
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001656/// ParseStructDeclaration - Parse a struct declaration without the terminating
1657/// semicolon.
1658///
Reid Spencer5f016e22007-07-11 17:01:13 +00001659/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001660/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001661/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001662/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001663/// struct-declarator-list:
1664/// struct-declarator
1665/// struct-declarator-list ',' struct-declarator
1666/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1667/// struct-declarator:
1668/// declarator
1669/// [GNU] declarator attributes[opt]
1670/// declarator[opt] ':' constant-expression
1671/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1672///
Chris Lattnere1359422008-04-10 06:46:29 +00001673void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001674ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001675 if (Tok.is(tok::kw___extension__)) {
1676 // __extension__ silences extension warnings in the subexpression.
1677 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001678 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001679 return ParseStructDeclaration(DS, Fields);
1680 }
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Steve Naroff28a7ca82007-08-20 22:28:22 +00001682 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001683 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001684 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001686 // If there are no declarators, this is a free-standing declaration
1687 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001688 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001689 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001690 return;
1691 }
1692
1693 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001694 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001695 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001696 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001697 FieldDeclarator DeclaratorInfo(DS);
1698
1699 // Attributes are only allowed here on successive declarators.
1700 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1701 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001702 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001703 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1704 }
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Steve Naroff28a7ca82007-08-20 22:28:22 +00001706 /// struct-declarator: declarator
1707 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001708 if (Tok.isNot(tok::colon)) {
1709 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1710 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001711 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001712 }
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Chris Lattner04d66662007-10-09 17:33:22 +00001714 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001715 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001716 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001717 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001718 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001719 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001720 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001721 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001722
Steve Naroff28a7ca82007-08-20 22:28:22 +00001723 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001724 if (Tok.is(tok::kw___attribute)) {
1725 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001726 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001727 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1728 }
1729
John McCallbdd563e2009-11-03 02:38:08 +00001730 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001731 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1732 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001733
Steve Naroff28a7ca82007-08-20 22:28:22 +00001734 // If we don't have a comma, it is either the end of the list (a ';')
1735 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001736 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001737 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001738
Steve Naroff28a7ca82007-08-20 22:28:22 +00001739 // Consume the comma.
1740 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001741
John McCallbdd563e2009-11-03 02:38:08 +00001742 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001743 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001744}
1745
1746/// ParseStructUnionBody
1747/// struct-contents:
1748/// struct-declaration-list
1749/// [EXT] empty
1750/// [GNU] "struct-declaration-list" without terminatoring ';'
1751/// struct-declaration-list:
1752/// struct-declaration
1753/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001754/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001755///
Reid Spencer5f016e22007-07-11 17:01:13 +00001756void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001757 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001758 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1759 PP.getSourceManager(),
1760 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001764 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001765 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001766
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1768 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001769 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001770 Diag(Tok, diag::ext_empty_struct_union_enum)
1771 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001772
Chris Lattnerb28317a2009-03-28 19:18:32 +00001773 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001774
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001776 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001780 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001781 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001782 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001783 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 ConsumeToken();
1785 continue;
1786 }
Chris Lattnere1359422008-04-10 06:46:29 +00001787
1788 // Parse all the comma separated declarators.
1789 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001790
John McCallbdd563e2009-11-03 02:38:08 +00001791 if (!Tok.is(tok::at)) {
1792 struct CFieldCallback : FieldCallback {
1793 Parser &P;
1794 DeclPtrTy TagDecl;
1795 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1796
1797 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1798 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1799 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1800
1801 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001802 // Install the declarator into the current TagDecl.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001803 DeclPtrTy Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001804 FD.D.getDeclSpec().getSourceRange().getBegin(),
1805 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001806 FieldDecls.push_back(Field);
1807 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001808 }
John McCallbdd563e2009-11-03 02:38:08 +00001809 } Callback(*this, TagDecl, FieldDecls);
1810
1811 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001812 } else { // Handle @defs
1813 ConsumeToken();
1814 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1815 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001816 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001817 continue;
1818 }
1819 ConsumeToken();
1820 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1821 if (!Tok.is(tok::identifier)) {
1822 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001823 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001824 continue;
1825 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001826 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001827 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001828 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001829 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1830 ConsumeToken();
1831 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001832 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001833
Chris Lattner04d66662007-10-09 17:33:22 +00001834 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001835 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001836 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001837 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 break;
1839 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001840 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1841 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001842 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001843 // If we stopped at a ';', eat it.
1844 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 }
1846 }
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Steve Naroff60fccee2007-10-29 21:38:07 +00001848 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Ted Kremenek1e377652010-02-11 02:19:13 +00001850 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001852 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001853 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001854
Douglas Gregor23c94db2010-07-02 17:43:08 +00001855 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001856 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001857 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001858 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001859 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001860 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001861}
1862
1863
1864/// ParseEnumSpecifier
1865/// enum-specifier: [C99 6.7.2.2]
1866/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001867///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001868/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1869/// '}' attributes[opt]
1870/// 'enum' identifier
1871/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001872///
1873/// [C++] elaborated-type-specifier:
1874/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1875///
Chris Lattner4c97d762009-04-12 21:49:30 +00001876void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001877 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001878 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001880 if (Tok.is(tok::code_completion)) {
1881 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001882 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001883 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001884 }
1885
Ted Kremenek1e377652010-02-11 02:19:13 +00001886 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001887 // If attributes exist after tag, parse them.
1888 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001889 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001890
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001891 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001892 if (getLang().CPlusPlus) {
1893 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1894 return;
1895
1896 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001897 Diag(Tok, diag::err_expected_ident);
1898 if (Tok.isNot(tok::l_brace)) {
1899 // Has no name and is not a definition.
1900 // Skip the rest of this declarator, up until the comma or semicolon.
1901 SkipUntil(tok::comma, true);
1902 return;
1903 }
1904 }
1905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001907 // Must have either 'enum name' or 'enum {...}'.
1908 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1909 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001911 // Skip the rest of this declarator, up until the comma or semicolon.
1912 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001913 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001914 }
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001916 // If an identifier is present, consume and remember it.
1917 IdentifierInfo *Name = 0;
1918 SourceLocation NameLoc;
1919 if (Tok.is(tok::identifier)) {
1920 Name = Tok.getIdentifierInfo();
1921 NameLoc = ConsumeToken();
1922 }
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001924 // There are three options here. If we have 'enum foo;', then this is a
1925 // forward declaration. If we have 'enum foo {...' then this is a
1926 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1927 //
1928 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1929 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1930 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1931 //
John McCall0f434ec2009-07-31 02:45:11 +00001932 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001933 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001934 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001935 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001936 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001937 else
John McCall0f434ec2009-07-31 02:45:11 +00001938 TUK = Action::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00001939
1940 // enums cannot be templates, although they can be referenced from a
1941 // template.
1942 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1943 TUK != Action::TUK_Reference) {
1944 Diag(Tok, diag::err_enum_template);
1945
1946 // Skip the rest of this declarator, up until the comma or semicolon.
1947 SkipUntil(tok::comma, true);
1948 return;
1949 }
1950
Douglas Gregor402abb52009-05-28 23:31:59 +00001951 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001952 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00001953 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1954 const char *PrevSpec = 0;
1955 unsigned DiagID;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001956 DeclPtrTy TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
Ted Kremenek1e377652010-02-11 02:19:13 +00001957 StartLoc, SS, Name, NameLoc, Attr.get(),
1958 AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001959 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001960 Owned, IsDependent);
Douglas Gregor48c89f42010-04-24 16:38:41 +00001961 if (IsDependent) {
1962 // This enum has a dependent nested-name-specifier. Handle it as a
1963 // dependent tag.
1964 if (!Name) {
1965 DS.SetTypeSpecError();
1966 Diag(Tok, diag::err_expected_type_name_after_typename);
1967 return;
1968 }
1969
Douglas Gregor23c94db2010-07-02 17:43:08 +00001970 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00001971 TUK, SS, Name, StartLoc,
1972 NameLoc);
1973 if (Type.isInvalid()) {
1974 DS.SetTypeSpecError();
1975 return;
1976 }
1977
1978 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
1979 Type.get(), false))
1980 Diag(StartLoc, DiagID) << PrevSpec;
1981
1982 return;
1983 }
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Douglas Gregor48c89f42010-04-24 16:38:41 +00001985 if (!TagDecl.get()) {
1986 // The action failed to produce an enumeration tag. If this is a
1987 // definition, consume the entire definition.
1988 if (Tok.is(tok::l_brace)) {
1989 ConsumeBrace();
1990 SkipUntil(tok::r_brace);
1991 }
1992
1993 DS.SetTypeSpecError();
1994 return;
1995 }
1996
Chris Lattner04d66662007-10-09 17:33:22 +00001997 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002000 // FIXME: The DeclSpec should keep the locations of both the keyword and the
2001 // name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002002 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00002003 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00002004 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002005}
2006
2007/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2008/// enumerator-list:
2009/// enumerator
2010/// enumerator-list ',' enumerator
2011/// enumerator:
2012/// enumeration-constant
2013/// enumeration-constant '=' constant-expression
2014/// enumeration-constant:
2015/// identifier
2016///
Chris Lattnerb28317a2009-03-28 19:18:32 +00002017void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002018 // Enter the scope of the enum body and start the definition.
2019 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002020 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002021
Reid Spencer5f016e22007-07-11 17:01:13 +00002022 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Chris Lattner7946dd32007-08-27 17:24:30 +00002024 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002025 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002026 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002027
Chris Lattnerb28317a2009-03-28 19:18:32 +00002028 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002029
Chris Lattnerb28317a2009-03-28 19:18:32 +00002030 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Reid Spencer5f016e22007-07-11 17:01:13 +00002032 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002033 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2035 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002038 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00002039 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002040 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002041 AssignedVal = ParseConstantExpression();
2042 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002043 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 }
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Reid Spencer5f016e22007-07-11 17:01:13 +00002046 // Install the enumerator constant into EnumDecl.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002047 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002048 LastEnumConstDecl,
2049 IdentLoc, Ident,
2050 EqualLoc,
2051 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 EnumConstantDecls.push_back(EnumConstDecl);
2053 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Chris Lattner04d66662007-10-09 17:33:22 +00002055 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002056 break;
2057 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002058
2059 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002060 !(getLang().C99 || getLang().CPlusPlus0x))
2061 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2062 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002063 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002064 }
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002067 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002068
Ted Kremenek1e377652010-02-11 02:19:13 +00002069 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002071 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002072 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002073
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002074 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2075 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00002076 getCurScope(), Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Douglas Gregor72de6672009-01-08 20:45:30 +00002078 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002079 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002080}
2081
2082/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002083/// start of a type-qualifier-list.
2084bool Parser::isTypeQualifier() const {
2085 switch (Tok.getKind()) {
2086 default: return false;
2087 // type-qualifier
2088 case tok::kw_const:
2089 case tok::kw_volatile:
2090 case tok::kw_restrict:
2091 return true;
2092 }
2093}
2094
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002095/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2096/// is definitely a type-specifier. Return false if it isn't part of a type
2097/// specifier or if we're not sure.
2098bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2099 switch (Tok.getKind()) {
2100 default: return false;
2101 // type-specifiers
2102 case tok::kw_short:
2103 case tok::kw_long:
2104 case tok::kw_signed:
2105 case tok::kw_unsigned:
2106 case tok::kw__Complex:
2107 case tok::kw__Imaginary:
2108 case tok::kw_void:
2109 case tok::kw_char:
2110 case tok::kw_wchar_t:
2111 case tok::kw_char16_t:
2112 case tok::kw_char32_t:
2113 case tok::kw_int:
2114 case tok::kw_float:
2115 case tok::kw_double:
2116 case tok::kw_bool:
2117 case tok::kw__Bool:
2118 case tok::kw__Decimal32:
2119 case tok::kw__Decimal64:
2120 case tok::kw__Decimal128:
2121 case tok::kw___vector:
2122
2123 // struct-or-union-specifier (C99) or class-specifier (C++)
2124 case tok::kw_class:
2125 case tok::kw_struct:
2126 case tok::kw_union:
2127 // enum-specifier
2128 case tok::kw_enum:
2129
2130 // typedef-name
2131 case tok::annot_typename:
2132 return true;
2133 }
2134}
2135
Steve Naroff5f8aa692008-02-11 23:15:56 +00002136/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002137/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002138bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 switch (Tok.getKind()) {
2140 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Chris Lattner166a8fc2009-01-04 23:41:41 +00002142 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002143 if (TryAltiVecVectorToken())
2144 return true;
2145 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002146 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002147 // Annotate typenames and C++ scope specifiers. If we get one, just
2148 // recurse to handle whatever we get.
2149 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002150 return true;
2151 if (Tok.is(tok::identifier))
2152 return false;
2153 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002154
Chris Lattner166a8fc2009-01-04 23:41:41 +00002155 case tok::coloncolon: // ::foo::bar
2156 if (NextToken().is(tok::kw_new) || // ::new
2157 NextToken().is(tok::kw_delete)) // ::delete
2158 return false;
2159
Chris Lattner166a8fc2009-01-04 23:41:41 +00002160 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002161 return true;
2162 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Reid Spencer5f016e22007-07-11 17:01:13 +00002164 // GNU attributes support.
2165 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002166 // GNU typeof support.
2167 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Reid Spencer5f016e22007-07-11 17:01:13 +00002169 // type-specifiers
2170 case tok::kw_short:
2171 case tok::kw_long:
2172 case tok::kw_signed:
2173 case tok::kw_unsigned:
2174 case tok::kw__Complex:
2175 case tok::kw__Imaginary:
2176 case tok::kw_void:
2177 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002178 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002179 case tok::kw_char16_t:
2180 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 case tok::kw_int:
2182 case tok::kw_float:
2183 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002184 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002185 case tok::kw__Bool:
2186 case tok::kw__Decimal32:
2187 case tok::kw__Decimal64:
2188 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002189 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Chris Lattner99dc9142008-04-13 18:59:07 +00002191 // struct-or-union-specifier (C99) or class-specifier (C++)
2192 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 case tok::kw_struct:
2194 case tok::kw_union:
2195 // enum-specifier
2196 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 // type-qualifier
2199 case tok::kw_const:
2200 case tok::kw_volatile:
2201 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002202
2203 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002204 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002205 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002206
Chris Lattner7c186be2008-10-20 00:25:30 +00002207 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2208 case tok::less:
2209 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Steve Naroff239f0732008-12-25 14:16:32 +00002211 case tok::kw___cdecl:
2212 case tok::kw___stdcall:
2213 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002214 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002215 case tok::kw___w64:
2216 case tok::kw___ptr64:
2217 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002218 }
2219}
2220
2221/// isDeclarationSpecifier() - Return true if the current token is part of a
2222/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002223bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002224 switch (Tok.getKind()) {
2225 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002226
Chris Lattner166a8fc2009-01-04 23:41:41 +00002227 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002228 // Unfortunate hack to support "Class.factoryMethod" notation.
2229 if (getLang().ObjC1 && NextToken().is(tok::period))
2230 return false;
John Thompson82287d12010-02-05 00:12:22 +00002231 if (TryAltiVecVectorToken())
2232 return true;
2233 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002234 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002235 // Annotate typenames and C++ scope specifiers. If we get one, just
2236 // recurse to handle whatever we get.
2237 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002238 return true;
2239 if (Tok.is(tok::identifier))
2240 return false;
2241 return isDeclarationSpecifier();
2242
Chris Lattner166a8fc2009-01-04 23:41:41 +00002243 case tok::coloncolon: // ::foo::bar
2244 if (NextToken().is(tok::kw_new) || // ::new
2245 NextToken().is(tok::kw_delete)) // ::delete
2246 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Chris Lattner166a8fc2009-01-04 23:41:41 +00002248 // Annotate typenames and C++ scope specifiers. If we get one, just
2249 // recurse to handle whatever we get.
2250 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002251 return true;
2252 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Reid Spencer5f016e22007-07-11 17:01:13 +00002254 // storage-class-specifier
2255 case tok::kw_typedef:
2256 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002257 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002258 case tok::kw_static:
2259 case tok::kw_auto:
2260 case tok::kw_register:
2261 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002262
Reid Spencer5f016e22007-07-11 17:01:13 +00002263 // type-specifiers
2264 case tok::kw_short:
2265 case tok::kw_long:
2266 case tok::kw_signed:
2267 case tok::kw_unsigned:
2268 case tok::kw__Complex:
2269 case tok::kw__Imaginary:
2270 case tok::kw_void:
2271 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002272 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002273 case tok::kw_char16_t:
2274 case tok::kw_char32_t:
2275
Reid Spencer5f016e22007-07-11 17:01:13 +00002276 case tok::kw_int:
2277 case tok::kw_float:
2278 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002279 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002280 case tok::kw__Bool:
2281 case tok::kw__Decimal32:
2282 case tok::kw__Decimal64:
2283 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002284 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Chris Lattner99dc9142008-04-13 18:59:07 +00002286 // struct-or-union-specifier (C99) or class-specifier (C++)
2287 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002288 case tok::kw_struct:
2289 case tok::kw_union:
2290 // enum-specifier
2291 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002292
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 // type-qualifier
2294 case tok::kw_const:
2295 case tok::kw_volatile:
2296 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002297
Reid Spencer5f016e22007-07-11 17:01:13 +00002298 // function-specifier
2299 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002300 case tok::kw_virtual:
2301 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002302
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002303 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002304 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002305
Chris Lattner1ef08762007-08-09 17:01:07 +00002306 // GNU typeof support.
2307 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Chris Lattner1ef08762007-08-09 17:01:07 +00002309 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002310 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002311 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Chris Lattnerf3948c42008-07-26 03:38:44 +00002313 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2314 case tok::less:
2315 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Steve Naroff47f52092009-01-06 19:34:12 +00002317 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002318 case tok::kw___cdecl:
2319 case tok::kw___stdcall:
2320 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002321 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002322 case tok::kw___w64:
2323 case tok::kw___ptr64:
2324 case tok::kw___forceinline:
2325 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 }
2327}
2328
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002329bool Parser::isConstructorDeclarator() {
2330 TentativeParsingAction TPA(*this);
2331
2332 // Parse the C++ scope specifier.
2333 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002334 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2335 TPA.Revert();
2336 return false;
2337 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002338
2339 // Parse the constructor name.
2340 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2341 // We already know that we have a constructor name; just consume
2342 // the token.
2343 ConsumeToken();
2344 } else {
2345 TPA.Revert();
2346 return false;
2347 }
2348
2349 // Current class name must be followed by a left parentheses.
2350 if (Tok.isNot(tok::l_paren)) {
2351 TPA.Revert();
2352 return false;
2353 }
2354 ConsumeParen();
2355
2356 // A right parentheses or ellipsis signals that we have a constructor.
2357 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2358 TPA.Revert();
2359 return true;
2360 }
2361
2362 // If we need to, enter the specified scope.
2363 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002364 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002365 DeclScopeObj.EnterDeclaratorScope();
2366
2367 // Check whether the next token(s) are part of a declaration
2368 // specifier, in which case we have the start of a parameter and,
2369 // therefore, we know that this is a constructor.
2370 bool IsConstructor = isDeclarationSpecifier();
2371 TPA.Revert();
2372 return IsConstructor;
2373}
Reid Spencer5f016e22007-07-11 17:01:13 +00002374
2375/// ParseTypeQualifierListOpt
2376/// type-qualifier-list: [C99 6.7.5]
2377/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002378/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002379/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002380/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002381/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2382/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002383///
Sean Huntbbd37c62009-11-21 08:43:09 +00002384void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2385 bool CXX0XAttributesAllowed) {
2386 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2387 SourceLocation Loc = Tok.getLocation();
2388 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2389 if (CXX0XAttributesAllowed)
2390 DS.AddAttributes(Attr.AttrList);
2391 else
2392 Diag(Loc, diag::err_attributes_not_allowed);
2393 }
2394
Reid Spencer5f016e22007-07-11 17:01:13 +00002395 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002396 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002397 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002398 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002399 SourceLocation Loc = Tok.getLocation();
2400
2401 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002403 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2404 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002405 break;
2406 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002407 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2408 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002409 break;
2410 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002411 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2412 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002413 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002414 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002415 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002416 case tok::kw___cdecl:
2417 case tok::kw___stdcall:
2418 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002419 case tok::kw___thiscall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002420 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002421 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2422 continue;
2423 }
2424 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002425 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002426 if (GNUAttributesAllowed) {
2427 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002428 continue; // do *not* consume the next token!
2429 }
2430 // otherwise, FALL THROUGH!
2431 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002432 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002433 // If this is not a type-qualifier token, we're done reading type
2434 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002435 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002436 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002437 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002438
Reid Spencer5f016e22007-07-11 17:01:13 +00002439 // If the specifier combination wasn't legal, issue a diagnostic.
2440 if (isInvalid) {
2441 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002442 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002443 }
2444 ConsumeToken();
2445 }
2446}
2447
2448
2449/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2450///
2451void Parser::ParseDeclarator(Declarator &D) {
2452 /// This implements the 'declarator' production in the C grammar, then checks
2453 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002454 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002455}
2456
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002457/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2458/// is parsed by the function passed to it. Pass null, and the direct-declarator
2459/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002460/// ptr-operator production.
2461///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002462/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2463/// [C] pointer[opt] direct-declarator
2464/// [C++] direct-declarator
2465/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002466///
2467/// pointer: [C99 6.7.5]
2468/// '*' type-qualifier-list[opt]
2469/// '*' type-qualifier-list[opt] pointer
2470///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002471/// ptr-operator:
2472/// '*' cv-qualifier-seq[opt]
2473/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002474/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002475/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002476/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002477/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002478void Parser::ParseDeclaratorInternal(Declarator &D,
2479 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002480 if (Diags.hasAllExtensionsSilenced())
2481 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002482 // C++ member pointers start with a '::' or a nested-name.
2483 // Member pointers get special handling, since there's no place for the
2484 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002485 if (getLang().CPlusPlus &&
2486 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2487 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002488 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002489 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2490
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002491 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002492 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002493 // The scope spec really belongs to the direct-declarator.
2494 D.getCXXScopeSpec() = SS;
2495 if (DirectDeclParser)
2496 (this->*DirectDeclParser)(D);
2497 return;
2498 }
2499
2500 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002501 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002502 DeclSpec DS;
2503 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002504 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002505
2506 // Recurse to parse whatever is left.
2507 ParseDeclaratorInternal(D, DirectDeclParser);
2508
2509 // Sema will have to catch (syntactically invalid) pointers into global
2510 // scope. It has to catch pointers into namespace scope anyway.
2511 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002512 Loc, DS.TakeAttributes()),
2513 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002514 return;
2515 }
2516 }
2517
2518 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002519 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002520 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002521 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002522 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002523 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002524 if (DirectDeclParser)
2525 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002526 return;
2527 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002528
Sebastian Redl05532f22009-03-15 22:02:01 +00002529 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2530 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002531 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002532 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002533
Chris Lattner9af55002009-03-27 04:18:06 +00002534 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002535 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002536 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002537
Reid Spencer5f016e22007-07-11 17:01:13 +00002538 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002539 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002540
Reid Spencer5f016e22007-07-11 17:01:13 +00002541 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002542 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002543 if (Kind == tok::star)
2544 // Remember that we parsed a pointer type, and remember the type-quals.
2545 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002546 DS.TakeAttributes()),
2547 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002548 else
2549 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002550 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002551 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002552 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 } else {
2554 // Is a reference
2555 DeclSpec DS;
2556
Sebastian Redl743de1f2009-03-23 00:00:23 +00002557 // Complain about rvalue references in C++03, but then go on and build
2558 // the declarator.
2559 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2560 Diag(Loc, diag::err_rvalue_reference);
2561
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2563 // cv-qualifiers are introduced through the use of a typedef or of a
2564 // template type argument, in which case the cv-qualifiers are ignored.
2565 //
2566 // [GNU] Retricted references are allowed.
2567 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002568 // [C++0x] Attributes on references are not allowed.
2569 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002570 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002571
2572 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2573 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2574 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002575 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2577 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002578 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 }
2580
2581 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002582 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002583
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002584 if (D.getNumTypeObjects() > 0) {
2585 // C++ [dcl.ref]p4: There shall be no references to references.
2586 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2587 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002588 if (const IdentifierInfo *II = D.getIdentifier())
2589 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2590 << II;
2591 else
2592 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2593 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002594
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002595 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002596 // can go ahead and build the (technically ill-formed)
2597 // declarator: reference collapsing will take care of it.
2598 }
2599 }
2600
Reid Spencer5f016e22007-07-11 17:01:13 +00002601 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002602 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002603 DS.TakeAttributes(),
2604 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002605 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002606 }
2607}
2608
2609/// ParseDirectDeclarator
2610/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002611/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002612/// '(' declarator ')'
2613/// [GNU] '(' attributes declarator ')'
2614/// [C90] direct-declarator '[' constant-expression[opt] ']'
2615/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2616/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2617/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2618/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2619/// direct-declarator '(' parameter-type-list ')'
2620/// direct-declarator '(' identifier-list[opt] ')'
2621/// [GNU] direct-declarator '(' parameter-forward-declarations
2622/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002623/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2624/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002625/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002626///
2627/// declarator-id: [C++ 8]
2628/// id-expression
2629/// '::'[opt] nested-name-specifier[opt] type-name
2630///
2631/// id-expression: [C++ 5.1]
2632/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002633/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002634///
2635/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002636/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002637/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002638/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002639/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002640/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002641///
Reid Spencer5f016e22007-07-11 17:01:13 +00002642void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002643 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002644
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002645 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2646 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002647 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002648 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2649 true);
John McCall9ba61662010-02-26 08:45:28 +00002650 }
2651
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002652 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002653 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002654 // Change the declaration context for name lookup, until this function
2655 // is exited (and the declarator has been parsed).
2656 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002657 }
2658
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002659 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2660 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2661 // We found something that indicates the start of an unqualified-id.
2662 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002663 bool AllowConstructorName;
2664 if (D.getDeclSpec().hasTypeSpecifier())
2665 AllowConstructorName = false;
2666 else if (D.getCXXScopeSpec().isSet())
2667 AllowConstructorName =
2668 (D.getContext() == Declarator::FileContext ||
2669 (D.getContext() == Declarator::MemberContext &&
2670 D.getDeclSpec().isFriendSpecified()));
2671 else
2672 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2673
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002674 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2675 /*EnteringContext=*/true,
2676 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002677 AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002678 /*ObjectType=*/0,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002679 D.getName()) ||
2680 // Once we're past the identifier, if the scope was bad, mark the
2681 // whole declarator bad.
2682 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002683 D.SetIdentifier(0, Tok.getLocation());
2684 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002685 } else {
2686 // Parsed the unqualified-id; update range information and move along.
2687 if (D.getSourceRange().getBegin().isInvalid())
2688 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2689 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002690 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002691 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002692 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002693 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002694 assert(!getLang().CPlusPlus &&
2695 "There's a C++-specific check for tok::identifier above");
2696 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2697 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2698 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002699 goto PastIdentifier;
2700 }
2701
2702 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 // direct-declarator: '(' declarator ')'
2704 // direct-declarator: '(' attributes declarator ')'
2705 // Example: 'char (*X)' or 'int (*XX)(void)'
2706 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002707
2708 // If the declarator was parenthesized, we entered the declarator
2709 // scope when parsing the parenthesized declarator, then exited
2710 // the scope already. Re-enter the scope, if we need to.
2711 if (D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002712 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002713 // Change the declaration context for name lookup, until this function
2714 // is exited (and the declarator has been parsed).
2715 DeclScopeObj.EnterDeclaratorScope();
2716 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002717 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002718 // This could be something simple like "int" (in which case the declarator
2719 // portion is empty), if an abstract-declarator is allowed.
2720 D.SetIdentifier(0, Tok.getLocation());
2721 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002722 if (D.getContext() == Declarator::MemberContext)
2723 Diag(Tok, diag::err_expected_member_name_or_semi)
2724 << D.getDeclSpec().getSourceRange();
2725 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002726 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002727 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002728 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002730 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002731 }
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002733 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002734 assert(D.isPastIdentifier() &&
2735 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Sean Huntbbd37c62009-11-21 08:43:09 +00002737 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002738 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002739 && isCXX0XAttributeSpecifier(true)) {
2740 SourceLocation AttrEndLoc;
2741 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2742 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2743 }
2744
Reid Spencer5f016e22007-07-11 17:01:13 +00002745 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002746 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002747 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2748 // In such a case, check if we actually have a function declarator; if it
2749 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002750 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2751 // When not in file scope, warn for ambiguous function declarators, just
2752 // in case the author intended it as a variable definition.
2753 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2754 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2755 break;
2756 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002757 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002758 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002759 ParseBracketDeclarator(D);
2760 } else {
2761 break;
2762 }
2763 }
2764}
2765
Chris Lattneref4715c2008-04-06 05:45:57 +00002766/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2767/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002768/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002769/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2770///
2771/// direct-declarator:
2772/// '(' declarator ')'
2773/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002774/// direct-declarator '(' parameter-type-list ')'
2775/// direct-declarator '(' identifier-list[opt] ')'
2776/// [GNU] direct-declarator '(' parameter-forward-declarations
2777/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002778///
2779void Parser::ParseParenDeclarator(Declarator &D) {
2780 SourceLocation StartLoc = ConsumeParen();
2781 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Chris Lattner7399ee02008-10-20 02:05:46 +00002783 // Eat any attributes before we look at whether this is a grouping or function
2784 // declarator paren. If this is a grouping paren, the attribute applies to
2785 // the type being built up, for example:
2786 // int (__attribute__(()) *x)(long y)
2787 // If this ends up not being a grouping paren, the attribute applies to the
2788 // first argument, for example:
2789 // int (__attribute__(()) int x)
2790 // In either case, we need to eat any attributes to be able to determine what
2791 // sort of paren this is.
2792 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002793 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002794 bool RequiresArg = false;
2795 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002796 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002797
Chris Lattner7399ee02008-10-20 02:05:46 +00002798 // We require that the argument list (if this is a non-grouping paren) be
2799 // present even if the attribute list was empty.
2800 RequiresArg = true;
2801 }
Steve Naroff239f0732008-12-25 14:16:32 +00002802 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002803 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002804 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2805 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002806 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002807 }
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Chris Lattneref4715c2008-04-06 05:45:57 +00002809 // If we haven't past the identifier yet (or where the identifier would be
2810 // stored, if this is an abstract declarator), then this is probably just
2811 // grouping parens. However, if this could be an abstract-declarator, then
2812 // this could also be the start of function arguments (consider 'void()').
2813 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002814
Chris Lattneref4715c2008-04-06 05:45:57 +00002815 if (!D.mayOmitIdentifier()) {
2816 // If this can't be an abstract-declarator, this *must* be a grouping
2817 // paren, because we haven't seen the identifier yet.
2818 isGrouping = true;
2819 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002820 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002821 isDeclarationSpecifier()) { // 'int(int)' is a function.
2822 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2823 // considered to be a type, not a K&R identifier-list.
2824 isGrouping = false;
2825 } else {
2826 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2827 isGrouping = true;
2828 }
Mike Stump1eb44332009-09-09 15:08:12 +00002829
Chris Lattneref4715c2008-04-06 05:45:57 +00002830 // If this is a grouping paren, handle:
2831 // direct-declarator: '(' declarator ')'
2832 // direct-declarator: '(' attributes declarator ')'
2833 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002834 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002835 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002836 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002837 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002838
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002839 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002840 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002841 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002842
2843 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002844 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002845 return;
2846 }
Mike Stump1eb44332009-09-09 15:08:12 +00002847
Chris Lattneref4715c2008-04-06 05:45:57 +00002848 // Okay, if this wasn't a grouping paren, it must be the start of a function
2849 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002850 // identifier (and remember where it would have been), then call into
2851 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002852 D.SetIdentifier(0, Tok.getLocation());
2853
Ted Kremenek1e377652010-02-11 02:19:13 +00002854 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002855}
2856
2857/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2858/// declarator D up to a paren, which indicates that we are parsing function
2859/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002860///
Chris Lattner7399ee02008-10-20 02:05:46 +00002861/// If AttrList is non-null, then the caller parsed those arguments immediately
2862/// after the open paren - they should be considered to be the first argument of
2863/// a parameter. If RequiresArg is true, then the first argument of the
2864/// function is required to be present and required to not be an identifier
2865/// list.
2866///
Reid Spencer5f016e22007-07-11 17:01:13 +00002867/// This method also handles this portion of the grammar:
2868/// parameter-type-list: [C99 6.7.5]
2869/// parameter-list
2870/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002871/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002872///
2873/// parameter-list: [C99 6.7.5]
2874/// parameter-declaration
2875/// parameter-list ',' parameter-declaration
2876///
2877/// parameter-declaration: [C99 6.7.5]
2878/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002879/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002880/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002881/// declaration-specifiers abstract-declarator[opt]
2882/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002883/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002884/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2885///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002886/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002887/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002888///
Chris Lattner7399ee02008-10-20 02:05:46 +00002889void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2890 AttributeList *AttrList,
2891 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002892 // lparen is already consumed!
2893 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002894
Chris Lattner7399ee02008-10-20 02:05:46 +00002895 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002896 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002897 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002898 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002899 delete AttrList;
2900 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002901
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002902 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2903 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002904
2905 // cv-qualifier-seq[opt].
2906 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002907 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002908 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002909 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002910 llvm::SmallVector<TypeTy*, 2> Exceptions;
2911 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002912 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002913 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002914 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002915 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002916
2917 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002918 if (Tok.is(tok::kw_throw)) {
2919 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002920 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002921 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002922 hasAnyExceptionSpec);
2923 assert(Exceptions.size() == ExceptionRanges.size() &&
2924 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002925 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002926 }
2927
Chris Lattnerf97409f2008-04-06 06:57:35 +00002928 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002929 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002930 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002931 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002932 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002933 /*arglist*/ 0, 0,
2934 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002935 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002936 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002937 Exceptions.data(),
2938 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002939 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002940 LParenLoc, RParenLoc, D),
2941 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002942 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002943 }
2944
Chris Lattner7399ee02008-10-20 02:05:46 +00002945 // Alternatively, this parameter list may be an identifier list form for a
2946 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00002947 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2948 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00002949 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002950 // K&R identifier lists can't have typedefs as identifiers, per
2951 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002952 if (RequiresArg) {
2953 Diag(Tok, diag::err_argument_required_after_attribute);
2954 delete AttrList;
2955 }
Chris Lattner83a94472010-05-14 17:23:36 +00002956
Steve Naroff2d081c42009-01-28 19:16:40 +00002957 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00002958 // normal declarators, not for abstract-declarators. Get the first
2959 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002960 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00002961 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002962
2963 // Identifier lists follow a really simple grammar: the identifiers can
2964 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2965 // identifier lists are really rare in the brave new modern world, and it
2966 // is very common for someone to typo a type in a non-k&r style list. If
2967 // we are presented with something like: "void foo(intptr x, float y)",
2968 // we don't want to start parsing the function declarator as though it is
2969 // a K&R style declarator just because intptr is an invalid type.
2970 //
2971 // To handle this, we check to see if the token after the first identifier
2972 // is a "," or ")". Only if so, do we parse it as an identifier list.
2973 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2974 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
2975 FirstTok.getIdentifierInfo(),
2976 FirstTok.getLocation(), D);
2977
2978 // If we get here, the code is invalid. Push the first identifier back
2979 // into the token stream and parse the first argument as an (invalid)
2980 // normal argument declarator.
2981 PP.EnterToken(Tok);
2982 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00002983 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002984 }
Mike Stump1eb44332009-09-09 15:08:12 +00002985
Chris Lattnerf97409f2008-04-06 06:57:35 +00002986 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Chris Lattnerf97409f2008-04-06 06:57:35 +00002988 // Build up an array of information about the parsed arguments.
2989 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002990
2991 // Enter function-declaration scope, limiting any declarators to the
2992 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002993 ParseScope PrototypeScope(this,
2994 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Chris Lattnerf97409f2008-04-06 06:57:35 +00002996 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002997 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002998 while (1) {
2999 if (Tok.is(tok::ellipsis)) {
3000 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003001 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003002 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003003 }
Mike Stump1eb44332009-09-09 15:08:12 +00003004
Chris Lattnerf97409f2008-04-06 06:57:35 +00003005 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Chris Lattnerf97409f2008-04-06 06:57:35 +00003007 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003008 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003009 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003010
3011 // If the caller parsed attributes for the first argument, add them now.
3012 if (AttrList) {
3013 DS.AddAttributes(AttrList);
3014 AttrList = 0; // Only apply the attributes to the first parameter.
3015 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003016 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003017
Chris Lattnerf97409f2008-04-06 06:57:35 +00003018 // Parse the declarator. This is "PrototypeContext", because we must
3019 // accept either 'declarator' or 'abstract-declarator' here.
3020 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3021 ParseDeclarator(ParmDecl);
3022
3023 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003024 if (Tok.is(tok::kw___attribute)) {
3025 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003026 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003027 ParmDecl.AddAttributes(AttrList, Loc);
3028 }
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Chris Lattnerf97409f2008-04-06 06:57:35 +00003030 // Remember this parsed parameter in ParamInfo.
3031 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003032
Douglas Gregor72b505b2008-12-16 21:30:33 +00003033 // DefArgToks is used when the parsing of default arguments needs
3034 // to be delayed.
3035 CachedTokens *DefArgToks = 0;
3036
Chris Lattnerf97409f2008-04-06 06:57:35 +00003037 // If no parameter was specified, verify that *something* was specified,
3038 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003039 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3040 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003041 // Completely missing, emit error.
3042 Diag(DSStart, diag::err_missing_param);
3043 } else {
3044 // Otherwise, we have something. Add it and let semantic analysis try
3045 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003046
Chris Lattnerf97409f2008-04-06 06:57:35 +00003047 // Inform the actions module about the parameter declarator, so it gets
3048 // added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003049 DeclPtrTy Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003050
3051 // Parse the default argument, if any. We parse the default
3052 // arguments in all dialects; the semantic analysis in
3053 // ActOnParamDefaultArgument will reject the default argument in
3054 // C.
3055 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003056 SourceLocation EqualLoc = Tok.getLocation();
3057
Chris Lattner04421082008-04-08 04:40:51 +00003058 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003059 if (D.getContext() == Declarator::MemberContext) {
3060 // If we're inside a class definition, cache the tokens
3061 // corresponding to the default argument. We'll actually parse
3062 // them when we see the end of the class definition.
3063 // FIXME: Templates will require something similar.
3064 // FIXME: Can we use a smart pointer for Toks?
3065 DefArgToks = new CachedTokens;
3066
Mike Stump1eb44332009-09-09 15:08:12 +00003067 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003068 /*StopAtSemi=*/true,
3069 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003070 delete DefArgToks;
3071 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003072 Actions.ActOnParamDefaultArgumentError(Param);
3073 } else
Mike Stump1eb44332009-09-09 15:08:12 +00003074 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003075 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00003076 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003077 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003078 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003079
Douglas Gregor72b505b2008-12-16 21:30:33 +00003080 OwningExprResult DefArgResult(ParseAssignmentExpression());
3081 if (DefArgResult.isInvalid()) {
3082 Actions.ActOnParamDefaultArgumentError(Param);
3083 SkipUntil(tok::comma, tok::r_paren, true, true);
3084 } else {
3085 // Inform the actions module about the default argument
3086 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00003087 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00003088 }
Chris Lattner04421082008-04-08 04:40:51 +00003089 }
3090 }
Mike Stump1eb44332009-09-09 15:08:12 +00003091
3092 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3093 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003094 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003095 }
3096
3097 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003098 if (Tok.isNot(tok::comma)) {
3099 if (Tok.is(tok::ellipsis)) {
3100 IsVariadic = true;
3101 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3102
3103 if (!getLang().CPlusPlus) {
3104 // We have ellipsis without a preceding ',', which is ill-formed
3105 // in C. Complain and provide the fix.
3106 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003107 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003108 }
3109 }
3110
3111 break;
3112 }
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Chris Lattnerf97409f2008-04-06 06:57:35 +00003114 // Consume the comma.
3115 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003116 }
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Chris Lattnerf97409f2008-04-06 06:57:35 +00003118 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003119 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003121 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003122 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3123 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003124
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003125 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003126 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003127 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003128 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00003129 llvm::SmallVector<TypeTy*, 2> Exceptions;
3130 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003131
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003132 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003133 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003134 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003135 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003136 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003137
3138 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003139 if (Tok.is(tok::kw_throw)) {
3140 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003141 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003142 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003143 hasAnyExceptionSpec);
3144 assert(Exceptions.size() == ExceptionRanges.size() &&
3145 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003146 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003147 }
3148
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003150 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003151 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003152 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003153 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003154 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003155 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003156 Exceptions.data(),
3157 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003158 Exceptions.size(),
3159 LParenLoc, RParenLoc, D),
3160 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003161}
3162
Chris Lattner66d28652008-04-06 06:34:08 +00003163/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3164/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003165/// first identifier has already been consumed, and the current token is the
3166/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003167///
3168/// identifier-list: [C99 6.7.5]
3169/// identifier
3170/// identifier-list ',' identifier
3171///
3172void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003173 IdentifierInfo *FirstIdent,
3174 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003175 Declarator &D) {
3176 // Build up an array of information about the parsed arguments.
3177 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3178 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003179
Chris Lattner66d28652008-04-06 06:34:08 +00003180 // If there was no identifier specified for the declarator, either we are in
3181 // an abstract-declarator, or we are in a parameter declarator which was found
3182 // to be abstract. In abstract-declarators, identifier lists are not valid:
3183 // diagnose this.
3184 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003185 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003186
Chris Lattner83a94472010-05-14 17:23:36 +00003187 // The first identifier was already read, and is known to be the first
3188 // identifier in the list. Remember this identifier in ParamInfo.
3189 ParamsSoFar.insert(FirstIdent);
3190 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003191 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00003192
Chris Lattner66d28652008-04-06 06:34:08 +00003193 while (Tok.is(tok::comma)) {
3194 // Eat the comma.
3195 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Chris Lattner50c64772008-04-06 06:39:19 +00003197 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003198 if (Tok.isNot(tok::identifier)) {
3199 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003200 SkipUntil(tok::r_paren);
3201 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003202 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003203
Chris Lattner66d28652008-04-06 06:34:08 +00003204 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003205
3206 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003207 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003208 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003209
Chris Lattner66d28652008-04-06 06:34:08 +00003210 // Verify that the argument identifier has not already been mentioned.
3211 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003212 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003213 } else {
3214 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003215 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003216 Tok.getLocation(),
3217 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00003218 }
Mike Stump1eb44332009-09-09 15:08:12 +00003219
Chris Lattner66d28652008-04-06 06:34:08 +00003220 // Eat the identifier.
3221 ConsumeToken();
3222 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003223
3224 // If we have the closing ')', eat it and we're done.
3225 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3226
Chris Lattner50c64772008-04-06 06:39:19 +00003227 // Remember that we parsed a function type, and remember the attributes. This
3228 // function type is always a K&R style function type, which is not varargs and
3229 // has no prototype.
3230 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003231 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003232 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003233 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003234 /*exception*/false,
3235 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003236 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003237 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003238}
Chris Lattneref4715c2008-04-06 05:45:57 +00003239
Reid Spencer5f016e22007-07-11 17:01:13 +00003240/// [C90] direct-declarator '[' constant-expression[opt] ']'
3241/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3242/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3243/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3244/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3245void Parser::ParseBracketDeclarator(Declarator &D) {
3246 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Chris Lattner378c7e42008-12-18 07:27:21 +00003248 // C array syntax has many features, but by-far the most common is [] and [4].
3249 // This code does a fast path to handle some of the most obvious cases.
3250 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003251 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003252 //FIXME: Use these
3253 CXX0XAttributeList Attr;
3254 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3255 Attr = ParseCXX0XAttributes();
3256 }
3257
Chris Lattner378c7e42008-12-18 07:27:21 +00003258 // Remember that we parsed the empty array type.
3259 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003260 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3261 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003262 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003263 return;
3264 } else if (Tok.getKind() == tok::numeric_constant &&
3265 GetLookAheadToken(1).is(tok::r_square)) {
3266 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00003267 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003268 ConsumeToken();
3269
Sebastian Redlab197ba2009-02-09 18:23:29 +00003270 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003271 //FIXME: Use these
3272 CXX0XAttributeList Attr;
3273 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3274 Attr = ParseCXX0XAttributes();
3275 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003276
3277 // If there was an error parsing the assignment-expression, recover.
3278 if (ExprRes.isInvalid())
3279 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Chris Lattner378c7e42008-12-18 07:27:21 +00003281 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003282 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3283 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003284 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003285 return;
3286 }
Mike Stump1eb44332009-09-09 15:08:12 +00003287
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 // If valid, this location is the position where we read the 'static' keyword.
3289 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003290 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003291 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003292
Reid Spencer5f016e22007-07-11 17:01:13 +00003293 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003294 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003295 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003296 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003297
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 // If we haven't already read 'static', check to see if there is one after the
3299 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003300 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3304 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00003305 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00003306
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003307 // Handle the case where we have '[*]' as the array size. However, a leading
3308 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3309 // the the token after the star is a ']'. Since stars in arrays are
3310 // infrequent, use of lookahead is not costly here.
3311 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003312 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003313
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003314 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003315 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003316 StaticLoc = SourceLocation(); // Drop the static.
3317 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003318 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003319 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003320 // Note, in C89, this production uses the constant-expr production instead
3321 // of assignment-expr. The only difference is that assignment-expr allows
3322 // things like '=' and '*='. Sema rejects these in C89 mode because they
3323 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003324
Douglas Gregore0762c92009-06-19 23:52:42 +00003325 // Parse the constant-expression or assignment-expression now (depending
3326 // on dialect).
3327 if (getLang().CPlusPlus)
3328 NumElements = ParseConstantExpression();
3329 else
3330 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003331 }
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Reid Spencer5f016e22007-07-11 17:01:13 +00003333 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003334 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003335 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003336 // If the expression was invalid, skip it.
3337 SkipUntil(tok::r_square);
3338 return;
3339 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003340
3341 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3342
Sean Huntbbd37c62009-11-21 08:43:09 +00003343 //FIXME: Use these
3344 CXX0XAttributeList Attr;
3345 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3346 Attr = ParseCXX0XAttributes();
3347 }
3348
Chris Lattner378c7e42008-12-18 07:27:21 +00003349 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003350 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3351 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003352 NumElements.release(),
3353 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003354 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003355}
3356
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003357/// [GNU] typeof-specifier:
3358/// typeof ( expressions )
3359/// typeof ( type-name )
3360/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003361///
3362void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003363 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003364 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003365 SourceLocation StartLoc = ConsumeToken();
3366
John McCallcfb708c2010-01-13 20:03:27 +00003367 const bool hasParens = Tok.is(tok::l_paren);
3368
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003369 bool isCastExpr;
3370 TypeTy *CastTy;
3371 SourceRange CastRange;
3372 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3373 isCastExpr,
3374 CastTy,
3375 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003376 if (hasParens)
3377 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003378
3379 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003380 // FIXME: Not accurate, the range gets one token more than it should.
3381 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003382 else
3383 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003384
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003385 if (isCastExpr) {
3386 if (!CastTy) {
3387 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003388 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003389 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003390
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003391 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003392 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003393 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3394 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003395 DiagID, CastTy))
3396 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003397 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003398 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003399
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003400 // If we get here, the operand to the typeof was an expresion.
3401 if (Operand.isInvalid()) {
3402 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003403 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003404 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003405
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003406 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003407 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003408 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3409 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003410 DiagID, Operand.release()))
3411 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003412}
Chris Lattner1b492422010-02-28 18:33:55 +00003413
3414
3415/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3416/// from TryAltiVecVectorToken.
3417bool Parser::TryAltiVecVectorTokenOutOfLine() {
3418 Token Next = NextToken();
3419 switch (Next.getKind()) {
3420 default: return false;
3421 case tok::kw_short:
3422 case tok::kw_long:
3423 case tok::kw_signed:
3424 case tok::kw_unsigned:
3425 case tok::kw_void:
3426 case tok::kw_char:
3427 case tok::kw_int:
3428 case tok::kw_float:
3429 case tok::kw_double:
3430 case tok::kw_bool:
3431 case tok::kw___pixel:
3432 Tok.setKind(tok::kw___vector);
3433 return true;
3434 case tok::identifier:
3435 if (Next.getIdentifierInfo() == Ident_pixel) {
3436 Tok.setKind(tok::kw___vector);
3437 return true;
3438 }
3439 return false;
3440 }
3441}
3442
3443bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3444 const char *&PrevSpec, unsigned &DiagID,
3445 bool &isInvalid) {
3446 if (Tok.getIdentifierInfo() == Ident_vector) {
3447 Token Next = NextToken();
3448 switch (Next.getKind()) {
3449 case tok::kw_short:
3450 case tok::kw_long:
3451 case tok::kw_signed:
3452 case tok::kw_unsigned:
3453 case tok::kw_void:
3454 case tok::kw_char:
3455 case tok::kw_int:
3456 case tok::kw_float:
3457 case tok::kw_double:
3458 case tok::kw_bool:
3459 case tok::kw___pixel:
3460 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3461 return true;
3462 case tok::identifier:
3463 if (Next.getIdentifierInfo() == Ident_pixel) {
3464 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3465 return true;
3466 }
3467 break;
3468 default:
3469 break;
3470 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003471 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003472 DS.isTypeAltiVecVector()) {
3473 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3474 return true;
3475 }
3476 return false;
3477}
3478