blob: 7e59170b370e9cd96c5c19ce4b8596e6099d8ee0 [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson74d7f0d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor423984d2008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor556877c2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattnera5235172007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000019#include "clang/Parse/Template.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattnera5235172007-08-25 06:57:03 +000021using namespace clang;
22
23/// ParseNamespace - We know that the current token is a namespace keyword. This
24/// may either be a top level namespace or a block-level namespace alias.
25///
26/// namespace-definition: [C++ 7.3: basic.namespace]
27/// named-namespace-definition
28/// unnamed-namespace-definition
29///
30/// unnamed-namespace-definition:
31/// 'namespace' attributes[opt] '{' namespace-body '}'
32///
33/// named-namespace-definition:
34/// original-namespace-definition
35/// extension-namespace-definition
36///
37/// original-namespace-definition:
38/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
39///
40/// extension-namespace-definition:
41/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000042///
Chris Lattnera5235172007-08-25 06:57:03 +000043/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
44/// 'namespace' identifier '=' qualified-namespace-specifier ';'
45///
Chris Lattner49836b42009-04-02 04:16:50 +000046Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
47 SourceLocation &DeclEnd) {
Chris Lattner76c72282007-10-09 17:33:22 +000048 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000049 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump11289f42009-09-09 15:08:12 +000050
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000051 if (Tok.is(tok::code_completion)) {
52 Actions.CodeCompleteNamespaceDecl(CurScope);
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 Gregor05cfc292010-05-14 05:08:22 +000090 if (CurScope->isClassScope() || CurScope->isTemplateParamScope() ||
91 CurScope->isInObjcMethodScope() || CurScope->getBlockParent() ||
92 CurScope->getFnParent()) {
93 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 =
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000102 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace,
103 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)) {
138 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
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
162 return Actions.ActOnNamespaceAliasDef(CurScope, 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
187 = Actions.ActOnStartLinkageSpecification(CurScope,
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);
Mike Stump11289f42009-09-09 15:08:12 +0000200 return Actions.ActOnFinishLinkageSpecification(CurScope, 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 Gregor07665a62009-01-05 19:45:36 +0000219 return Actions.ActOnFinishLinkageSpecification(CurScope, 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)) {
233 Actions.CodeCompleteUsing(CurScope);
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)) {
270 Actions.CodeCompleteUsingDirective(CurScope);
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
307 return Actions.ActOnUsingDirective(CurScope, 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
John McCalla0097262009-12-11 02:10:03 +0000371 return Actions.ActOnUsingDeclaration(CurScope, 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;
511 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, CurScope,
512 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 Gregor18473f32010-01-12 21:28:44 +0000545 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, CurScope, 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;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000553 return Type;
554}
555
Douglas Gregor556877c2008-04-13 21:30:24 +0000556/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
557/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
558/// until we reach the start of a definition or see a token that
Sebastian Redl2b372722010-02-03 21:21:43 +0000559/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregor556877c2008-04-13 21:30:24 +0000560///
561/// class-specifier: [C++ class]
562/// class-head '{' member-specification[opt] '}'
563/// class-head '{' member-specification[opt] '}' attributes[opt]
564/// class-head:
565/// class-key identifier[opt] base-clause[opt]
566/// class-key nested-name-specifier identifier base-clause[opt]
567/// class-key nested-name-specifier[opt] simple-template-id
568/// base-clause[opt]
569/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000570/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000571/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000572/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000573/// simple-template-id base-clause[opt]
574/// class-key:
575/// 'class'
576/// 'struct'
577/// 'union'
578///
579/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000580/// class-key ::[opt] nested-name-specifier[opt] identifier
581/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
582/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000583///
584/// Note that the C++ class-specifier and elaborated-type-specifier,
585/// together, subsume the C99 struct-or-union-specifier:
586///
587/// struct-or-union-specifier: [C99 6.7.2.1]
588/// struct-or-union identifier[opt] '{' struct-contents '}'
589/// struct-or-union identifier
590/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
591/// '}' attributes[opt]
592/// [GNU] struct-or-union attributes[opt] identifier
593/// struct-or-union:
594/// 'struct'
595/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000596void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
597 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000598 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redl2b372722010-02-03 21:21:43 +0000599 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000600 DeclSpec::TST TagType;
601 if (TagTokKind == tok::kw_struct)
602 TagType = DeclSpec::TST_struct;
603 else if (TagTokKind == tok::kw_class)
604 TagType = DeclSpec::TST_class;
605 else {
606 assert(TagTokKind == tok::kw_union && "Not a class specifier");
607 TagType = DeclSpec::TST_union;
608 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000609
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000610 if (Tok.is(tok::code_completion)) {
611 // Code completion for a struct, class, or union name.
612 Actions.CodeCompleteTag(CurScope, TagType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000613 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000614 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000615
Alexis Hunt96d5c762009-11-21 08:43:09 +0000616 AttributeList *AttrList = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000617 // If attributes exist after tag, parse them.
618 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000619 AttrList = ParseGNUAttributes();
Douglas Gregor556877c2008-04-13 21:30:24 +0000620
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000621 // If declspecs exist after tag, parse them.
Eli Friedman53339e02009-06-08 23:27:34 +0000622 if (Tok.is(tok::kw___declspec))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000623 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000624
Alexis Hunt96d5c762009-11-21 08:43:09 +0000625 // If C++0x attributes exist here, parse them.
626 // FIXME: Are we consistent with the ordering of parsing of different
627 // styles of attributes?
628 if (isCXX0XAttributeSpecifier())
629 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregor119b0c72009-09-04 05:53:02 +0000631 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
632 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
633 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000634 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000635 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
636 // properly.
637 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
638 Tok.setKind(tok::identifier);
639 }
640
641 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
642 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
643 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000644 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000645 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
646 // properly.
647 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
648 Tok.setKind(tok::identifier);
649 }
Mike Stump11289f42009-09-09 15:08:12 +0000650
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000651 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +0000652 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000653 if (getLang().CPlusPlus) {
654 // "FOO : BAR" is not a potential typo for "FOO::BAR".
655 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000656
John McCall1f476a12010-02-26 08:45:28 +0000657 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
658 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000659 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
660 Diag(Tok, diag::err_expected_ident);
661 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000662
Douglas Gregor916462b2009-10-30 21:46:58 +0000663 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
664
Douglas Gregor67a65642009-02-17 23:15:12 +0000665 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000666 IdentifierInfo *Name = 0;
667 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000668 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000669 if (Tok.is(tok::identifier)) {
670 Name = Tok.getIdentifierInfo();
671 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000672
Douglas Gregord5a479c2010-05-30 22:30:21 +0000673 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000674 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +0000675 // Eat the template argument list and try to continue parsing this as
676 // a class (or template thereof).
677 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000678 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000679 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor916462b2009-10-30 21:46:58 +0000680 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000681 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000682 // We couldn't parse the template argument list at all, so don't
683 // try to give any location information for the list.
684 LAngleLoc = RAngleLoc = SourceLocation();
685 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000686
Douglas Gregor916462b2009-10-30 21:46:58 +0000687 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000688 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000689 << (TagType == DeclSpec::TST_class? 0
690 : TagType == DeclSpec::TST_struct? 1
691 : 2)
692 << Name
693 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000694
695 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000696 // we've removed its template argument list.
697 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
698 if (TemplateParams && TemplateParams->size() > 1) {
699 TemplateParams->pop_back();
700 } else {
701 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000702 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000703 = ParsedTemplateInfo::NonTemplate;
704 }
705 } else if (TemplateInfo.Kind
706 == ParsedTemplateInfo::ExplicitInstantiation) {
707 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000708 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000709 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +0000710 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000711 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000712 = SourceLocation();
713 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
714 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000715 }
Douglas Gregor916462b2009-10-30 21:46:58 +0000716 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000717 } else if (Tok.is(tok::annot_template_id)) {
718 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
719 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000720
Douglas Gregorb67535d2009-03-31 00:43:58 +0000721 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000722 // The template-name in the simple-template-id refers to
723 // something other than a class template. Give an appropriate
724 // error message and skip to the ';'.
725 SourceRange Range(NameLoc);
726 if (SS.isNotEmpty())
727 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000728
Douglas Gregor7f741122009-02-25 19:37:18 +0000729 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
730 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000731
Douglas Gregor7f741122009-02-25 19:37:18 +0000732 DS.SetTypeSpecError();
733 SkipUntil(tok::semi, false, true);
734 TemplateId->Destroy();
735 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000736 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000737 }
738
John McCall07e91c02009-08-06 02:15:43 +0000739 // There are four options here. If we have 'struct foo;', then this
740 // is either a forward declaration or a friend declaration, which
741 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor7f741122009-02-25 19:37:18 +0000742 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregor556877c2008-04-13 21:30:24 +0000743 // something like 'struct foo xyz', a reference.
Sebastian Redl2b372722010-02-03 21:21:43 +0000744 // However, in some contexts, things look like declarations but are just
745 // references, e.g.
746 // new struct s;
747 // or
748 // &T::operator struct s;
749 // For these, SuppressDeclarations is true.
John McCall9bb74a52009-07-31 02:45:11 +0000750 Action::TagUseKind TUK;
Sebastian Redl2b372722010-02-03 21:21:43 +0000751 if (SuppressDeclarations)
752 TUK = Action::TUK_Reference;
753 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregor3dad8422009-09-26 06:47:28 +0000754 if (DS.isFriendSpecified()) {
755 // C++ [class.friend]p2:
756 // A class shall not be defined in a friend declaration.
757 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
758 << SourceRange(DS.getFriendSpecLoc());
759
760 // Skip everything up to the semicolon, so that this looks like a proper
761 // friend class (or template thereof) declaration.
762 SkipUntil(tok::semi, true, true);
763 TUK = Action::TUK_Friend;
764 } else {
765 // Okay, this is a class definition.
766 TUK = Action::TUK_Definition;
767 }
768 } else if (Tok.is(tok::semi))
John McCall07e91c02009-08-06 02:15:43 +0000769 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000770 else
John McCall9bb74a52009-07-31 02:45:11 +0000771 TUK = Action::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000772
John McCall9bb74a52009-07-31 02:45:11 +0000773 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregor556877c2008-04-13 21:30:24 +0000774 // We have a declaration or reference to an anonymous class.
Chris Lattner6d29c102008-11-18 07:48:38 +0000775 Diag(StartLoc, diag::err_anon_type_definition)
776 << DeclSpec::getSpecifierName(TagType);
Douglas Gregor556877c2008-04-13 21:30:24 +0000777
Douglas Gregor556877c2008-04-13 21:30:24 +0000778 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000779
780 if (TemplateId)
781 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000782 return;
783 }
784
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000785 // Create the tag portion of the class or class template.
John McCall7f41d982009-09-11 04:59:25 +0000786 Action::DeclResult TagOrTempResult = true; // invalid
787 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000788
Douglas Gregord6ab8742009-05-28 23:31:59 +0000789 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000790 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000791 // Explicit specialization, class template partial specialization,
792 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000793 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000794 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000795 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000796 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000797 TUK == Action::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000798 // This is an explicit instantiation of a class template.
799 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000800 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000801 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000802 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000803 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000804 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000805 SS,
Mike Stump11289f42009-09-09 15:08:12 +0000806 TemplateTy::make(TemplateId->Template),
807 TemplateId->TemplateNameLoc,
808 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000809 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000810 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000811 AttrList);
John McCallb7c5c272010-04-14 00:24:33 +0000812
813 // Friend template-ids are treated as references unless
814 // they have template headers, in which case they're ill-formed
815 // (FIXME: "template <class T> friend class A<T>::B<int>;").
816 // We diagnose this error in ActOnClassTemplateSpecialization.
817 } else if (TUK == Action::TUK_Reference ||
818 (TUK == Action::TUK_Friend &&
819 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCall7f41d982009-09-11 04:59:25 +0000820 TypeResult
John McCalld8fe9af2009-09-08 17:47:29 +0000821 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
822 TemplateId->TemplateNameLoc,
823 TemplateId->LAngleLoc,
824 TemplateArgsPtr,
John McCalld8fe9af2009-09-08 17:47:29 +0000825 TemplateId->RAngleLoc);
826
John McCall7f41d982009-09-11 04:59:25 +0000827 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
828 TagType, StartLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000829 } else {
830 // This is an explicit specialization or a class template
831 // partial specialization.
832 TemplateParameterLists FakedParamLists;
833
834 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
835 // This looks like an explicit instantiation, because we have
836 // something like
837 //
838 // template class Foo<X>
839 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000840 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000841 // meant to be an explicit specialization, but the user forgot
842 // the '<>' after 'template'.
John McCall9bb74a52009-07-31 02:45:11 +0000843 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000844
Mike Stump11289f42009-09-09 15:08:12 +0000845 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000846 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000847 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000848 diag::err_explicit_instantiation_with_definition)
849 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregora771f462010-03-31 17:46:05 +0000850 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000851
852 // Create a fake template parameter list that contains only
853 // "template<>", so that we treat this construct as a class
854 // template specialization.
855 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000856 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000857 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000858 LAngleLoc,
859 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000860 LAngleLoc));
861 TemplateParams = &FakedParamLists;
862 }
863
864 // Build the class template specialization.
865 TagOrTempResult
John McCall9bb74a52009-07-31 02:45:11 +0000866 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000867 StartLoc, SS,
Mike Stump11289f42009-09-09 15:08:12 +0000868 TemplateTy::make(TemplateId->Template),
869 TemplateId->TemplateNameLoc,
870 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000871 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000872 TemplateId->RAngleLoc,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000873 AttrList,
Mike Stump11289f42009-09-09 15:08:12 +0000874 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000875 TemplateParams? &(*TemplateParams)[0] : 0,
876 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000877 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000878 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000879 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000880 TUK == Action::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000881 // Explicit instantiation of a member of a class template
882 // specialization, e.g.,
883 //
884 // template struct Outer<int>::Inner;
885 //
886 TagOrTempResult
Mike Stump11289f42009-09-09 15:08:12 +0000887 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor43e75172009-09-04 06:33:52 +0000888 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000889 TemplateInfo.TemplateLoc,
890 TagType, StartLoc, SS, Name,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000891 NameLoc, AttrList);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000892 } else {
893 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall9bb74a52009-07-31 02:45:11 +0000894 TUK == Action::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000895 // FIXME: Diagnose this particular error.
896 }
897
John McCall7f41d982009-09-11 04:59:25 +0000898 bool IsDependent = false;
899
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000900 // Declaration or definition of a class type
Mike Stump11289f42009-09-09 15:08:12 +0000901 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000902 Name, NameLoc, AttrList, AS,
Mike Stump11289f42009-09-09 15:08:12 +0000903 Action::MultiTemplateParamsArg(Actions,
Douglas Gregore93e46c2009-07-22 23:48:44 +0000904 TemplateParams? &(*TemplateParams)[0] : 0,
905 TemplateParams? TemplateParams->size() : 0),
John McCall7f41d982009-09-11 04:59:25 +0000906 Owned, IsDependent);
907
908 // If ActOnTag said the type was dependent, try again with the
909 // less common call.
910 if (IsDependent)
911 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000912 SS, Name, StartLoc, NameLoc);
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000913 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000914
Douglas Gregor556877c2008-04-13 21:30:24 +0000915 // If there is a body, parse it and inform the actions module.
John McCall2d814c32009-12-19 21:48:58 +0000916 if (TUK == Action::TUK_Definition) {
917 assert(Tok.is(tok::l_brace) ||
918 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000919 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +0000920 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000921 else
Douglas Gregorc08f4892009-03-25 00:13:59 +0000922 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +0000923 }
924
John McCall7f41d982009-09-11 04:59:25 +0000925 void *Result;
926 if (!TypeResult.isInvalid()) {
927 TagType = DeclSpec::TST_typename;
928 Result = TypeResult.get();
929 Owned = false;
930 } else if (!TagOrTempResult.isInvalid()) {
931 Result = TagOrTempResult.get().getAs<void>();
932 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000933 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +0000934 return;
935 }
Mike Stump11289f42009-09-09 15:08:12 +0000936
John McCall49bfce42009-08-03 20:12:06 +0000937 const char *PrevSpec = 0;
938 unsigned DiagID;
John McCall7f41d982009-09-11 04:59:25 +0000939
Douglas Gregor72100632010-01-25 16:33:23 +0000940 // FIXME: The DeclSpec should keep the locations of both the keyword and the
941 // name (if there is one).
942 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000943
Douglas Gregor72100632010-01-25 16:33:23 +0000944 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCall7f41d982009-09-11 04:59:25 +0000945 Result, Owned))
John McCall49bfce42009-08-03 20:12:06 +0000946 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000947
Chris Lattnercf251412010-02-02 01:23:29 +0000948 // At this point, we've successfully parsed a class-specifier in 'definition'
949 // form (e.g. "struct foo { int x; }". While we could just return here, we're
950 // going to look at what comes after it to improve error recovery. If an
951 // impossible token occurs next, we assume that the programmer forgot a ; at
952 // the end of the declaration and recover that way.
953 //
954 // This switch enumerates the valid "follow" set for definition.
955 if (TUK == Action::TUK_Definition) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +0000956 bool ExpectedSemi = true;
Chris Lattnercf251412010-02-02 01:23:29 +0000957 switch (Tok.getKind()) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +0000958 default: break;
Chris Lattnercf251412010-02-02 01:23:29 +0000959 case tok::semi: // struct foo {...} ;
Chris Lattnerafe6a842010-02-02 17:32:27 +0000960 case tok::star: // struct foo {...} * P;
961 case tok::amp: // struct foo {...} & R = ...
962 case tok::identifier: // struct foo {...} V ;
963 case tok::r_paren: //(struct foo {...} ) {4}
964 case tok::annot_cxxscope: // struct foo {...} a:: b;
965 case tok::annot_typename: // struct foo {...} a ::b;
966 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattner5e854b92010-02-03 20:41:24 +0000967 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner35af0ab2010-02-03 01:45:03 +0000968 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerfd48afe2010-02-28 18:18:36 +0000969 ExpectedSemi = false;
970 break;
971 // Type qualifiers
972 case tok::kw_const: // struct foo {...} const x;
973 case tok::kw_volatile: // struct foo {...} volatile x;
974 case tok::kw_restrict: // struct foo {...} restrict x;
975 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattnerafe6a842010-02-02 17:32:27 +0000976 // Storage-class specifiers
977 case tok::kw_static: // struct foo {...} static x;
978 case tok::kw_extern: // struct foo {...} extern x;
979 case tok::kw_typedef: // struct foo {...} typedef x;
980 case tok::kw_register: // struct foo {...} register x;
981 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregorc9a99c52010-05-17 18:19:56 +0000982 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerfd48afe2010-02-28 18:18:36 +0000983 // As shown above, type qualifiers and storage class specifiers absolutely
984 // can occur after class specifiers according to the grammar. However,
985 // almost noone actually writes code like this. If we see one of these,
986 // it is much more likely that someone missed a semi colon and the
987 // type/storage class specifier we're seeing is part of the *next*
988 // intended declaration, as in:
989 //
990 // struct foo { ... }
991 // typedef int X;
992 //
993 // We'd really like to emit a missing semicolon error instead of emitting
994 // an error on the 'int' saying that you can't have two type specifiers in
995 // the same declaration of X. Because of this, we look ahead past this
996 // token to see if it's a type specifier. If so, we know the code is
997 // otherwise invalid, so we can produce the expected semi error.
998 if (!isKnownToBeTypeSpecifier(NextToken()))
999 ExpectedSemi = false;
Chris Lattnercf251412010-02-02 01:23:29 +00001000 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001001
1002 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattnercf251412010-02-02 01:23:29 +00001003 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001004 if (!getLang().CPlusPlus)
1005 ExpectedSemi = false;
1006 break;
1007 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001008
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001009 if (ExpectedSemi) {
Chris Lattnercf251412010-02-02 01:23:29 +00001010 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1011 TagType == DeclSpec::TST_class ? "class"
1012 : TagType == DeclSpec::TST_struct? "struct" : "union");
1013 // Push this token back into the preprocessor and change our current token
1014 // to ';' so that the rest of the code recovers as though there were an
1015 // ';' after the definition.
1016 PP.EnterToken(Tok);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001017 Tok.setKind(tok::semi);
Chris Lattnercf251412010-02-02 01:23:29 +00001018 }
1019 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001020}
1021
Mike Stump11289f42009-09-09 15:08:12 +00001022/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001023///
1024/// base-clause : [C++ class.derived]
1025/// ':' base-specifier-list
1026/// base-specifier-list:
1027/// base-specifier '...'[opt]
1028/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner83f095c2009-03-28 19:18:32 +00001029void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001030 assert(Tok.is(tok::colon) && "Not a base clause");
1031 ConsumeToken();
1032
Douglas Gregor29a92472008-10-22 17:49:05 +00001033 // Build up an array of parsed base specifiers.
1034 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1035
Douglas Gregor556877c2008-04-13 21:30:24 +00001036 while (true) {
1037 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001038 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001039 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001040 // Skip the rest of this base specifier, up until the comma or
1041 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001042 SkipUntil(tok::comma, tok::l_brace, true, true);
1043 } else {
1044 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001045 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001046 }
1047
1048 // If the next token is a comma, consume it and keep reading
1049 // base-specifiers.
1050 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001051
Douglas Gregor556877c2008-04-13 21:30:24 +00001052 // Consume the comma.
1053 ConsumeToken();
1054 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001055
1056 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001057 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001058}
1059
1060/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1061/// one entry in the base class list of a class specifier, for example:
1062/// class foo : public bar, virtual private baz {
1063/// 'public bar' and 'virtual private baz' are each base-specifiers.
1064///
1065/// base-specifier: [C++ class.derived]
1066/// ::[opt] nested-name-specifier[opt] class-name
1067/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1068/// class-name
1069/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1070/// class-name
Chris Lattner83f095c2009-03-28 19:18:32 +00001071Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001072 bool IsVirtual = false;
1073 SourceLocation StartLoc = Tok.getLocation();
1074
1075 // Parse the 'virtual' keyword.
1076 if (Tok.is(tok::kw_virtual)) {
1077 ConsumeToken();
1078 IsVirtual = true;
1079 }
1080
1081 // Parse an (optional) access specifier.
1082 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001083 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001084 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001085
Douglas Gregor556877c2008-04-13 21:30:24 +00001086 // Parse the 'virtual' keyword (again!), in case it came after the
1087 // access specifier.
1088 if (Tok.is(tok::kw_virtual)) {
1089 SourceLocation VirtualLoc = ConsumeToken();
1090 if (IsVirtual) {
1091 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001092 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001093 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001094 }
1095
1096 IsVirtual = true;
1097 }
1098
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001099 // Parse optional '::' and optional nested-name-specifier.
1100 CXXScopeSpec SS;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001101 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
Douglas Gregor53db22c2010-03-02 00:25:00 +00001102 /*EnteringContext=*/false);
Douglas Gregor556877c2008-04-13 21:30:24 +00001103
Douglas Gregor556877c2008-04-13 21:30:24 +00001104 // The location of the base class itself.
1105 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +00001106
1107 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001108 SourceLocation EndLocation;
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001109 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1110 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001111 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001112
1113 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001114 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001115
Douglas Gregor556877c2008-04-13 21:30:24 +00001116 // Notify semantic analysis that we have parsed a complete
1117 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +00001118 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001119 BaseType.get(), BaseLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001120}
1121
1122/// getAccessSpecifierIfPresent - Determine whether the next token is
1123/// a C++ access-specifier.
1124///
1125/// access-specifier: [C++ class.derived]
1126/// 'private'
1127/// 'protected'
1128/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001129AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001130 switch (Tok.getKind()) {
1131 default: return AS_none;
1132 case tok::kw_private: return AS_private;
1133 case tok::kw_protected: return AS_protected;
1134 case tok::kw_public: return AS_public;
1135 }
1136}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001137
Eli Friedman3af2a772009-07-22 21:45:50 +00001138void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1139 DeclPtrTy ThisDecl) {
1140 // We just declared a member function. If this member function
1141 // has any default arguments, we'll need to parse them later.
1142 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001143 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedman3af2a772009-07-22 21:45:50 +00001144 = DeclaratorInfo.getTypeObject(0).Fun;
1145 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1146 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1147 if (!LateMethod) {
1148 // Push this method onto the stack of late-parsed method
1149 // declarations.
1150 getCurrentClass().MethodDecls.push_back(
1151 LateParsedMethodDeclaration(ThisDecl));
1152 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregorc45a40a2009-08-22 00:34:47 +00001153 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001154
1155 // Add all of the parameters prior to this one (they don't
1156 // have default arguments).
1157 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1158 for (unsigned I = 0; I < ParamIdx; ++I)
1159 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001160 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001161 }
1162
1163 // Add this parameter to the list of parameters (it or may
1164 // not have a default argument).
1165 LateMethod->DefaultArgs.push_back(
1166 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1167 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1168 }
1169 }
1170}
1171
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001172/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1173///
1174/// member-declaration:
1175/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1176/// function-definition ';'[opt]
1177/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1178/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001179/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001180/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001181/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001182///
1183/// member-declarator-list:
1184/// member-declarator
1185/// member-declarator-list ',' member-declarator
1186///
1187/// member-declarator:
1188/// declarator pure-specifier[opt]
1189/// declarator constant-initializer[opt]
1190/// identifier[opt] ':' constant-expression
1191///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001192/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001193/// '= 0'
1194///
1195/// constant-initializer:
1196/// '=' constant-expression
1197///
Douglas Gregor3447e762009-08-20 22:52:58 +00001198void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1199 const ParsedTemplateInfo &TemplateInfo) {
John McCalla0097262009-12-11 02:10:03 +00001200 // Access declarations.
1201 if (!TemplateInfo.Kind &&
1202 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall1f476a12010-02-26 08:45:28 +00001203 !TryAnnotateCXXScopeToken() &&
John McCalla0097262009-12-11 02:10:03 +00001204 Tok.is(tok::annot_cxxscope)) {
1205 bool isAccessDecl = false;
1206 if (NextToken().is(tok::identifier))
1207 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1208 else
1209 isAccessDecl = NextToken().is(tok::kw_operator);
1210
1211 if (isAccessDecl) {
1212 // Collect the scope specifier token we annotated earlier.
1213 CXXScopeSpec SS;
1214 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1215
1216 // Try to parse an unqualified-id.
1217 UnqualifiedId Name;
1218 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1219 SkipUntil(tok::semi);
1220 return;
1221 }
1222
1223 // TODO: recover from mistakenly-qualified operator declarations.
1224 if (ExpectAndConsume(tok::semi,
1225 diag::err_expected_semi_after,
1226 "access declaration",
1227 tok::semi))
1228 return;
1229
1230 Actions.ActOnUsingDeclaration(CurScope, AS,
1231 false, SourceLocation(),
1232 SS, Name,
1233 /* AttrList */ 0,
1234 /* IsTypeName */ false,
1235 SourceLocation());
1236 return;
1237 }
1238 }
1239
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001240 // static_assert-declaration
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001241 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001242 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001243 SourceLocation DeclEnd;
1244 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001245 return;
1246 }
Mike Stump11289f42009-09-09 15:08:12 +00001247
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001248 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001249 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001250 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001251 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001252 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001253 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001254 return;
1255 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001256
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001257 // Handle: member-declaration ::= '__extension__' member-declaration
1258 if (Tok.is(tok::kw___extension__)) {
1259 // __extension__ silences extension warnings in the subexpression.
1260 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1261 ConsumeToken();
Douglas Gregor3447e762009-08-20 22:52:58 +00001262 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001263 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001264
Chris Lattnercf251412010-02-02 01:23:29 +00001265 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1266 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001267 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001268
Alexis Hunt96d5c762009-11-21 08:43:09 +00001269 CXX0XAttributeList AttrList;
1270 // Optional C++0x attribute-specifier
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001271 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Alexis Hunt96d5c762009-11-21 08:43:09 +00001272 AttrList = ParseCXX0XAttributes();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001273
Douglas Gregorfec52632009-06-20 00:51:54 +00001274 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001275 // FIXME: Check for template aliases
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001276
Alexis Hunt96d5c762009-11-21 08:43:09 +00001277 if (AttrList.HasAttr)
1278 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1279 << AttrList.Range;
Mike Stump11289f42009-09-09 15:08:12 +00001280
Douglas Gregorfec52632009-06-20 00:51:54 +00001281 // Eat 'using'.
1282 SourceLocation UsingLoc = ConsumeToken();
1283
1284 if (Tok.is(tok::kw_namespace)) {
1285 Diag(UsingLoc, diag::err_using_namespace_in_class);
1286 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00001287 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00001288 SourceLocation DeclEnd;
1289 // Otherwise, it must be using-declaration.
Anders Carlsson7b194b72009-08-29 19:54:19 +00001290 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001291 }
1292 return;
1293 }
1294
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001295 SourceLocation DSStart = Tok.getLocation();
1296 // decl-specifier-seq:
1297 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001298 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001299 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor3447e762009-08-20 22:52:58 +00001300 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001301
John McCall11083da2009-09-16 22:47:08 +00001302 Action::MultiTemplateParamsArg TemplateParams(Actions,
1303 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1304 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1305
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001306 if (Tok.is(tok::semi)) {
1307 ConsumeToken();
John McCallb54367d2010-05-21 20:45:30 +00001308 Actions.ParsedFreeStandingDeclSpec(CurScope, AS, DS);
John McCall07e91c02009-08-06 02:15:43 +00001309 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001310 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001311
John McCall28a6aea2009-11-04 02:18:39 +00001312 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001313
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001314 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001315 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1316 ColonProtectionRAIIObject X(*this);
1317
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001318 // Parse the first declarator.
1319 ParseDeclarator(DeclaratorInfo);
1320 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001321 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001322 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001323 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001324 if (Tok.is(tok::semi))
1325 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001326 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001327 }
1328
John Thompson5bc5cbe2009-11-25 22:58:06 +00001329 // If attributes exist after the declarator, but before an '{', parse them.
1330 if (Tok.is(tok::kw___attribute)) {
1331 SourceLocation Loc;
1332 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1333 DeclaratorInfo.AddAttributes(AttrList, Loc);
1334 }
1335
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001336 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001337 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001338 || (DeclaratorInfo.isFunctionDeclarator() &&
1339 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001340 if (!DeclaratorInfo.isFunctionDeclarator()) {
1341 Diag(Tok, diag::err_func_def_no_params);
1342 ConsumeBrace();
1343 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001344 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001345 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001346
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001347 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1348 Diag(Tok, diag::err_function_declared_typedef);
1349 // This recovery skips the entire function body. It would be nice
1350 // to simply call ParseCXXInlineMethodDef() below, however Sema
1351 // assumes the declarator represents a function, not a typedef.
1352 ConsumeBrace();
1353 SkipUntil(tok::r_brace, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001354 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001355 }
1356
Douglas Gregor3447e762009-08-20 22:52:58 +00001357 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001358 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001359 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001360 }
1361
1362 // member-declarator-list:
1363 // member-declarator
1364 // member-declarator-list ',' member-declarator
1365
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001366 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001367 OwningExprResult BitfieldSize(Actions);
1368 OwningExprResult Init(Actions);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001369 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001370
1371 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001372 // member-declarator:
1373 // declarator pure-specifier[opt]
1374 // declarator constant-initializer[opt]
1375 // identifier[opt] ':' constant-expression
1376
1377 if (Tok.is(tok::colon)) {
1378 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001379 BitfieldSize = ParseConstantExpression();
1380 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001381 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001382 }
Mike Stump11289f42009-09-09 15:08:12 +00001383
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001384 // pure-specifier:
1385 // '= 0'
1386 //
1387 // constant-initializer:
1388 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001389 //
1390 // defaulted/deleted function-definition:
1391 // '=' 'default' [TODO]
1392 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001393
1394 if (Tok.is(tok::equal)) {
1395 ConsumeToken();
Sebastian Redl42e92c42009-04-12 17:16:29 +00001396 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1397 ConsumeToken();
1398 Deleted = true;
1399 } else {
1400 Init = ParseInitializer();
1401 if (Init.isInvalid())
1402 SkipUntil(tok::comma, true, true);
1403 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001404 }
1405
1406 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001407 if (Tok.is(tok::kw___attribute)) {
1408 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001409 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001410 DeclaratorInfo.AddAttributes(AttrList, Loc);
1411 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001412
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001413 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001414 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001415 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001416
1417 DeclPtrTy ThisDecl;
1418 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001419 // TODO: handle initializers, bitfields, 'delete'
1420 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1421 /*IsDefinition*/ false,
1422 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001423 } else {
John McCall07e91c02009-08-06 02:15:43 +00001424 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1425 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001426 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001427 BitfieldSize.release(),
1428 Init.release(),
Sebastian Redld6f78502009-11-24 23:38:44 +00001429 /*IsDefinition*/Deleted,
John McCall07e91c02009-08-06 02:15:43 +00001430 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001431 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001432 if (ThisDecl)
1433 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001434
Douglas Gregor4d87df52008-12-16 21:30:33 +00001435 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001436 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001437 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001438 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001439 }
1440
John McCall28a6aea2009-11-04 02:18:39 +00001441 DeclaratorInfo.complete(ThisDecl);
1442
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001443 // If we don't have a comma, it is either the end of the list (a ';')
1444 // or an error, bail out.
1445 if (Tok.isNot(tok::comma))
1446 break;
Mike Stump11289f42009-09-09 15:08:12 +00001447
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001448 // Consume the comma.
1449 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001450
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001451 // Parse the next declarator.
1452 DeclaratorInfo.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001453 BitfieldSize = 0;
1454 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001455 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001456
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001457 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001458 if (Tok.is(tok::kw___attribute)) {
1459 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001460 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001461 DeclaratorInfo.AddAttributes(AttrList, Loc);
1462 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001463
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001464 if (Tok.isNot(tok::colon))
1465 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001466 }
1467
Chris Lattner916dbf12010-02-02 00:43:15 +00001468 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1469 // Skip to end of block or statement.
1470 SkipUntil(tok::r_brace, true, true);
1471 // If we stopped at a ';', eat it.
1472 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001473 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001474 }
1475
Chris Lattner916dbf12010-02-02 00:43:15 +00001476 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
1477 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001478}
1479
1480/// ParseCXXMemberSpecification - Parse the class definition.
1481///
1482/// member-specification:
1483/// member-declaration member-specification[opt]
1484/// access-specifier ':' member-specification[opt]
1485///
1486void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001487 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001488 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001489 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001490 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001491
Chris Lattnereae6cb62009-03-05 08:00:35 +00001492 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1493 PP.getSourceManager(),
1494 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001495
Douglas Gregoredf8f392010-01-16 20:52:59 +00001496 // Determine whether this is a non-nested class. Note that local
1497 // classes are *not* considered to be nested classes.
1498 bool NonNestedClass = true;
1499 if (!ClassStack.empty()) {
1500 for (const Scope *S = CurScope; S; S = S->getParent()) {
1501 if (S->isClassScope()) {
1502 // We're inside a class scope, so this is a nested class.
1503 NonNestedClass = false;
1504 break;
1505 }
1506
1507 if ((S->getFlags() & Scope::FnScope)) {
1508 // If we're in a function or function template declared in the
1509 // body of a class, then this is a local class rather than a
1510 // nested class.
1511 const Scope *Parent = S->getParent();
1512 if (Parent->isTemplateParamScope())
1513 Parent = Parent->getParent();
1514 if (Parent->isClassScope())
1515 break;
1516 }
1517 }
1518 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001519
1520 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001521 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001522
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001523 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001524 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001525
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001526 if (TagDecl)
1527 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001528
1529 if (Tok.is(tok::colon)) {
1530 ParseBaseClause(TagDecl);
1531
1532 if (!Tok.is(tok::l_brace)) {
1533 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00001534
1535 if (TagDecl)
1536 Actions.ActOnTagDefinitionError(CurScope, TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001537 return;
1538 }
1539 }
1540
1541 assert(Tok.is(tok::l_brace));
1542
1543 SourceLocation LBraceLoc = ConsumeBrace();
1544
John McCall08bede42010-05-28 08:11:17 +00001545 if (TagDecl)
1546 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
John McCall1c7e6ec2009-12-20 07:58:13 +00001547
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001548 // C++ 11p3: Members of a class defined with the keyword class are private
1549 // by default. Members of a class defined with the keywords struct or union
1550 // are public by default.
1551 AccessSpecifier CurAS;
1552 if (TagType == DeclSpec::TST_class)
1553 CurAS = AS_private;
1554 else
1555 CurAS = AS_public;
1556
1557 // While we still have something to read, read the member-declarations.
1558 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1559 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001560
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001561 // Check for extraneous top-level semicolon.
1562 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +00001563 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001564 << FixItHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001565 ConsumeToken();
1566 continue;
1567 }
1568
1569 AccessSpecifier AS = getAccessSpecifierIfPresent();
1570 if (AS != AS_none) {
1571 // Current token is a C++ access specifier.
1572 CurAS = AS;
1573 ConsumeToken();
1574 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1575 continue;
1576 }
1577
Douglas Gregor3447e762009-08-20 22:52:58 +00001578 // FIXME: Make sure we don't have a template here.
Mike Stump11289f42009-09-09 15:08:12 +00001579
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001580 // Parse all the comma separated declarators.
1581 ParseCXXClassMemberDeclaration(CurAS);
1582 }
Mike Stump11289f42009-09-09 15:08:12 +00001583
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001584 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001585
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001586 // If attributes exist after class contents, parse them.
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001587 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001588 if (Tok.is(tok::kw___attribute))
Douglas Gregorc48a10d2010-03-29 14:42:08 +00001589 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001590
John McCall08bede42010-05-28 08:11:17 +00001591 if (TagDecl)
1592 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1593 LBraceLoc, RBraceLoc,
1594 AttrList.get());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001595
1596 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1597 // complete within function bodies, default arguments,
1598 // exception-specifications, and constructor ctor-initializers (including
1599 // such things in nested classes).
1600 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001601 // FIXME: Only function bodies and constructor ctor-initializers are
1602 // parsed correctly, fix the rest.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001603 if (NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001604 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001605 // are complete and we can parse the delayed portions of method
1606 // declarations and the lexed inline method definitions.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001607 ParseLexedMethodDeclarations(getCurrentClass());
1608 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001609 }
1610
John McCall08bede42010-05-28 08:11:17 +00001611 if (TagDecl)
1612 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
John McCall2ff380a2010-03-17 00:38:33 +00001613
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001614 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001615 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001616 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001617}
Douglas Gregore8381c02008-11-05 04:29:56 +00001618
1619/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1620/// which explicitly initializes the members or base classes of a
1621/// class (C++ [class.base.init]). For example, the three initializers
1622/// after the ':' in the Derived constructor below:
1623///
1624/// @code
1625/// class Base { };
1626/// class Derived : Base {
1627/// int x;
1628/// float f;
1629/// public:
1630/// Derived(float f) : Base(), x(17), f(f) { }
1631/// };
1632/// @endcode
1633///
Mike Stump11289f42009-09-09 15:08:12 +00001634/// [C++] ctor-initializer:
1635/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001636///
Mike Stump11289f42009-09-09 15:08:12 +00001637/// [C++] mem-initializer-list:
1638/// mem-initializer
1639/// mem-initializer , mem-initializer-list
Chris Lattner83f095c2009-03-28 19:18:32 +00001640void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001641 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1642
1643 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001644
Douglas Gregore8381c02008-11-05 04:29:56 +00001645 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001646 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001647
Douglas Gregore8381c02008-11-05 04:29:56 +00001648 do {
1649 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001650 if (!MemInit.isInvalid())
1651 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001652 else
1653 AnyErrors = true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001654
Douglas Gregore8381c02008-11-05 04:29:56 +00001655 if (Tok.is(tok::comma))
1656 ConsumeToken();
1657 else if (Tok.is(tok::l_brace))
1658 break;
1659 else {
1660 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001661 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001662 SkipUntil(tok::l_brace, true, true);
1663 break;
1664 }
1665 } while (true);
1666
Mike Stump11289f42009-09-09 15:08:12 +00001667 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001668 MemInitializers.data(), MemInitializers.size(),
1669 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00001670}
1671
1672/// ParseMemInitializer - Parse a C++ member initializer, which is
1673/// part of a constructor initializer that explicitly initializes one
1674/// member or base class (C++ [class.base.init]). See
1675/// ParseConstructorInitializer for an example.
1676///
1677/// [C++] mem-initializer:
1678/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001679///
Douglas Gregore8381c02008-11-05 04:29:56 +00001680/// [C++] mem-initializer-id:
1681/// '::'[opt] nested-name-specifier[opt] class-name
1682/// identifier
Chris Lattner83f095c2009-03-28 19:18:32 +00001683Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001684 // parse '::'[opt] nested-name-specifier[opt]
1685 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001686 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001687 TypeTy *TemplateTypeTy = 0;
1688 if (Tok.is(tok::annot_template_id)) {
1689 TemplateIdAnnotation *TemplateId
1690 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +00001691 if (TemplateId->Kind == TNK_Type_template ||
1692 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001693 AnnotateTemplateIdTokenAsType(&SS);
1694 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1695 TemplateTypeTy = Tok.getAnnotationValue();
1696 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001697 }
1698 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001699 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001700 return true;
1701 }
Mike Stump11289f42009-09-09 15:08:12 +00001702
Douglas Gregore8381c02008-11-05 04:29:56 +00001703 // Get the identifier. This may be a member name or a class name,
1704 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001705 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001706 SourceLocation IdLoc = ConsumeToken();
1707
1708 // Parse the '('.
1709 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001710 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001711 return true;
1712 }
1713 SourceLocation LParenLoc = ConsumeParen();
1714
1715 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001716 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001717 CommaLocsTy CommaLocs;
1718 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1719 SkipUntil(tok::r_paren);
1720 return true;
1721 }
1722
1723 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1724
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001725 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1726 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001727 LParenLoc, ArgExprs.take(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001728 ArgExprs.size(), CommaLocs.data(),
1729 RParenLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00001730}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001731
1732/// ParseExceptionSpecification - Parse a C++ exception-specification
1733/// (C++ [except.spec]).
1734///
Douglas Gregor356513d2008-12-01 18:00:20 +00001735/// exception-specification:
1736/// 'throw' '(' type-id-list [opt] ')'
1737/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00001738///
Douglas Gregor356513d2008-12-01 18:00:20 +00001739/// type-id-list:
1740/// type-id
1741/// type-id-list ',' type-id
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001742///
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001743bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redld6434562009-05-29 18:02:33 +00001744 llvm::SmallVector<TypeTy*, 2>
1745 &Exceptions,
1746 llvm::SmallVector<SourceRange, 2>
1747 &Ranges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001748 bool &hasAnyExceptionSpec) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001749 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00001750
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001751 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001752
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001753 if (!Tok.is(tok::l_paren)) {
1754 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1755 }
1756 SourceLocation LParenLoc = ConsumeParen();
1757
Douglas Gregor356513d2008-12-01 18:00:20 +00001758 // Parse throw(...), a Microsoft extension that means "this function
1759 // can throw anything".
1760 if (Tok.is(tok::ellipsis)) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001761 hasAnyExceptionSpec = true;
Douglas Gregor356513d2008-12-01 18:00:20 +00001762 SourceLocation EllipsisLoc = ConsumeToken();
1763 if (!getLang().Microsoft)
1764 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001765 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor356513d2008-12-01 18:00:20 +00001766 return false;
1767 }
1768
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001769 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00001770 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001771 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00001772 TypeResult Res(ParseTypeName(&Range));
1773 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001774 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00001775 Ranges.push_back(Range);
1776 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001777 if (Tok.is(tok::comma))
1778 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001779 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001780 break;
1781 }
1782
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001783 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor2afd0be2008-11-25 03:22:00 +00001784 return false;
1785}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001786
1787/// \brief We have just started parsing the definition of a new class,
1788/// so push that class onto our stack of classes that is currently
1789/// being parsed.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001790void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1791 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001792 "Nested class without outer class");
Douglas Gregoredf8f392010-01-16 20:52:59 +00001793 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001794}
1795
1796/// \brief Deallocate the given parsed class and all of its nested
1797/// classes.
1798void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1799 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1800 DeallocateParsedClasses(Class->NestedClasses[I]);
1801 delete Class;
1802}
1803
1804/// \brief Pop the top class of the stack of classes that are
1805/// currently being parsed.
1806///
1807/// This routine should be called when we have finished parsing the
1808/// definition of a class, but have not yet popped the Scope
1809/// associated with the class's definition.
1810///
1811/// \returns true if the class we've popped is a top-level class,
1812/// false otherwise.
1813void Parser::PopParsingClass() {
1814 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00001815
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001816 ParsingClass *Victim = ClassStack.top();
1817 ClassStack.pop();
1818 if (Victim->TopLevelClass) {
1819 // Deallocate all of the nested classes of this class,
1820 // recursively: we don't need to keep any of this information.
1821 DeallocateParsedClasses(Victim);
1822 return;
Mike Stump11289f42009-09-09 15:08:12 +00001823 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001824 assert(!ClassStack.empty() && "Missing top-level class?");
1825
1826 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1827 Victim->NestedClasses.empty()) {
1828 // The victim is a nested class, but we will not need to perform
1829 // any processing after the definition of this class since it has
1830 // no members whose handling was delayed. Therefore, we can just
1831 // remove this nested class.
1832 delete Victim;
1833 return;
1834 }
1835
1836 // This nested class has some members that will need to be processed
1837 // after the top-level class is completely defined. Therefore, add
1838 // it to the list of nested classes within its parent.
1839 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1840 ClassStack.top()->NestedClasses.push_back(Victim);
1841 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1842}
Alexis Hunt96d5c762009-11-21 08:43:09 +00001843
1844/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1845/// parses standard attributes.
1846///
1847/// [C++0x] attribute-specifier:
1848/// '[' '[' attribute-list ']' ']'
1849///
1850/// [C++0x] attribute-list:
1851/// attribute[opt]
1852/// attribute-list ',' attribute[opt]
1853///
1854/// [C++0x] attribute:
1855/// attribute-token attribute-argument-clause[opt]
1856///
1857/// [C++0x] attribute-token:
1858/// identifier
1859/// attribute-scoped-token
1860///
1861/// [C++0x] attribute-scoped-token:
1862/// attribute-namespace '::' identifier
1863///
1864/// [C++0x] attribute-namespace:
1865/// identifier
1866///
1867/// [C++0x] attribute-argument-clause:
1868/// '(' balanced-token-seq ')'
1869///
1870/// [C++0x] balanced-token-seq:
1871/// balanced-token
1872/// balanced-token-seq balanced-token
1873///
1874/// [C++0x] balanced-token:
1875/// '(' balanced-token-seq ')'
1876/// '[' balanced-token-seq ']'
1877/// '{' balanced-token-seq '}'
1878/// any token but '(', ')', '[', ']', '{', or '}'
1879CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1880 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1881 && "Not a C++0x attribute list");
1882
1883 SourceLocation StartLoc = Tok.getLocation(), Loc;
1884 AttributeList *CurrAttr = 0;
1885
1886 ConsumeBracket();
1887 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001888
Alexis Hunt96d5c762009-11-21 08:43:09 +00001889 if (Tok.is(tok::comma)) {
1890 Diag(Tok.getLocation(), diag::err_expected_ident);
1891 ConsumeToken();
1892 }
1893
1894 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1895 // attribute not present
1896 if (Tok.is(tok::comma)) {
1897 ConsumeToken();
1898 continue;
1899 }
1900
1901 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1902 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001903
Alexis Hunt96d5c762009-11-21 08:43:09 +00001904 // scoped attribute
1905 if (Tok.is(tok::coloncolon)) {
1906 ConsumeToken();
1907
1908 if (!Tok.is(tok::identifier)) {
1909 Diag(Tok.getLocation(), diag::err_expected_ident);
1910 SkipUntil(tok::r_square, tok::comma, true, true);
1911 continue;
1912 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001913
Alexis Hunt96d5c762009-11-21 08:43:09 +00001914 ScopeName = AttrName;
1915 ScopeLoc = AttrLoc;
1916
1917 AttrName = Tok.getIdentifierInfo();
1918 AttrLoc = ConsumeToken();
1919 }
1920
1921 bool AttrParsed = false;
1922 // No scoped names are supported; ideally we could put all non-standard
1923 // attributes into namespaces.
1924 if (!ScopeName) {
1925 switch(AttributeList::getKind(AttrName))
1926 {
1927 // No arguments
Alexis Hunt54a02542009-11-25 04:20:27 +00001928 case AttributeList::AT_base_check:
1929 case AttributeList::AT_carries_dependency:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001930 case AttributeList::AT_final:
Alexis Hunt54a02542009-11-25 04:20:27 +00001931 case AttributeList::AT_hiding:
1932 case AttributeList::AT_noreturn:
1933 case AttributeList::AT_override: {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001934 if (Tok.is(tok::l_paren)) {
1935 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1936 << AttrName->getName();
1937 break;
1938 }
1939
1940 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1941 SourceLocation(), 0, 0, CurrAttr, false,
1942 true);
1943 AttrParsed = true;
1944 break;
1945 }
1946
1947 // One argument; must be a type-id or assignment-expression
1948 case AttributeList::AT_aligned: {
1949 if (Tok.isNot(tok::l_paren)) {
1950 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1951 << AttrName->getName();
1952 break;
1953 }
1954 SourceLocation ParamLoc = ConsumeParen();
1955
1956 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1957
1958 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1959
1960 ExprVector ArgExprs(Actions);
1961 ArgExprs.push_back(ArgExpr.release());
1962 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1963 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1964 false, true);
1965
1966 AttrParsed = true;
1967 break;
1968 }
1969
1970 // Silence warnings
1971 default: break;
1972 }
1973 }
1974
1975 // Skip the entire parameter clause, if any
1976 if (!AttrParsed && Tok.is(tok::l_paren)) {
1977 ConsumeParen();
1978 // SkipUntil maintains the balancedness of tokens.
1979 SkipUntil(tok::r_paren, false);
1980 }
1981 }
1982
1983 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1984 SkipUntil(tok::r_square, false);
1985 Loc = Tok.getLocation();
1986 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1987 SkipUntil(tok::r_square, false);
1988
1989 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1990 return Attr;
1991}
1992
1993/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1994/// attribute.
1995///
1996/// FIXME: Simply returns an alignof() expression if the argument is a
1997/// type. Ideally, the type should be propagated directly into Sema.
1998///
1999/// [C++0x] 'align' '(' type-id ')'
2000/// [C++0x] 'align' '(' assignment-expression ')'
2001Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
2002 if (isTypeIdInParens()) {
2003 EnterExpressionEvaluationContext Unevaluated(Actions,
2004 Action::Unevaluated);
2005 SourceLocation TypeLoc = Tok.getLocation();
2006 TypeTy *Ty = ParseTypeName().get();
2007 SourceRange TypeRange(Start, Tok.getLocation());
2008 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
2009 TypeRange);
2010 } else
2011 return ParseConstantExpression();
2012}