blob: 2a3b5d665cc4c998bb82c41699d838ce5ee8dfa5 [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson74d7f0d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor423984d2008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor556877c2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattnera5235172007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000019#include "clang/Parse/Template.h"
Chris Lattnerd19c1c02008-12-18 01:12:00 +000020#include "ExtensionRAIIObject.h"
Chris Lattnera5235172007-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 Stump11289f42009-09-09 15:08:12 +000042///
Chris Lattnera5235172007-08-25 06:57:03 +000043/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
44/// 'namespace' identifier '=' qualified-namespace-specifier ';'
45///
Chris Lattner49836b42009-04-02 04:16:50 +000046Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
47 SourceLocation &DeclEnd) {
Chris Lattner76c72282007-10-09 17:33:22 +000048 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000049 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump11289f42009-09-09 15:08:12 +000050
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000051 if (Tok.is(tok::code_completion)) {
52 Actions.CodeCompleteNamespaceDecl(CurScope);
53 ConsumeToken();
54 }
55
Chris Lattnera5235172007-08-25 06:57:03 +000056 SourceLocation IdentLoc;
57 IdentifierInfo *Ident = 0;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000058
59 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000060
Chris Lattner76c72282007-10-09 17:33:22 +000061 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000062 Ident = Tok.getIdentifierInfo();
63 IdentLoc = ConsumeToken(); // eat the identifier.
64 }
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattnera5235172007-08-25 06:57:03 +000066 // Read label attributes, if present.
Chris Lattner83f095c2009-03-28 19:18:32 +000067 Action::AttrTy *AttrList = 0;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000068 if (Tok.is(tok::kw___attribute)) {
69 attrTok = Tok;
70
Chris Lattnera5235172007-08-25 06:57:03 +000071 // FIXME: save these somewhere.
Alexis Hunt96d5c762009-11-21 08:43:09 +000072 AttrList = ParseGNUAttributes();
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073 }
Mike Stump11289f42009-09-09 15:08:12 +000074
Douglas Gregor6b6bba42009-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 Lattner49836b42009-04-02 04:16:50 +000079 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000080 }
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattner4de55aa2009-03-29 14:02:43 +000082 if (Tok.isNot(tok::l_brace)) {
Mike Stump11289f42009-09-09 15:08:12 +000083 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +000084 diag::err_expected_ident_lbrace);
85 return DeclPtrTy();
Chris Lattnera5235172007-08-25 06:57:03 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner4de55aa2009-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 Stump11289f42009-09-09 15:08:12 +000099
Alexis Hunt96d5c762009-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 Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattner4de55aa2009-03-29 14:02:43 +0000107 // Leave the namespace scope.
108 NamespaceScope.Exit();
109
Chris Lattner49836b42009-04-02 04:16:50 +0000110 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
111 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000112
Chris Lattner49836b42009-04-02 04:16:50 +0000113 DeclEnd = RBraceLoc;
Chris Lattner4de55aa2009-03-29 14:02:43 +0000114 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000115}
Chris Lattner38376f12008-01-12 07:05:38 +0000116
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000117/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
118/// alias definition.
119///
Anders Carlsson47952ae2009-03-28 22:53:22 +0000120Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000121 SourceLocation AliasLoc,
Chris Lattner49836b42009-04-02 04:16:50 +0000122 IdentifierInfo *Alias,
123 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000124 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000125
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000126 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000127
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000128 if (Tok.is(tok::code_completion)) {
129 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
130 ConsumeToken();
131 }
132
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000133 CXXScopeSpec SS;
134 // Parse (optional) nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000135 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlsson1894f0d42009-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 Lattner83f095c2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000142 }
143
144 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000145 IdentifierInfo *Ident = Tok.getIdentifierInfo();
146 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000147
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000148 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000149 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000150 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
151 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000152
153 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000154 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000155}
156
Chris Lattner38376f12008-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///
Chris Lattner83f095c2009-03-28 19:18:32 +0000164Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000165 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner38376f12008-01-12 07:05:38 +0000166 llvm::SmallVector<char, 8> LangBuffer;
167 // LangBuffer is guaranteed to be big enough.
168 LangBuffer.resize(Tok.getLength());
169 const char *LangBufPtr = &LangBuffer[0];
170 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
171
172 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000173
Douglas Gregor07665a62009-01-05 19:45:36 +0000174 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +0000175 DeclPtrTy LinkageSpec
176 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor07665a62009-01-05 19:45:36 +0000177 /*FIXME: */SourceLocation(),
178 Loc, LangBufPtr, StrSize,
Mike Stump11289f42009-09-09 15:08:12 +0000179 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000180 : SourceLocation());
181
Alexis Hunt96d5c762009-11-21 08:43:09 +0000182 CXX0XAttributeList Attr;
183 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
184 Attr = ParseCXX0XAttributes();
185 }
186
Douglas Gregor07665a62009-01-05 19:45:36 +0000187 if (Tok.isNot(tok::l_brace)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000188 ParseDeclarationOrFunctionDefinition(Attr.AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000189 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000190 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000191 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000192
Alexis Hunt96d5c762009-11-21 08:43:09 +0000193 if (Attr.HasAttr)
194 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
195 << Attr.Range;
196
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000197 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000198 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000199 CXX0XAttributeList Attr;
200 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
201 Attr = ParseCXX0XAttributes();
202 ParseExternalDeclaration(Attr);
Chris Lattner38376f12008-01-12 07:05:38 +0000203 }
204
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000205 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor07665a62009-01-05 19:45:36 +0000206 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000207}
Douglas Gregor556877c2008-04-13 21:30:24 +0000208
Douglas Gregord7c4d982008-12-30 03:27:21 +0000209/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
210/// using-directive. Assumes that current token is 'using'.
Chris Lattner49836b42009-04-02 04:16:50 +0000211Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000212 SourceLocation &DeclEnd,
213 CXX0XAttributeList Attr) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000214 assert(Tok.is(tok::kw_using) && "Not using token");
215
216 // Eat 'using'.
217 SourceLocation UsingLoc = ConsumeToken();
218
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000219 if (Tok.is(tok::code_completion)) {
220 Actions.CodeCompleteUsing(CurScope);
221 ConsumeToken();
222 }
223
Chris Lattner9b01ca12009-01-06 06:55:51 +0000224 if (Tok.is(tok::kw_namespace))
Douglas Gregord7c4d982008-12-30 03:27:21 +0000225 // Next token after 'using' is 'namespace' so it must be using-directive
Alexis Hunt96d5c762009-11-21 08:43:09 +0000226 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
227
228 if (Attr.HasAttr)
229 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
230 << Attr.Range;
Chris Lattner9b01ca12009-01-06 06:55:51 +0000231
232 // Otherwise, it must be using-declaration.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000233 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner49836b42009-04-02 04:16:50 +0000234 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000235}
236
237/// ParseUsingDirective - Parse C++ using-directive, assumes
238/// that current token is 'namespace' and 'using' was already parsed.
239///
240/// using-directive: [C++ 7.3.p4: namespace.udir]
241/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
242/// namespace-name ;
243/// [GNU] using-directive:
244/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
245/// namespace-name attributes[opt] ;
246///
Chris Lattner83f095c2009-03-28 19:18:32 +0000247Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000248 SourceLocation UsingLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000249 SourceLocation &DeclEnd,
250 AttributeList *Attr) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000251 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
252
253 // Eat 'namespace'.
254 SourceLocation NamespcLoc = ConsumeToken();
255
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000256 if (Tok.is(tok::code_completion)) {
257 Actions.CodeCompleteUsingDirective(CurScope);
258 ConsumeToken();
259 }
260
Douglas Gregord7c4d982008-12-30 03:27:21 +0000261 CXXScopeSpec SS;
262 // Parse (optional) nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000263 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000264
Douglas Gregord7c4d982008-12-30 03:27:21 +0000265 IdentifierInfo *NamespcName = 0;
266 SourceLocation IdentLoc = SourceLocation();
267
268 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000269 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000270 Diag(Tok, diag::err_expected_namespace_name);
271 // If there was invalid namespace name, skip to end of decl, and eat ';'.
272 SkipUntil(tok::semi);
273 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattner83f095c2009-03-28 19:18:32 +0000274 return DeclPtrTy();
Douglas Gregord7c4d982008-12-30 03:27:21 +0000275 }
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000277 // Parse identifier.
278 NamespcName = Tok.getIdentifierInfo();
279 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000281 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000282 bool GNUAttr = false;
283 if (Tok.is(tok::kw___attribute)) {
284 GNUAttr = true;
285 Attr = addAttributeLists(Attr, ParseGNUAttributes());
286 }
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000288 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000289 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000290 ExpectAndConsume(tok::semi,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000291 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner34a95662009-06-14 00:07:48 +0000292 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000293
294 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000295 IdentLoc, NamespcName, Attr);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000296}
297
298/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
299/// 'using' was already seen.
300///
301/// using-declaration: [C++ 7.3.p3: namespace.udecl]
302/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000303/// unqualified-id
304/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000305///
Chris Lattner83f095c2009-03-28 19:18:32 +0000306Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000307 SourceLocation UsingLoc,
Anders Carlsson7b194b72009-08-29 19:54:19 +0000308 SourceLocation &DeclEnd,
309 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000310 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000311 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000312 bool IsTypeName;
313
314 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000315 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000316 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000317 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000318 ConsumeToken();
319 IsTypeName = true;
320 }
321 else
322 IsTypeName = false;
323
324 // Parse nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000325 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000326
327 AttributeList *AttrList = 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000328
329 // Check nested-name specifier.
330 if (SS.isInvalid()) {
331 SkipUntil(tok::semi);
332 return DeclPtrTy();
333 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000334
335 // Parse the unqualified-id. We allow parsing of both constructor and
336 // destructor names and allow the action module to diagnose any semantic
337 // errors.
338 UnqualifiedId Name;
339 if (ParseUnqualifiedId(SS,
340 /*EnteringContext=*/false,
341 /*AllowDestructorName=*/true,
342 /*AllowConstructorName=*/true,
343 /*ObjectType=*/0,
344 Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000345 SkipUntil(tok::semi);
346 return DeclPtrTy();
347 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000348
Douglas Gregorfec52632009-06-20 00:51:54 +0000349 // Parse (optional) attributes (most likely GNU strong-using extension).
350 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000351 AttrList = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000352
Douglas Gregorfec52632009-06-20 00:51:54 +0000353 // Eat ';'.
354 DeclEnd = Tok.getLocation();
355 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor220f4272009-11-04 16:30:06 +0000356 AttrList ? "attributes list" : "using declaration",
357 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000358
Douglas Gregor220f4272009-11-04 16:30:06 +0000359 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS, Name,
John McCalle61f2ba2009-11-18 02:36:19 +0000360 AttrList, IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000361}
362
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000363/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
364///
365/// static_assert-declaration:
366/// static_assert ( constant-expression , string-literal ) ;
367///
Chris Lattner49836b42009-04-02 04:16:50 +0000368Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000369 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
370 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000371
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000372 if (Tok.isNot(tok::l_paren)) {
373 Diag(Tok, diag::err_expected_lparen);
Chris Lattner83f095c2009-03-28 19:18:32 +0000374 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000377 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000378
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000379 OwningExprResult AssertExpr(ParseConstantExpression());
380 if (AssertExpr.isInvalid()) {
381 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000382 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000383 }
Mike Stump11289f42009-09-09 15:08:12 +0000384
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000385 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner83f095c2009-03-28 19:18:32 +0000386 return DeclPtrTy();
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000387
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000388 if (Tok.isNot(tok::string_literal)) {
389 Diag(Tok, diag::err_expected_string_literal);
390 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000391 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000394 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000395 if (AssertMessage.isInvalid())
Chris Lattner83f095c2009-03-28 19:18:32 +0000396 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000397
Anders Carlsson27de6a52009-03-15 18:44:04 +0000398 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattner49836b42009-04-02 04:16:50 +0000400 DeclEnd = Tok.getLocation();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000401 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
402
Mike Stump11289f42009-09-09 15:08:12 +0000403 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson27de6a52009-03-15 18:44:04 +0000404 move(AssertMessage));
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000405}
406
Anders Carlsson74948d02009-06-24 17:47:40 +0000407/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
408///
409/// 'decltype' ( expression )
410///
411void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
412 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
413
414 SourceLocation StartLoc = ConsumeToken();
415 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000416
417 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000418 "decltype")) {
419 SkipUntil(tok::r_paren);
420 return;
421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Anders Carlsson74948d02009-06-24 17:47:40 +0000423 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000424
Anders Carlsson74948d02009-06-24 17:47:40 +0000425 // C++0x [dcl.type.simple]p4:
426 // The operand of the decltype specifier is an unevaluated operand.
427 EnterExpressionEvaluationContext Unevaluated(Actions,
428 Action::Unevaluated);
429 OwningExprResult Result = ParseExpression();
430 if (Result.isInvalid()) {
431 SkipUntil(tok::r_paren);
432 return;
433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson74948d02009-06-24 17:47:40 +0000435 // Match the ')'
436 SourceLocation RParenLoc;
437 if (Tok.is(tok::r_paren))
438 RParenLoc = ConsumeParen();
439 else
440 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000441
Anders Carlsson74948d02009-06-24 17:47:40 +0000442 if (RParenLoc.isInvalid())
443 return;
444
445 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000446 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000447 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000448 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000449 DiagID, Result.release()))
450 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000451}
452
Douglas Gregor831c93f2008-11-05 20:51:48 +0000453/// ParseClassName - Parse a C++ class-name, which names a class. Note
454/// that we only check that the result names a type; semantic analysis
455/// will need to verify that the type names a class. The result is
Douglas Gregord54dfb82009-02-25 23:52:28 +0000456/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000457/// found.
458///
459/// class-name: [C++ 9.1]
460/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000461/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000462///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000463Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000464 const CXXScopeSpec *SS,
465 bool DestrExpected) {
Douglas Gregord54dfb82009-02-25 23:52:28 +0000466 // Check whether we have a template-id that names a type.
467 if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +0000468 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000469 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000470 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000471 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000472
473 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
474 TypeTy *Type = Tok.getAnnotationValue();
475 EndLocation = Tok.getAnnotationEndLoc();
476 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000477
478 if (Type)
479 return Type;
480 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000481 }
482
483 // Fall through to produce an error below.
484 }
485
Douglas Gregor831c93f2008-11-05 20:51:48 +0000486 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000487 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000488 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000489 }
490
491 // We have an identifier; check whether it is actually a type.
Mike Stump11289f42009-09-09 15:08:12 +0000492 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor5e0962f2009-08-26 18:27:52 +0000493 Tok.getLocation(), CurScope, SS,
494 true);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000495 if (!Type) {
Mike Stump11289f42009-09-09 15:08:12 +0000496 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000497 : diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000498 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000499 }
500
501 // Consume the identifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000502 EndLocation = ConsumeToken();
Douglas Gregor831c93f2008-11-05 20:51:48 +0000503 return Type;
504}
505
Douglas Gregor556877c2008-04-13 21:30:24 +0000506/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
507/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
508/// until we reach the start of a definition or see a token that
509/// cannot start a definition.
510///
511/// class-specifier: [C++ class]
512/// class-head '{' member-specification[opt] '}'
513/// class-head '{' member-specification[opt] '}' attributes[opt]
514/// class-head:
515/// class-key identifier[opt] base-clause[opt]
516/// class-key nested-name-specifier identifier base-clause[opt]
517/// class-key nested-name-specifier[opt] simple-template-id
518/// base-clause[opt]
519/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000520/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000521/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000522/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000523/// simple-template-id base-clause[opt]
524/// class-key:
525/// 'class'
526/// 'struct'
527/// 'union'
528///
529/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000530/// class-key ::[opt] nested-name-specifier[opt] identifier
531/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
532/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000533///
534/// Note that the C++ class-specifier and elaborated-type-specifier,
535/// together, subsume the C99 struct-or-union-specifier:
536///
537/// struct-or-union-specifier: [C99 6.7.2.1]
538/// struct-or-union identifier[opt] '{' struct-contents '}'
539/// struct-or-union identifier
540/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
541/// '}' attributes[opt]
542/// [GNU] struct-or-union attributes[opt] identifier
543/// struct-or-union:
544/// 'struct'
545/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000546void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
547 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000548 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor6c2adff2009-03-25 22:00:53 +0000549 AccessSpecifier AS) {
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000550 DeclSpec::TST TagType;
551 if (TagTokKind == tok::kw_struct)
552 TagType = DeclSpec::TST_struct;
553 else if (TagTokKind == tok::kw_class)
554 TagType = DeclSpec::TST_class;
555 else {
556 assert(TagTokKind == tok::kw_union && "Not a class specifier");
557 TagType = DeclSpec::TST_union;
558 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000559
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000560 if (Tok.is(tok::code_completion)) {
561 // Code completion for a struct, class, or union name.
562 Actions.CodeCompleteTag(CurScope, TagType);
563 ConsumeToken();
564 }
565
Alexis Hunt96d5c762009-11-21 08:43:09 +0000566 AttributeList *AttrList = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000567 // If attributes exist after tag, parse them.
568 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000569 AttrList = ParseGNUAttributes();
Douglas Gregor556877c2008-04-13 21:30:24 +0000570
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000571 // If declspecs exist after tag, parse them.
Eli Friedman53339e02009-06-08 23:27:34 +0000572 if (Tok.is(tok::kw___declspec))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000573 AttrList = ParseMicrosoftDeclSpec(AttrList);
574
575 // If C++0x attributes exist here, parse them.
576 // FIXME: Are we consistent with the ordering of parsing of different
577 // styles of attributes?
578 if (isCXX0XAttributeSpecifier())
579 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Douglas Gregor119b0c72009-09-04 05:53:02 +0000581 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
582 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
583 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000584 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000585 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
586 // properly.
587 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
588 Tok.setKind(tok::identifier);
589 }
590
591 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
592 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
593 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000594 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000595 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
596 // properly.
597 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
598 Tok.setKind(tok::identifier);
599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000601 // Parse the (optional) nested-name-specifier.
602 CXXScopeSpec SS;
Mike Stump11289f42009-09-09 15:08:12 +0000603 if (getLang().CPlusPlus &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000604 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor7f741122009-02-25 19:37:18 +0000605 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000606 Diag(Tok, diag::err_expected_ident);
Douglas Gregor67a65642009-02-17 23:15:12 +0000607
Douglas Gregor916462b2009-10-30 21:46:58 +0000608 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
609
Douglas Gregor67a65642009-02-17 23:15:12 +0000610 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000611 IdentifierInfo *Name = 0;
612 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000613 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000614 if (Tok.is(tok::identifier)) {
615 Name = Tok.getIdentifierInfo();
616 NameLoc = ConsumeToken();
Douglas Gregor916462b2009-10-30 21:46:58 +0000617
618 if (Tok.is(tok::less)) {
619 // The name was supposed to refer to a template, but didn't.
620 // Eat the template argument list and try to continue parsing this as
621 // a class (or template thereof).
622 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000623 SourceLocation LAngleLoc, RAngleLoc;
624 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
625 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000626 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000627 // We couldn't parse the template argument list at all, so don't
628 // try to give any location information for the list.
629 LAngleLoc = RAngleLoc = SourceLocation();
630 }
631
632 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000633 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000634 << (TagType == DeclSpec::TST_class? 0
635 : TagType == DeclSpec::TST_struct? 1
636 : 2)
637 << Name
638 << SourceRange(LAngleLoc, RAngleLoc);
639
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000640 // Strip off the last template parameter list if it was empty, since
641 // we've removed its template argument list.
642 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
643 if (TemplateParams && TemplateParams->size() > 1) {
644 TemplateParams->pop_back();
645 } else {
646 TemplateParams = 0;
647 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
648 = ParsedTemplateInfo::NonTemplate;
649 }
650 } else if (TemplateInfo.Kind
651 == ParsedTemplateInfo::ExplicitInstantiation) {
652 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000653 TemplateParams = 0;
654 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
655 = ParsedTemplateInfo::NonTemplate;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000656 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
657 = SourceLocation();
658 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
659 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000660 }
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000661
Douglas Gregor916462b2009-10-30 21:46:58 +0000662
663 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000664 } else if (Tok.is(tok::annot_template_id)) {
665 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
666 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000667
Douglas Gregorb67535d2009-03-31 00:43:58 +0000668 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000669 // The template-name in the simple-template-id refers to
670 // something other than a class template. Give an appropriate
671 // error message and skip to the ';'.
672 SourceRange Range(NameLoc);
673 if (SS.isNotEmpty())
674 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000675
Douglas Gregor7f741122009-02-25 19:37:18 +0000676 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
677 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Douglas Gregor7f741122009-02-25 19:37:18 +0000679 DS.SetTypeSpecError();
680 SkipUntil(tok::semi, false, true);
681 TemplateId->Destroy();
682 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000683 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000684 }
685
John McCall07e91c02009-08-06 02:15:43 +0000686 // There are four options here. If we have 'struct foo;', then this
687 // is either a forward declaration or a friend declaration, which
688 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor7f741122009-02-25 19:37:18 +0000689 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregor556877c2008-04-13 21:30:24 +0000690 // something like 'struct foo xyz', a reference.
John McCall9bb74a52009-07-31 02:45:11 +0000691 Action::TagUseKind TUK;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000692 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
693 if (DS.isFriendSpecified()) {
694 // C++ [class.friend]p2:
695 // A class shall not be defined in a friend declaration.
696 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
697 << SourceRange(DS.getFriendSpecLoc());
698
699 // Skip everything up to the semicolon, so that this looks like a proper
700 // friend class (or template thereof) declaration.
701 SkipUntil(tok::semi, true, true);
702 TUK = Action::TUK_Friend;
703 } else {
704 // Okay, this is a class definition.
705 TUK = Action::TUK_Definition;
706 }
707 } else if (Tok.is(tok::semi))
John McCall07e91c02009-08-06 02:15:43 +0000708 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000709 else
John McCall9bb74a52009-07-31 02:45:11 +0000710 TUK = Action::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000711
John McCall9bb74a52009-07-31 02:45:11 +0000712 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000713 // We have a declaration or reference to an anonymous class.
Chris Lattner6d29c102008-11-18 07:48:38 +0000714 Diag(StartLoc, diag::err_anon_type_definition)
715 << DeclSpec::getSpecifierName(TagType);
Douglas Gregor556877c2008-04-13 21:30:24 +0000716
Douglas Gregor556877c2008-04-13 21:30:24 +0000717 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000718
719 if (TemplateId)
720 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000721 return;
722 }
723
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000724 // Create the tag portion of the class or class template.
John McCall7f41d982009-09-11 04:59:25 +0000725 Action::DeclResult TagOrTempResult = true; // invalid
726 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000727
John McCall9bb74a52009-07-31 02:45:11 +0000728 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000729 // to turn that template-id into a type.
730
Douglas Gregord6ab8742009-05-28 23:31:59 +0000731 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000732 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000733 // Explicit specialization, class template partial specialization,
734 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000735 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000736 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000737 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000738 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000739 TUK == Action::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000740 // This is an explicit instantiation of a class template.
741 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000742 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000743 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000744 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000745 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000746 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000747 SS,
Mike Stump11289f42009-09-09 15:08:12 +0000748 TemplateTy::make(TemplateId->Template),
749 TemplateId->TemplateNameLoc,
750 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000751 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000752 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000753 AttrList);
Douglas Gregor2208a292009-09-26 20:57:03 +0000754 } else if (TUK == Action::TUK_Reference) {
John McCall7f41d982009-09-11 04:59:25 +0000755 TypeResult
John McCalld8fe9af2009-09-08 17:47:29 +0000756 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
757 TemplateId->TemplateNameLoc,
758 TemplateId->LAngleLoc,
759 TemplateArgsPtr,
John McCalld8fe9af2009-09-08 17:47:29 +0000760 TemplateId->RAngleLoc);
761
John McCall7f41d982009-09-11 04:59:25 +0000762 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
763 TagType, StartLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000764 } else {
765 // This is an explicit specialization or a class template
766 // partial specialization.
767 TemplateParameterLists FakedParamLists;
768
769 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
770 // This looks like an explicit instantiation, because we have
771 // something like
772 //
773 // template class Foo<X>
774 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000775 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000776 // meant to be an explicit specialization, but the user forgot
777 // the '<>' after 'template'.
John McCall9bb74a52009-07-31 02:45:11 +0000778 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000779
Mike Stump11289f42009-09-09 15:08:12 +0000780 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000781 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000782 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000783 diag::err_explicit_instantiation_with_definition)
784 << SourceRange(TemplateInfo.TemplateLoc)
785 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
786
787 // Create a fake template parameter list that contains only
788 // "template<>", so that we treat this construct as a class
789 // template specialization.
790 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000791 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000792 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000793 LAngleLoc,
794 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000795 LAngleLoc));
796 TemplateParams = &FakedParamLists;
797 }
798
799 // Build the class template specialization.
800 TagOrTempResult
John McCall9bb74a52009-07-31 02:45:11 +0000801 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000802 StartLoc, SS,
Mike Stump11289f42009-09-09 15:08:12 +0000803 TemplateTy::make(TemplateId->Template),
804 TemplateId->TemplateNameLoc,
805 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000806 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000807 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000808 AttrList,
Mike Stump11289f42009-09-09 15:08:12 +0000809 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000810 TemplateParams? &(*TemplateParams)[0] : 0,
811 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000812 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000813 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000814 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000815 TUK == Action::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000816 // Explicit instantiation of a member of a class template
817 // specialization, e.g.,
818 //
819 // template struct Outer<int>::Inner;
820 //
821 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000822 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000823 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000824 TemplateInfo.TemplateLoc,
825 TagType, StartLoc, SS, Name,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000826 NameLoc, AttrList);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000827 } else {
828 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000829 TUK == Action::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000830 // FIXME: Diagnose this particular error.
831 }
832
John McCall7f41d982009-09-11 04:59:25 +0000833 bool IsDependent = false;
834
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000835 // Declaration or definition of a class type
Mike Stump11289f42009-09-09 15:08:12 +0000836 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000837 Name, NameLoc, AttrList, AS,
Mike Stump11289f42009-09-09 15:08:12 +0000838 Action::MultiTemplateParamsArg(Actions,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000839 TemplateParams? &(*TemplateParams)[0] : 0,
840 TemplateParams? TemplateParams->size() : 0),
John McCall7f41d982009-09-11 04:59:25 +0000841 Owned, IsDependent);
842
843 // If ActOnTag said the type was dependent, try again with the
844 // less common call.
845 if (IsDependent)
846 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
847 SS, Name, StartLoc, NameLoc);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000848 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000849
850 // Parse the optional base clause (C++ only).
Chris Lattnera3778332009-02-16 22:07:16 +0000851 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc08f4892009-03-25 00:13:59 +0000852 ParseBaseClause(TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000853
854 // If there is a body, parse it and inform the actions module.
855 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000856 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +0000857 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000858 else
Douglas Gregorc08f4892009-03-25 00:13:59 +0000859 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall9bb74a52009-07-31 02:45:11 +0000860 else if (TUK == Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000861 // FIXME: Complain that we have a base-specifier list but no
862 // definition.
Chris Lattner6d29c102008-11-18 07:48:38 +0000863 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregor556877c2008-04-13 21:30:24 +0000864 }
865
John McCall7f41d982009-09-11 04:59:25 +0000866 void *Result;
867 if (!TypeResult.isInvalid()) {
868 TagType = DeclSpec::TST_typename;
869 Result = TypeResult.get();
870 Owned = false;
871 } else if (!TagOrTempResult.isInvalid()) {
872 Result = TagOrTempResult.get().getAs<void>();
873 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000874 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +0000875 return;
876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
John McCall49bfce42009-08-03 20:12:06 +0000878 const char *PrevSpec = 0;
879 unsigned DiagID;
John McCall7f41d982009-09-11 04:59:25 +0000880
John McCall49bfce42009-08-03 20:12:06 +0000881 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
John McCall7f41d982009-09-11 04:59:25 +0000882 Result, Owned))
John McCall49bfce42009-08-03 20:12:06 +0000883 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregor556877c2008-04-13 21:30:24 +0000884}
885
Mike Stump11289f42009-09-09 15:08:12 +0000886/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +0000887///
888/// base-clause : [C++ class.derived]
889/// ':' base-specifier-list
890/// base-specifier-list:
891/// base-specifier '...'[opt]
892/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner83f095c2009-03-28 19:18:32 +0000893void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000894 assert(Tok.is(tok::colon) && "Not a base clause");
895 ConsumeToken();
896
Douglas Gregor29a92472008-10-22 17:49:05 +0000897 // Build up an array of parsed base specifiers.
898 llvm::SmallVector<BaseTy *, 8> BaseInfo;
899
Douglas Gregor556877c2008-04-13 21:30:24 +0000900 while (true) {
901 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +0000902 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +0000903 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000904 // Skip the rest of this base specifier, up until the comma or
905 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +0000906 SkipUntil(tok::comma, tok::l_brace, true, true);
907 } else {
908 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +0000909 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000910 }
911
912 // If the next token is a comma, consume it and keep reading
913 // base-specifiers.
914 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Douglas Gregor556877c2008-04-13 21:30:24 +0000916 // Consume the comma.
917 ConsumeToken();
918 }
Douglas Gregor29a92472008-10-22 17:49:05 +0000919
920 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +0000921 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +0000922}
923
924/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
925/// one entry in the base class list of a class specifier, for example:
926/// class foo : public bar, virtual private baz {
927/// 'public bar' and 'virtual private baz' are each base-specifiers.
928///
929/// base-specifier: [C++ class.derived]
930/// ::[opt] nested-name-specifier[opt] class-name
931/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
932/// class-name
933/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
934/// class-name
Chris Lattner83f095c2009-03-28 19:18:32 +0000935Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000936 bool IsVirtual = false;
937 SourceLocation StartLoc = Tok.getLocation();
938
939 // Parse the 'virtual' keyword.
940 if (Tok.is(tok::kw_virtual)) {
941 ConsumeToken();
942 IsVirtual = true;
943 }
944
945 // Parse an (optional) access specifier.
946 AccessSpecifier Access = getAccessSpecifierIfPresent();
947 if (Access)
948 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000949
Douglas Gregor556877c2008-04-13 21:30:24 +0000950 // Parse the 'virtual' keyword (again!), in case it came after the
951 // access specifier.
952 if (Tok.is(tok::kw_virtual)) {
953 SourceLocation VirtualLoc = ConsumeToken();
954 if (IsVirtual) {
955 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +0000956 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000957 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregor556877c2008-04-13 21:30:24 +0000958 }
959
960 IsVirtual = true;
961 }
962
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000963 // Parse optional '::' and optional nested-name-specifier.
964 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000965 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregor556877c2008-04-13 21:30:24 +0000966
Douglas Gregor556877c2008-04-13 21:30:24 +0000967 // The location of the base class itself.
968 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +0000969
970 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000971 SourceLocation EndLocation;
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000972 TypeResult BaseType = ParseClassName(EndLocation, &SS);
973 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +0000974 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000975
976 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000977 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +0000978
Douglas Gregor556877c2008-04-13 21:30:24 +0000979 // Notify semantic analysis that we have parsed a complete
980 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +0000981 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000982 BaseType.get(), BaseLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +0000983}
984
985/// getAccessSpecifierIfPresent - Determine whether the next token is
986/// a C++ access-specifier.
987///
988/// access-specifier: [C++ class.derived]
989/// 'private'
990/// 'protected'
991/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +0000992AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +0000993 switch (Tok.getKind()) {
994 default: return AS_none;
995 case tok::kw_private: return AS_private;
996 case tok::kw_protected: return AS_protected;
997 case tok::kw_public: return AS_public;
998 }
999}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001000
Eli Friedman3af2a772009-07-22 21:45:50 +00001001void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1002 DeclPtrTy ThisDecl) {
1003 // We just declared a member function. If this member function
1004 // has any default arguments, we'll need to parse them later.
1005 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001006 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedman3af2a772009-07-22 21:45:50 +00001007 = DeclaratorInfo.getTypeObject(0).Fun;
1008 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1009 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1010 if (!LateMethod) {
1011 // Push this method onto the stack of late-parsed method
1012 // declarations.
1013 getCurrentClass().MethodDecls.push_back(
1014 LateParsedMethodDeclaration(ThisDecl));
1015 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregorc45a40a2009-08-22 00:34:47 +00001016 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001017
1018 // Add all of the parameters prior to this one (they don't
1019 // have default arguments).
1020 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1021 for (unsigned I = 0; I < ParamIdx; ++I)
1022 LateMethod->DefaultArgs.push_back(
1023 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1024 }
1025
1026 // Add this parameter to the list of parameters (it or may
1027 // not have a default argument).
1028 LateMethod->DefaultArgs.push_back(
1029 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1030 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1031 }
1032 }
1033}
1034
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001035/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1036///
1037/// member-declaration:
1038/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1039/// function-definition ';'[opt]
1040/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1041/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001042/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001043/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001044/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001045///
1046/// member-declarator-list:
1047/// member-declarator
1048/// member-declarator-list ',' member-declarator
1049///
1050/// member-declarator:
1051/// declarator pure-specifier[opt]
1052/// declarator constant-initializer[opt]
1053/// identifier[opt] ':' constant-expression
1054///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001055/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001056/// '= 0'
1057///
1058/// constant-initializer:
1059/// '=' constant-expression
1060///
Douglas Gregor3447e762009-08-20 22:52:58 +00001061void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1062 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001063 // static_assert-declaration
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001064 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001065 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001066 SourceLocation DeclEnd;
1067 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001068 return;
1069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001071 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001072 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001073 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001074 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001075 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001076 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001077 return;
1078 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001079
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001080 // Handle: member-declaration ::= '__extension__' member-declaration
1081 if (Tok.is(tok::kw___extension__)) {
1082 // __extension__ silences extension warnings in the subexpression.
1083 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1084 ConsumeToken();
Douglas Gregor3447e762009-08-20 22:52:58 +00001085 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001086 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001087
Alexis Hunt96d5c762009-11-21 08:43:09 +00001088 CXX0XAttributeList AttrList;
1089 // Optional C++0x attribute-specifier
1090 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1091 AttrList = ParseCXX0XAttributes();
1092 }
1093
Douglas Gregorfec52632009-06-20 00:51:54 +00001094 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001095 // FIXME: Check for template aliases
Alexis Hunt96d5c762009-11-21 08:43:09 +00001096
1097 if (AttrList.HasAttr)
1098 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1099 << AttrList.Range;
Mike Stump11289f42009-09-09 15:08:12 +00001100
Douglas Gregorfec52632009-06-20 00:51:54 +00001101 // Eat 'using'.
1102 SourceLocation UsingLoc = ConsumeToken();
1103
1104 if (Tok.is(tok::kw_namespace)) {
1105 Diag(UsingLoc, diag::err_using_namespace_in_class);
1106 SkipUntil(tok::semi, true, true);
1107 }
1108 else {
1109 SourceLocation DeclEnd;
1110 // Otherwise, it must be using-declaration.
Anders Carlsson7b194b72009-08-29 19:54:19 +00001111 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001112 }
1113 return;
1114 }
1115
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001116 SourceLocation DSStart = Tok.getLocation();
1117 // decl-specifier-seq:
1118 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001119 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001120 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor3447e762009-08-20 22:52:58 +00001121 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001122
John McCall11083da2009-09-16 22:47:08 +00001123 Action::MultiTemplateParamsArg TemplateParams(Actions,
1124 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1125 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1126
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001127 if (Tok.is(tok::semi)) {
1128 ConsumeToken();
Douglas Gregor3dad8422009-09-26 06:47:28 +00001129 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall07e91c02009-08-06 02:15:43 +00001130 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001131 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001132
John McCall28a6aea2009-11-04 02:18:39 +00001133 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001134
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001135 if (Tok.isNot(tok::colon)) {
1136 // Parse the first declarator.
1137 ParseDeclarator(DeclaratorInfo);
1138 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001139 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001140 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001141 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001142 if (Tok.is(tok::semi))
1143 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001144 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001145 }
1146
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001147 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001148 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001149 || (DeclaratorInfo.isFunctionDeclarator() &&
1150 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001151 if (!DeclaratorInfo.isFunctionDeclarator()) {
1152 Diag(Tok, diag::err_func_def_no_params);
1153 ConsumeBrace();
1154 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001155 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001156 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001157
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001158 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1159 Diag(Tok, diag::err_function_declared_typedef);
1160 // This recovery skips the entire function body. It would be nice
1161 // to simply call ParseCXXInlineMethodDef() below, however Sema
1162 // assumes the declarator represents a function, not a typedef.
1163 ConsumeBrace();
1164 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001165 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001166 }
1167
Douglas Gregor3447e762009-08-20 22:52:58 +00001168 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001169 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001170 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001171 }
1172
1173 // member-declarator-list:
1174 // member-declarator
1175 // member-declarator-list ',' member-declarator
1176
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001177 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001178 OwningExprResult BitfieldSize(Actions);
1179 OwningExprResult Init(Actions);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001180 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001181
1182 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001183 // member-declarator:
1184 // declarator pure-specifier[opt]
1185 // declarator constant-initializer[opt]
1186 // identifier[opt] ':' constant-expression
1187
1188 if (Tok.is(tok::colon)) {
1189 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001190 BitfieldSize = ParseConstantExpression();
1191 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001192 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001193 }
Mike Stump11289f42009-09-09 15:08:12 +00001194
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001195 // pure-specifier:
1196 // '= 0'
1197 //
1198 // constant-initializer:
1199 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001200 //
1201 // defaulted/deleted function-definition:
1202 // '=' 'default' [TODO]
1203 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001204
1205 if (Tok.is(tok::equal)) {
1206 ConsumeToken();
Sebastian Redl42e92c42009-04-12 17:16:29 +00001207 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1208 ConsumeToken();
1209 Deleted = true;
1210 } else {
1211 Init = ParseInitializer();
1212 if (Init.isInvalid())
1213 SkipUntil(tok::comma, true, true);
1214 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001215 }
1216
1217 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001218 if (Tok.is(tok::kw___attribute)) {
1219 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001220 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001221 DeclaratorInfo.AddAttributes(AttrList, Loc);
1222 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001223
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001224 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001225 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001226 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001227
1228 DeclPtrTy ThisDecl;
1229 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001230 // TODO: handle initializers, bitfields, 'delete'
1231 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1232 /*IsDefinition*/ false,
1233 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001234 } else {
John McCall07e91c02009-08-06 02:15:43 +00001235 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1236 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001237 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001238 BitfieldSize.release(),
1239 Init.release(),
Sebastian Redld6f78502009-11-24 23:38:44 +00001240 /*IsDefinition*/Deleted,
John McCall07e91c02009-08-06 02:15:43 +00001241 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001242 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001243 if (ThisDecl)
1244 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001245
Douglas Gregor4d87df52008-12-16 21:30:33 +00001246 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001247 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001248 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001249 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001250 }
1251
John McCall28a6aea2009-11-04 02:18:39 +00001252 DeclaratorInfo.complete(ThisDecl);
1253
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001254 // If we don't have a comma, it is either the end of the list (a ';')
1255 // or an error, bail out.
1256 if (Tok.isNot(tok::comma))
1257 break;
Mike Stump11289f42009-09-09 15:08:12 +00001258
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001259 // Consume the comma.
1260 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001261
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001262 // Parse the next declarator.
1263 DeclaratorInfo.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001264 BitfieldSize = 0;
1265 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001266 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001267
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001268 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001269 if (Tok.is(tok::kw___attribute)) {
1270 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001271 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001272 DeclaratorInfo.AddAttributes(AttrList, Loc);
1273 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001274
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001275 if (Tok.isNot(tok::colon))
1276 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001277 }
1278
1279 if (Tok.is(tok::semi)) {
1280 ConsumeToken();
Eli Friedman55b9ecb2009-05-29 01:49:24 +00001281 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001282 DeclsInGroup.size());
1283 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001284 }
1285
1286 Diag(Tok, diag::err_expected_semi_decl_list);
1287 // Skip to end of block or statement
1288 SkipUntil(tok::r_brace, true, true);
1289 if (Tok.is(tok::semi))
1290 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001291 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001292}
1293
1294/// ParseCXXMemberSpecification - Parse the class definition.
1295///
1296/// member-specification:
1297/// member-declaration member-specification[opt]
1298/// access-specifier ':' member-specification[opt]
1299///
1300void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001301 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001302 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001303 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001304 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001305
Chris Lattnereae6cb62009-03-05 08:00:35 +00001306 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1307 PP.getSourceManager(),
1308 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001309
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001310 SourceLocation LBraceLoc = ConsumeBrace();
1311
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001312 // Determine whether this is a top-level (non-nested) class.
Mike Stump11289f42009-09-09 15:08:12 +00001313 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001314 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001315
1316 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001317 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001318
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001319 // Note that we are parsing a new (potentially-nested) class definition.
1320 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1321
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001322 if (TagDecl)
1323 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1324 else {
1325 SkipUntil(tok::r_brace, false, false);
1326 return;
1327 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001328
1329 // C++ 11p3: Members of a class defined with the keyword class are private
1330 // by default. Members of a class defined with the keywords struct or union
1331 // are public by default.
1332 AccessSpecifier CurAS;
1333 if (TagType == DeclSpec::TST_class)
1334 CurAS = AS_private;
1335 else
1336 CurAS = AS_public;
1337
1338 // While we still have something to read, read the member-declarations.
1339 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1340 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001341
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001342 // Check for extraneous top-level semicolon.
1343 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +00001344 Diag(Tok, diag::ext_extra_struct_semi)
1345 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001346 ConsumeToken();
1347 continue;
1348 }
1349
1350 AccessSpecifier AS = getAccessSpecifierIfPresent();
1351 if (AS != AS_none) {
1352 // Current token is a C++ access specifier.
1353 CurAS = AS;
1354 ConsumeToken();
1355 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1356 continue;
1357 }
1358
Douglas Gregor3447e762009-08-20 22:52:58 +00001359 // FIXME: Make sure we don't have a template here.
Mike Stump11289f42009-09-09 15:08:12 +00001360
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001361 // Parse all the comma separated declarators.
1362 ParseCXXClassMemberDeclaration(CurAS);
1363 }
Mike Stump11289f42009-09-09 15:08:12 +00001364
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001365 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001366
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001367 AttributeList *AttrList = 0;
1368 // If attributes exist after class contents, parse them.
1369 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001370 AttrList = ParseGNUAttributes(); // FIXME: where should I put them?
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001371
1372 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1373 LBraceLoc, RBraceLoc);
1374
1375 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1376 // complete within function bodies, default arguments,
1377 // exception-specifications, and constructor ctor-initializers (including
1378 // such things in nested classes).
1379 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001380 // FIXME: Only function bodies and constructor ctor-initializers are
1381 // parsed correctly, fix the rest.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001382 if (TopLevelClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001383 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001384 // are complete and we can parse the delayed portions of method
1385 // declarations and the lexed inline method definitions.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001386 ParseLexedMethodDeclarations(getCurrentClass());
1387 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001388 }
1389
1390 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001391 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001392 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001393
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001394 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001395}
Douglas Gregore8381c02008-11-05 04:29:56 +00001396
1397/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1398/// which explicitly initializes the members or base classes of a
1399/// class (C++ [class.base.init]). For example, the three initializers
1400/// after the ':' in the Derived constructor below:
1401///
1402/// @code
1403/// class Base { };
1404/// class Derived : Base {
1405/// int x;
1406/// float f;
1407/// public:
1408/// Derived(float f) : Base(), x(17), f(f) { }
1409/// };
1410/// @endcode
1411///
Mike Stump11289f42009-09-09 15:08:12 +00001412/// [C++] ctor-initializer:
1413/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001414///
Mike Stump11289f42009-09-09 15:08:12 +00001415/// [C++] mem-initializer-list:
1416/// mem-initializer
1417/// mem-initializer , mem-initializer-list
Chris Lattner83f095c2009-03-28 19:18:32 +00001418void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001419 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1420
1421 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001422
Douglas Gregore8381c02008-11-05 04:29:56 +00001423 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump11289f42009-09-09 15:08:12 +00001424
Douglas Gregore8381c02008-11-05 04:29:56 +00001425 do {
1426 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001427 if (!MemInit.isInvalid())
1428 MemInitializers.push_back(MemInit.get());
Douglas Gregore8381c02008-11-05 04:29:56 +00001429
1430 if (Tok.is(tok::comma))
1431 ConsumeToken();
1432 else if (Tok.is(tok::l_brace))
1433 break;
1434 else {
1435 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001436 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001437 SkipUntil(tok::l_brace, true, true);
1438 break;
1439 }
1440 } while (true);
1441
Mike Stump11289f42009-09-09 15:08:12 +00001442 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001443 MemInitializers.data(), MemInitializers.size());
Douglas Gregore8381c02008-11-05 04:29:56 +00001444}
1445
1446/// ParseMemInitializer - Parse a C++ member initializer, which is
1447/// part of a constructor initializer that explicitly initializes one
1448/// member or base class (C++ [class.base.init]). See
1449/// ParseConstructorInitializer for an example.
1450///
1451/// [C++] mem-initializer:
1452/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001453///
Douglas Gregore8381c02008-11-05 04:29:56 +00001454/// [C++] mem-initializer-id:
1455/// '::'[opt] nested-name-specifier[opt] class-name
1456/// identifier
Chris Lattner83f095c2009-03-28 19:18:32 +00001457Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001458 // parse '::'[opt] nested-name-specifier[opt]
1459 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001460 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001461 TypeTy *TemplateTypeTy = 0;
1462 if (Tok.is(tok::annot_template_id)) {
1463 TemplateIdAnnotation *TemplateId
1464 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1465 if (TemplateId->Kind == TNK_Type_template) {
1466 AnnotateTemplateIdTokenAsType(&SS);
1467 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1468 TemplateTypeTy = Tok.getAnnotationValue();
1469 }
1470 // FIXME. May need to check for TNK_Dependent_template as well.
1471 }
1472 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001473 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001474 return true;
1475 }
Mike Stump11289f42009-09-09 15:08:12 +00001476
Douglas Gregore8381c02008-11-05 04:29:56 +00001477 // Get the identifier. This may be a member name or a class name,
1478 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001479 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001480 SourceLocation IdLoc = ConsumeToken();
1481
1482 // Parse the '('.
1483 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001484 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001485 return true;
1486 }
1487 SourceLocation LParenLoc = ConsumeParen();
1488
1489 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001490 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001491 CommaLocsTy CommaLocs;
1492 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1493 SkipUntil(tok::r_paren);
1494 return true;
1495 }
1496
1497 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1498
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001499 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1500 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001501 LParenLoc, ArgExprs.take(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001502 ArgExprs.size(), CommaLocs.data(),
1503 RParenLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00001504}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001505
1506/// ParseExceptionSpecification - Parse a C++ exception-specification
1507/// (C++ [except.spec]).
1508///
Douglas Gregor356513d2008-12-01 18:00:20 +00001509/// exception-specification:
1510/// 'throw' '(' type-id-list [opt] ')'
1511/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00001512///
Douglas Gregor356513d2008-12-01 18:00:20 +00001513/// type-id-list:
1514/// type-id
1515/// type-id-list ',' type-id
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001516///
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001517bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redld6434562009-05-29 18:02:33 +00001518 llvm::SmallVector<TypeTy*, 2>
1519 &Exceptions,
1520 llvm::SmallVector<SourceRange, 2>
1521 &Ranges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001522 bool &hasAnyExceptionSpec) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001523 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00001524
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001525 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001526
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001527 if (!Tok.is(tok::l_paren)) {
1528 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1529 }
1530 SourceLocation LParenLoc = ConsumeParen();
1531
Douglas Gregor356513d2008-12-01 18:00:20 +00001532 // Parse throw(...), a Microsoft extension that means "this function
1533 // can throw anything".
1534 if (Tok.is(tok::ellipsis)) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001535 hasAnyExceptionSpec = true;
Douglas Gregor356513d2008-12-01 18:00:20 +00001536 SourceLocation EllipsisLoc = ConsumeToken();
1537 if (!getLang().Microsoft)
1538 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001539 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor356513d2008-12-01 18:00:20 +00001540 return false;
1541 }
1542
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001543 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00001544 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001545 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00001546 TypeResult Res(ParseTypeName(&Range));
1547 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001548 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00001549 Ranges.push_back(Range);
1550 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001551 if (Tok.is(tok::comma))
1552 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001553 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001554 break;
1555 }
1556
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001557 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001558 return false;
1559}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001560
1561/// \brief We have just started parsing the definition of a new class,
1562/// so push that class onto our stack of classes that is currently
1563/// being parsed.
1564void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001565 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001566 "Nested class without outer class");
1567 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1568}
1569
1570/// \brief Deallocate the given parsed class and all of its nested
1571/// classes.
1572void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1573 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1574 DeallocateParsedClasses(Class->NestedClasses[I]);
1575 delete Class;
1576}
1577
1578/// \brief Pop the top class of the stack of classes that are
1579/// currently being parsed.
1580///
1581/// This routine should be called when we have finished parsing the
1582/// definition of a class, but have not yet popped the Scope
1583/// associated with the class's definition.
1584///
1585/// \returns true if the class we've popped is a top-level class,
1586/// false otherwise.
1587void Parser::PopParsingClass() {
1588 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00001589
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001590 ParsingClass *Victim = ClassStack.top();
1591 ClassStack.pop();
1592 if (Victim->TopLevelClass) {
1593 // Deallocate all of the nested classes of this class,
1594 // recursively: we don't need to keep any of this information.
1595 DeallocateParsedClasses(Victim);
1596 return;
Mike Stump11289f42009-09-09 15:08:12 +00001597 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001598 assert(!ClassStack.empty() && "Missing top-level class?");
1599
1600 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1601 Victim->NestedClasses.empty()) {
1602 // The victim is a nested class, but we will not need to perform
1603 // any processing after the definition of this class since it has
1604 // no members whose handling was delayed. Therefore, we can just
1605 // remove this nested class.
1606 delete Victim;
1607 return;
1608 }
1609
1610 // This nested class has some members that will need to be processed
1611 // after the top-level class is completely defined. Therefore, add
1612 // it to the list of nested classes within its parent.
1613 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1614 ClassStack.top()->NestedClasses.push_back(Victim);
1615 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1616}
Alexis Hunt96d5c762009-11-21 08:43:09 +00001617
1618/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1619/// parses standard attributes.
1620///
1621/// [C++0x] attribute-specifier:
1622/// '[' '[' attribute-list ']' ']'
1623///
1624/// [C++0x] attribute-list:
1625/// attribute[opt]
1626/// attribute-list ',' attribute[opt]
1627///
1628/// [C++0x] attribute:
1629/// attribute-token attribute-argument-clause[opt]
1630///
1631/// [C++0x] attribute-token:
1632/// identifier
1633/// attribute-scoped-token
1634///
1635/// [C++0x] attribute-scoped-token:
1636/// attribute-namespace '::' identifier
1637///
1638/// [C++0x] attribute-namespace:
1639/// identifier
1640///
1641/// [C++0x] attribute-argument-clause:
1642/// '(' balanced-token-seq ')'
1643///
1644/// [C++0x] balanced-token-seq:
1645/// balanced-token
1646/// balanced-token-seq balanced-token
1647///
1648/// [C++0x] balanced-token:
1649/// '(' balanced-token-seq ')'
1650/// '[' balanced-token-seq ']'
1651/// '{' balanced-token-seq '}'
1652/// any token but '(', ')', '[', ']', '{', or '}'
1653CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1654 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1655 && "Not a C++0x attribute list");
1656
1657 SourceLocation StartLoc = Tok.getLocation(), Loc;
1658 AttributeList *CurrAttr = 0;
1659
1660 ConsumeBracket();
1661 ConsumeBracket();
1662
1663 if (Tok.is(tok::comma)) {
1664 Diag(Tok.getLocation(), diag::err_expected_ident);
1665 ConsumeToken();
1666 }
1667
1668 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1669 // attribute not present
1670 if (Tok.is(tok::comma)) {
1671 ConsumeToken();
1672 continue;
1673 }
1674
1675 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1676 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1677
1678 // scoped attribute
1679 if (Tok.is(tok::coloncolon)) {
1680 ConsumeToken();
1681
1682 if (!Tok.is(tok::identifier)) {
1683 Diag(Tok.getLocation(), diag::err_expected_ident);
1684 SkipUntil(tok::r_square, tok::comma, true, true);
1685 continue;
1686 }
1687
1688 ScopeName = AttrName;
1689 ScopeLoc = AttrLoc;
1690
1691 AttrName = Tok.getIdentifierInfo();
1692 AttrLoc = ConsumeToken();
1693 }
1694
1695 bool AttrParsed = false;
1696 // No scoped names are supported; ideally we could put all non-standard
1697 // attributes into namespaces.
1698 if (!ScopeName) {
1699 switch(AttributeList::getKind(AttrName))
1700 {
1701 // No arguments
1702 case AttributeList::AT_noreturn:
1703 case AttributeList::AT_final:
1704 case AttributeList::AT_carries_dependency: {
1705 if (Tok.is(tok::l_paren)) {
1706 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1707 << AttrName->getName();
1708 break;
1709 }
1710
1711 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1712 SourceLocation(), 0, 0, CurrAttr, false,
1713 true);
1714 AttrParsed = true;
1715 break;
1716 }
1717
1718 // One argument; must be a type-id or assignment-expression
1719 case AttributeList::AT_aligned: {
1720 if (Tok.isNot(tok::l_paren)) {
1721 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1722 << AttrName->getName();
1723 break;
1724 }
1725 SourceLocation ParamLoc = ConsumeParen();
1726
1727 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1728
1729 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1730
1731 ExprVector ArgExprs(Actions);
1732 ArgExprs.push_back(ArgExpr.release());
1733 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1734 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1735 false, true);
1736
1737 AttrParsed = true;
1738 break;
1739 }
1740
1741 // Silence warnings
1742 default: break;
1743 }
1744 }
1745
1746 // Skip the entire parameter clause, if any
1747 if (!AttrParsed && Tok.is(tok::l_paren)) {
1748 ConsumeParen();
1749 // SkipUntil maintains the balancedness of tokens.
1750 SkipUntil(tok::r_paren, false);
1751 }
1752 }
1753
1754 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1755 SkipUntil(tok::r_square, false);
1756 Loc = Tok.getLocation();
1757 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1758 SkipUntil(tok::r_square, false);
1759
1760 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1761 return Attr;
1762}
1763
1764/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1765/// attribute.
1766///
1767/// FIXME: Simply returns an alignof() expression if the argument is a
1768/// type. Ideally, the type should be propagated directly into Sema.
1769///
1770/// [C++0x] 'align' '(' type-id ')'
1771/// [C++0x] 'align' '(' assignment-expression ')'
1772Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1773 if (isTypeIdInParens()) {
1774 EnterExpressionEvaluationContext Unevaluated(Actions,
1775 Action::Unevaluated);
1776 SourceLocation TypeLoc = Tok.getLocation();
1777 TypeTy *Ty = ParseTypeName().get();
1778 SourceRange TypeRange(Start, Tok.getLocation());
1779 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1780 TypeRange);
1781 } else
1782 return ParseConstantExpression();
1783}