blob: 61a494193c79fc2a264df45674c6d4ffd362e253 [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 Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.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.
Chris Lattnerd62268a2009-12-07 01:38:03 +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///
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000164Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
165 unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000166 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner38376f12008-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 Lattner38376f12008-01-12 07:05:38 +0000174
Douglas Gregor07665a62009-01-05 19:45:36 +0000175 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +0000176 DeclPtrTy LinkageSpec
177 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor07665a62009-01-05 19:45:36 +0000178 /*FIXME: */SourceLocation(),
179 Loc, LangBufPtr, StrSize,
Mike Stump11289f42009-09-09 15:08:12 +0000180 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000181 : SourceLocation());
182
Alexis Hunt96d5c762009-11-21 08:43:09 +0000183 CXX0XAttributeList Attr;
184 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
185 Attr = ParseCXX0XAttributes();
186 }
187
Douglas Gregor07665a62009-01-05 19:45:36 +0000188 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000189 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000190 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000191 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000192 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000193
Alexis Hunt96d5c762009-11-21 08:43:09 +0000194 if (Attr.HasAttr)
195 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
196 << Attr.Range;
197
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000198 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000199 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000200 CXX0XAttributeList Attr;
201 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
202 Attr = ParseCXX0XAttributes();
203 ParseExternalDeclaration(Attr);
Chris Lattner38376f12008-01-12 07:05:38 +0000204 }
205
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000206 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor07665a62009-01-05 19:45:36 +0000207 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000208}
Douglas Gregor556877c2008-04-13 21:30:24 +0000209
Douglas Gregord7c4d982008-12-30 03:27:21 +0000210/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
211/// using-directive. Assumes that current token is 'using'.
Chris Lattner49836b42009-04-02 04:16:50 +0000212Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000213 SourceLocation &DeclEnd,
214 CXX0XAttributeList Attr) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000220 if (Tok.is(tok::code_completion)) {
221 Actions.CodeCompleteUsing(CurScope);
222 ConsumeToken();
223 }
224
Chris Lattner9b01ca12009-01-06 06:55:51 +0000225 if (Tok.is(tok::kw_namespace))
Douglas Gregord7c4d982008-12-30 03:27:21 +0000226 // Next token after 'using' is 'namespace' so it must be using-directive
Alexis Hunt96d5c762009-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 Lattner9b01ca12009-01-06 06:55:51 +0000232
233 // Otherwise, it must be using-declaration.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000234 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner49836b42009-04-02 04:16:50 +0000235 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-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 Lattner83f095c2009-03-28 19:18:32 +0000248Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000249 SourceLocation UsingLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000250 SourceLocation &DeclEnd,
251 AttributeList *Attr) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000257 if (Tok.is(tok::code_completion)) {
258 Actions.CodeCompleteUsingDirective(CurScope);
259 ConsumeToken();
260 }
261
Douglas Gregord7c4d982008-12-30 03:27:21 +0000262 CXXScopeSpec SS;
263 // Parse (optional) nested-name-specifier.
Chris Lattnerd62268a2009-12-07 01:38:03 +0000264 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000265
Douglas Gregord7c4d982008-12-30 03:27:21 +0000266 IdentifierInfo *NamespcName = 0;
267 SourceLocation IdentLoc = SourceLocation();
268
269 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000270 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-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 Lattner83f095c2009-03-28 19:18:32 +0000275 return DeclPtrTy();
Douglas Gregord7c4d982008-12-30 03:27:21 +0000276 }
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000278 // Parse identifier.
279 NamespcName = Tok.getIdentifierInfo();
280 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000282 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-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 Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000289 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000290 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000291 ExpectAndConsume(tok::semi,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000292 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner34a95662009-06-14 00:07:48 +0000293 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000294
295 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000296 IdentLoc, NamespcName, Attr);
Douglas Gregord7c4d982008-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 Gregorfec52632009-06-20 00:51:54 +0000304/// unqualified-id
305/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000306///
Chris Lattner83f095c2009-03-28 19:18:32 +0000307Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000308 SourceLocation UsingLoc,
Anders Carlsson7b194b72009-08-29 19:54:19 +0000309 SourceLocation &DeclEnd,
310 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000311 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000312 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000313 bool IsTypeName;
314
315 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000316 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000317 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000318 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000319 ConsumeToken();
320 IsTypeName = true;
321 }
322 else
323 IsTypeName = false;
324
325 // Parse nested-name-specifier.
Chris Lattnerd62268a2009-12-07 01:38:03 +0000326 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000327
328 AttributeList *AttrList = 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000329
330 // Check nested-name specifier.
331 if (SS.isInvalid()) {
332 SkipUntil(tok::semi);
333 return DeclPtrTy();
334 }
Douglas Gregor220f4272009-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 Gregorfec52632009-06-20 00:51:54 +0000346 SkipUntil(tok::semi);
347 return DeclPtrTy();
348 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000349
Douglas Gregorfec52632009-06-20 00:51:54 +0000350 // Parse (optional) attributes (most likely GNU strong-using extension).
351 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000352 AttrList = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000353
Douglas Gregorfec52632009-06-20 00:51:54 +0000354 // Eat ';'.
355 DeclEnd = Tok.getLocation();
356 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor220f4272009-11-04 16:30:06 +0000357 AttrList ? "attributes list" : "using declaration",
358 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000359
John McCalla0097262009-12-11 02:10:03 +0000360 return Actions.ActOnUsingDeclaration(CurScope, AS, true, UsingLoc, SS, Name,
John McCalle61f2ba2009-11-18 02:36:19 +0000361 AttrList, IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000362}
363
Anders Carlssonf24fcff62009-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 Lattner49836b42009-04-02 04:16:50 +0000369Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000370 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
371 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000373 if (Tok.isNot(tok::l_paren)) {
374 Diag(Tok, diag::err_expected_lparen);
Chris Lattner83f095c2009-03-28 19:18:32 +0000375 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000378 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000379
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000380 OwningExprResult AssertExpr(ParseConstantExpression());
381 if (AssertExpr.isInvalid()) {
382 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000383 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000384 }
Mike Stump11289f42009-09-09 15:08:12 +0000385
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000386 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner83f095c2009-03-28 19:18:32 +0000387 return DeclPtrTy();
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000388
Anders Carlssonf24fcff62009-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 Lattner83f095c2009-03-28 19:18:32 +0000392 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000395 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000396 if (AssertMessage.isInvalid())
Chris Lattner83f095c2009-03-28 19:18:32 +0000397 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000398
Anders Carlsson27de6a52009-03-15 18:44:04 +0000399 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattner49836b42009-04-02 04:16:50 +0000401 DeclEnd = Tok.getLocation();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000402 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
403
Mike Stump11289f42009-09-09 15:08:12 +0000404 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson27de6a52009-03-15 18:44:04 +0000405 move(AssertMessage));
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000406}
407
Anders Carlsson74948d02009-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 Stump11289f42009-09-09 15:08:12 +0000417
418 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000419 "decltype")) {
420 SkipUntil(tok::r_paren);
421 return;
422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Anders Carlsson74948d02009-06-24 17:47:40 +0000424 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000425
Anders Carlsson74948d02009-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 Stump11289f42009-09-09 15:08:12 +0000435
Anders Carlsson74948d02009-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 Stump11289f42009-09-09 15:08:12 +0000442
Anders Carlsson74948d02009-06-24 17:47:40 +0000443 if (RParenLoc.isInvalid())
444 return;
445
446 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000447 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000448 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000449 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000450 DiagID, Result.release()))
451 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000452}
453
Douglas Gregor831c93f2008-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 Gregord54dfb82009-02-25 23:52:28 +0000457/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000458/// found.
459///
460/// class-name: [C++ 9.1]
461/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000462/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000463///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000464Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000465 const CXXScopeSpec *SS,
466 bool DestrExpected) {
Douglas Gregord54dfb82009-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 Stump11289f42009-09-09 15:08:12 +0000469 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000470 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +0000471 if (TemplateId->Kind == TNK_Type_template ||
472 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000473 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregord54dfb82009-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 Gregorfe3d7d02009-04-01 21:51:26 +0000479
480 if (Type)
481 return Type;
482 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000483 }
484
485 // Fall through to produce an error below.
486 }
487
Douglas Gregor831c93f2008-11-05 20:51:48 +0000488 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000489 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000490 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000491 }
492
493 // We have an identifier; check whether it is actually a type.
Mike Stump11289f42009-09-09 15:08:12 +0000494 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor5e0962f2009-08-26 18:27:52 +0000495 Tok.getLocation(), CurScope, SS,
496 true);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000497 if (!Type) {
Mike Stump11289f42009-09-09 15:08:12 +0000498 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +0000499 : diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000500 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000501 }
502
503 // Consume the identifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +0000504 EndLocation = ConsumeToken();
Douglas Gregor831c93f2008-11-05 20:51:48 +0000505 return Type;
506}
507
Douglas Gregor556877c2008-04-13 21:30:24 +0000508/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
509/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
510/// until we reach the start of a definition or see a token that
511/// cannot start a definition.
512///
513/// class-specifier: [C++ class]
514/// class-head '{' member-specification[opt] '}'
515/// class-head '{' member-specification[opt] '}' attributes[opt]
516/// class-head:
517/// class-key identifier[opt] base-clause[opt]
518/// class-key nested-name-specifier identifier base-clause[opt]
519/// class-key nested-name-specifier[opt] simple-template-id
520/// base-clause[opt]
521/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000522/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000523/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000524/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000525/// simple-template-id base-clause[opt]
526/// class-key:
527/// 'class'
528/// 'struct'
529/// 'union'
530///
531/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000532/// class-key ::[opt] nested-name-specifier[opt] identifier
533/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
534/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000535///
536/// Note that the C++ class-specifier and elaborated-type-specifier,
537/// together, subsume the C99 struct-or-union-specifier:
538///
539/// struct-or-union-specifier: [C99 6.7.2.1]
540/// struct-or-union identifier[opt] '{' struct-contents '}'
541/// struct-or-union identifier
542/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
543/// '}' attributes[opt]
544/// [GNU] struct-or-union attributes[opt] identifier
545/// struct-or-union:
546/// 'struct'
547/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000548void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
549 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000550 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor6c2adff2009-03-25 22:00:53 +0000551 AccessSpecifier AS) {
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000552 DeclSpec::TST TagType;
553 if (TagTokKind == tok::kw_struct)
554 TagType = DeclSpec::TST_struct;
555 else if (TagTokKind == tok::kw_class)
556 TagType = DeclSpec::TST_class;
557 else {
558 assert(TagTokKind == tok::kw_union && "Not a class specifier");
559 TagType = DeclSpec::TST_union;
560 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000561
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000562 if (Tok.is(tok::code_completion)) {
563 // Code completion for a struct, class, or union name.
564 Actions.CodeCompleteTag(CurScope, TagType);
565 ConsumeToken();
566 }
567
Alexis Hunt96d5c762009-11-21 08:43:09 +0000568 AttributeList *AttrList = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000569 // If attributes exist after tag, parse them.
570 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000571 AttrList = ParseGNUAttributes();
Douglas Gregor556877c2008-04-13 21:30:24 +0000572
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000573 // If declspecs exist after tag, parse them.
Eli Friedman53339e02009-06-08 23:27:34 +0000574 if (Tok.is(tok::kw___declspec))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000575 AttrList = ParseMicrosoftDeclSpec(AttrList);
576
577 // If C++0x attributes exist here, parse them.
578 // FIXME: Are we consistent with the ordering of parsing of different
579 // styles of attributes?
580 if (isCXX0XAttributeSpecifier())
581 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000582
Douglas Gregor119b0c72009-09-04 05:53:02 +0000583 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
584 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
585 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000586 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000587 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
588 // properly.
589 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
590 Tok.setKind(tok::identifier);
591 }
592
593 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
594 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
595 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000596 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000597 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
598 // properly.
599 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
600 Tok.setKind(tok::identifier);
601 }
Mike Stump11289f42009-09-09 15:08:12 +0000602
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000603 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +0000604 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000605 if (getLang().CPlusPlus) {
606 // "FOO : BAR" is not a potential typo for "FOO::BAR".
607 ColonProtectionRAIIObject X(*this);
608
609 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
610 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
611 Diag(Tok, diag::err_expected_ident);
612 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000613
Douglas Gregor916462b2009-10-30 21:46:58 +0000614 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
615
Douglas Gregor67a65642009-02-17 23:15:12 +0000616 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000617 IdentifierInfo *Name = 0;
618 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000619 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000620 if (Tok.is(tok::identifier)) {
621 Name = Tok.getIdentifierInfo();
622 NameLoc = ConsumeToken();
Douglas Gregor916462b2009-10-30 21:46:58 +0000623
624 if (Tok.is(tok::less)) {
625 // The name was supposed to refer to a template, but didn't.
626 // Eat the template argument list and try to continue parsing this as
627 // a class (or template thereof).
628 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000629 SourceLocation LAngleLoc, RAngleLoc;
630 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
631 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000632 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000633 // We couldn't parse the template argument list at all, so don't
634 // try to give any location information for the list.
635 LAngleLoc = RAngleLoc = SourceLocation();
636 }
637
638 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000639 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000640 << (TagType == DeclSpec::TST_class? 0
641 : TagType == DeclSpec::TST_struct? 1
642 : 2)
643 << Name
644 << SourceRange(LAngleLoc, RAngleLoc);
645
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000646 // Strip off the last template parameter list if it was empty, since
647 // we've removed its template argument list.
648 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
649 if (TemplateParams && TemplateParams->size() > 1) {
650 TemplateParams->pop_back();
651 } else {
652 TemplateParams = 0;
653 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
654 = ParsedTemplateInfo::NonTemplate;
655 }
656 } else if (TemplateInfo.Kind
657 == ParsedTemplateInfo::ExplicitInstantiation) {
658 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000659 TemplateParams = 0;
660 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
661 = ParsedTemplateInfo::NonTemplate;
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000662 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
663 = SourceLocation();
664 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
665 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000666 }
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000667
Douglas Gregor916462b2009-10-30 21:46:58 +0000668
669 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000670 } else if (Tok.is(tok::annot_template_id)) {
671 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
672 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000673
Douglas Gregorb67535d2009-03-31 00:43:58 +0000674 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000675 // The template-name in the simple-template-id refers to
676 // something other than a class template. Give an appropriate
677 // error message and skip to the ';'.
678 SourceRange Range(NameLoc);
679 if (SS.isNotEmpty())
680 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000681
Douglas Gregor7f741122009-02-25 19:37:18 +0000682 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
683 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000684
Douglas Gregor7f741122009-02-25 19:37:18 +0000685 DS.SetTypeSpecError();
686 SkipUntil(tok::semi, false, true);
687 TemplateId->Destroy();
688 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000689 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000690 }
691
John McCall07e91c02009-08-06 02:15:43 +0000692 // There are four options here. If we have 'struct foo;', then this
693 // is either a forward declaration or a friend declaration, which
694 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor7f741122009-02-25 19:37:18 +0000695 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregor556877c2008-04-13 21:30:24 +0000696 // something like 'struct foo xyz', a reference.
John McCall9bb74a52009-07-31 02:45:11 +0000697 Action::TagUseKind TUK;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000698 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) {
699 if (DS.isFriendSpecified()) {
700 // C++ [class.friend]p2:
701 // A class shall not be defined in a friend declaration.
702 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
703 << SourceRange(DS.getFriendSpecLoc());
704
705 // Skip everything up to the semicolon, so that this looks like a proper
706 // friend class (or template thereof) declaration.
707 SkipUntil(tok::semi, true, true);
708 TUK = Action::TUK_Friend;
709 } else {
710 // Okay, this is a class definition.
711 TUK = Action::TUK_Definition;
712 }
713 } else if (Tok.is(tok::semi))
John McCall07e91c02009-08-06 02:15:43 +0000714 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000715 else
John McCall9bb74a52009-07-31 02:45:11 +0000716 TUK = Action::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000717
John McCall9bb74a52009-07-31 02:45:11 +0000718 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000719 // We have a declaration or reference to an anonymous class.
Chris Lattner6d29c102008-11-18 07:48:38 +0000720 Diag(StartLoc, diag::err_anon_type_definition)
721 << DeclSpec::getSpecifierName(TagType);
Douglas Gregor556877c2008-04-13 21:30:24 +0000722
Douglas Gregor556877c2008-04-13 21:30:24 +0000723 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000724
725 if (TemplateId)
726 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000727 return;
728 }
729
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000730 // Create the tag portion of the class or class template.
John McCall7f41d982009-09-11 04:59:25 +0000731 Action::DeclResult TagOrTempResult = true; // invalid
732 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000733
John McCall9bb74a52009-07-31 02:45:11 +0000734 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000735 // to turn that template-id into a type.
736
Douglas Gregord6ab8742009-05-28 23:31:59 +0000737 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000738 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000739 // Explicit specialization, class template partial specialization,
740 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000741 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000742 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000743 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000744 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000745 TUK == Action::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000746 // This is an explicit instantiation of a class template.
747 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000748 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000749 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000750 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000751 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000752 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000753 SS,
Mike Stump11289f42009-09-09 15:08:12 +0000754 TemplateTy::make(TemplateId->Template),
755 TemplateId->TemplateNameLoc,
756 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000757 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000758 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000759 AttrList);
Douglas Gregor2208a292009-09-26 20:57:03 +0000760 } else if (TUK == Action::TUK_Reference) {
John McCall7f41d982009-09-11 04:59:25 +0000761 TypeResult
John McCalld8fe9af2009-09-08 17:47:29 +0000762 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
763 TemplateId->TemplateNameLoc,
764 TemplateId->LAngleLoc,
765 TemplateArgsPtr,
John McCalld8fe9af2009-09-08 17:47:29 +0000766 TemplateId->RAngleLoc);
767
John McCall7f41d982009-09-11 04:59:25 +0000768 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
769 TagType, StartLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000770 } else {
771 // This is an explicit specialization or a class template
772 // partial specialization.
773 TemplateParameterLists FakedParamLists;
774
775 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
776 // This looks like an explicit instantiation, because we have
777 // something like
778 //
779 // template class Foo<X>
780 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000781 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000782 // meant to be an explicit specialization, but the user forgot
783 // the '<>' after 'template'.
John McCall9bb74a52009-07-31 02:45:11 +0000784 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000785
Mike Stump11289f42009-09-09 15:08:12 +0000786 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000787 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000788 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000789 diag::err_explicit_instantiation_with_definition)
790 << SourceRange(TemplateInfo.TemplateLoc)
791 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
792
793 // Create a fake template parameter list that contains only
794 // "template<>", so that we treat this construct as a class
795 // template specialization.
796 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000797 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000798 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000799 LAngleLoc,
800 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000801 LAngleLoc));
802 TemplateParams = &FakedParamLists;
803 }
804
805 // Build the class template specialization.
806 TagOrTempResult
John McCall9bb74a52009-07-31 02:45:11 +0000807 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000808 StartLoc, SS,
Mike Stump11289f42009-09-09 15:08:12 +0000809 TemplateTy::make(TemplateId->Template),
810 TemplateId->TemplateNameLoc,
811 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000812 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000813 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000814 AttrList,
Mike Stump11289f42009-09-09 15:08:12 +0000815 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000816 TemplateParams? &(*TemplateParams)[0] : 0,
817 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000818 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000819 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000820 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000821 TUK == Action::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000822 // Explicit instantiation of a member of a class template
823 // specialization, e.g.,
824 //
825 // template struct Outer<int>::Inner;
826 //
827 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000828 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000829 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000830 TemplateInfo.TemplateLoc,
831 TagType, StartLoc, SS, Name,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000832 NameLoc, AttrList);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000833 } else {
834 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000835 TUK == Action::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000836 // FIXME: Diagnose this particular error.
837 }
838
John McCall7f41d982009-09-11 04:59:25 +0000839 bool IsDependent = false;
840
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000841 // Declaration or definition of a class type
Mike Stump11289f42009-09-09 15:08:12 +0000842 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000843 Name, NameLoc, AttrList, AS,
Mike Stump11289f42009-09-09 15:08:12 +0000844 Action::MultiTemplateParamsArg(Actions,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000845 TemplateParams? &(*TemplateParams)[0] : 0,
846 TemplateParams? TemplateParams->size() : 0),
John McCall7f41d982009-09-11 04:59:25 +0000847 Owned, IsDependent);
848
849 // If ActOnTag said the type was dependent, try again with the
850 // less common call.
851 if (IsDependent)
852 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
853 SS, Name, StartLoc, NameLoc);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000854 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000855
Douglas Gregor556877c2008-04-13 21:30:24 +0000856 // If there is a body, parse it and inform the actions module.
John McCall2d814c32009-12-19 21:48:58 +0000857 if (TUK == Action::TUK_Definition) {
858 assert(Tok.is(tok::l_brace) ||
859 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000860 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +0000861 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000862 else
Douglas Gregorc08f4892009-03-25 00:13:59 +0000863 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
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)
Chris Lattner3c7b86f2009-12-06 17:36:05 +0000957 << CodeModificationHint::CreateRemoval(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) {
John McCalla0097262009-12-11 02:10:03 +00001063 // Access declarations.
1064 if (!TemplateInfo.Kind &&
1065 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
1066 TryAnnotateCXXScopeToken() &&
1067 Tok.is(tok::annot_cxxscope)) {
1068 bool isAccessDecl = false;
1069 if (NextToken().is(tok::identifier))
1070 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1071 else
1072 isAccessDecl = NextToken().is(tok::kw_operator);
1073
1074 if (isAccessDecl) {
1075 // Collect the scope specifier token we annotated earlier.
1076 CXXScopeSpec SS;
1077 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1078
1079 // Try to parse an unqualified-id.
1080 UnqualifiedId Name;
1081 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1082 SkipUntil(tok::semi);
1083 return;
1084 }
1085
1086 // TODO: recover from mistakenly-qualified operator declarations.
1087 if (ExpectAndConsume(tok::semi,
1088 diag::err_expected_semi_after,
1089 "access declaration",
1090 tok::semi))
1091 return;
1092
1093 Actions.ActOnUsingDeclaration(CurScope, AS,
1094 false, SourceLocation(),
1095 SS, Name,
1096 /* AttrList */ 0,
1097 /* IsTypeName */ false,
1098 SourceLocation());
1099 return;
1100 }
1101 }
1102
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001103 // static_assert-declaration
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001104 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001105 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001106 SourceLocation DeclEnd;
1107 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001108 return;
1109 }
Mike Stump11289f42009-09-09 15:08:12 +00001110
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001111 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001112 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001113 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001114 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001115 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001116 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001117 return;
1118 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001119
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001120 // Handle: member-declaration ::= '__extension__' member-declaration
1121 if (Tok.is(tok::kw___extension__)) {
1122 // __extension__ silences extension warnings in the subexpression.
1123 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1124 ConsumeToken();
Douglas Gregor3447e762009-08-20 22:52:58 +00001125 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001126 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001127
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001128 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1129 ColonProtectionRAIIObject X(*this);
1130
Alexis Hunt96d5c762009-11-21 08:43:09 +00001131 CXX0XAttributeList AttrList;
1132 // Optional C++0x attribute-specifier
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001133 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Alexis Hunt96d5c762009-11-21 08:43:09 +00001134 AttrList = ParseCXX0XAttributes();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001135
Douglas Gregorfec52632009-06-20 00:51:54 +00001136 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001137 // FIXME: Check for template aliases
Alexis Hunt96d5c762009-11-21 08:43:09 +00001138
1139 if (AttrList.HasAttr)
1140 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1141 << AttrList.Range;
Mike Stump11289f42009-09-09 15:08:12 +00001142
Douglas Gregorfec52632009-06-20 00:51:54 +00001143 // Eat 'using'.
1144 SourceLocation UsingLoc = ConsumeToken();
1145
1146 if (Tok.is(tok::kw_namespace)) {
1147 Diag(UsingLoc, diag::err_using_namespace_in_class);
1148 SkipUntil(tok::semi, true, true);
1149 }
1150 else {
1151 SourceLocation DeclEnd;
1152 // Otherwise, it must be using-declaration.
Anders Carlsson7b194b72009-08-29 19:54:19 +00001153 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001154 }
1155 return;
1156 }
1157
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001158 SourceLocation DSStart = Tok.getLocation();
1159 // decl-specifier-seq:
1160 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001161 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001162 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor3447e762009-08-20 22:52:58 +00001163 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001164
John McCall11083da2009-09-16 22:47:08 +00001165 Action::MultiTemplateParamsArg TemplateParams(Actions,
1166 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1167 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1168
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001169 if (Tok.is(tok::semi)) {
1170 ConsumeToken();
Douglas Gregor3dad8422009-09-26 06:47:28 +00001171 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall07e91c02009-08-06 02:15:43 +00001172 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001173 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001174
John McCall28a6aea2009-11-04 02:18:39 +00001175 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001176
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001177 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001178 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1179 ColonProtectionRAIIObject X(*this);
1180
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001181 // Parse the first declarator.
1182 ParseDeclarator(DeclaratorInfo);
1183 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001184 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001185 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001186 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001187 if (Tok.is(tok::semi))
1188 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001189 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001190 }
1191
John Thompson5bc5cbe2009-11-25 22:58:06 +00001192 // If attributes exist after the declarator, but before an '{', parse them.
1193 if (Tok.is(tok::kw___attribute)) {
1194 SourceLocation Loc;
1195 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1196 DeclaratorInfo.AddAttributes(AttrList, Loc);
1197 }
1198
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001199 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001200 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001201 || (DeclaratorInfo.isFunctionDeclarator() &&
1202 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001203 if (!DeclaratorInfo.isFunctionDeclarator()) {
1204 Diag(Tok, diag::err_func_def_no_params);
1205 ConsumeBrace();
1206 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001207 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001208 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001209
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001210 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1211 Diag(Tok, diag::err_function_declared_typedef);
1212 // This recovery skips the entire function body. It would be nice
1213 // to simply call ParseCXXInlineMethodDef() below, however Sema
1214 // assumes the declarator represents a function, not a typedef.
1215 ConsumeBrace();
1216 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001217 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001218 }
1219
Douglas Gregor3447e762009-08-20 22:52:58 +00001220 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001221 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001222 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001223 }
1224
1225 // member-declarator-list:
1226 // member-declarator
1227 // member-declarator-list ',' member-declarator
1228
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001229 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001230 OwningExprResult BitfieldSize(Actions);
1231 OwningExprResult Init(Actions);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001232 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001233
1234 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001235 // member-declarator:
1236 // declarator pure-specifier[opt]
1237 // declarator constant-initializer[opt]
1238 // identifier[opt] ':' constant-expression
1239
1240 if (Tok.is(tok::colon)) {
1241 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001242 BitfieldSize = ParseConstantExpression();
1243 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001244 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001247 // pure-specifier:
1248 // '= 0'
1249 //
1250 // constant-initializer:
1251 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001252 //
1253 // defaulted/deleted function-definition:
1254 // '=' 'default' [TODO]
1255 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001256
1257 if (Tok.is(tok::equal)) {
1258 ConsumeToken();
Sebastian Redl42e92c42009-04-12 17:16:29 +00001259 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1260 ConsumeToken();
1261 Deleted = true;
1262 } else {
1263 Init = ParseInitializer();
1264 if (Init.isInvalid())
1265 SkipUntil(tok::comma, true, true);
1266 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001267 }
1268
1269 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001270 if (Tok.is(tok::kw___attribute)) {
1271 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001272 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001273 DeclaratorInfo.AddAttributes(AttrList, Loc);
1274 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001275
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001276 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001277 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001278 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001279
1280 DeclPtrTy ThisDecl;
1281 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001282 // TODO: handle initializers, bitfields, 'delete'
1283 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1284 /*IsDefinition*/ false,
1285 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001286 } else {
John McCall07e91c02009-08-06 02:15:43 +00001287 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1288 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001289 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001290 BitfieldSize.release(),
1291 Init.release(),
Sebastian Redld6f78502009-11-24 23:38:44 +00001292 /*IsDefinition*/Deleted,
John McCall07e91c02009-08-06 02:15:43 +00001293 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001294 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001295 if (ThisDecl)
1296 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001297
Douglas Gregor4d87df52008-12-16 21:30:33 +00001298 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001299 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001300 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001301 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001302 }
1303
John McCall28a6aea2009-11-04 02:18:39 +00001304 DeclaratorInfo.complete(ThisDecl);
1305
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001306 // If we don't have a comma, it is either the end of the list (a ';')
1307 // or an error, bail out.
1308 if (Tok.isNot(tok::comma))
1309 break;
Mike Stump11289f42009-09-09 15:08:12 +00001310
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001311 // Consume the comma.
1312 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001313
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001314 // Parse the next declarator.
1315 DeclaratorInfo.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001316 BitfieldSize = 0;
1317 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001318 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001319
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001320 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001321 if (Tok.is(tok::kw___attribute)) {
1322 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001323 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001324 DeclaratorInfo.AddAttributes(AttrList, Loc);
1325 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001326
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001327 if (Tok.isNot(tok::colon))
1328 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001329 }
1330
1331 if (Tok.is(tok::semi)) {
1332 ConsumeToken();
Eli Friedman55b9ecb2009-05-29 01:49:24 +00001333 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001334 DeclsInGroup.size());
1335 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001336 }
1337
1338 Diag(Tok, diag::err_expected_semi_decl_list);
1339 // Skip to end of block or statement
1340 SkipUntil(tok::r_brace, true, true);
1341 if (Tok.is(tok::semi))
1342 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001343 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001344}
1345
1346/// ParseCXXMemberSpecification - Parse the class definition.
1347///
1348/// member-specification:
1349/// member-declaration member-specification[opt]
1350/// access-specifier ':' member-specification[opt]
1351///
1352void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001353 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001354 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001355 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001356 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001357
Chris Lattnereae6cb62009-03-05 08:00:35 +00001358 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1359 PP.getSourceManager(),
1360 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001361
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001362 // Determine whether this is a top-level (non-nested) class.
Mike Stump11289f42009-09-09 15:08:12 +00001363 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001364 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001365
1366 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001367 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001368
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001369 // Note that we are parsing a new (potentially-nested) class definition.
1370 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1371
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001372 if (TagDecl)
1373 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001374
1375 if (Tok.is(tok::colon)) {
1376 ParseBaseClause(TagDecl);
1377
1378 if (!Tok.is(tok::l_brace)) {
1379 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
1380 return;
1381 }
1382 }
1383
1384 assert(Tok.is(tok::l_brace));
1385
1386 SourceLocation LBraceLoc = ConsumeBrace();
1387
1388 if (!TagDecl) {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001389 SkipUntil(tok::r_brace, false, false);
1390 return;
1391 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001392
John McCall1c7e6ec2009-12-20 07:58:13 +00001393 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
1394
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001395 // C++ 11p3: Members of a class defined with the keyword class are private
1396 // by default. Members of a class defined with the keywords struct or union
1397 // are public by default.
1398 AccessSpecifier CurAS;
1399 if (TagType == DeclSpec::TST_class)
1400 CurAS = AS_private;
1401 else
1402 CurAS = AS_public;
1403
1404 // While we still have something to read, read the member-declarations.
1405 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1406 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001407
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001408 // Check for extraneous top-level semicolon.
1409 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +00001410 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001411 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001412 ConsumeToken();
1413 continue;
1414 }
1415
1416 AccessSpecifier AS = getAccessSpecifierIfPresent();
1417 if (AS != AS_none) {
1418 // Current token is a C++ access specifier.
1419 CurAS = AS;
1420 ConsumeToken();
1421 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1422 continue;
1423 }
1424
Douglas Gregor3447e762009-08-20 22:52:58 +00001425 // FIXME: Make sure we don't have a template here.
Mike Stump11289f42009-09-09 15:08:12 +00001426
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001427 // Parse all the comma separated declarators.
1428 ParseCXXClassMemberDeclaration(CurAS);
1429 }
Mike Stump11289f42009-09-09 15:08:12 +00001430
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001431 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001432
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001433 AttributeList *AttrList = 0;
1434 // If attributes exist after class contents, parse them.
1435 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001436 AttrList = ParseGNUAttributes(); // FIXME: where should I put them?
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001437
1438 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1439 LBraceLoc, RBraceLoc);
1440
1441 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1442 // complete within function bodies, default arguments,
1443 // exception-specifications, and constructor ctor-initializers (including
1444 // such things in nested classes).
1445 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001446 // FIXME: Only function bodies and constructor ctor-initializers are
1447 // parsed correctly, fix the rest.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001448 if (TopLevelClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001449 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001450 // are complete and we can parse the delayed portions of method
1451 // declarations and the lexed inline method definitions.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001452 ParseLexedMethodDeclarations(getCurrentClass());
1453 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001454 }
1455
1456 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001457 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001458 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001459
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001460 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001461}
Douglas Gregore8381c02008-11-05 04:29:56 +00001462
1463/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1464/// which explicitly initializes the members or base classes of a
1465/// class (C++ [class.base.init]). For example, the three initializers
1466/// after the ':' in the Derived constructor below:
1467///
1468/// @code
1469/// class Base { };
1470/// class Derived : Base {
1471/// int x;
1472/// float f;
1473/// public:
1474/// Derived(float f) : Base(), x(17), f(f) { }
1475/// };
1476/// @endcode
1477///
Mike Stump11289f42009-09-09 15:08:12 +00001478/// [C++] ctor-initializer:
1479/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001480///
Mike Stump11289f42009-09-09 15:08:12 +00001481/// [C++] mem-initializer-list:
1482/// mem-initializer
1483/// mem-initializer , mem-initializer-list
Chris Lattner83f095c2009-03-28 19:18:32 +00001484void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001485 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1486
1487 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001488
Douglas Gregore8381c02008-11-05 04:29:56 +00001489 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump11289f42009-09-09 15:08:12 +00001490
Douglas Gregore8381c02008-11-05 04:29:56 +00001491 do {
1492 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001493 if (!MemInit.isInvalid())
1494 MemInitializers.push_back(MemInit.get());
Douglas Gregore8381c02008-11-05 04:29:56 +00001495
1496 if (Tok.is(tok::comma))
1497 ConsumeToken();
1498 else if (Tok.is(tok::l_brace))
1499 break;
1500 else {
1501 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001502 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001503 SkipUntil(tok::l_brace, true, true);
1504 break;
1505 }
1506 } while (true);
1507
Mike Stump11289f42009-09-09 15:08:12 +00001508 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001509 MemInitializers.data(), MemInitializers.size());
Douglas Gregore8381c02008-11-05 04:29:56 +00001510}
1511
1512/// ParseMemInitializer - Parse a C++ member initializer, which is
1513/// part of a constructor initializer that explicitly initializes one
1514/// member or base class (C++ [class.base.init]). See
1515/// ParseConstructorInitializer for an example.
1516///
1517/// [C++] mem-initializer:
1518/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001519///
Douglas Gregore8381c02008-11-05 04:29:56 +00001520/// [C++] mem-initializer-id:
1521/// '::'[opt] nested-name-specifier[opt] class-name
1522/// identifier
Chris Lattner83f095c2009-03-28 19:18:32 +00001523Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001524 // parse '::'[opt] nested-name-specifier[opt]
1525 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001526 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001527 TypeTy *TemplateTypeTy = 0;
1528 if (Tok.is(tok::annot_template_id)) {
1529 TemplateIdAnnotation *TemplateId
1530 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +00001531 if (TemplateId->Kind == TNK_Type_template ||
1532 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001533 AnnotateTemplateIdTokenAsType(&SS);
1534 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1535 TemplateTypeTy = Tok.getAnnotationValue();
1536 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001537 }
1538 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001539 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001540 return true;
1541 }
Mike Stump11289f42009-09-09 15:08:12 +00001542
Douglas Gregore8381c02008-11-05 04:29:56 +00001543 // Get the identifier. This may be a member name or a class name,
1544 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001545 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001546 SourceLocation IdLoc = ConsumeToken();
1547
1548 // Parse the '('.
1549 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001550 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001551 return true;
1552 }
1553 SourceLocation LParenLoc = ConsumeParen();
1554
1555 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001556 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001557 CommaLocsTy CommaLocs;
1558 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1559 SkipUntil(tok::r_paren);
1560 return true;
1561 }
1562
1563 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1564
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001565 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1566 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001567 LParenLoc, ArgExprs.take(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001568 ArgExprs.size(), CommaLocs.data(),
1569 RParenLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00001570}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001571
1572/// ParseExceptionSpecification - Parse a C++ exception-specification
1573/// (C++ [except.spec]).
1574///
Douglas Gregor356513d2008-12-01 18:00:20 +00001575/// exception-specification:
1576/// 'throw' '(' type-id-list [opt] ')'
1577/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00001578///
Douglas Gregor356513d2008-12-01 18:00:20 +00001579/// type-id-list:
1580/// type-id
1581/// type-id-list ',' type-id
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001582///
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001583bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redld6434562009-05-29 18:02:33 +00001584 llvm::SmallVector<TypeTy*, 2>
1585 &Exceptions,
1586 llvm::SmallVector<SourceRange, 2>
1587 &Ranges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001588 bool &hasAnyExceptionSpec) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001589 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00001590
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001591 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001592
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001593 if (!Tok.is(tok::l_paren)) {
1594 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1595 }
1596 SourceLocation LParenLoc = ConsumeParen();
1597
Douglas Gregor356513d2008-12-01 18:00:20 +00001598 // Parse throw(...), a Microsoft extension that means "this function
1599 // can throw anything".
1600 if (Tok.is(tok::ellipsis)) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001601 hasAnyExceptionSpec = true;
Douglas Gregor356513d2008-12-01 18:00:20 +00001602 SourceLocation EllipsisLoc = ConsumeToken();
1603 if (!getLang().Microsoft)
1604 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001605 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor356513d2008-12-01 18:00:20 +00001606 return false;
1607 }
1608
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001609 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00001610 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001611 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00001612 TypeResult Res(ParseTypeName(&Range));
1613 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001614 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00001615 Ranges.push_back(Range);
1616 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001617 if (Tok.is(tok::comma))
1618 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001619 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001620 break;
1621 }
1622
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001623 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001624 return false;
1625}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001626
1627/// \brief We have just started parsing the definition of a new class,
1628/// so push that class onto our stack of classes that is currently
1629/// being parsed.
1630void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001631 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001632 "Nested class without outer class");
1633 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1634}
1635
1636/// \brief Deallocate the given parsed class and all of its nested
1637/// classes.
1638void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1639 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1640 DeallocateParsedClasses(Class->NestedClasses[I]);
1641 delete Class;
1642}
1643
1644/// \brief Pop the top class of the stack of classes that are
1645/// currently being parsed.
1646///
1647/// This routine should be called when we have finished parsing the
1648/// definition of a class, but have not yet popped the Scope
1649/// associated with the class's definition.
1650///
1651/// \returns true if the class we've popped is a top-level class,
1652/// false otherwise.
1653void Parser::PopParsingClass() {
1654 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00001655
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001656 ParsingClass *Victim = ClassStack.top();
1657 ClassStack.pop();
1658 if (Victim->TopLevelClass) {
1659 // Deallocate all of the nested classes of this class,
1660 // recursively: we don't need to keep any of this information.
1661 DeallocateParsedClasses(Victim);
1662 return;
Mike Stump11289f42009-09-09 15:08:12 +00001663 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001664 assert(!ClassStack.empty() && "Missing top-level class?");
1665
1666 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1667 Victim->NestedClasses.empty()) {
1668 // The victim is a nested class, but we will not need to perform
1669 // any processing after the definition of this class since it has
1670 // no members whose handling was delayed. Therefore, we can just
1671 // remove this nested class.
1672 delete Victim;
1673 return;
1674 }
1675
1676 // This nested class has some members that will need to be processed
1677 // after the top-level class is completely defined. Therefore, add
1678 // it to the list of nested classes within its parent.
1679 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1680 ClassStack.top()->NestedClasses.push_back(Victim);
1681 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1682}
Alexis Hunt96d5c762009-11-21 08:43:09 +00001683
1684/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1685/// parses standard attributes.
1686///
1687/// [C++0x] attribute-specifier:
1688/// '[' '[' attribute-list ']' ']'
1689///
1690/// [C++0x] attribute-list:
1691/// attribute[opt]
1692/// attribute-list ',' attribute[opt]
1693///
1694/// [C++0x] attribute:
1695/// attribute-token attribute-argument-clause[opt]
1696///
1697/// [C++0x] attribute-token:
1698/// identifier
1699/// attribute-scoped-token
1700///
1701/// [C++0x] attribute-scoped-token:
1702/// attribute-namespace '::' identifier
1703///
1704/// [C++0x] attribute-namespace:
1705/// identifier
1706///
1707/// [C++0x] attribute-argument-clause:
1708/// '(' balanced-token-seq ')'
1709///
1710/// [C++0x] balanced-token-seq:
1711/// balanced-token
1712/// balanced-token-seq balanced-token
1713///
1714/// [C++0x] balanced-token:
1715/// '(' balanced-token-seq ')'
1716/// '[' balanced-token-seq ']'
1717/// '{' balanced-token-seq '}'
1718/// any token but '(', ')', '[', ']', '{', or '}'
1719CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1720 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1721 && "Not a C++0x attribute list");
1722
1723 SourceLocation StartLoc = Tok.getLocation(), Loc;
1724 AttributeList *CurrAttr = 0;
1725
1726 ConsumeBracket();
1727 ConsumeBracket();
1728
1729 if (Tok.is(tok::comma)) {
1730 Diag(Tok.getLocation(), diag::err_expected_ident);
1731 ConsumeToken();
1732 }
1733
1734 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1735 // attribute not present
1736 if (Tok.is(tok::comma)) {
1737 ConsumeToken();
1738 continue;
1739 }
1740
1741 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1742 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1743
1744 // scoped attribute
1745 if (Tok.is(tok::coloncolon)) {
1746 ConsumeToken();
1747
1748 if (!Tok.is(tok::identifier)) {
1749 Diag(Tok.getLocation(), diag::err_expected_ident);
1750 SkipUntil(tok::r_square, tok::comma, true, true);
1751 continue;
1752 }
1753
1754 ScopeName = AttrName;
1755 ScopeLoc = AttrLoc;
1756
1757 AttrName = Tok.getIdentifierInfo();
1758 AttrLoc = ConsumeToken();
1759 }
1760
1761 bool AttrParsed = false;
1762 // No scoped names are supported; ideally we could put all non-standard
1763 // attributes into namespaces.
1764 if (!ScopeName) {
1765 switch(AttributeList::getKind(AttrName))
1766 {
1767 // No arguments
Alexis Hunt54a02542009-11-25 04:20:27 +00001768 case AttributeList::AT_base_check:
1769 case AttributeList::AT_carries_dependency:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001770 case AttributeList::AT_final:
Alexis Hunt54a02542009-11-25 04:20:27 +00001771 case AttributeList::AT_hiding:
1772 case AttributeList::AT_noreturn:
1773 case AttributeList::AT_override: {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001774 if (Tok.is(tok::l_paren)) {
1775 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1776 << AttrName->getName();
1777 break;
1778 }
1779
1780 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1781 SourceLocation(), 0, 0, CurrAttr, false,
1782 true);
1783 AttrParsed = true;
1784 break;
1785 }
1786
1787 // One argument; must be a type-id or assignment-expression
1788 case AttributeList::AT_aligned: {
1789 if (Tok.isNot(tok::l_paren)) {
1790 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1791 << AttrName->getName();
1792 break;
1793 }
1794 SourceLocation ParamLoc = ConsumeParen();
1795
1796 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1797
1798 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1799
1800 ExprVector ArgExprs(Actions);
1801 ArgExprs.push_back(ArgExpr.release());
1802 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1803 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1804 false, true);
1805
1806 AttrParsed = true;
1807 break;
1808 }
1809
1810 // Silence warnings
1811 default: break;
1812 }
1813 }
1814
1815 // Skip the entire parameter clause, if any
1816 if (!AttrParsed && Tok.is(tok::l_paren)) {
1817 ConsumeParen();
1818 // SkipUntil maintains the balancedness of tokens.
1819 SkipUntil(tok::r_paren, false);
1820 }
1821 }
1822
1823 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1824 SkipUntil(tok::r_square, false);
1825 Loc = Tok.getLocation();
1826 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1827 SkipUntil(tok::r_square, false);
1828
1829 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1830 return Attr;
1831}
1832
1833/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1834/// attribute.
1835///
1836/// FIXME: Simply returns an alignof() expression if the argument is a
1837/// type. Ideally, the type should be propagated directly into Sema.
1838///
1839/// [C++0x] 'align' '(' type-id ')'
1840/// [C++0x] 'align' '(' assignment-expression ')'
1841Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1842 if (isTypeIdInParens()) {
1843 EnterExpressionEvaluationContext Unevaluated(Actions,
1844 Action::Unevaluated);
1845 SourceLocation TypeLoc = Tok.getLocation();
1846 TypeTy *Ty = ParseTypeName().get();
1847 SourceRange TypeRange(Start, Tok.getLocation());
1848 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1849 TypeRange);
1850 } else
1851 return ParseConstantExpression();
1852}