blob: a5ef817d43d02b161df8126af02cb8e990f3c3aa [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ 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.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000019#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000021using namespace clang;
22
23/// ParseNamespace - We know that the current token is a namespace keyword. This
24/// may either be a top level namespace or a block-level namespace alias.
25///
26/// namespace-definition: [C++ 7.3: basic.namespace]
27/// named-namespace-definition
28/// unnamed-namespace-definition
29///
30/// unnamed-namespace-definition:
31/// 'namespace' attributes[opt] '{' namespace-body '}'
32///
33/// named-namespace-definition:
34/// original-namespace-definition
35/// extension-namespace-definition
36///
37/// original-namespace-definition:
38/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
39///
40/// extension-namespace-definition:
41/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000042///
Chris Lattner8f08cb72007-08-25 06:57:03 +000043/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
44/// 'namespace' identifier '=' qualified-namespace-specifier ';'
45///
Chris Lattner97144fc2009-04-02 04:16:50 +000046Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
47 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000048 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000049 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor49f40bd2009-09-18 19:03:04 +000051 if (Tok.is(tok::code_completion)) {
52 Actions.CodeCompleteNamespaceDecl(CurScope);
53 ConsumeToken();
54 }
55
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 SourceLocation IdentLoc;
57 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000058
59 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner04d66662007-10-09 17:33:22 +000061 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000062 Ident = Tok.getIdentifierInfo();
63 IdentLoc = ConsumeToken(); // eat the identifier.
64 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000067 Action::AttrTy *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000068 if (Tok.is(tok::kw___attribute)) {
69 attrTok = Tok;
70
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // FIXME: save these somewhere.
Sean Huntbbd37c62009-11-21 08:43:09 +000072 AttrList = ParseGNUAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor6a588dd2009-06-17 19:49:00 +000075 if (Tok.is(tok::equal)) {
76 if (AttrList)
77 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
78
Chris Lattner97144fc2009-04-02 04:16:50 +000079 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner51448322009-03-29 14:02:43 +000082 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000084 diag::err_expected_ident_lbrace);
85 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 SourceLocation LBrace = ConsumeBrace();
89
90 // Enter a scope for the namespace.
91 ParseScope NamespaceScope(this, Scope::DeclScope);
92
93 DeclPtrTy NamespcDecl =
94 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
95
96 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
97 PP.getSourceManager(),
98 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +000099
Sean Huntbbd37c62009-11-21 08:43:09 +0000100 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
101 CXX0XAttributeList Attr;
102 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
103 Attr = ParseCXX0XAttributes();
104 ParseExternalDeclaration(Attr);
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner51448322009-03-29 14:02:43 +0000107 // Leave the namespace scope.
108 NamespaceScope.Exit();
109
Chris Lattner97144fc2009-04-02 04:16:50 +0000110 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
111 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000112
Chris Lattner97144fc2009-04-02 04:16:50 +0000113 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000114 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000115}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000116
Anders Carlssonf67606a2009-03-28 04:07:16 +0000117/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
118/// alias definition.
119///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000120Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000121 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000122 IdentifierInfo *Alias,
123 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000124 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Anders Carlssonf67606a2009-03-28 04:07:16 +0000126 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000128 if (Tok.is(tok::code_completion)) {
129 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
130 ConsumeToken();
131 }
132
Anders Carlssonf67606a2009-03-28 04:07:16 +0000133 CXXScopeSpec SS;
134 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000135 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000136
137 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
138 Diag(Tok, diag::err_expected_namespace_name);
139 // Skip to end of the definition and eat the ';'.
140 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 }
143
144 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000145 IdentifierInfo *Ident = Tok.getIdentifierInfo();
146 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssonf67606a2009-03-28 04:07:16 +0000148 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000149 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000150 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
151 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
153 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000154 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000155}
156
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000157/// ParseLinkage - We know that the current token is a string_literal
158/// and just before that, that extern was seen.
159///
160/// linkage-specification: [C++ 7.5p2: dcl.link]
161/// 'extern' string-literal '{' declaration-seq[opt] '}'
162/// 'extern' string-literal declaration
163///
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000164Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
165 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000166 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000167 llvm::SmallVector<char, 8> LangBuffer;
168 // LangBuffer is guaranteed to be big enough.
169 LangBuffer.resize(Tok.getLength());
170 const char *LangBufPtr = &LangBuffer[0];
171 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
172
173 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000174
Douglas Gregor074149e2009-01-05 19:45:36 +0000175 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000176 DeclPtrTy LinkageSpec
177 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000178 /*FIXME: */SourceLocation(),
179 Loc, LangBufPtr, StrSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000180 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000181 : SourceLocation());
182
Sean Huntbbd37c62009-11-21 08:43:09 +0000183 CXX0XAttributeList Attr;
184 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
185 Attr = ParseCXX0XAttributes();
186 }
187
Douglas Gregor074149e2009-01-05 19:45:36 +0000188 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000189 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000190 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000191 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000192 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000193
Sean Huntbbd37c62009-11-21 08:43:09 +0000194 if (Attr.HasAttr)
195 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
196 << Attr.Range;
197
Douglas Gregorf44515a2008-12-16 22:23:02 +0000198 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000199 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000200 CXX0XAttributeList Attr;
201 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
202 Attr = ParseCXX0XAttributes();
203 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000204 }
205
Douglas Gregorf44515a2008-12-16 22:23:02 +0000206 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000207 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000208}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000209
Douglas Gregorf780abc2008-12-30 03:27:21 +0000210/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
211/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000212Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000213 SourceLocation &DeclEnd,
214 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000215 assert(Tok.is(tok::kw_using) && "Not using token");
216
217 // Eat 'using'.
218 SourceLocation UsingLoc = ConsumeToken();
219
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000220 if (Tok.is(tok::code_completion)) {
221 Actions.CodeCompleteUsing(CurScope);
222 ConsumeToken();
223 }
224
Chris Lattner2f274772009-01-06 06:55:51 +0000225 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000227 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
228
229 if (Attr.HasAttr)
230 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
231 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000232
233 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000234 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000235 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000236}
237
238/// ParseUsingDirective - Parse C++ using-directive, assumes
239/// that current token is 'namespace' and 'using' was already parsed.
240///
241/// using-directive: [C++ 7.3.p4: namespace.udir]
242/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
243/// namespace-name ;
244/// [GNU] using-directive:
245/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
246/// namespace-name attributes[opt] ;
247///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000248Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000249 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000250 SourceLocation &DeclEnd,
251 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000252 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
253
254 // Eat 'namespace'.
255 SourceLocation NamespcLoc = ConsumeToken();
256
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000257 if (Tok.is(tok::code_completion)) {
258 Actions.CodeCompleteUsingDirective(CurScope);
259 ConsumeToken();
260 }
261
Douglas Gregorf780abc2008-12-30 03:27:21 +0000262 CXXScopeSpec SS;
263 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000264 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000265
Douglas Gregorf780abc2008-12-30 03:27:21 +0000266 IdentifierInfo *NamespcName = 0;
267 SourceLocation IdentLoc = SourceLocation();
268
269 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000270 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000271 Diag(Tok, diag::err_expected_namespace_name);
272 // If there was invalid namespace name, skip to end of decl, and eat ';'.
273 SkipUntil(tok::semi);
274 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000275 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner823c44e2009-01-06 07:27:21 +0000278 // Parse identifier.
279 NamespcName = Tok.getIdentifierInfo();
280 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner823c44e2009-01-06 07:27:21 +0000282 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000283 bool GNUAttr = false;
284 if (Tok.is(tok::kw___attribute)) {
285 GNUAttr = true;
286 Attr = addAttributeLists(Attr, ParseGNUAttributes());
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner823c44e2009-01-06 07:27:21 +0000289 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000290 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000291 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000292 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000293 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000294
295 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000296 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000297}
298
299/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
300/// 'using' was already seen.
301///
302/// using-declaration: [C++ 7.3.p3: namespace.udecl]
303/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000304/// unqualified-id
305/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000306///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000307Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000308 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000309 SourceLocation &DeclEnd,
310 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000311 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000312 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000313 bool IsTypeName;
314
315 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000316 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000317 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000318 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000319 ConsumeToken();
320 IsTypeName = true;
321 }
322 else
323 IsTypeName = false;
324
325 // Parse nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000326 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000327
328 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000329
330 // Check nested-name specifier.
331 if (SS.isInvalid()) {
332 SkipUntil(tok::semi);
333 return DeclPtrTy();
334 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000335
336 // Parse the unqualified-id. We allow parsing of both constructor and
337 // destructor names and allow the action module to diagnose any semantic
338 // errors.
339 UnqualifiedId Name;
340 if (ParseUnqualifiedId(SS,
341 /*EnteringContext=*/false,
342 /*AllowDestructorName=*/true,
343 /*AllowConstructorName=*/true,
344 /*ObjectType=*/0,
345 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000346 SkipUntil(tok::semi);
347 return DeclPtrTy();
348 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000349
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000350 // Parse (optional) attributes (most likely GNU strong-using extension).
351 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000352 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000354 // Eat ';'.
355 DeclEnd = Tok.getLocation();
356 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000357 AttrList ? "attributes list" : "using declaration",
358 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000359
John McCall60fa3cf2009-12-11 02:10:03 +0000360 return Actions.ActOnUsingDeclaration(CurScope, AS, true, UsingLoc, SS, Name,
John McCall7ba107a2009-11-18 02:36:19 +0000361 AttrList, IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000362}
363
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000364/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
365///
366/// static_assert-declaration:
367/// static_assert ( constant-expression , string-literal ) ;
368///
Chris Lattner97144fc2009-04-02 04:16:50 +0000369Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000370 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
371 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000373 if (Tok.isNot(tok::l_paren)) {
374 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000375 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000378 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000379
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000380 OwningExprResult AssertExpr(ParseConstantExpression());
381 if (AssertExpr.isInvalid()) {
382 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000383 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlssonad5f9602009-03-13 23:29:20 +0000386 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000387 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000388
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000389 if (Tok.isNot(tok::string_literal)) {
390 Diag(Tok, diag::err_expected_string_literal);
391 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000392 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000395 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000396 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000397 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000398
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000399 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner97144fc2009-04-02 04:16:50 +0000401 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000402 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
403
Mike Stump1eb44332009-09-09 15:08:12 +0000404 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000405 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000406}
407
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000408/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
409///
410/// 'decltype' ( expression )
411///
412void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
413 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
414
415 SourceLocation StartLoc = ConsumeToken();
416 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
418 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000419 "decltype")) {
420 SkipUntil(tok::r_paren);
421 return;
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000424 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000426 // C++0x [dcl.type.simple]p4:
427 // The operand of the decltype specifier is an unevaluated operand.
428 EnterExpressionEvaluationContext Unevaluated(Actions,
429 Action::Unevaluated);
430 OwningExprResult Result = ParseExpression();
431 if (Result.isInvalid()) {
432 SkipUntil(tok::r_paren);
433 return;
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000436 // Match the ')'
437 SourceLocation RParenLoc;
438 if (Tok.is(tok::r_paren))
439 RParenLoc = ConsumeParen();
440 else
441 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000443 if (RParenLoc.isInvalid())
444 return;
445
446 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000447 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000448 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000449 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000450 DiagID, Result.release()))
451 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000452}
453
Douglas Gregor42a552f2008-11-05 20:51:48 +0000454/// ParseClassName - Parse a C++ class-name, which names a class. Note
455/// that we only check that the result names a type; semantic analysis
456/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000457/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000458/// found.
459///
460/// class-name: [C++ 9.1]
461/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000462/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000463///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000464Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000465 const CXXScopeSpec *SS,
466 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000467 // Check whether we have a template-id that names a type.
468 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000469 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000470 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000471 if (TemplateId->Kind == TNK_Type_template ||
472 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000473 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000474
475 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
476 TypeTy *Type = Tok.getAnnotationValue();
477 EndLocation = Tok.getAnnotationEndLoc();
478 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000479
480 if (Type)
481 return Type;
482 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000483 }
484
485 // Fall through to produce an error below.
486 }
487
Douglas Gregor42a552f2008-11-05 20:51:48 +0000488 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000489 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000490 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000491 }
492
Douglas Gregor84d0a192010-01-12 21:28:44 +0000493 IdentifierInfo *Id = Tok.getIdentifierInfo();
494 SourceLocation IdLoc = ConsumeToken();
495
496 if (Tok.is(tok::less)) {
497 // It looks the user intended to write a template-id here, but the
498 // template-name was wrong. Try to fix that.
499 TemplateNameKind TNK = TNK_Type_template;
500 TemplateTy Template;
501 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, CurScope,
502 SS, Template, TNK)) {
503 Diag(IdLoc, diag::err_unknown_template_name)
504 << Id;
505 }
506
507 if (!Template)
508 return true;
509
510 // Form the template name
511 UnqualifiedId TemplateName;
512 TemplateName.setIdentifier(Id, IdLoc);
513
514 // Parse the full template-id, then turn it into a type.
515 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
516 SourceLocation(), true))
517 return true;
518 if (TNK == TNK_Dependent_template_name)
519 AnnotateTemplateIdTokenAsType(SS);
520
521 // If we didn't end up with a typename token, there's nothing more we
522 // can do.
523 if (Tok.isNot(tok::annot_typename))
524 return true;
525
526 // Retrieve the type from the annotation token, consume that token, and
527 // return.
528 EndLocation = Tok.getAnnotationEndLoc();
529 TypeTy *Type = Tok.getAnnotationValue();
530 ConsumeToken();
531 return Type;
532 }
533
Douglas Gregor42a552f2008-11-05 20:51:48 +0000534 // We have an identifier; check whether it is actually a type.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000535 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, CurScope, SS, true);
536 if (!Type) {
537 Diag(IdLoc, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000538 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000539 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000540 }
541
542 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000543 EndLocation = IdLoc;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000544 return Type;
545}
546
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000547/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
548/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
549/// until we reach the start of a definition or see a token that
550/// cannot start a definition.
551///
552/// class-specifier: [C++ class]
553/// class-head '{' member-specification[opt] '}'
554/// class-head '{' member-specification[opt] '}' attributes[opt]
555/// class-head:
556/// class-key identifier[opt] base-clause[opt]
557/// class-key nested-name-specifier identifier base-clause[opt]
558/// class-key nested-name-specifier[opt] simple-template-id
559/// base-clause[opt]
560/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000561/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000562/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000563/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000564/// simple-template-id base-clause[opt]
565/// class-key:
566/// 'class'
567/// 'struct'
568/// 'union'
569///
570/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000571/// class-key ::[opt] nested-name-specifier[opt] identifier
572/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
573/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000574///
575/// Note that the C++ class-specifier and elaborated-type-specifier,
576/// together, subsume the C99 struct-or-union-specifier:
577///
578/// struct-or-union-specifier: [C99 6.7.2.1]
579/// struct-or-union identifier[opt] '{' struct-contents '}'
580/// struct-or-union identifier
581/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
582/// '}' attributes[opt]
583/// [GNU] struct-or-union attributes[opt] identifier
584/// struct-or-union:
585/// 'struct'
586/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000587void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
588 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000589 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000590 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000591 DeclSpec::TST TagType;
592 if (TagTokKind == tok::kw_struct)
593 TagType = DeclSpec::TST_struct;
594 else if (TagTokKind == tok::kw_class)
595 TagType = DeclSpec::TST_class;
596 else {
597 assert(TagTokKind == tok::kw_union && "Not a class specifier");
598 TagType = DeclSpec::TST_union;
599 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000600
Douglas Gregor374929f2009-09-18 15:37:17 +0000601 if (Tok.is(tok::code_completion)) {
602 // Code completion for a struct, class, or union name.
603 Actions.CodeCompleteTag(CurScope, TagType);
604 ConsumeToken();
605 }
606
Sean Huntbbd37c62009-11-21 08:43:09 +0000607 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000608 // If attributes exist after tag, parse them.
609 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000610 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000611
Steve Narofff59e17e2008-12-24 20:59:21 +0000612 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000613 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000614 AttrList = ParseMicrosoftDeclSpec(AttrList);
615
616 // If C++0x attributes exist here, parse them.
617 // FIXME: Are we consistent with the ordering of parsing of different
618 // styles of attributes?
619 if (isCXX0XAttributeSpecifier())
620 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Douglas Gregorb117a602009-09-04 05:53:02 +0000622 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
623 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
624 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000625 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000626 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
627 // properly.
628 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
629 Tok.setKind(tok::identifier);
630 }
631
632 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
633 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
634 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000635 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000636 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
637 // properly.
638 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
639 Tok.setKind(tok::identifier);
640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000642 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000643 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000644 if (getLang().CPlusPlus) {
645 // "FOO : BAR" is not a potential typo for "FOO::BAR".
646 ColonProtectionRAIIObject X(*this);
647
648 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
649 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
650 Diag(Tok, diag::err_expected_ident);
651 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000652
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000653 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
654
Douglas Gregorcc636682009-02-17 23:15:12 +0000655 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000656 IdentifierInfo *Name = 0;
657 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000658 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000659 if (Tok.is(tok::identifier)) {
660 Name = Tok.getIdentifierInfo();
661 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000662
663 if (Tok.is(tok::less)) {
664 // The name was supposed to refer to a template, but didn't.
665 // Eat the template argument list and try to continue parsing this as
666 // a class (or template thereof).
667 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000668 SourceLocation LAngleLoc, RAngleLoc;
669 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
670 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000671 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000672 // We couldn't parse the template argument list at all, so don't
673 // try to give any location information for the list.
674 LAngleLoc = RAngleLoc = SourceLocation();
675 }
676
677 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000678 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000679 << (TagType == DeclSpec::TST_class? 0
680 : TagType == DeclSpec::TST_struct? 1
681 : 2)
682 << Name
683 << SourceRange(LAngleLoc, RAngleLoc);
684
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000685 // Strip off the last template parameter list if it was empty, since
686 // we've removed its template argument list.
687 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
688 if (TemplateParams && TemplateParams->size() > 1) {
689 TemplateParams->pop_back();
690 } else {
691 TemplateParams = 0;
692 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
693 = ParsedTemplateInfo::NonTemplate;
694 }
695 } else if (TemplateInfo.Kind
696 == ParsedTemplateInfo::ExplicitInstantiation) {
697 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000698 TemplateParams = 0;
699 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
700 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000701 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
702 = SourceLocation();
703 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
704 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000705 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000706
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000707
708 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000709 } else if (Tok.is(tok::annot_template_id)) {
710 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
711 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000712
Douglas Gregorc45c2322009-03-31 00:43:58 +0000713 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000714 // The template-name in the simple-template-id refers to
715 // something other than a class template. Give an appropriate
716 // error message and skip to the ';'.
717 SourceRange Range(NameLoc);
718 if (SS.isNotEmpty())
719 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000720
Douglas Gregor39a8de12009-02-25 19:37:18 +0000721 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
722 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregor39a8de12009-02-25 19:37:18 +0000724 DS.SetTypeSpecError();
725 SkipUntil(tok::semi, false, true);
726 TemplateId->Destroy();
727 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000728 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000729 }
730
John McCall67d1a672009-08-06 02:15:43 +0000731 // There are four options here. If we have 'struct foo;', then this
732 // is either a forward declaration or a friend declaration, which
733 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000734 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000735 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000736 Action::TagUseKind TUK;
Douglas Gregord85bea22009-09-26 06:47:28 +0000737 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
738 if (DS.isFriendSpecified()) {
739 // C++ [class.friend]p2:
740 // A class shall not be defined in a friend declaration.
741 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
742 << SourceRange(DS.getFriendSpecLoc());
743
744 // Skip everything up to the semicolon, so that this looks like a proper
745 // friend class (or template thereof) declaration.
746 SkipUntil(tok::semi, true, true);
747 TUK = Action::TUK_Friend;
748 } else {
749 // Okay, this is a class definition.
750 TUK = Action::TUK_Definition;
751 }
752 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000753 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000754 else
John McCall0f434ec2009-07-31 02:45:11 +0000755 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000756
John McCall0f434ec2009-07-31 02:45:11 +0000757 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000758 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000759 Diag(StartLoc, diag::err_anon_type_definition)
760 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000761
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000762 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000763
764 if (TemplateId)
765 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000766 return;
767 }
768
Douglas Gregorddc29e12009-02-06 22:42:48 +0000769 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000770 Action::DeclResult TagOrTempResult = true; // invalid
771 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000772
John McCall0f434ec2009-07-31 02:45:11 +0000773 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000774 // to turn that template-id into a type.
775
Douglas Gregor402abb52009-05-28 23:31:59 +0000776 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000777 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000778 // Explicit specialization, class template partial specialization,
779 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000780 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000781 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000782 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000783 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000784 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000785 // This is an explicit instantiation of a class template.
786 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000787 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000788 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000789 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000790 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000791 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000792 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000793 TemplateTy::make(TemplateId->Template),
794 TemplateId->TemplateNameLoc,
795 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000796 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000797 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000798 AttrList);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000799 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000800 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000801 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
802 TemplateId->TemplateNameLoc,
803 TemplateId->LAngleLoc,
804 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000805 TemplateId->RAngleLoc);
806
John McCallc4e70192009-09-11 04:59:25 +0000807 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
808 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000809 } else {
810 // This is an explicit specialization or a class template
811 // partial specialization.
812 TemplateParameterLists FakedParamLists;
813
814 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
815 // This looks like an explicit instantiation, because we have
816 // something like
817 //
818 // template class Foo<X>
819 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000820 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000821 // meant to be an explicit specialization, but the user forgot
822 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000823 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000824
Mike Stump1eb44332009-09-09 15:08:12 +0000825 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000826 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000827 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000828 diag::err_explicit_instantiation_with_definition)
829 << SourceRange(TemplateInfo.TemplateLoc)
830 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
831
832 // Create a fake template parameter list that contains only
833 // "template<>", so that we treat this construct as a class
834 // template specialization.
835 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000836 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000837 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000838 LAngleLoc,
839 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000840 LAngleLoc));
841 TemplateParams = &FakedParamLists;
842 }
843
844 // Build the class template specialization.
845 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000846 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000847 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000848 TemplateTy::make(TemplateId->Template),
849 TemplateId->TemplateNameLoc,
850 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000851 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000852 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000853 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000854 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000855 TemplateParams? &(*TemplateParams)[0] : 0,
856 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000857 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000858 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000859 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000860 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000861 // Explicit instantiation of a member of a class template
862 // specialization, e.g.,
863 //
864 // template struct Outer<int>::Inner;
865 //
866 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000867 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000868 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000869 TemplateInfo.TemplateLoc,
870 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000871 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000872 } else {
873 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000874 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000875 // FIXME: Diagnose this particular error.
876 }
877
John McCallc4e70192009-09-11 04:59:25 +0000878 bool IsDependent = false;
879
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000880 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000881 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000882 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000883 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000884 TemplateParams? &(*TemplateParams)[0] : 0,
885 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000886 Owned, IsDependent);
887
888 // If ActOnTag said the type was dependent, try again with the
889 // less common call.
890 if (IsDependent)
891 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
892 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000893 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000894
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000895 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000896 if (TUK == Action::TUK_Definition) {
897 assert(Tok.is(tok::l_brace) ||
898 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000899 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000900 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000901 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000902 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000903 }
904
John McCallc4e70192009-09-11 04:59:25 +0000905 void *Result;
906 if (!TypeResult.isInvalid()) {
907 TagType = DeclSpec::TST_typename;
908 Result = TypeResult.get();
909 Owned = false;
910 } else if (!TagOrTempResult.isInvalid()) {
911 Result = TagOrTempResult.get().getAs<void>();
912 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000913 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000914 return;
915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
John McCallfec54012009-08-03 20:12:06 +0000917 const char *PrevSpec = 0;
918 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000919
John McCallfec54012009-08-03 20:12:06 +0000920 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000921 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000922 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000923}
924
Mike Stump1eb44332009-09-09 15:08:12 +0000925/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000926///
927/// base-clause : [C++ class.derived]
928/// ':' base-specifier-list
929/// base-specifier-list:
930/// base-specifier '...'[opt]
931/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000932void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000933 assert(Tok.is(tok::colon) && "Not a base clause");
934 ConsumeToken();
935
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000936 // Build up an array of parsed base specifiers.
937 llvm::SmallVector<BaseTy *, 8> BaseInfo;
938
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000939 while (true) {
940 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000941 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000942 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000943 // Skip the rest of this base specifier, up until the comma or
944 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000945 SkipUntil(tok::comma, tok::l_brace, true, true);
946 } else {
947 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000948 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000949 }
950
951 // If the next token is a comma, consume it and keep reading
952 // base-specifiers.
953 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000955 // Consume the comma.
956 ConsumeToken();
957 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000958
959 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000960 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000961}
962
963/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
964/// one entry in the base class list of a class specifier, for example:
965/// class foo : public bar, virtual private baz {
966/// 'public bar' and 'virtual private baz' are each base-specifiers.
967///
968/// base-specifier: [C++ class.derived]
969/// ::[opt] nested-name-specifier[opt] class-name
970/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
971/// class-name
972/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
973/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000974Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000975 bool IsVirtual = false;
976 SourceLocation StartLoc = Tok.getLocation();
977
978 // Parse the 'virtual' keyword.
979 if (Tok.is(tok::kw_virtual)) {
980 ConsumeToken();
981 IsVirtual = true;
982 }
983
984 // Parse an (optional) access specifier.
985 AccessSpecifier Access = getAccessSpecifierIfPresent();
986 if (Access)
987 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000989 // Parse the 'virtual' keyword (again!), in case it came after the
990 // access specifier.
991 if (Tok.is(tok::kw_virtual)) {
992 SourceLocation VirtualLoc = ConsumeToken();
993 if (IsVirtual) {
994 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000995 Diag(VirtualLoc, diag::err_dup_virtual)
Chris Lattner29d9c1a2009-12-06 17:36:05 +0000996 << CodeModificationHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000997 }
998
999 IsVirtual = true;
1000 }
1001
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001002 // Parse optional '::' and optional nested-name-specifier.
1003 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001004 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001005
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001006 // The location of the base class itself.
1007 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001008
1009 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001010 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001011 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1012 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001013 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
1015 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001016 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001018 // Notify semantic analysis that we have parsed a complete
1019 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001020 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001021 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001022}
1023
1024/// getAccessSpecifierIfPresent - Determine whether the next token is
1025/// a C++ access-specifier.
1026///
1027/// access-specifier: [C++ class.derived]
1028/// 'private'
1029/// 'protected'
1030/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001031AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001032 switch (Tok.getKind()) {
1033 default: return AS_none;
1034 case tok::kw_private: return AS_private;
1035 case tok::kw_protected: return AS_protected;
1036 case tok::kw_public: return AS_public;
1037 }
1038}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001039
Eli Friedmand33133c2009-07-22 21:45:50 +00001040void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1041 DeclPtrTy ThisDecl) {
1042 // We just declared a member function. If this member function
1043 // has any default arguments, we'll need to parse them later.
1044 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001045 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001046 = DeclaratorInfo.getTypeObject(0).Fun;
1047 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1048 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1049 if (!LateMethod) {
1050 // Push this method onto the stack of late-parsed method
1051 // declarations.
1052 getCurrentClass().MethodDecls.push_back(
1053 LateParsedMethodDeclaration(ThisDecl));
1054 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001055 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001056
1057 // Add all of the parameters prior to this one (they don't
1058 // have default arguments).
1059 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1060 for (unsigned I = 0; I < ParamIdx; ++I)
1061 LateMethod->DefaultArgs.push_back(
1062 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1063 }
1064
1065 // Add this parameter to the list of parameters (it or may
1066 // not have a default argument).
1067 LateMethod->DefaultArgs.push_back(
1068 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1069 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1070 }
1071 }
1072}
1073
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001074/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1075///
1076/// member-declaration:
1077/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1078/// function-definition ';'[opt]
1079/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1080/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001081/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001082/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001083/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001084///
1085/// member-declarator-list:
1086/// member-declarator
1087/// member-declarator-list ',' member-declarator
1088///
1089/// member-declarator:
1090/// declarator pure-specifier[opt]
1091/// declarator constant-initializer[opt]
1092/// identifier[opt] ':' constant-expression
1093///
Sebastian Redle2b68332009-04-12 17:16:29 +00001094/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001095/// '= 0'
1096///
1097/// constant-initializer:
1098/// '=' constant-expression
1099///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001100void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1101 const ParsedTemplateInfo &TemplateInfo) {
John McCall60fa3cf2009-12-11 02:10:03 +00001102 // Access declarations.
1103 if (!TemplateInfo.Kind &&
1104 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
1105 TryAnnotateCXXScopeToken() &&
1106 Tok.is(tok::annot_cxxscope)) {
1107 bool isAccessDecl = false;
1108 if (NextToken().is(tok::identifier))
1109 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1110 else
1111 isAccessDecl = NextToken().is(tok::kw_operator);
1112
1113 if (isAccessDecl) {
1114 // Collect the scope specifier token we annotated earlier.
1115 CXXScopeSpec SS;
1116 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1117
1118 // Try to parse an unqualified-id.
1119 UnqualifiedId Name;
1120 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1121 SkipUntil(tok::semi);
1122 return;
1123 }
1124
1125 // TODO: recover from mistakenly-qualified operator declarations.
1126 if (ExpectAndConsume(tok::semi,
1127 diag::err_expected_semi_after,
1128 "access declaration",
1129 tok::semi))
1130 return;
1131
1132 Actions.ActOnUsingDeclaration(CurScope, AS,
1133 false, SourceLocation(),
1134 SS, Name,
1135 /* AttrList */ 0,
1136 /* IsTypeName */ false,
1137 SourceLocation());
1138 return;
1139 }
1140 }
1141
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001142 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001143 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001144 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001145 SourceLocation DeclEnd;
1146 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001147 return;
1148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Chris Lattner682bf922009-03-29 16:50:03 +00001150 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001151 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001152 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001153 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001154 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001155 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001156 return;
1157 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001158
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001159 // Handle: member-declaration ::= '__extension__' member-declaration
1160 if (Tok.is(tok::kw___extension__)) {
1161 // __extension__ silences extension warnings in the subexpression.
1162 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1163 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001164 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001165 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001166
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001167 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1168 ColonProtectionRAIIObject X(*this);
1169
Sean Huntbbd37c62009-11-21 08:43:09 +00001170 CXX0XAttributeList AttrList;
1171 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001172 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001173 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001174
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001175 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001176 // FIXME: Check for template aliases
Sean Huntbbd37c62009-11-21 08:43:09 +00001177
1178 if (AttrList.HasAttr)
1179 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1180 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001182 // Eat 'using'.
1183 SourceLocation UsingLoc = ConsumeToken();
1184
1185 if (Tok.is(tok::kw_namespace)) {
1186 Diag(UsingLoc, diag::err_using_namespace_in_class);
1187 SkipUntil(tok::semi, true, true);
1188 }
1189 else {
1190 SourceLocation DeclEnd;
1191 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001192 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001193 }
1194 return;
1195 }
1196
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001197 SourceLocation DSStart = Tok.getLocation();
1198 // decl-specifier-seq:
1199 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001200 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001201 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001202 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001203
John McCalldd4a3b02009-09-16 22:47:08 +00001204 Action::MultiTemplateParamsArg TemplateParams(Actions,
1205 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1206 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1207
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001208 if (Tok.is(tok::semi)) {
1209 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001210 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001211 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001212 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001213
John McCall54abf7d2009-11-04 02:18:39 +00001214 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001215
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001216 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001217 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1218 ColonProtectionRAIIObject X(*this);
1219
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001220 // Parse the first declarator.
1221 ParseDeclarator(DeclaratorInfo);
1222 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001223 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001224 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001225 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001226 if (Tok.is(tok::semi))
1227 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001228 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001229 }
1230
John Thompson1b2fc0f2009-11-25 22:58:06 +00001231 // If attributes exist after the declarator, but before an '{', parse them.
1232 if (Tok.is(tok::kw___attribute)) {
1233 SourceLocation Loc;
1234 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1235 DeclaratorInfo.AddAttributes(AttrList, Loc);
1236 }
1237
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001238 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001239 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001240 || (DeclaratorInfo.isFunctionDeclarator() &&
1241 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001242 if (!DeclaratorInfo.isFunctionDeclarator()) {
1243 Diag(Tok, diag::err_func_def_no_params);
1244 ConsumeBrace();
1245 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001246 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001247 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001248
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001249 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1250 Diag(Tok, diag::err_function_declared_typedef);
1251 // This recovery skips the entire function body. It would be nice
1252 // to simply call ParseCXXInlineMethodDef() below, however Sema
1253 // assumes the declarator represents a function, not a typedef.
1254 ConsumeBrace();
1255 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001256 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001257 }
1258
Douglas Gregor37b372b2009-08-20 22:52:58 +00001259 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001260 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001261 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001262 }
1263
1264 // member-declarator-list:
1265 // member-declarator
1266 // member-declarator-list ',' member-declarator
1267
Chris Lattner682bf922009-03-29 16:50:03 +00001268 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001269 OwningExprResult BitfieldSize(Actions);
1270 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001271 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001272
1273 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001274 // member-declarator:
1275 // declarator pure-specifier[opt]
1276 // declarator constant-initializer[opt]
1277 // identifier[opt] ':' constant-expression
1278
1279 if (Tok.is(tok::colon)) {
1280 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001281 BitfieldSize = ParseConstantExpression();
1282 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001283 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001284 }
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001286 // pure-specifier:
1287 // '= 0'
1288 //
1289 // constant-initializer:
1290 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001291 //
1292 // defaulted/deleted function-definition:
1293 // '=' 'default' [TODO]
1294 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001295
1296 if (Tok.is(tok::equal)) {
1297 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001298 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1299 ConsumeToken();
1300 Deleted = true;
1301 } else {
1302 Init = ParseInitializer();
1303 if (Init.isInvalid())
1304 SkipUntil(tok::comma, true, true);
1305 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001306 }
1307
1308 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001309 if (Tok.is(tok::kw___attribute)) {
1310 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001311 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001312 DeclaratorInfo.AddAttributes(AttrList, Loc);
1313 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001314
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001315 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001316 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001317 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001318
1319 DeclPtrTy ThisDecl;
1320 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001321 // TODO: handle initializers, bitfields, 'delete'
1322 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1323 /*IsDefinition*/ false,
1324 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001325 } else {
John McCall67d1a672009-08-06 02:15:43 +00001326 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1327 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001328 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001329 BitfieldSize.release(),
1330 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001331 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001332 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001333 }
Chris Lattner682bf922009-03-29 16:50:03 +00001334 if (ThisDecl)
1335 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001336
Douglas Gregor72b505b2008-12-16 21:30:33 +00001337 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001338 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001339 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001340 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001341 }
1342
John McCall54abf7d2009-11-04 02:18:39 +00001343 DeclaratorInfo.complete(ThisDecl);
1344
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001345 // If we don't have a comma, it is either the end of the list (a ';')
1346 // or an error, bail out.
1347 if (Tok.isNot(tok::comma))
1348 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001350 // Consume the comma.
1351 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001353 // Parse the next declarator.
1354 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001355 BitfieldSize = 0;
1356 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001357 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001359 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001360 if (Tok.is(tok::kw___attribute)) {
1361 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001362 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001363 DeclaratorInfo.AddAttributes(AttrList, Loc);
1364 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001365
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001366 if (Tok.isNot(tok::colon))
1367 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001368 }
1369
1370 if (Tok.is(tok::semi)) {
1371 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001372 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001373 DeclsInGroup.size());
1374 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001375 }
1376
1377 Diag(Tok, diag::err_expected_semi_decl_list);
1378 // Skip to end of block or statement
1379 SkipUntil(tok::r_brace, true, true);
1380 if (Tok.is(tok::semi))
1381 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001382 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001383}
1384
1385/// ParseCXXMemberSpecification - Parse the class definition.
1386///
1387/// member-specification:
1388/// member-declaration member-specification[opt]
1389/// access-specifier ':' member-specification[opt]
1390///
1391void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001392 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001393 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001394 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001395 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001396
Chris Lattner49f28ca2009-03-05 08:00:35 +00001397 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1398 PP.getSourceManager(),
1399 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Douglas Gregor26997fd2010-01-16 20:52:59 +00001401 // Determine whether this is a non-nested class. Note that local
1402 // classes are *not* considered to be nested classes.
1403 bool NonNestedClass = true;
1404 if (!ClassStack.empty()) {
1405 for (const Scope *S = CurScope; S; S = S->getParent()) {
1406 if (S->isClassScope()) {
1407 // We're inside a class scope, so this is a nested class.
1408 NonNestedClass = false;
1409 break;
1410 }
1411
1412 if ((S->getFlags() & Scope::FnScope)) {
1413 // If we're in a function or function template declared in the
1414 // body of a class, then this is a local class rather than a
1415 // nested class.
1416 const Scope *Parent = S->getParent();
1417 if (Parent->isTemplateParamScope())
1418 Parent = Parent->getParent();
1419 if (Parent->isClassScope())
1420 break;
1421 }
1422 }
1423 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001424
1425 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001426 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001427
Douglas Gregor6569d682009-05-27 23:11:45 +00001428 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001429 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001430
Douglas Gregorddc29e12009-02-06 22:42:48 +00001431 if (TagDecl)
1432 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001433
1434 if (Tok.is(tok::colon)) {
1435 ParseBaseClause(TagDecl);
1436
1437 if (!Tok.is(tok::l_brace)) {
1438 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
1439 return;
1440 }
1441 }
1442
1443 assert(Tok.is(tok::l_brace));
1444
1445 SourceLocation LBraceLoc = ConsumeBrace();
1446
1447 if (!TagDecl) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001448 SkipUntil(tok::r_brace, false, false);
1449 return;
1450 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001451
John McCallf9368152009-12-20 07:58:13 +00001452 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
1453
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001454 // C++ 11p3: Members of a class defined with the keyword class are private
1455 // by default. Members of a class defined with the keywords struct or union
1456 // are public by default.
1457 AccessSpecifier CurAS;
1458 if (TagType == DeclSpec::TST_class)
1459 CurAS = AS_private;
1460 else
1461 CurAS = AS_public;
1462
1463 // While we still have something to read, read the member-declarations.
1464 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1465 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001467 // Check for extraneous top-level semicolon.
1468 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001469 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001470 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001471 ConsumeToken();
1472 continue;
1473 }
1474
1475 AccessSpecifier AS = getAccessSpecifierIfPresent();
1476 if (AS != AS_none) {
1477 // Current token is a C++ access specifier.
1478 CurAS = AS;
1479 ConsumeToken();
1480 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1481 continue;
1482 }
1483
Douglas Gregor37b372b2009-08-20 22:52:58 +00001484 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001486 // Parse all the comma separated declarators.
1487 ParseCXXClassMemberDeclaration(CurAS);
1488 }
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001490 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001492 AttributeList *AttrList = 0;
1493 // If attributes exist after class contents, parse them.
1494 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001495 AttrList = ParseGNUAttributes(); // FIXME: where should I put them?
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001496
1497 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1498 LBraceLoc, RBraceLoc);
1499
1500 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1501 // complete within function bodies, default arguments,
1502 // exception-specifications, and constructor ctor-initializers (including
1503 // such things in nested classes).
1504 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001505 // FIXME: Only function bodies and constructor ctor-initializers are
1506 // parsed correctly, fix the rest.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001507 if (NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001508 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001509 // are complete and we can parse the delayed portions of method
1510 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001511 ParseLexedMethodDeclarations(getCurrentClass());
1512 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001513 }
1514
1515 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001516 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001517 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001518
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001519 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001520}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001521
1522/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1523/// which explicitly initializes the members or base classes of a
1524/// class (C++ [class.base.init]). For example, the three initializers
1525/// after the ':' in the Derived constructor below:
1526///
1527/// @code
1528/// class Base { };
1529/// class Derived : Base {
1530/// int x;
1531/// float f;
1532/// public:
1533/// Derived(float f) : Base(), x(17), f(f) { }
1534/// };
1535/// @endcode
1536///
Mike Stump1eb44332009-09-09 15:08:12 +00001537/// [C++] ctor-initializer:
1538/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001539///
Mike Stump1eb44332009-09-09 15:08:12 +00001540/// [C++] mem-initializer-list:
1541/// mem-initializer
1542/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001543void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001544 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1545
1546 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Douglas Gregor7ad83902008-11-05 04:29:56 +00001548 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregor7ad83902008-11-05 04:29:56 +00001550 do {
1551 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001552 if (!MemInit.isInvalid())
1553 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001554
1555 if (Tok.is(tok::comma))
1556 ConsumeToken();
1557 else if (Tok.is(tok::l_brace))
1558 break;
1559 else {
1560 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001561 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001562 SkipUntil(tok::l_brace, true, true);
1563 break;
1564 }
1565 } while (true);
1566
Mike Stump1eb44332009-09-09 15:08:12 +00001567 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001568 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001569}
1570
1571/// ParseMemInitializer - Parse a C++ member initializer, which is
1572/// part of a constructor initializer that explicitly initializes one
1573/// member or base class (C++ [class.base.init]). See
1574/// ParseConstructorInitializer for an example.
1575///
1576/// [C++] mem-initializer:
1577/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001578///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001579/// [C++] mem-initializer-id:
1580/// '::'[opt] nested-name-specifier[opt] class-name
1581/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001582Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001583 // parse '::'[opt] nested-name-specifier[opt]
1584 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001585 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001586 TypeTy *TemplateTypeTy = 0;
1587 if (Tok.is(tok::annot_template_id)) {
1588 TemplateIdAnnotation *TemplateId
1589 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001590 if (TemplateId->Kind == TNK_Type_template ||
1591 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001592 AnnotateTemplateIdTokenAsType(&SS);
1593 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1594 TemplateTypeTy = Tok.getAnnotationValue();
1595 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001596 }
1597 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001598 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001599 return true;
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregor7ad83902008-11-05 04:29:56 +00001602 // Get the identifier. This may be a member name or a class name,
1603 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001604 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001605 SourceLocation IdLoc = ConsumeToken();
1606
1607 // Parse the '('.
1608 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001609 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001610 return true;
1611 }
1612 SourceLocation LParenLoc = ConsumeParen();
1613
1614 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001615 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001616 CommaLocsTy CommaLocs;
1617 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1618 SkipUntil(tok::r_paren);
1619 return true;
1620 }
1621
1622 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1623
Fariborz Jahanian96174332009-07-01 19:21:19 +00001624 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1625 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001626 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001627 ArgExprs.size(), CommaLocs.data(),
1628 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001629}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001630
1631/// ParseExceptionSpecification - Parse a C++ exception-specification
1632/// (C++ [except.spec]).
1633///
Douglas Gregora4745612008-12-01 18:00:20 +00001634/// exception-specification:
1635/// 'throw' '(' type-id-list [opt] ')'
1636/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001637///
Douglas Gregora4745612008-12-01 18:00:20 +00001638/// type-id-list:
1639/// type-id
1640/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001641///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001642bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001643 llvm::SmallVector<TypeTy*, 2>
1644 &Exceptions,
1645 llvm::SmallVector<SourceRange, 2>
1646 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001647 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001648 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001650 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001652 if (!Tok.is(tok::l_paren)) {
1653 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1654 }
1655 SourceLocation LParenLoc = ConsumeParen();
1656
Douglas Gregora4745612008-12-01 18:00:20 +00001657 // Parse throw(...), a Microsoft extension that means "this function
1658 // can throw anything".
1659 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001660 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001661 SourceLocation EllipsisLoc = ConsumeToken();
1662 if (!getLang().Microsoft)
1663 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001664 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001665 return false;
1666 }
1667
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001668 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001669 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001670 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001671 TypeResult Res(ParseTypeName(&Range));
1672 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001673 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001674 Ranges.push_back(Range);
1675 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001676 if (Tok.is(tok::comma))
1677 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001678 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001679 break;
1680 }
1681
Sebastian Redlab197ba2009-02-09 18:23:29 +00001682 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001683 return false;
1684}
Douglas Gregor6569d682009-05-27 23:11:45 +00001685
1686/// \brief We have just started parsing the definition of a new class,
1687/// so push that class onto our stack of classes that is currently
1688/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001689void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1690 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001691 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001692 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001693}
1694
1695/// \brief Deallocate the given parsed class and all of its nested
1696/// classes.
1697void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1698 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1699 DeallocateParsedClasses(Class->NestedClasses[I]);
1700 delete Class;
1701}
1702
1703/// \brief Pop the top class of the stack of classes that are
1704/// currently being parsed.
1705///
1706/// This routine should be called when we have finished parsing the
1707/// definition of a class, but have not yet popped the Scope
1708/// associated with the class's definition.
1709///
1710/// \returns true if the class we've popped is a top-level class,
1711/// false otherwise.
1712void Parser::PopParsingClass() {
1713 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Douglas Gregor6569d682009-05-27 23:11:45 +00001715 ParsingClass *Victim = ClassStack.top();
1716 ClassStack.pop();
1717 if (Victim->TopLevelClass) {
1718 // Deallocate all of the nested classes of this class,
1719 // recursively: we don't need to keep any of this information.
1720 DeallocateParsedClasses(Victim);
1721 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001722 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001723 assert(!ClassStack.empty() && "Missing top-level class?");
1724
1725 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1726 Victim->NestedClasses.empty()) {
1727 // The victim is a nested class, but we will not need to perform
1728 // any processing after the definition of this class since it has
1729 // no members whose handling was delayed. Therefore, we can just
1730 // remove this nested class.
1731 delete Victim;
1732 return;
1733 }
1734
1735 // This nested class has some members that will need to be processed
1736 // after the top-level class is completely defined. Therefore, add
1737 // it to the list of nested classes within its parent.
1738 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1739 ClassStack.top()->NestedClasses.push_back(Victim);
1740 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1741}
Sean Huntbbd37c62009-11-21 08:43:09 +00001742
1743/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1744/// parses standard attributes.
1745///
1746/// [C++0x] attribute-specifier:
1747/// '[' '[' attribute-list ']' ']'
1748///
1749/// [C++0x] attribute-list:
1750/// attribute[opt]
1751/// attribute-list ',' attribute[opt]
1752///
1753/// [C++0x] attribute:
1754/// attribute-token attribute-argument-clause[opt]
1755///
1756/// [C++0x] attribute-token:
1757/// identifier
1758/// attribute-scoped-token
1759///
1760/// [C++0x] attribute-scoped-token:
1761/// attribute-namespace '::' identifier
1762///
1763/// [C++0x] attribute-namespace:
1764/// identifier
1765///
1766/// [C++0x] attribute-argument-clause:
1767/// '(' balanced-token-seq ')'
1768///
1769/// [C++0x] balanced-token-seq:
1770/// balanced-token
1771/// balanced-token-seq balanced-token
1772///
1773/// [C++0x] balanced-token:
1774/// '(' balanced-token-seq ')'
1775/// '[' balanced-token-seq ']'
1776/// '{' balanced-token-seq '}'
1777/// any token but '(', ')', '[', ']', '{', or '}'
1778CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1779 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1780 && "Not a C++0x attribute list");
1781
1782 SourceLocation StartLoc = Tok.getLocation(), Loc;
1783 AttributeList *CurrAttr = 0;
1784
1785 ConsumeBracket();
1786 ConsumeBracket();
1787
1788 if (Tok.is(tok::comma)) {
1789 Diag(Tok.getLocation(), diag::err_expected_ident);
1790 ConsumeToken();
1791 }
1792
1793 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1794 // attribute not present
1795 if (Tok.is(tok::comma)) {
1796 ConsumeToken();
1797 continue;
1798 }
1799
1800 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1801 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1802
1803 // scoped attribute
1804 if (Tok.is(tok::coloncolon)) {
1805 ConsumeToken();
1806
1807 if (!Tok.is(tok::identifier)) {
1808 Diag(Tok.getLocation(), diag::err_expected_ident);
1809 SkipUntil(tok::r_square, tok::comma, true, true);
1810 continue;
1811 }
1812
1813 ScopeName = AttrName;
1814 ScopeLoc = AttrLoc;
1815
1816 AttrName = Tok.getIdentifierInfo();
1817 AttrLoc = ConsumeToken();
1818 }
1819
1820 bool AttrParsed = false;
1821 // No scoped names are supported; ideally we could put all non-standard
1822 // attributes into namespaces.
1823 if (!ScopeName) {
1824 switch(AttributeList::getKind(AttrName))
1825 {
1826 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001827 case AttributeList::AT_base_check:
1828 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001829 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001830 case AttributeList::AT_hiding:
1831 case AttributeList::AT_noreturn:
1832 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001833 if (Tok.is(tok::l_paren)) {
1834 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1835 << AttrName->getName();
1836 break;
1837 }
1838
1839 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1840 SourceLocation(), 0, 0, CurrAttr, false,
1841 true);
1842 AttrParsed = true;
1843 break;
1844 }
1845
1846 // One argument; must be a type-id or assignment-expression
1847 case AttributeList::AT_aligned: {
1848 if (Tok.isNot(tok::l_paren)) {
1849 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1850 << AttrName->getName();
1851 break;
1852 }
1853 SourceLocation ParamLoc = ConsumeParen();
1854
1855 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1856
1857 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1858
1859 ExprVector ArgExprs(Actions);
1860 ArgExprs.push_back(ArgExpr.release());
1861 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1862 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1863 false, true);
1864
1865 AttrParsed = true;
1866 break;
1867 }
1868
1869 // Silence warnings
1870 default: break;
1871 }
1872 }
1873
1874 // Skip the entire parameter clause, if any
1875 if (!AttrParsed && Tok.is(tok::l_paren)) {
1876 ConsumeParen();
1877 // SkipUntil maintains the balancedness of tokens.
1878 SkipUntil(tok::r_paren, false);
1879 }
1880 }
1881
1882 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1883 SkipUntil(tok::r_square, false);
1884 Loc = Tok.getLocation();
1885 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1886 SkipUntil(tok::r_square, false);
1887
1888 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1889 return Attr;
1890}
1891
1892/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1893/// attribute.
1894///
1895/// FIXME: Simply returns an alignof() expression if the argument is a
1896/// type. Ideally, the type should be propagated directly into Sema.
1897///
1898/// [C++0x] 'align' '(' type-id ')'
1899/// [C++0x] 'align' '(' assignment-expression ')'
1900Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1901 if (isTypeIdInParens()) {
1902 EnterExpressionEvaluationContext Unevaluated(Actions,
1903 Action::Unevaluated);
1904 SourceLocation TypeLoc = Tok.getLocation();
1905 TypeTy *Ty = ParseTypeName().get();
1906 SourceRange TypeRange(Start, Tok.getLocation());
1907 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1908 TypeRange);
1909 } else
1910 return ParseConstantExpression();
1911}