blob: 7ed07a277c11a4a8ac7d365ee6d452355af176c7 [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)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000052 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +000053 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000054 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000055
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.
Ted Kremenekc162e8e2010-02-11 02:19:13 +000067 llvm::OwningPtr<AttributeList> AttrList;
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.
Ted Kremenekc162e8e2010-02-11 02:19:13 +000072 AttrList.reset(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
Douglas Gregor0be31a22010-07-02 17:43:08 +000090 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
91 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
92 getCurScope()->getFnParent()) {
Douglas Gregor05cfc292010-05-14 05:08:22 +000093 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
94 SkipUntil(tok::r_brace, false);
95 return DeclPtrTy();
96 }
97
Chris Lattner4de55aa2009-03-29 14:02:43 +000098 // Enter a scope for the namespace.
99 ParseScope NamespaceScope(this, Scope::DeclScope);
100
101 DeclPtrTy NamespcDecl =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000102 Actions.ActOnStartNamespaceDef(getCurScope(), IdentLoc, Ident, LBrace,
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000103 AttrList.get());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000104
105 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
106 PP.getSourceManager(),
107 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000108
Alexis Hunt96d5c762009-11-21 08:43:09 +0000109 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
110 CXX0XAttributeList Attr;
111 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
112 Attr = ParseCXX0XAttributes();
113 ParseExternalDeclaration(Attr);
114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattner4de55aa2009-03-29 14:02:43 +0000116 // Leave the namespace scope.
117 NamespaceScope.Exit();
118
Chris Lattner49836b42009-04-02 04:16:50 +0000119 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
120 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000121
Chris Lattner49836b42009-04-02 04:16:50 +0000122 DeclEnd = RBraceLoc;
Chris Lattner4de55aa2009-03-29 14:02:43 +0000123 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000124}
Chris Lattner38376f12008-01-12 07:05:38 +0000125
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000126/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
127/// alias definition.
128///
Anders Carlsson47952ae2009-03-28 22:53:22 +0000129Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000130 SourceLocation AliasLoc,
Chris Lattner49836b42009-04-02 04:16:50 +0000131 IdentifierInfo *Alias,
132 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000133 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000134
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000135 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000136
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000137 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000138 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000139 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000140 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000141
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000142 CXXScopeSpec SS;
143 // Parse (optional) nested-name-specifier.
Chris Lattnerd62268a2009-12-07 01:38:03 +0000144 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000145
146 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
147 Diag(Tok, diag::err_expected_namespace_name);
148 // Skip to end of the definition and eat the ';'.
149 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000150 return DeclPtrTy();
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000151 }
152
153 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000154 IdentifierInfo *Ident = Tok.getIdentifierInfo();
155 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000156
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000157 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000158 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000159 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
160 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000161
Douglas Gregor0be31a22010-07-02 17:43:08 +0000162 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000163 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000164}
165
Chris Lattner38376f12008-01-12 07:05:38 +0000166/// ParseLinkage - We know that the current token is a string_literal
167/// and just before that, that extern was seen.
168///
169/// linkage-specification: [C++ 7.5p2: dcl.link]
170/// 'extern' string-literal '{' declaration-seq[opt] '}'
171/// 'extern' string-literal declaration
172///
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000173Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
174 unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000175 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000176 llvm::SmallString<8> LangBuffer;
Chris Lattner38376f12008-01-12 07:05:38 +0000177 // LangBuffer is guaranteed to be big enough.
Douglas Gregordc970f02010-03-16 22:30:13 +0000178 bool Invalid = false;
179 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
180 if (Invalid)
181 return DeclPtrTy();
Chris Lattner38376f12008-01-12 07:05:38 +0000182
183 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000184
Douglas Gregor07665a62009-01-05 19:45:36 +0000185 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +0000186 DeclPtrTy LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000187 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor07665a62009-01-05 19:45:36 +0000188 /*FIXME: */SourceLocation(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000189 Loc, Lang,
Mike Stump11289f42009-09-09 15:08:12 +0000190 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000191 : SourceLocation());
192
Alexis Hunt96d5c762009-11-21 08:43:09 +0000193 CXX0XAttributeList Attr;
194 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
195 Attr = ParseCXX0XAttributes();
196 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000197
Douglas Gregor07665a62009-01-05 19:45:36 +0000198 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000199 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000200 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000201 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000202 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000203
Douglas Gregorb65a9132010-02-07 08:38:28 +0000204 DS.abort();
205
Alexis Hunt96d5c762009-11-21 08:43:09 +0000206 if (Attr.HasAttr)
207 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
208 << Attr.Range;
209
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000210 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000211 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000212 CXX0XAttributeList Attr;
213 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
214 Attr = ParseCXX0XAttributes();
215 ParseExternalDeclaration(Attr);
Chris Lattner38376f12008-01-12 07:05:38 +0000216 }
217
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000218 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000219 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000220}
Douglas Gregor556877c2008-04-13 21:30:24 +0000221
Douglas Gregord7c4d982008-12-30 03:27:21 +0000222/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
223/// using-directive. Assumes that current token is 'using'.
Chris Lattner49836b42009-04-02 04:16:50 +0000224Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000225 SourceLocation &DeclEnd,
226 CXX0XAttributeList Attr) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000227 assert(Tok.is(tok::kw_using) && "Not using token");
228
229 // Eat 'using'.
230 SourceLocation UsingLoc = ConsumeToken();
231
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000232 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000233 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000234 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000235 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000236
Chris Lattner9b01ca12009-01-06 06:55:51 +0000237 if (Tok.is(tok::kw_namespace))
Douglas Gregord7c4d982008-12-30 03:27:21 +0000238 // Next token after 'using' is 'namespace' so it must be using-directive
Alexis Hunt96d5c762009-11-21 08:43:09 +0000239 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
240
241 if (Attr.HasAttr)
242 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
243 << Attr.Range;
Chris Lattner9b01ca12009-01-06 06:55:51 +0000244
245 // Otherwise, it must be using-declaration.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000246 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner49836b42009-04-02 04:16:50 +0000247 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000248}
249
250/// ParseUsingDirective - Parse C++ using-directive, assumes
251/// that current token is 'namespace' and 'using' was already parsed.
252///
253/// using-directive: [C++ 7.3.p4: namespace.udir]
254/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
255/// namespace-name ;
256/// [GNU] using-directive:
257/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
258/// namespace-name attributes[opt] ;
259///
Chris Lattner83f095c2009-03-28 19:18:32 +0000260Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000261 SourceLocation UsingLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000262 SourceLocation &DeclEnd,
263 AttributeList *Attr) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000264 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
265
266 // Eat 'namespace'.
267 SourceLocation NamespcLoc = ConsumeToken();
268
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000269 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000270 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000271 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000272 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000273
Douglas Gregord7c4d982008-12-30 03:27:21 +0000274 CXXScopeSpec SS;
275 // Parse (optional) nested-name-specifier.
Chris Lattnerd62268a2009-12-07 01:38:03 +0000276 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000277
Douglas Gregord7c4d982008-12-30 03:27:21 +0000278 IdentifierInfo *NamespcName = 0;
279 SourceLocation IdentLoc = SourceLocation();
280
281 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000282 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000283 Diag(Tok, diag::err_expected_namespace_name);
284 // If there was invalid namespace name, skip to end of decl, and eat ';'.
285 SkipUntil(tok::semi);
286 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattner83f095c2009-03-28 19:18:32 +0000287 return DeclPtrTy();
Douglas Gregord7c4d982008-12-30 03:27:21 +0000288 }
Mike Stump11289f42009-09-09 15:08:12 +0000289
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000290 // Parse identifier.
291 NamespcName = Tok.getIdentifierInfo();
292 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000294 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000295 bool GNUAttr = false;
296 if (Tok.is(tok::kw___attribute)) {
297 GNUAttr = true;
298 Attr = addAttributeLists(Attr, ParseGNUAttributes());
299 }
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000301 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000302 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000303 ExpectAndConsume(tok::semi,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000304 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner34a95662009-06-14 00:07:48 +0000305 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000306
Douglas Gregor0be31a22010-07-02 17:43:08 +0000307 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000308 IdentLoc, NamespcName, Attr);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000309}
310
311/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
312/// 'using' was already seen.
313///
314/// using-declaration: [C++ 7.3.p3: namespace.udecl]
315/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000316/// unqualified-id
317/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000318///
Chris Lattner83f095c2009-03-28 19:18:32 +0000319Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000320 SourceLocation UsingLoc,
Anders Carlsson7b194b72009-08-29 19:54:19 +0000321 SourceLocation &DeclEnd,
322 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000323 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000324 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000325 bool IsTypeName;
326
327 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000328 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000329 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000330 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000331 ConsumeToken();
332 IsTypeName = true;
333 }
334 else
335 IsTypeName = false;
336
337 // Parse nested-name-specifier.
Chris Lattnerd62268a2009-12-07 01:38:03 +0000338 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000339
Douglas Gregorfec52632009-06-20 00:51:54 +0000340 // Check nested-name specifier.
341 if (SS.isInvalid()) {
342 SkipUntil(tok::semi);
343 return DeclPtrTy();
344 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000345
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000346 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000347 // destructor names and allow the action module to diagnose any semantic
348 // errors.
349 UnqualifiedId Name;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000350 if (ParseUnqualifiedId(SS,
Douglas Gregor220f4272009-11-04 16:30:06 +0000351 /*EnteringContext=*/false,
352 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000353 /*AllowConstructorName=*/true,
354 /*ObjectType=*/0,
Douglas Gregor220f4272009-11-04 16:30:06 +0000355 Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000356 SkipUntil(tok::semi);
357 return DeclPtrTy();
358 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000359
Douglas Gregorfec52632009-06-20 00:51:54 +0000360 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000361 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregorfec52632009-06-20 00:51:54 +0000362 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000363 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +0000364
Douglas Gregorfec52632009-06-20 00:51:54 +0000365 // Eat ';'.
366 DeclEnd = Tok.getLocation();
367 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000368 AttrList ? "attributes list" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000369 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000370
Douglas Gregor0be31a22010-07-02 17:43:08 +0000371 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, Name,
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000372 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000373}
374
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000375/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
376///
377/// static_assert-declaration:
378/// static_assert ( constant-expression , string-literal ) ;
379///
Chris Lattner49836b42009-04-02 04:16:50 +0000380Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000381 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
382 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000384 if (Tok.isNot(tok::l_paren)) {
385 Diag(Tok, diag::err_expected_lparen);
Chris Lattner83f095c2009-03-28 19:18:32 +0000386 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000389 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000390
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000391 OwningExprResult AssertExpr(ParseConstantExpression());
392 if (AssertExpr.isInvalid()) {
393 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000394 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000397 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner83f095c2009-03-28 19:18:32 +0000398 return DeclPtrTy();
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000399
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000400 if (Tok.isNot(tok::string_literal)) {
401 Diag(Tok, diag::err_expected_string_literal);
402 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000403 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000406 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000407 if (AssertMessage.isInvalid())
Chris Lattner83f095c2009-03-28 19:18:32 +0000408 return DeclPtrTy();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000409
Anders Carlsson27de6a52009-03-15 18:44:04 +0000410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner49836b42009-04-02 04:16:50 +0000412 DeclEnd = Tok.getLocation();
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000413 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
414
Mike Stump11289f42009-09-09 15:08:12 +0000415 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson27de6a52009-03-15 18:44:04 +0000416 move(AssertMessage));
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000417}
418
Anders Carlsson74948d02009-06-24 17:47:40 +0000419/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
420///
421/// 'decltype' ( expression )
422///
423void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
424 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
425
426 SourceLocation StartLoc = ConsumeToken();
427 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000428
429 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000430 "decltype")) {
431 SkipUntil(tok::r_paren);
432 return;
433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson74948d02009-06-24 17:47:40 +0000435 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000436
Anders Carlsson74948d02009-06-24 17:47:40 +0000437 // C++0x [dcl.type.simple]p4:
438 // The operand of the decltype specifier is an unevaluated operand.
439 EnterExpressionEvaluationContext Unevaluated(Actions,
440 Action::Unevaluated);
441 OwningExprResult Result = ParseExpression();
442 if (Result.isInvalid()) {
443 SkipUntil(tok::r_paren);
444 return;
445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Anders Carlsson74948d02009-06-24 17:47:40 +0000447 // Match the ')'
448 SourceLocation RParenLoc;
449 if (Tok.is(tok::r_paren))
450 RParenLoc = ConsumeParen();
451 else
452 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000453
Anders Carlsson74948d02009-06-24 17:47:40 +0000454 if (RParenLoc.isInvalid())
455 return;
456
457 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000458 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000459 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000460 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000461 DiagID, Result.release()))
462 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000463}
464
Douglas Gregor831c93f2008-11-05 20:51:48 +0000465/// ParseClassName - Parse a C++ class-name, which names a class. Note
466/// that we only check that the result names a type; semantic analysis
467/// will need to verify that the type names a class. The result is
Douglas Gregord54dfb82009-02-25 23:52:28 +0000468/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000469/// found.
470///
471/// class-name: [C++ 9.1]
472/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000473/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000474///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000475Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000476 CXXScopeSpec *SS) {
Douglas Gregord54dfb82009-02-25 23:52:28 +0000477 // Check whether we have a template-id that names a type.
478 if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +0000479 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000480 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +0000481 if (TemplateId->Kind == TNK_Type_template ||
482 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000483 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000484
485 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
486 TypeTy *Type = Tok.getAnnotationValue();
487 EndLocation = Tok.getAnnotationEndLoc();
488 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000489
490 if (Type)
491 return Type;
492 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000493 }
494
495 // Fall through to produce an error below.
496 }
497
Douglas Gregor831c93f2008-11-05 20:51:48 +0000498 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000499 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000500 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000501 }
502
Douglas Gregor18473f32010-01-12 21:28:44 +0000503 IdentifierInfo *Id = Tok.getIdentifierInfo();
504 SourceLocation IdLoc = ConsumeToken();
505
506 if (Tok.is(tok::less)) {
507 // It looks the user intended to write a template-id here, but the
508 // template-name was wrong. Try to fix that.
509 TemplateNameKind TNK = TNK_Type_template;
510 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000511 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor18473f32010-01-12 21:28:44 +0000512 SS, Template, TNK)) {
513 Diag(IdLoc, diag::err_unknown_template_name)
514 << Id;
515 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000516
Douglas Gregor18473f32010-01-12 21:28:44 +0000517 if (!Template)
518 return true;
519
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000520 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000521 UnqualifiedId TemplateName;
522 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000523
Douglas Gregor18473f32010-01-12 21:28:44 +0000524 // Parse the full template-id, then turn it into a type.
525 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
526 SourceLocation(), true))
527 return true;
528 if (TNK == TNK_Dependent_template_name)
529 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000530
Douglas Gregor18473f32010-01-12 21:28:44 +0000531 // If we didn't end up with a typename token, there's nothing more we
532 // can do.
533 if (Tok.isNot(tok::annot_typename))
534 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000535
Douglas Gregor18473f32010-01-12 21:28:44 +0000536 // Retrieve the type from the annotation token, consume that token, and
537 // return.
538 EndLocation = Tok.getAnnotationEndLoc();
539 TypeTy *Type = Tok.getAnnotationValue();
540 ConsumeToken();
541 return Type;
542 }
543
Douglas Gregor831c93f2008-11-05 20:51:48 +0000544 // We have an identifier; check whether it is actually a type.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000545 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000546 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000547 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000548 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000549 }
550
551 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000552 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000553
554 // Fake up a Declarator to use with ActOnTypeName.
555 DeclSpec DS;
556 DS.SetRangeStart(IdLoc);
557 DS.SetRangeEnd(EndLocation);
558 DS.getTypeSpecScope() = *SS;
559
560 const char *PrevSpec = 0;
561 unsigned DiagID;
562 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
563
564 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
565 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000566}
567
Douglas Gregor556877c2008-04-13 21:30:24 +0000568/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
569/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
570/// until we reach the start of a definition or see a token that
Sebastian Redl2b372722010-02-03 21:21:43 +0000571/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregor556877c2008-04-13 21:30:24 +0000572///
573/// class-specifier: [C++ class]
574/// class-head '{' member-specification[opt] '}'
575/// class-head '{' member-specification[opt] '}' attributes[opt]
576/// class-head:
577/// class-key identifier[opt] base-clause[opt]
578/// class-key nested-name-specifier identifier base-clause[opt]
579/// class-key nested-name-specifier[opt] simple-template-id
580/// base-clause[opt]
581/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000582/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000583/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000584/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000585/// simple-template-id base-clause[opt]
586/// class-key:
587/// 'class'
588/// 'struct'
589/// 'union'
590///
591/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000592/// class-key ::[opt] nested-name-specifier[opt] identifier
593/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
594/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000595///
596/// Note that the C++ class-specifier and elaborated-type-specifier,
597/// together, subsume the C99 struct-or-union-specifier:
598///
599/// struct-or-union-specifier: [C99 6.7.2.1]
600/// struct-or-union identifier[opt] '{' struct-contents '}'
601/// struct-or-union identifier
602/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
603/// '}' attributes[opt]
604/// [GNU] struct-or-union attributes[opt] identifier
605/// struct-or-union:
606/// 'struct'
607/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000608void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
609 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000610 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redl2b372722010-02-03 21:21:43 +0000611 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000612 DeclSpec::TST TagType;
613 if (TagTokKind == tok::kw_struct)
614 TagType = DeclSpec::TST_struct;
615 else if (TagTokKind == tok::kw_class)
616 TagType = DeclSpec::TST_class;
617 else {
618 assert(TagTokKind == tok::kw_union && "Not a class specifier");
619 TagType = DeclSpec::TST_union;
620 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000621
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000622 if (Tok.is(tok::code_completion)) {
623 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000624 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000625 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000626 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000627
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000628 // C++03 [temp.explicit] 14.7.2/8:
629 // The usual access checking rules do not apply to names used to specify
630 // explicit instantiations.
631 //
632 // As an extension we do not perform access checking on the names used to
633 // specify explicit specializations either. This is important to allow
634 // specializing traits classes for private types.
635 bool SuppressingAccessChecks = false;
636 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
637 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
638 Actions.ActOnStartSuppressingAccessChecks();
639 SuppressingAccessChecks = true;
640 }
641
Alexis Hunt96d5c762009-11-21 08:43:09 +0000642 AttributeList *AttrList = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000643 // If attributes exist after tag, parse them.
644 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000645 AttrList = ParseGNUAttributes();
Douglas Gregor556877c2008-04-13 21:30:24 +0000646
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000647 // If declspecs exist after tag, parse them.
Eli Friedman53339e02009-06-08 23:27:34 +0000648 if (Tok.is(tok::kw___declspec))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000649 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000650
Alexis Hunt96d5c762009-11-21 08:43:09 +0000651 // If C++0x attributes exist here, parse them.
652 // FIXME: Are we consistent with the ordering of parsing of different
653 // styles of attributes?
654 if (isCXX0XAttributeSpecifier())
655 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000656
Douglas Gregor119b0c72009-09-04 05:53:02 +0000657 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
658 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
659 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000660 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000661 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
662 // properly.
663 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
664 Tok.setKind(tok::identifier);
665 }
666
667 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
668 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
669 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000670 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000671 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
672 // properly.
673 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
674 Tok.setKind(tok::identifier);
675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000677 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +0000678 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000679 if (getLang().CPlusPlus) {
680 // "FOO : BAR" is not a potential typo for "FOO::BAR".
681 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000682
John McCall413021a2010-07-30 06:26:29 +0000683 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
684 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +0000685 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000686 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
687 Diag(Tok, diag::err_expected_ident);
688 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000689
Douglas Gregor916462b2009-10-30 21:46:58 +0000690 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
691
Douglas Gregor67a65642009-02-17 23:15:12 +0000692 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000693 IdentifierInfo *Name = 0;
694 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000695 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000696 if (Tok.is(tok::identifier)) {
697 Name = Tok.getIdentifierInfo();
698 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000699
Douglas Gregord5a479c2010-05-30 22:30:21 +0000700 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000701 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +0000702 // Eat the template argument list and try to continue parsing this as
703 // a class (or template thereof).
704 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000705 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000706 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor916462b2009-10-30 21:46:58 +0000707 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000708 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000709 // We couldn't parse the template argument list at all, so don't
710 // try to give any location information for the list.
711 LAngleLoc = RAngleLoc = SourceLocation();
712 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000713
Douglas Gregor916462b2009-10-30 21:46:58 +0000714 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000715 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000716 << (TagType == DeclSpec::TST_class? 0
717 : TagType == DeclSpec::TST_struct? 1
718 : 2)
719 << Name
720 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000721
722 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000723 // we've removed its template argument list.
724 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
725 if (TemplateParams && TemplateParams->size() > 1) {
726 TemplateParams->pop_back();
727 } else {
728 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000729 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000730 = ParsedTemplateInfo::NonTemplate;
731 }
732 } else if (TemplateInfo.Kind
733 == ParsedTemplateInfo::ExplicitInstantiation) {
734 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000735 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000736 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +0000737 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000738 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000739 = SourceLocation();
740 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
741 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000742 }
Douglas Gregor916462b2009-10-30 21:46:58 +0000743 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000744 } else if (Tok.is(tok::annot_template_id)) {
745 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
746 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000747
Douglas Gregorb67535d2009-03-31 00:43:58 +0000748 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000749 // The template-name in the simple-template-id refers to
750 // something other than a class template. Give an appropriate
751 // error message and skip to the ';'.
752 SourceRange Range(NameLoc);
753 if (SS.isNotEmpty())
754 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000755
Douglas Gregor7f741122009-02-25 19:37:18 +0000756 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
757 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000758
Douglas Gregor7f741122009-02-25 19:37:18 +0000759 DS.SetTypeSpecError();
760 SkipUntil(tok::semi, false, true);
761 TemplateId->Destroy();
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000762 if (SuppressingAccessChecks)
763 Actions.ActOnStopSuppressingAccessChecks();
764
Douglas Gregor7f741122009-02-25 19:37:18 +0000765 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000766 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000767 }
768
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000769 // As soon as we're finished parsing the class's template-id, turn access
770 // checking back on.
771 if (SuppressingAccessChecks)
772 Actions.ActOnStopSuppressingAccessChecks();
773
John McCall07e91c02009-08-06 02:15:43 +0000774 // There are four options here. If we have 'struct foo;', then this
775 // is either a forward declaration or a friend declaration, which
776 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor7f741122009-02-25 19:37:18 +0000777 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregor556877c2008-04-13 21:30:24 +0000778 // something like 'struct foo xyz', a reference.
Sebastian Redl2b372722010-02-03 21:21:43 +0000779 // However, in some contexts, things look like declarations but are just
780 // references, e.g.
781 // new struct s;
782 // or
783 // &T::operator struct s;
784 // For these, SuppressDeclarations is true.
John McCall9bb74a52009-07-31 02:45:11 +0000785 Action::TagUseKind TUK;
Sebastian Redl2b372722010-02-03 21:21:43 +0000786 if (SuppressDeclarations)
787 TUK = Action::TUK_Reference;
788 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregor3dad8422009-09-26 06:47:28 +0000789 if (DS.isFriendSpecified()) {
790 // C++ [class.friend]p2:
791 // A class shall not be defined in a friend declaration.
792 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
793 << SourceRange(DS.getFriendSpecLoc());
794
795 // Skip everything up to the semicolon, so that this looks like a proper
796 // friend class (or template thereof) declaration.
797 SkipUntil(tok::semi, true, true);
798 TUK = Action::TUK_Friend;
799 } else {
800 // Okay, this is a class definition.
801 TUK = Action::TUK_Definition;
802 }
803 } else if (Tok.is(tok::semi))
John McCall07e91c02009-08-06 02:15:43 +0000804 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000805 else
John McCall9bb74a52009-07-31 02:45:11 +0000806 TUK = Action::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000807
John McCall413021a2010-07-30 06:26:29 +0000808 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
809 TUK != Action::TUK_Definition)) {
810 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
811 // We have a declaration or reference to an anonymous class.
812 Diag(StartLoc, diag::err_anon_type_definition)
813 << DeclSpec::getSpecifierName(TagType);
814 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000815
Douglas Gregor556877c2008-04-13 21:30:24 +0000816 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000817
818 if (TemplateId)
819 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000820 return;
821 }
822
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000823 // Create the tag portion of the class or class template.
John McCall7f41d982009-09-11 04:59:25 +0000824 Action::DeclResult TagOrTempResult = true; // invalid
825 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000826
Douglas Gregord6ab8742009-05-28 23:31:59 +0000827 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000828 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000829 // Explicit specialization, class template partial specialization,
830 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000831 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000832 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000833 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000834 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000835 TUK == Action::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000836 // This is an explicit instantiation of a class template.
837 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000838 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000839 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000840 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000841 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000842 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000843 SS,
Mike Stump11289f42009-09-09 15:08:12 +0000844 TemplateTy::make(TemplateId->Template),
845 TemplateId->TemplateNameLoc,
846 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000847 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000848 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000849 AttrList);
John McCallb7c5c272010-04-14 00:24:33 +0000850
851 // Friend template-ids are treated as references unless
852 // they have template headers, in which case they're ill-formed
853 // (FIXME: "template <class T> friend class A<T>::B<int>;").
854 // We diagnose this error in ActOnClassTemplateSpecialization.
855 } else if (TUK == Action::TUK_Reference ||
856 (TUK == Action::TUK_Friend &&
857 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCall7f41d982009-09-11 04:59:25 +0000858 TypeResult
John McCalld8fe9af2009-09-08 17:47:29 +0000859 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
860 TemplateId->TemplateNameLoc,
861 TemplateId->LAngleLoc,
862 TemplateArgsPtr,
John McCalld8fe9af2009-09-08 17:47:29 +0000863 TemplateId->RAngleLoc);
864
John McCall7f41d982009-09-11 04:59:25 +0000865 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
866 TagType, StartLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000867 } else {
868 // This is an explicit specialization or a class template
869 // partial specialization.
870 TemplateParameterLists FakedParamLists;
871
872 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
873 // This looks like an explicit instantiation, because we have
874 // something like
875 //
876 // template class Foo<X>
877 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000878 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000879 // meant to be an explicit specialization, but the user forgot
880 // the '<>' after 'template'.
John McCall9bb74a52009-07-31 02:45:11 +0000881 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000882
Mike Stump11289f42009-09-09 15:08:12 +0000883 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000884 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000885 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000886 diag::err_explicit_instantiation_with_definition)
887 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregora771f462010-03-31 17:46:05 +0000888 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000889
890 // Create a fake template parameter list that contains only
891 // "template<>", so that we treat this construct as a class
892 // template specialization.
893 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000894 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000895 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000896 LAngleLoc,
897 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000898 LAngleLoc));
899 TemplateParams = &FakedParamLists;
900 }
901
902 // Build the class template specialization.
903 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000904 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000905 StartLoc, SS,
Mike Stump11289f42009-09-09 15:08:12 +0000906 TemplateTy::make(TemplateId->Template),
907 TemplateId->TemplateNameLoc,
908 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000909 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000910 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000911 AttrList,
Mike Stump11289f42009-09-09 15:08:12 +0000912 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000913 TemplateParams? &(*TemplateParams)[0] : 0,
914 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000915 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000916 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000917 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000918 TUK == Action::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000919 // Explicit instantiation of a member of a class template
920 // specialization, e.g.,
921 //
922 // template struct Outer<int>::Inner;
923 //
924 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000925 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000926 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000927 TemplateInfo.TemplateLoc,
928 TagType, StartLoc, SS, Name,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000929 NameLoc, AttrList);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000930 } else {
931 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000932 TUK == Action::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000933 // FIXME: Diagnose this particular error.
934 }
935
John McCall7f41d982009-09-11 04:59:25 +0000936 bool IsDependent = false;
937
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000938 // Declaration or definition of a class type
Douglas Gregor0be31a22010-07-02 17:43:08 +0000939 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, SS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000940 Name, NameLoc, AttrList, AS,
Mike Stump11289f42009-09-09 15:08:12 +0000941 Action::MultiTemplateParamsArg(Actions,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000942 TemplateParams? &(*TemplateParams)[0] : 0,
943 TemplateParams? TemplateParams->size() : 0),
John McCall7f41d982009-09-11 04:59:25 +0000944 Owned, IsDependent);
945
946 // If ActOnTag said the type was dependent, try again with the
947 // less common call.
948 if (IsDependent)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000949 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000950 SS, Name, StartLoc, NameLoc);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000951 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000952
Douglas Gregor556877c2008-04-13 21:30:24 +0000953 // If there is a body, parse it and inform the actions module.
John McCall2d814c32009-12-19 21:48:58 +0000954 if (TUK == Action::TUK_Definition) {
955 assert(Tok.is(tok::l_brace) ||
956 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000957 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +0000958 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000959 else
Douglas Gregorc08f4892009-03-25 00:13:59 +0000960 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000961 }
962
John McCall7f41d982009-09-11 04:59:25 +0000963 void *Result;
964 if (!TypeResult.isInvalid()) {
965 TagType = DeclSpec::TST_typename;
966 Result = TypeResult.get();
967 Owned = false;
968 } else if (!TagOrTempResult.isInvalid()) {
969 Result = TagOrTempResult.get().getAs<void>();
970 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000971 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +0000972 return;
973 }
Mike Stump11289f42009-09-09 15:08:12 +0000974
John McCall49bfce42009-08-03 20:12:06 +0000975 const char *PrevSpec = 0;
976 unsigned DiagID;
John McCall7f41d982009-09-11 04:59:25 +0000977
Douglas Gregor72100632010-01-25 16:33:23 +0000978 // FIXME: The DeclSpec should keep the locations of both the keyword and the
979 // name (if there is one).
980 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000981
Douglas Gregor72100632010-01-25 16:33:23 +0000982 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCall7f41d982009-09-11 04:59:25 +0000983 Result, Owned))
John McCall49bfce42009-08-03 20:12:06 +0000984 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000985
Chris Lattnercf251412010-02-02 01:23:29 +0000986 // At this point, we've successfully parsed a class-specifier in 'definition'
987 // form (e.g. "struct foo { int x; }". While we could just return here, we're
988 // going to look at what comes after it to improve error recovery. If an
989 // impossible token occurs next, we assume that the programmer forgot a ; at
990 // the end of the declaration and recover that way.
991 //
992 // This switch enumerates the valid "follow" set for definition.
993 if (TUK == Action::TUK_Definition) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +0000994 bool ExpectedSemi = true;
Chris Lattnercf251412010-02-02 01:23:29 +0000995 switch (Tok.getKind()) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +0000996 default: break;
Chris Lattnercf251412010-02-02 01:23:29 +0000997 case tok::semi: // struct foo {...} ;
Chris Lattnerafe6a842010-02-02 17:32:27 +0000998 case tok::star: // struct foo {...} * P;
999 case tok::amp: // struct foo {...} & R = ...
1000 case tok::identifier: // struct foo {...} V ;
1001 case tok::r_paren: //(struct foo {...} ) {4}
1002 case tok::annot_cxxscope: // struct foo {...} a:: b;
1003 case tok::annot_typename: // struct foo {...} a ::b;
1004 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattner5e854b92010-02-03 20:41:24 +00001005 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner35af0ab2010-02-03 01:45:03 +00001006 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001007 ExpectedSemi = false;
1008 break;
1009 // Type qualifiers
1010 case tok::kw_const: // struct foo {...} const x;
1011 case tok::kw_volatile: // struct foo {...} volatile x;
1012 case tok::kw_restrict: // struct foo {...} restrict x;
1013 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattnerafe6a842010-02-02 17:32:27 +00001014 // Storage-class specifiers
1015 case tok::kw_static: // struct foo {...} static x;
1016 case tok::kw_extern: // struct foo {...} extern x;
1017 case tok::kw_typedef: // struct foo {...} typedef x;
1018 case tok::kw_register: // struct foo {...} register x;
1019 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregorc9a99c52010-05-17 18:19:56 +00001020 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001021 // As shown above, type qualifiers and storage class specifiers absolutely
1022 // can occur after class specifiers according to the grammar. However,
1023 // almost noone actually writes code like this. If we see one of these,
1024 // it is much more likely that someone missed a semi colon and the
1025 // type/storage class specifier we're seeing is part of the *next*
1026 // intended declaration, as in:
1027 //
1028 // struct foo { ... }
1029 // typedef int X;
1030 //
1031 // We'd really like to emit a missing semicolon error instead of emitting
1032 // an error on the 'int' saying that you can't have two type specifiers in
1033 // the same declaration of X. Because of this, we look ahead past this
1034 // token to see if it's a type specifier. If so, we know the code is
1035 // otherwise invalid, so we can produce the expected semi error.
1036 if (!isKnownToBeTypeSpecifier(NextToken()))
1037 ExpectedSemi = false;
Chris Lattnercf251412010-02-02 01:23:29 +00001038 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001039
1040 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattnercf251412010-02-02 01:23:29 +00001041 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001042 if (!getLang().CPlusPlus)
1043 ExpectedSemi = false;
1044 break;
1045 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001046
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001047 if (ExpectedSemi) {
Chris Lattnercf251412010-02-02 01:23:29 +00001048 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1049 TagType == DeclSpec::TST_class ? "class"
1050 : TagType == DeclSpec::TST_struct? "struct" : "union");
1051 // Push this token back into the preprocessor and change our current token
1052 // to ';' so that the rest of the code recovers as though there were an
1053 // ';' after the definition.
1054 PP.EnterToken(Tok);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001055 Tok.setKind(tok::semi);
Chris Lattnercf251412010-02-02 01:23:29 +00001056 }
1057 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001058}
1059
Mike Stump11289f42009-09-09 15:08:12 +00001060/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001061///
1062/// base-clause : [C++ class.derived]
1063/// ':' base-specifier-list
1064/// base-specifier-list:
1065/// base-specifier '...'[opt]
1066/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner83f095c2009-03-28 19:18:32 +00001067void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001068 assert(Tok.is(tok::colon) && "Not a base clause");
1069 ConsumeToken();
1070
Douglas Gregor29a92472008-10-22 17:49:05 +00001071 // Build up an array of parsed base specifiers.
1072 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1073
Douglas Gregor556877c2008-04-13 21:30:24 +00001074 while (true) {
1075 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001076 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001077 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001078 // Skip the rest of this base specifier, up until the comma or
1079 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001080 SkipUntil(tok::comma, tok::l_brace, true, true);
1081 } else {
1082 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001083 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001084 }
1085
1086 // If the next token is a comma, consume it and keep reading
1087 // base-specifiers.
1088 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001089
Douglas Gregor556877c2008-04-13 21:30:24 +00001090 // Consume the comma.
1091 ConsumeToken();
1092 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001093
1094 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001095 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001096}
1097
1098/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1099/// one entry in the base class list of a class specifier, for example:
1100/// class foo : public bar, virtual private baz {
1101/// 'public bar' and 'virtual private baz' are each base-specifiers.
1102///
1103/// base-specifier: [C++ class.derived]
1104/// ::[opt] nested-name-specifier[opt] class-name
1105/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1106/// class-name
1107/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1108/// class-name
Chris Lattner83f095c2009-03-28 19:18:32 +00001109Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001110 bool IsVirtual = false;
1111 SourceLocation StartLoc = Tok.getLocation();
1112
1113 // Parse the 'virtual' keyword.
1114 if (Tok.is(tok::kw_virtual)) {
1115 ConsumeToken();
1116 IsVirtual = true;
1117 }
1118
1119 // Parse an (optional) access specifier.
1120 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001121 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001122 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001123
Douglas Gregor556877c2008-04-13 21:30:24 +00001124 // Parse the 'virtual' keyword (again!), in case it came after the
1125 // access specifier.
1126 if (Tok.is(tok::kw_virtual)) {
1127 SourceLocation VirtualLoc = ConsumeToken();
1128 if (IsVirtual) {
1129 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001130 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001131 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001132 }
1133
1134 IsVirtual = true;
1135 }
1136
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001137 // Parse optional '::' and optional nested-name-specifier.
1138 CXXScopeSpec SS;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001139 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
Douglas Gregor53db22c2010-03-02 00:25:00 +00001140 /*EnteringContext=*/false);
Douglas Gregor556877c2008-04-13 21:30:24 +00001141
Douglas Gregor556877c2008-04-13 21:30:24 +00001142 // The location of the base class itself.
1143 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +00001144
1145 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001146 SourceLocation EndLocation;
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001147 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1148 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001149 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001150
1151 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001152 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001153
Douglas Gregor556877c2008-04-13 21:30:24 +00001154 // Notify semantic analysis that we have parsed a complete
1155 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +00001156 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001157 BaseType.get(), BaseLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001158}
1159
1160/// getAccessSpecifierIfPresent - Determine whether the next token is
1161/// a C++ access-specifier.
1162///
1163/// access-specifier: [C++ class.derived]
1164/// 'private'
1165/// 'protected'
1166/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001167AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001168 switch (Tok.getKind()) {
1169 default: return AS_none;
1170 case tok::kw_private: return AS_private;
1171 case tok::kw_protected: return AS_protected;
1172 case tok::kw_public: return AS_public;
1173 }
1174}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001175
Eli Friedman3af2a772009-07-22 21:45:50 +00001176void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1177 DeclPtrTy ThisDecl) {
1178 // We just declared a member function. If this member function
1179 // has any default arguments, we'll need to parse them later.
1180 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001181 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedman3af2a772009-07-22 21:45:50 +00001182 = DeclaratorInfo.getTypeObject(0).Fun;
1183 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1184 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1185 if (!LateMethod) {
1186 // Push this method onto the stack of late-parsed method
1187 // declarations.
1188 getCurrentClass().MethodDecls.push_back(
1189 LateParsedMethodDeclaration(ThisDecl));
1190 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001191 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001192
1193 // Add all of the parameters prior to this one (they don't
1194 // have default arguments).
1195 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1196 for (unsigned I = 0; I < ParamIdx; ++I)
1197 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001198 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001199 }
1200
1201 // Add this parameter to the list of parameters (it or may
1202 // not have a default argument).
1203 LateMethod->DefaultArgs.push_back(
1204 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1205 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1206 }
1207 }
1208}
1209
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001210/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1211///
1212/// member-declaration:
1213/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1214/// function-definition ';'[opt]
1215/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1216/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001217/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001218/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001219/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001220///
1221/// member-declarator-list:
1222/// member-declarator
1223/// member-declarator-list ',' member-declarator
1224///
1225/// member-declarator:
1226/// declarator pure-specifier[opt]
1227/// declarator constant-initializer[opt]
1228/// identifier[opt] ':' constant-expression
1229///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001230/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001231/// '= 0'
1232///
1233/// constant-initializer:
1234/// '=' constant-expression
1235///
Douglas Gregor3447e762009-08-20 22:52:58 +00001236void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCall796c2a52010-07-16 08:13:16 +00001237 const ParsedTemplateInfo &TemplateInfo,
1238 ParsingDeclRAIIObject *TemplateDiags) {
John McCalla0097262009-12-11 02:10:03 +00001239 // Access declarations.
1240 if (!TemplateInfo.Kind &&
1241 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall1f476a12010-02-26 08:45:28 +00001242 !TryAnnotateCXXScopeToken() &&
John McCalla0097262009-12-11 02:10:03 +00001243 Tok.is(tok::annot_cxxscope)) {
1244 bool isAccessDecl = false;
1245 if (NextToken().is(tok::identifier))
1246 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1247 else
1248 isAccessDecl = NextToken().is(tok::kw_operator);
1249
1250 if (isAccessDecl) {
1251 // Collect the scope specifier token we annotated earlier.
1252 CXXScopeSpec SS;
1253 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1254
1255 // Try to parse an unqualified-id.
1256 UnqualifiedId Name;
1257 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1258 SkipUntil(tok::semi);
1259 return;
1260 }
1261
1262 // TODO: recover from mistakenly-qualified operator declarations.
1263 if (ExpectAndConsume(tok::semi,
1264 diag::err_expected_semi_after,
1265 "access declaration",
1266 tok::semi))
1267 return;
1268
Douglas Gregor0be31a22010-07-02 17:43:08 +00001269 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCalla0097262009-12-11 02:10:03 +00001270 false, SourceLocation(),
1271 SS, Name,
1272 /* AttrList */ 0,
1273 /* IsTypeName */ false,
1274 SourceLocation());
1275 return;
1276 }
1277 }
1278
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001279 // static_assert-declaration
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001280 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001281 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001282 SourceLocation DeclEnd;
1283 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001284 return;
1285 }
Mike Stump11289f42009-09-09 15:08:12 +00001286
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001287 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001288 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001289 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001290 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001291 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001292 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001293 return;
1294 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001295
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001296 // Handle: member-declaration ::= '__extension__' member-declaration
1297 if (Tok.is(tok::kw___extension__)) {
1298 // __extension__ silences extension warnings in the subexpression.
1299 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1300 ConsumeToken();
John McCall796c2a52010-07-16 08:13:16 +00001301 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001302 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001303
Chris Lattnercf251412010-02-02 01:23:29 +00001304 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1305 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001306 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001307
Alexis Hunt96d5c762009-11-21 08:43:09 +00001308 CXX0XAttributeList AttrList;
1309 // Optional C++0x attribute-specifier
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001310 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Alexis Hunt96d5c762009-11-21 08:43:09 +00001311 AttrList = ParseCXX0XAttributes();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001312
Douglas Gregorfec52632009-06-20 00:51:54 +00001313 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001314 // FIXME: Check for template aliases
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001315
Alexis Hunt96d5c762009-11-21 08:43:09 +00001316 if (AttrList.HasAttr)
1317 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1318 << AttrList.Range;
Mike Stump11289f42009-09-09 15:08:12 +00001319
Douglas Gregorfec52632009-06-20 00:51:54 +00001320 // Eat 'using'.
1321 SourceLocation UsingLoc = ConsumeToken();
1322
1323 if (Tok.is(tok::kw_namespace)) {
1324 Diag(UsingLoc, diag::err_using_namespace_in_class);
1325 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00001326 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00001327 SourceLocation DeclEnd;
1328 // Otherwise, it must be using-declaration.
Anders Carlsson7b194b72009-08-29 19:54:19 +00001329 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001330 }
1331 return;
1332 }
1333
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001334 SourceLocation DSStart = Tok.getLocation();
1335 // decl-specifier-seq:
1336 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00001337 ParsingDeclSpec DS(*this, TemplateDiags);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001338 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor3447e762009-08-20 22:52:58 +00001339 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001340
John McCall11083da2009-09-16 22:47:08 +00001341 Action::MultiTemplateParamsArg TemplateParams(Actions,
1342 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1343 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1344
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001345 if (Tok.is(tok::semi)) {
1346 ConsumeToken();
John McCall796c2a52010-07-16 08:13:16 +00001347 DeclPtrTy TheDecl =
1348 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1349 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00001350 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001351 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001352
John McCall28a6aea2009-11-04 02:18:39 +00001353 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001354
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001355 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001356 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1357 ColonProtectionRAIIObject X(*this);
1358
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001359 // Parse the first declarator.
1360 ParseDeclarator(DeclaratorInfo);
1361 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001362 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001363 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001364 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001365 if (Tok.is(tok::semi))
1366 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001367 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001368 }
1369
John Thompson5bc5cbe2009-11-25 22:58:06 +00001370 // If attributes exist after the declarator, but before an '{', parse them.
1371 if (Tok.is(tok::kw___attribute)) {
1372 SourceLocation Loc;
1373 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1374 DeclaratorInfo.AddAttributes(AttrList, Loc);
1375 }
1376
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001377 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001378 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001379 || (DeclaratorInfo.isFunctionDeclarator() &&
1380 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001381 if (!DeclaratorInfo.isFunctionDeclarator()) {
1382 Diag(Tok, diag::err_func_def_no_params);
1383 ConsumeBrace();
1384 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001385 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001386 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001387
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001388 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1389 Diag(Tok, diag::err_function_declared_typedef);
1390 // This recovery skips the entire function body. It would be nice
1391 // to simply call ParseCXXInlineMethodDef() below, however Sema
1392 // assumes the declarator represents a function, not a typedef.
1393 ConsumeBrace();
1394 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001395 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001396 }
1397
Douglas Gregor3447e762009-08-20 22:52:58 +00001398 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001399 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001400 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001401 }
1402
1403 // member-declarator-list:
1404 // member-declarator
1405 // member-declarator-list ',' member-declarator
1406
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001407 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001408 OwningExprResult BitfieldSize(Actions);
1409 OwningExprResult Init(Actions);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001410 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001411
1412 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001413 // member-declarator:
1414 // declarator pure-specifier[opt]
1415 // declarator constant-initializer[opt]
1416 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001417 if (Tok.is(tok::colon)) {
1418 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001419 BitfieldSize = ParseConstantExpression();
1420 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001421 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001422 }
Mike Stump11289f42009-09-09 15:08:12 +00001423
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001424 // pure-specifier:
1425 // '= 0'
1426 //
1427 // constant-initializer:
1428 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001429 //
1430 // defaulted/deleted function-definition:
1431 // '=' 'default' [TODO]
1432 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001433 if (Tok.is(tok::equal)) {
1434 ConsumeToken();
Sebastian Redl42e92c42009-04-12 17:16:29 +00001435 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1436 ConsumeToken();
1437 Deleted = true;
1438 } else {
1439 Init = ParseInitializer();
1440 if (Init.isInvalid())
1441 SkipUntil(tok::comma, true, true);
1442 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001443 }
1444
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001445 // If a simple-asm-expr is present, parse it.
1446 if (Tok.is(tok::kw_asm)) {
1447 SourceLocation Loc;
1448 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
1449 if (AsmLabel.isInvalid())
1450 SkipUntil(tok::comma, true, true);
1451
1452 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1453 DeclaratorInfo.SetRangeEnd(Loc);
1454 }
1455
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001456 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001457 if (Tok.is(tok::kw___attribute)) {
1458 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001459 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001460 DeclaratorInfo.AddAttributes(AttrList, Loc);
1461 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001462
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001463 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001464 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001465 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001466
1467 DeclPtrTy ThisDecl;
1468 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001469 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00001470 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCall2f212b32009-09-11 21:02:39 +00001471 /*IsDefinition*/ false,
1472 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001473 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001474 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00001475 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001476 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001477 BitfieldSize.release(),
1478 Init.release(),
Sebastian Redld6f78502009-11-24 23:38:44 +00001479 /*IsDefinition*/Deleted,
John McCall07e91c02009-08-06 02:15:43 +00001480 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001481 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001482 if (ThisDecl)
1483 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001484
Douglas Gregor4d87df52008-12-16 21:30:33 +00001485 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001486 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001487 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001488 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001489 }
1490
John McCall28a6aea2009-11-04 02:18:39 +00001491 DeclaratorInfo.complete(ThisDecl);
1492
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001493 // If we don't have a comma, it is either the end of the list (a ';')
1494 // or an error, bail out.
1495 if (Tok.isNot(tok::comma))
1496 break;
Mike Stump11289f42009-09-09 15:08:12 +00001497
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001498 // Consume the comma.
1499 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001500
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001501 // Parse the next declarator.
1502 DeclaratorInfo.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001503 BitfieldSize = 0;
1504 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001505 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001506
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001507 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001508 if (Tok.is(tok::kw___attribute)) {
1509 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001510 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001511 DeclaratorInfo.AddAttributes(AttrList, Loc);
1512 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001513
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001514 if (Tok.isNot(tok::colon))
1515 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001516 }
1517
Chris Lattner916dbf12010-02-02 00:43:15 +00001518 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1519 // Skip to end of block or statement.
1520 SkipUntil(tok::r_brace, true, true);
1521 // If we stopped at a ';', eat it.
1522 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001523 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001524 }
1525
Douglas Gregor0be31a22010-07-02 17:43:08 +00001526 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattner916dbf12010-02-02 00:43:15 +00001527 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001528}
1529
1530/// ParseCXXMemberSpecification - Parse the class definition.
1531///
1532/// member-specification:
1533/// member-declaration member-specification[opt]
1534/// access-specifier ':' member-specification[opt]
1535///
1536void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001537 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001538 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001539 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001540 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001541
Chris Lattnereae6cb62009-03-05 08:00:35 +00001542 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1543 PP.getSourceManager(),
1544 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregoredf8f392010-01-16 20:52:59 +00001546 // Determine whether this is a non-nested class. Note that local
1547 // classes are *not* considered to be nested classes.
1548 bool NonNestedClass = true;
1549 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001550 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00001551 if (S->isClassScope()) {
1552 // We're inside a class scope, so this is a nested class.
1553 NonNestedClass = false;
1554 break;
1555 }
1556
1557 if ((S->getFlags() & Scope::FnScope)) {
1558 // If we're in a function or function template declared in the
1559 // body of a class, then this is a local class rather than a
1560 // nested class.
1561 const Scope *Parent = S->getParent();
1562 if (Parent->isTemplateParamScope())
1563 Parent = Parent->getParent();
1564 if (Parent->isClassScope())
1565 break;
1566 }
1567 }
1568 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001569
1570 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001571 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001572
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001573 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001574 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001575
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001576 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001577 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001578
1579 if (Tok.is(tok::colon)) {
1580 ParseBaseClause(TagDecl);
1581
1582 if (!Tok.is(tok::l_brace)) {
1583 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00001584
1585 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001586 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001587 return;
1588 }
1589 }
1590
1591 assert(Tok.is(tok::l_brace));
1592
1593 SourceLocation LBraceLoc = ConsumeBrace();
1594
John McCall08bede42010-05-28 08:11:17 +00001595 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001596 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCall1c7e6ec2009-12-20 07:58:13 +00001597
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001598 // C++ 11p3: Members of a class defined with the keyword class are private
1599 // by default. Members of a class defined with the keywords struct or union
1600 // are public by default.
1601 AccessSpecifier CurAS;
1602 if (TagType == DeclSpec::TST_class)
1603 CurAS = AS_private;
1604 else
1605 CurAS = AS_public;
1606
Douglas Gregor9377c822010-06-21 22:31:09 +00001607 SourceLocation RBraceLoc;
1608 if (TagDecl) {
1609 // While we still have something to read, read the member-declarations.
1610 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1611 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001612
Douglas Gregor9377c822010-06-21 22:31:09 +00001613 // Check for extraneous top-level semicolon.
1614 if (Tok.is(tok::semi)) {
1615 Diag(Tok, diag::ext_extra_struct_semi)
1616 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1617 << FixItHint::CreateRemoval(Tok.getLocation());
1618 ConsumeToken();
1619 continue;
1620 }
1621
1622 AccessSpecifier AS = getAccessSpecifierIfPresent();
1623 if (AS != AS_none) {
1624 // Current token is a C++ access specifier.
1625 CurAS = AS;
1626 SourceLocation ASLoc = Tok.getLocation();
1627 ConsumeToken();
1628 if (Tok.is(tok::colon))
1629 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1630 else
1631 Diag(Tok, diag::err_expected_colon);
1632 ConsumeToken();
1633 continue;
1634 }
1635
1636 // FIXME: Make sure we don't have a template here.
1637
1638 // Parse all the comma separated declarators.
1639 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001640 }
1641
Douglas Gregor9377c822010-06-21 22:31:09 +00001642 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1643 } else {
1644 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001645 }
Mike Stump11289f42009-09-09 15:08:12 +00001646
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001647 // If attributes exist after class contents, parse them.
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001648 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001649 if (Tok.is(tok::kw___attribute))
Douglas Gregorc48a10d2010-03-29 14:42:08 +00001650 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001651
John McCall08bede42010-05-28 08:11:17 +00001652 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001653 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall08bede42010-05-28 08:11:17 +00001654 LBraceLoc, RBraceLoc,
1655 AttrList.get());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001656
1657 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1658 // complete within function bodies, default arguments,
1659 // exception-specifications, and constructor ctor-initializers (including
1660 // such things in nested classes).
1661 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001662 // FIXME: Only function bodies and constructor ctor-initializers are
1663 // parsed correctly, fix the rest.
Douglas Gregor9377c822010-06-21 22:31:09 +00001664 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001665 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001666 // are complete and we can parse the delayed portions of method
1667 // declarations and the lexed inline method definitions.
Douglas Gregor428119e2010-06-16 23:45:56 +00001668 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001669 ParseLexedMethodDeclarations(getCurrentClass());
1670 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00001671 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001672 }
1673
John McCall08bede42010-05-28 08:11:17 +00001674 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001675 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCall2ff380a2010-03-17 00:38:33 +00001676
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001677 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001678 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001679 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001680}
Douglas Gregore8381c02008-11-05 04:29:56 +00001681
1682/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1683/// which explicitly initializes the members or base classes of a
1684/// class (C++ [class.base.init]). For example, the three initializers
1685/// after the ':' in the Derived constructor below:
1686///
1687/// @code
1688/// class Base { };
1689/// class Derived : Base {
1690/// int x;
1691/// float f;
1692/// public:
1693/// Derived(float f) : Base(), x(17), f(f) { }
1694/// };
1695/// @endcode
1696///
Mike Stump11289f42009-09-09 15:08:12 +00001697/// [C++] ctor-initializer:
1698/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001699///
Mike Stump11289f42009-09-09 15:08:12 +00001700/// [C++] mem-initializer-list:
1701/// mem-initializer
1702/// mem-initializer , mem-initializer-list
Chris Lattner83f095c2009-03-28 19:18:32 +00001703void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001704 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1705
1706 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001707
Douglas Gregore8381c02008-11-05 04:29:56 +00001708 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001709 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001710
Douglas Gregore8381c02008-11-05 04:29:56 +00001711 do {
1712 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001713 if (!MemInit.isInvalid())
1714 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001715 else
1716 AnyErrors = true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001717
Douglas Gregore8381c02008-11-05 04:29:56 +00001718 if (Tok.is(tok::comma))
1719 ConsumeToken();
1720 else if (Tok.is(tok::l_brace))
1721 break;
1722 else {
1723 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001724 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001725 SkipUntil(tok::l_brace, true, true);
1726 break;
1727 }
1728 } while (true);
1729
Mike Stump11289f42009-09-09 15:08:12 +00001730 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001731 MemInitializers.data(), MemInitializers.size(),
1732 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00001733}
1734
1735/// ParseMemInitializer - Parse a C++ member initializer, which is
1736/// part of a constructor initializer that explicitly initializes one
1737/// member or base class (C++ [class.base.init]). See
1738/// ParseConstructorInitializer for an example.
1739///
1740/// [C++] mem-initializer:
1741/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001742///
Douglas Gregore8381c02008-11-05 04:29:56 +00001743/// [C++] mem-initializer-id:
1744/// '::'[opt] nested-name-specifier[opt] class-name
1745/// identifier
Chris Lattner83f095c2009-03-28 19:18:32 +00001746Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001747 // parse '::'[opt] nested-name-specifier[opt]
1748 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001749 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001750 TypeTy *TemplateTypeTy = 0;
1751 if (Tok.is(tok::annot_template_id)) {
1752 TemplateIdAnnotation *TemplateId
1753 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +00001754 if (TemplateId->Kind == TNK_Type_template ||
1755 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001756 AnnotateTemplateIdTokenAsType(&SS);
1757 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1758 TemplateTypeTy = Tok.getAnnotationValue();
1759 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001760 }
1761 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001762 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001763 return true;
1764 }
Mike Stump11289f42009-09-09 15:08:12 +00001765
Douglas Gregore8381c02008-11-05 04:29:56 +00001766 // Get the identifier. This may be a member name or a class name,
1767 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001768 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001769 SourceLocation IdLoc = ConsumeToken();
1770
1771 // Parse the '('.
1772 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001773 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001774 return true;
1775 }
1776 SourceLocation LParenLoc = ConsumeParen();
1777
1778 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001779 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001780 CommaLocsTy CommaLocs;
1781 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1782 SkipUntil(tok::r_paren);
1783 return true;
1784 }
1785
1786 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1787
Douglas Gregor0be31a22010-07-02 17:43:08 +00001788 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001789 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001790 LParenLoc, ArgExprs.take(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001791 ArgExprs.size(), CommaLocs.data(),
1792 RParenLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00001793}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001794
1795/// ParseExceptionSpecification - Parse a C++ exception-specification
1796/// (C++ [except.spec]).
1797///
Douglas Gregor356513d2008-12-01 18:00:20 +00001798/// exception-specification:
1799/// 'throw' '(' type-id-list [opt] ')'
1800/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00001801///
Douglas Gregor356513d2008-12-01 18:00:20 +00001802/// type-id-list:
1803/// type-id
1804/// type-id-list ',' type-id
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001805///
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001806bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redld6434562009-05-29 18:02:33 +00001807 llvm::SmallVector<TypeTy*, 2>
1808 &Exceptions,
1809 llvm::SmallVector<SourceRange, 2>
1810 &Ranges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001811 bool &hasAnyExceptionSpec) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001812 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00001813
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001814 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001815
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001816 if (!Tok.is(tok::l_paren)) {
1817 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1818 }
1819 SourceLocation LParenLoc = ConsumeParen();
1820
Douglas Gregor356513d2008-12-01 18:00:20 +00001821 // Parse throw(...), a Microsoft extension that means "this function
1822 // can throw anything".
1823 if (Tok.is(tok::ellipsis)) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001824 hasAnyExceptionSpec = true;
Douglas Gregor356513d2008-12-01 18:00:20 +00001825 SourceLocation EllipsisLoc = ConsumeToken();
1826 if (!getLang().Microsoft)
1827 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001828 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor356513d2008-12-01 18:00:20 +00001829 return false;
1830 }
1831
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001832 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00001833 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001834 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00001835 TypeResult Res(ParseTypeName(&Range));
1836 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001837 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00001838 Ranges.push_back(Range);
1839 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001840 if (Tok.is(tok::comma))
1841 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001842 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001843 break;
1844 }
1845
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001846 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001847 return false;
1848}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001849
1850/// \brief We have just started parsing the definition of a new class,
1851/// so push that class onto our stack of classes that is currently
1852/// being parsed.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001853void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1854 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001855 "Nested class without outer class");
Douglas Gregoredf8f392010-01-16 20:52:59 +00001856 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001857}
1858
1859/// \brief Deallocate the given parsed class and all of its nested
1860/// classes.
1861void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1862 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1863 DeallocateParsedClasses(Class->NestedClasses[I]);
1864 delete Class;
1865}
1866
1867/// \brief Pop the top class of the stack of classes that are
1868/// currently being parsed.
1869///
1870/// This routine should be called when we have finished parsing the
1871/// definition of a class, but have not yet popped the Scope
1872/// associated with the class's definition.
1873///
1874/// \returns true if the class we've popped is a top-level class,
1875/// false otherwise.
1876void Parser::PopParsingClass() {
1877 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00001878
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001879 ParsingClass *Victim = ClassStack.top();
1880 ClassStack.pop();
1881 if (Victim->TopLevelClass) {
1882 // Deallocate all of the nested classes of this class,
1883 // recursively: we don't need to keep any of this information.
1884 DeallocateParsedClasses(Victim);
1885 return;
Mike Stump11289f42009-09-09 15:08:12 +00001886 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001887 assert(!ClassStack.empty() && "Missing top-level class?");
1888
1889 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1890 Victim->NestedClasses.empty()) {
1891 // The victim is a nested class, but we will not need to perform
1892 // any processing after the definition of this class since it has
1893 // no members whose handling was delayed. Therefore, we can just
1894 // remove this nested class.
1895 delete Victim;
1896 return;
1897 }
1898
1899 // This nested class has some members that will need to be processed
1900 // after the top-level class is completely defined. Therefore, add
1901 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001902 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001903 ClassStack.top()->NestedClasses.push_back(Victim);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001904 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001905}
Alexis Hunt96d5c762009-11-21 08:43:09 +00001906
1907/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1908/// parses standard attributes.
1909///
1910/// [C++0x] attribute-specifier:
1911/// '[' '[' attribute-list ']' ']'
1912///
1913/// [C++0x] attribute-list:
1914/// attribute[opt]
1915/// attribute-list ',' attribute[opt]
1916///
1917/// [C++0x] attribute:
1918/// attribute-token attribute-argument-clause[opt]
1919///
1920/// [C++0x] attribute-token:
1921/// identifier
1922/// attribute-scoped-token
1923///
1924/// [C++0x] attribute-scoped-token:
1925/// attribute-namespace '::' identifier
1926///
1927/// [C++0x] attribute-namespace:
1928/// identifier
1929///
1930/// [C++0x] attribute-argument-clause:
1931/// '(' balanced-token-seq ')'
1932///
1933/// [C++0x] balanced-token-seq:
1934/// balanced-token
1935/// balanced-token-seq balanced-token
1936///
1937/// [C++0x] balanced-token:
1938/// '(' balanced-token-seq ')'
1939/// '[' balanced-token-seq ']'
1940/// '{' balanced-token-seq '}'
1941/// any token but '(', ')', '[', ']', '{', or '}'
1942CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1943 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1944 && "Not a C++0x attribute list");
1945
1946 SourceLocation StartLoc = Tok.getLocation(), Loc;
1947 AttributeList *CurrAttr = 0;
1948
1949 ConsumeBracket();
1950 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001951
Alexis Hunt96d5c762009-11-21 08:43:09 +00001952 if (Tok.is(tok::comma)) {
1953 Diag(Tok.getLocation(), diag::err_expected_ident);
1954 ConsumeToken();
1955 }
1956
1957 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1958 // attribute not present
1959 if (Tok.is(tok::comma)) {
1960 ConsumeToken();
1961 continue;
1962 }
1963
1964 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1965 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001966
Alexis Hunt96d5c762009-11-21 08:43:09 +00001967 // scoped attribute
1968 if (Tok.is(tok::coloncolon)) {
1969 ConsumeToken();
1970
1971 if (!Tok.is(tok::identifier)) {
1972 Diag(Tok.getLocation(), diag::err_expected_ident);
1973 SkipUntil(tok::r_square, tok::comma, true, true);
1974 continue;
1975 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001976
Alexis Hunt96d5c762009-11-21 08:43:09 +00001977 ScopeName = AttrName;
1978 ScopeLoc = AttrLoc;
1979
1980 AttrName = Tok.getIdentifierInfo();
1981 AttrLoc = ConsumeToken();
1982 }
1983
1984 bool AttrParsed = false;
1985 // No scoped names are supported; ideally we could put all non-standard
1986 // attributes into namespaces.
1987 if (!ScopeName) {
1988 switch(AttributeList::getKind(AttrName))
1989 {
1990 // No arguments
Alexis Hunt54a02542009-11-25 04:20:27 +00001991 case AttributeList::AT_base_check:
1992 case AttributeList::AT_carries_dependency:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001993 case AttributeList::AT_final:
Alexis Hunt54a02542009-11-25 04:20:27 +00001994 case AttributeList::AT_hiding:
1995 case AttributeList::AT_noreturn:
1996 case AttributeList::AT_override: {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001997 if (Tok.is(tok::l_paren)) {
1998 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1999 << AttrName->getName();
2000 break;
2001 }
2002
2003 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
2004 SourceLocation(), 0, 0, CurrAttr, false,
2005 true);
2006 AttrParsed = true;
2007 break;
2008 }
2009
2010 // One argument; must be a type-id or assignment-expression
2011 case AttributeList::AT_aligned: {
2012 if (Tok.isNot(tok::l_paren)) {
2013 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2014 << AttrName->getName();
2015 break;
2016 }
2017 SourceLocation ParamLoc = ConsumeParen();
2018
2019 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
2020
2021 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2022
2023 ExprVector ArgExprs(Actions);
2024 ArgExprs.push_back(ArgExpr.release());
2025 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
2026 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2027 false, true);
2028
2029 AttrParsed = true;
2030 break;
2031 }
2032
2033 // Silence warnings
2034 default: break;
2035 }
2036 }
2037
2038 // Skip the entire parameter clause, if any
2039 if (!AttrParsed && Tok.is(tok::l_paren)) {
2040 ConsumeParen();
2041 // SkipUntil maintains the balancedness of tokens.
2042 SkipUntil(tok::r_paren, false);
2043 }
2044 }
2045
2046 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2047 SkipUntil(tok::r_square, false);
2048 Loc = Tok.getLocation();
2049 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2050 SkipUntil(tok::r_square, false);
2051
2052 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2053 return Attr;
2054}
2055
2056/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2057/// attribute.
2058///
2059/// FIXME: Simply returns an alignof() expression if the argument is a
2060/// type. Ideally, the type should be propagated directly into Sema.
2061///
2062/// [C++0x] 'align' '(' type-id ')'
2063/// [C++0x] 'align' '(' assignment-expression ')'
2064Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
2065 if (isTypeIdInParens()) {
2066 EnterExpressionEvaluationContext Unevaluated(Actions,
2067 Action::Unevaluated);
2068 SourceLocation TypeLoc = Tok.getLocation();
2069 TypeTy *Ty = ParseTypeName().get();
2070 SourceRange TypeRange(Start, Tok.getLocation());
Nick Lewycky19b9f952010-07-26 16:56:01 +00002071 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty, TypeRange);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002072 } else
2073 return ParseConstantExpression();
2074}