blob: 1d812583672540bf156dc443941b14d70d33fe60 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000019#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000021using namespace clang;
22
23/// ParseNamespace - We know that the current token is a namespace keyword. This
24/// may either be a top level namespace or a block-level namespace alias.
25///
26/// namespace-definition: [C++ 7.3: basic.namespace]
27/// named-namespace-definition
28/// unnamed-namespace-definition
29///
30/// unnamed-namespace-definition:
31/// 'namespace' attributes[opt] '{' namespace-body '}'
32///
33/// named-namespace-definition:
34/// original-namespace-definition
35/// extension-namespace-definition
36///
37/// original-namespace-definition:
38/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
39///
40/// extension-namespace-definition:
41/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000042///
Chris Lattner8f08cb72007-08-25 06:57:03 +000043/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
44/// 'namespace' identifier '=' qualified-namespace-specifier ';'
45///
Chris Lattner97144fc2009-04-02 04:16:50 +000046Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
47 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000048 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000049 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor49f40bd2009-09-18 19:03:04 +000051 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000052 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000053 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000054 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000055
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 SourceLocation IdentLoc;
57 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000058
59 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner04d66662007-10-09 17:33:22 +000061 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000062 Ident = Tok.getIdentifierInfo();
63 IdentLoc = ConsumeToken(); // eat the identifier.
64 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 // Read label attributes, if present.
Ted Kremenek1e377652010-02-11 02:19:13 +000067 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000068 if (Tok.is(tok::kw___attribute)) {
69 attrTok = Tok;
70
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // FIXME: save these somewhere.
Ted Kremenek1e377652010-02-11 02:19:13 +000072 AttrList.reset(ParseGNUAttributes());
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor6a588dd2009-06-17 19:49:00 +000075 if (Tok.is(tok::equal)) {
76 if (AttrList)
77 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
78
Chris Lattner97144fc2009-04-02 04:16:50 +000079 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner51448322009-03-29 14:02:43 +000082 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000084 diag::err_expected_ident_lbrace);
85 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 SourceLocation LBrace = ConsumeBrace();
89
Douglas Gregor23c94db2010-07-02 17:43:08 +000090 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
91 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
92 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +000093 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
94 SkipUntil(tok::r_brace, false);
95 return DeclPtrTy();
96 }
97
Chris Lattner51448322009-03-29 14:02:43 +000098 // Enter a scope for the namespace.
99 ParseScope NamespaceScope(this, Scope::DeclScope);
100
101 DeclPtrTy NamespcDecl =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000102 Actions.ActOnStartNamespaceDef(getCurScope(), IdentLoc, Ident, LBrace,
Ted Kremenek1e377652010-02-11 02:19:13 +0000103 AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +0000104
105 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
106 PP.getSourceManager(),
107 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Sean Huntbbd37c62009-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 Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner51448322009-03-29 14:02:43 +0000116 // Leave the namespace scope.
117 NamespaceScope.Exit();
118
Chris Lattner97144fc2009-04-02 04:16:50 +0000119 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
120 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000121
Chris Lattner97144fc2009-04-02 04:16:50 +0000122 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000123 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000124}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000125
Anders Carlssonf67606a2009-03-28 04:07:16 +0000126/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
127/// alias definition.
128///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000129Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000130 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000131 IdentifierInfo *Alias,
132 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000133 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Anders Carlssonf67606a2009-03-28 04:07:16 +0000135 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000137 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000138 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000139 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000140 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000141
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 CXXScopeSpec SS;
143 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000144 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-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 Lattnerb28317a2009-03-28 19:18:32 +0000150 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000151 }
152
153 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000154 IdentifierInfo *Ident = Tok.getIdentifierInfo();
155 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlssonf67606a2009-03-28 04:07:16 +0000157 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000158 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000159 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
160 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Douglas Gregor23c94db2010-07-02 17:43:08 +0000162 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000163 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000164}
165
Chris Lattnerc6fdc342008-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 Jahanian3acd9aa2009-12-09 21:39:38 +0000173Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
174 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000175 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000176 llvm::SmallString<8> LangBuffer;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000177 // LangBuffer is guaranteed to be big enough.
Douglas Gregor453091c2010-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 Lattnerc6fdc342008-01-12 07:05:38 +0000182
183 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000184
Douglas Gregor074149e2009-01-05 19:45:36 +0000185 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000186 DeclPtrTy LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000187 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor074149e2009-01-05 19:45:36 +0000188 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000189 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000190 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000191 : SourceLocation());
192
Sean Huntbbd37c62009-11-21 08:43:09 +0000193 CXX0XAttributeList Attr;
194 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
195 Attr = ParseCXX0XAttributes();
196 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000197
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000199 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000200 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000201 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000202 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000203
Douglas Gregor63a01132010-02-07 08:38:28 +0000204 DS.abort();
205
Sean Huntbbd37c62009-11-21 08:43:09 +0000206 if (Attr.HasAttr)
207 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
208 << Attr.Range;
209
Douglas Gregorf44515a2008-12-16 22:23:02 +0000210 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000211 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000212 CXX0XAttributeList Attr;
213 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
214 Attr = ParseCXX0XAttributes();
215 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000216 }
217
Douglas Gregorf44515a2008-12-16 22:23:02 +0000218 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000219 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000220}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000221
Douglas Gregorf780abc2008-12-30 03:27:21 +0000222/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
223/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000224Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000225 SourceLocation &DeclEnd,
226 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-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 Gregor49f40bd2009-09-18 19:03:04 +0000232 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000233 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000234 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000235 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000236
Chris Lattner2f274772009-01-06 06:55:51 +0000237 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000238 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-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 Lattner2f274772009-01-06 06:55:51 +0000244
245 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000246 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000247 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000260Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000261 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000262 SourceLocation &DeclEnd,
263 AttributeList *Attr) {
Douglas Gregorf780abc2008-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 Gregor49f40bd2009-09-18 19:03:04 +0000269 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000270 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000271 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000272 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000273
Douglas Gregorf780abc2008-12-30 03:27:21 +0000274 CXXScopeSpec SS;
275 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000276 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000277
Douglas Gregorf780abc2008-12-30 03:27:21 +0000278 IdentifierInfo *NamespcName = 0;
279 SourceLocation IdentLoc = SourceLocation();
280
281 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000282 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-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 Lattnerb28317a2009-03-28 19:18:32 +0000287 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattner823c44e2009-01-06 07:27:21 +0000290 // Parse identifier.
291 NamespcName = Tok.getIdentifierInfo();
292 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattner823c44e2009-01-06 07:27:21 +0000294 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-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 Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattner823c44e2009-01-06 07:27:21 +0000301 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000302 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000303 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000304 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000305 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000306
Douglas Gregor23c94db2010-07-02 17:43:08 +0000307 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000308 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-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 Gregor9cfbe482009-06-20 00:51:54 +0000316/// unqualified-id
317/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000318///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000319Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000320 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000321 SourceLocation &DeclEnd,
322 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000323 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000324 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000325 bool IsTypeName;
326
327 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000328 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000329 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000330 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000331 ConsumeToken();
332 IsTypeName = true;
333 }
334 else
335 IsTypeName = false;
336
337 // Parse nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000338 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000339
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000340 // Check nested-name specifier.
341 if (SS.isInvalid()) {
342 SkipUntil(tok::semi);
343 return DeclPtrTy();
344 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000345
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000346 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000347 // destructor names and allow the action module to diagnose any semantic
348 // errors.
349 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000350 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000351 /*EnteringContext=*/false,
352 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000353 /*AllowConstructorName=*/true,
354 /*ObjectType=*/0,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000355 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000356 SkipUntil(tok::semi);
357 return DeclPtrTy();
358 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000359
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000360 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000361 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000362 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000363 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000365 // Eat ';'.
366 DeclEnd = Tok.getLocation();
367 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000368 AttrList ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000369 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000370
Douglas Gregor23c94db2010-07-02 17:43:08 +0000371 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000372 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000373}
374
Anders Carlsson511d7ab2009-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 Lattner97144fc2009-04-02 04:16:50 +0000380Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000381 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
382 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000384 if (Tok.isNot(tok::l_paren)) {
385 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000386 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000389 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000390
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000391 OwningExprResult AssertExpr(ParseConstantExpression());
392 if (AssertExpr.isInvalid()) {
393 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000394 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Anders Carlssonad5f9602009-03-13 23:29:20 +0000397 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000398 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000399
Anders Carlsson511d7ab2009-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 Lattnerb28317a2009-03-28 19:18:32 +0000403 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000406 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000407 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000408 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000409
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattner97144fc2009-04-02 04:16:50 +0000412 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000413 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
414
Mike Stump1eb44332009-09-09 15:08:12 +0000415 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000416 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000417}
418
Anders Carlsson6fd634f2009-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 Stump1eb44332009-09-09 15:08:12 +0000428
429 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000430 "decltype")) {
431 SkipUntil(tok::r_paren);
432 return;
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000435 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anders Carlsson6fd634f2009-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 Stump1eb44332009-09-09 15:08:12 +0000446
Anders Carlsson6fd634f2009-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 Stump1eb44332009-09-09 15:08:12 +0000453
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000454 if (RParenLoc.isInvalid())
455 return;
456
457 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000458 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000459 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000460 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000461 DiagID, Result.release()))
462 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000463}
464
Douglas Gregor42a552f2008-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 Gregor7f43d672009-02-25 23:52:28 +0000468/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000469/// found.
470///
471/// class-name: [C++ 9.1]
472/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000473/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000474///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000475Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000476 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-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 Stump1eb44332009-09-09 15:08:12 +0000479 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000480 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000481 if (TemplateId->Kind == TNK_Type_template ||
482 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000483 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-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 Gregor31a19b62009-04-01 21:51:26 +0000489
490 if (Type)
491 return Type;
492 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000493 }
494
495 // Fall through to produce an error below.
496 }
497
Douglas Gregor42a552f2008-11-05 20:51:48 +0000498 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000499 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000500 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000501 }
502
Douglas Gregor84d0a192010-01-12 21:28:44 +0000503 IdentifierInfo *Id = Tok.getIdentifierInfo();
504 SourceLocation IdLoc = ConsumeToken();
505
506 if (Tok.is(tok::less)) {
507 // It looks the user intended to write a template-id here, but the
508 // template-name was wrong. Try to fix that.
509 TemplateNameKind TNK = TNK_Type_template;
510 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000511 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000512 SS, Template, TNK)) {
513 Diag(IdLoc, diag::err_unknown_template_name)
514 << Id;
515 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000516
Douglas Gregor84d0a192010-01-12 21:28:44 +0000517 if (!Template)
518 return true;
519
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000520 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000521 UnqualifiedId TemplateName;
522 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000523
Douglas Gregor84d0a192010-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 Rajaratnam19357542010-03-13 10:17:05 +0000530
Douglas Gregor84d0a192010-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 Rajaratnam19357542010-03-13 10:17:05 +0000535
Douglas Gregor84d0a192010-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 Gregor42a552f2008-11-05 20:51:48 +0000544 // We have an identifier; check whether it is actually a type.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000545 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000546 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000547 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000548 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000549 }
550
551 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000552 EndLocation = IdLoc;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000553 return Type;
554}
555
Douglas Gregore37ac4f2008-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 Redld9bafa72010-02-03 21:21:43 +0000559/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-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 Stump1eb44332009-09-09 15:08:12 +0000570/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000571/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000572/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-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 Stump1eb44332009-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 Gregore37ac4f2008-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 Lattner4c97d762009-04-12 21:49:30 +0000596void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
597 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000598 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000599 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-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 Gregore37ac4f2008-04-13 21:30:24 +0000609
Douglas Gregor374929f2009-09-18 15:37:17 +0000610 if (Tok.is(tok::code_completion)) {
611 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000612 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000613 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000614 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000615
Chandler Carruth926c4b42010-06-28 08:39:25 +0000616 // C++03 [temp.explicit] 14.7.2/8:
617 // The usual access checking rules do not apply to names used to specify
618 // explicit instantiations.
619 //
620 // As an extension we do not perform access checking on the names used to
621 // specify explicit specializations either. This is important to allow
622 // specializing traits classes for private types.
623 bool SuppressingAccessChecks = false;
624 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
625 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
626 Actions.ActOnStartSuppressingAccessChecks();
627 SuppressingAccessChecks = true;
628 }
629
Sean Huntbbd37c62009-11-21 08:43:09 +0000630 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000631 // If attributes exist after tag, parse them.
632 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000633 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000634
Steve Narofff59e17e2008-12-24 20:59:21 +0000635 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000636 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000637 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000638
Sean Huntbbd37c62009-11-21 08:43:09 +0000639 // If C++0x attributes exist here, parse them.
640 // FIXME: Are we consistent with the ordering of parsing of different
641 // styles of attributes?
642 if (isCXX0XAttributeSpecifier())
643 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Douglas Gregorb117a602009-09-04 05:53:02 +0000645 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
646 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
647 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000648 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000649 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
650 // properly.
651 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
652 Tok.setKind(tok::identifier);
653 }
654
655 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
656 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
657 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000658 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000659 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
660 // properly.
661 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
662 Tok.setKind(tok::identifier);
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000665 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000666 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000667 if (getLang().CPlusPlus) {
668 // "FOO : BAR" is not a potential typo for "FOO::BAR".
669 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000670
John McCall9ba61662010-02-26 08:45:28 +0000671 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
672 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000673 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
674 Diag(Tok, diag::err_expected_ident);
675 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000676
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000677 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
678
Douglas Gregorcc636682009-02-17 23:15:12 +0000679 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000680 IdentifierInfo *Name = 0;
681 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000682 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000683 if (Tok.is(tok::identifier)) {
684 Name = Tok.getIdentifierInfo();
685 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000686
Douglas Gregor5ee37342010-05-30 22:30:21 +0000687 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000688 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000689 // Eat the template argument list and try to continue parsing this as
690 // a class (or template thereof).
691 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000692 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000693 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000694 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000695 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000696 // We couldn't parse the template argument list at all, so don't
697 // try to give any location information for the list.
698 LAngleLoc = RAngleLoc = SourceLocation();
699 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000700
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000701 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000702 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000703 << (TagType == DeclSpec::TST_class? 0
704 : TagType == DeclSpec::TST_struct? 1
705 : 2)
706 << Name
707 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000708
709 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000710 // we've removed its template argument list.
711 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
712 if (TemplateParams && TemplateParams->size() > 1) {
713 TemplateParams->pop_back();
714 } else {
715 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000716 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000717 = ParsedTemplateInfo::NonTemplate;
718 }
719 } else if (TemplateInfo.Kind
720 == ParsedTemplateInfo::ExplicitInstantiation) {
721 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000722 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000723 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000724 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000725 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000726 = SourceLocation();
727 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
728 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000729 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000730 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000731 } else if (Tok.is(tok::annot_template_id)) {
732 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
733 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000734
Douglas Gregorc45c2322009-03-31 00:43:58 +0000735 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000736 // The template-name in the simple-template-id refers to
737 // something other than a class template. Give an appropriate
738 // error message and skip to the ';'.
739 SourceRange Range(NameLoc);
740 if (SS.isNotEmpty())
741 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000742
Douglas Gregor39a8de12009-02-25 19:37:18 +0000743 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
744 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Douglas Gregor39a8de12009-02-25 19:37:18 +0000746 DS.SetTypeSpecError();
747 SkipUntil(tok::semi, false, true);
748 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000749 if (SuppressingAccessChecks)
750 Actions.ActOnStopSuppressingAccessChecks();
751
Douglas Gregor39a8de12009-02-25 19:37:18 +0000752 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000753 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000754 }
755
Chandler Carruth926c4b42010-06-28 08:39:25 +0000756 // As soon as we're finished parsing the class's template-id, turn access
757 // checking back on.
758 if (SuppressingAccessChecks)
759 Actions.ActOnStopSuppressingAccessChecks();
760
John McCall67d1a672009-08-06 02:15:43 +0000761 // There are four options here. If we have 'struct foo;', then this
762 // is either a forward declaration or a friend declaration, which
763 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000764 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000765 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000766 // However, in some contexts, things look like declarations but are just
767 // references, e.g.
768 // new struct s;
769 // or
770 // &T::operator struct s;
771 // For these, SuppressDeclarations is true.
John McCall0f434ec2009-07-31 02:45:11 +0000772 Action::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000773 if (SuppressDeclarations)
774 TUK = Action::TUK_Reference;
775 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000776 if (DS.isFriendSpecified()) {
777 // C++ [class.friend]p2:
778 // A class shall not be defined in a friend declaration.
779 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
780 << SourceRange(DS.getFriendSpecLoc());
781
782 // Skip everything up to the semicolon, so that this looks like a proper
783 // friend class (or template thereof) declaration.
784 SkipUntil(tok::semi, true, true);
785 TUK = Action::TUK_Friend;
786 } else {
787 // Okay, this is a class definition.
788 TUK = Action::TUK_Definition;
789 }
790 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000791 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000792 else
John McCall0f434ec2009-07-31 02:45:11 +0000793 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000794
John McCall0f434ec2009-07-31 02:45:11 +0000795 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000796 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000797 Diag(StartLoc, diag::err_anon_type_definition)
798 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000799
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000800 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000801
802 if (TemplateId)
803 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000804 return;
805 }
806
Douglas Gregorddc29e12009-02-06 22:42:48 +0000807 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000808 Action::DeclResult TagOrTempResult = true; // invalid
809 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000810
Douglas Gregor402abb52009-05-28 23:31:59 +0000811 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000812 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000813 // Explicit specialization, class template partial specialization,
814 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000815 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000816 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000817 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000818 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000819 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000820 // This is an explicit instantiation of a class template.
821 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000822 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000823 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000824 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000825 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000826 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000827 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000828 TemplateTy::make(TemplateId->Template),
829 TemplateId->TemplateNameLoc,
830 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000831 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000832 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000833 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000834
835 // Friend template-ids are treated as references unless
836 // they have template headers, in which case they're ill-formed
837 // (FIXME: "template <class T> friend class A<T>::B<int>;").
838 // We diagnose this error in ActOnClassTemplateSpecialization.
839 } else if (TUK == Action::TUK_Reference ||
840 (TUK == Action::TUK_Friend &&
841 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000842 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000843 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
844 TemplateId->TemplateNameLoc,
845 TemplateId->LAngleLoc,
846 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000847 TemplateId->RAngleLoc);
848
John McCallc4e70192009-09-11 04:59:25 +0000849 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
850 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000851 } else {
852 // This is an explicit specialization or a class template
853 // partial specialization.
854 TemplateParameterLists FakedParamLists;
855
856 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
857 // This looks like an explicit instantiation, because we have
858 // something like
859 //
860 // template class Foo<X>
861 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000862 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000863 // meant to be an explicit specialization, but the user forgot
864 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000865 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000866
Mike Stump1eb44332009-09-09 15:08:12 +0000867 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000868 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000869 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000870 diag::err_explicit_instantiation_with_definition)
871 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000872 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000873
874 // Create a fake template parameter list that contains only
875 // "template<>", so that we treat this construct as a class
876 // template specialization.
877 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000878 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000879 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000880 LAngleLoc,
881 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000882 LAngleLoc));
883 TemplateParams = &FakedParamLists;
884 }
885
886 // Build the class template specialization.
887 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000888 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000889 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000890 TemplateTy::make(TemplateId->Template),
891 TemplateId->TemplateNameLoc,
892 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000893 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000894 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000895 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000896 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000897 TemplateParams? &(*TemplateParams)[0] : 0,
898 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000899 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000900 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000901 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000902 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000903 // Explicit instantiation of a member of a class template
904 // specialization, e.g.,
905 //
906 // template struct Outer<int>::Inner;
907 //
908 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000909 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000910 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000911 TemplateInfo.TemplateLoc,
912 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000913 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000914 } else {
915 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000916 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000917 // FIXME: Diagnose this particular error.
918 }
919
John McCallc4e70192009-09-11 04:59:25 +0000920 bool IsDependent = false;
921
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000922 // Declaration or definition of a class type
Douglas Gregor23c94db2010-07-02 17:43:08 +0000923 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000924 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000925 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000926 TemplateParams? &(*TemplateParams)[0] : 0,
927 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000928 Owned, IsDependent);
929
930 // If ActOnTag said the type was dependent, try again with the
931 // less common call.
932 if (IsDependent)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000933 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000934 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000935 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000936
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000937 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000938 if (TUK == Action::TUK_Definition) {
939 assert(Tok.is(tok::l_brace) ||
940 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000941 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000942 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000943 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000944 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000945 }
946
John McCallc4e70192009-09-11 04:59:25 +0000947 void *Result;
948 if (!TypeResult.isInvalid()) {
949 TagType = DeclSpec::TST_typename;
950 Result = TypeResult.get();
951 Owned = false;
952 } else if (!TagOrTempResult.isInvalid()) {
953 Result = TagOrTempResult.get().getAs<void>();
954 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000955 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000956 return;
957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
John McCallfec54012009-08-03 20:12:06 +0000959 const char *PrevSpec = 0;
960 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000961
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000962 // FIXME: The DeclSpec should keep the locations of both the keyword and the
963 // name (if there is one).
964 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000965
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000966 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000967 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000968 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000969
Chris Lattner4ed5d912010-02-02 01:23:29 +0000970 // At this point, we've successfully parsed a class-specifier in 'definition'
971 // form (e.g. "struct foo { int x; }". While we could just return here, we're
972 // going to look at what comes after it to improve error recovery. If an
973 // impossible token occurs next, we assume that the programmer forgot a ; at
974 // the end of the declaration and recover that way.
975 //
976 // This switch enumerates the valid "follow" set for definition.
977 if (TUK == Action::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000978 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000979 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000980 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000981 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000982 case tok::star: // struct foo {...} * P;
983 case tok::amp: // struct foo {...} & R = ...
984 case tok::identifier: // struct foo {...} V ;
985 case tok::r_paren: //(struct foo {...} ) {4}
986 case tok::annot_cxxscope: // struct foo {...} a:: b;
987 case tok::annot_typename: // struct foo {...} a ::b;
988 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +0000989 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +0000990 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000991 ExpectedSemi = false;
992 break;
993 // Type qualifiers
994 case tok::kw_const: // struct foo {...} const x;
995 case tok::kw_volatile: // struct foo {...} volatile x;
996 case tok::kw_restrict: // struct foo {...} restrict x;
997 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +0000998 // Storage-class specifiers
999 case tok::kw_static: // struct foo {...} static x;
1000 case tok::kw_extern: // struct foo {...} extern x;
1001 case tok::kw_typedef: // struct foo {...} typedef x;
1002 case tok::kw_register: // struct foo {...} register x;
1003 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001004 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001005 // As shown above, type qualifiers and storage class specifiers absolutely
1006 // can occur after class specifiers according to the grammar. However,
1007 // almost noone actually writes code like this. If we see one of these,
1008 // it is much more likely that someone missed a semi colon and the
1009 // type/storage class specifier we're seeing is part of the *next*
1010 // intended declaration, as in:
1011 //
1012 // struct foo { ... }
1013 // typedef int X;
1014 //
1015 // We'd really like to emit a missing semicolon error instead of emitting
1016 // an error on the 'int' saying that you can't have two type specifiers in
1017 // the same declaration of X. Because of this, we look ahead past this
1018 // token to see if it's a type specifier. If so, we know the code is
1019 // otherwise invalid, so we can produce the expected semi error.
1020 if (!isKnownToBeTypeSpecifier(NextToken()))
1021 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001022 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001023
1024 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001025 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001026 if (!getLang().CPlusPlus)
1027 ExpectedSemi = false;
1028 break;
1029 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001030
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001031 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001032 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1033 TagType == DeclSpec::TST_class ? "class"
1034 : TagType == DeclSpec::TST_struct? "struct" : "union");
1035 // Push this token back into the preprocessor and change our current token
1036 // to ';' so that the rest of the code recovers as though there were an
1037 // ';' after the definition.
1038 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001039 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001040 }
1041 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001042}
1043
Mike Stump1eb44332009-09-09 15:08:12 +00001044/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001045///
1046/// base-clause : [C++ class.derived]
1047/// ':' base-specifier-list
1048/// base-specifier-list:
1049/// base-specifier '...'[opt]
1050/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +00001051void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001052 assert(Tok.is(tok::colon) && "Not a base clause");
1053 ConsumeToken();
1054
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001055 // Build up an array of parsed base specifiers.
1056 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1057
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001058 while (true) {
1059 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001060 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001061 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001062 // Skip the rest of this base specifier, up until the comma or
1063 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001064 SkipUntil(tok::comma, tok::l_brace, true, true);
1065 } else {
1066 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001067 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001068 }
1069
1070 // If the next token is a comma, consume it and keep reading
1071 // base-specifiers.
1072 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001074 // Consume the comma.
1075 ConsumeToken();
1076 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001077
1078 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001079 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001080}
1081
1082/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1083/// one entry in the base class list of a class specifier, for example:
1084/// class foo : public bar, virtual private baz {
1085/// 'public bar' and 'virtual private baz' are each base-specifiers.
1086///
1087/// base-specifier: [C++ class.derived]
1088/// ::[opt] nested-name-specifier[opt] class-name
1089/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1090/// class-name
1091/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1092/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +00001093Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001094 bool IsVirtual = false;
1095 SourceLocation StartLoc = Tok.getLocation();
1096
1097 // Parse the 'virtual' keyword.
1098 if (Tok.is(tok::kw_virtual)) {
1099 ConsumeToken();
1100 IsVirtual = true;
1101 }
1102
1103 // Parse an (optional) access specifier.
1104 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001105 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001106 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001108 // Parse the 'virtual' keyword (again!), in case it came after the
1109 // access specifier.
1110 if (Tok.is(tok::kw_virtual)) {
1111 SourceLocation VirtualLoc = ConsumeToken();
1112 if (IsVirtual) {
1113 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001114 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001115 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001116 }
1117
1118 IsVirtual = true;
1119 }
1120
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001121 // Parse optional '::' and optional nested-name-specifier.
1122 CXXScopeSpec SS;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001123 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
Douglas Gregord2b43bf2010-03-02 00:25:00 +00001124 /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001125
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001126 // The location of the base class itself.
1127 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001128
1129 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001130 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001131 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1132 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001133 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001134
1135 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001136 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001138 // Notify semantic analysis that we have parsed a complete
1139 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001140 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001141 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001142}
1143
1144/// getAccessSpecifierIfPresent - Determine whether the next token is
1145/// a C++ access-specifier.
1146///
1147/// access-specifier: [C++ class.derived]
1148/// 'private'
1149/// 'protected'
1150/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001151AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001152 switch (Tok.getKind()) {
1153 default: return AS_none;
1154 case tok::kw_private: return AS_private;
1155 case tok::kw_protected: return AS_protected;
1156 case tok::kw_public: return AS_public;
1157 }
1158}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001159
Eli Friedmand33133c2009-07-22 21:45:50 +00001160void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1161 DeclPtrTy ThisDecl) {
1162 // We just declared a member function. If this member function
1163 // has any default arguments, we'll need to parse them later.
1164 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001165 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001166 = DeclaratorInfo.getTypeObject(0).Fun;
1167 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1168 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1169 if (!LateMethod) {
1170 // Push this method onto the stack of late-parsed method
1171 // declarations.
1172 getCurrentClass().MethodDecls.push_back(
1173 LateParsedMethodDeclaration(ThisDecl));
1174 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001175 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001176
1177 // Add all of the parameters prior to this one (they don't
1178 // have default arguments).
1179 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1180 for (unsigned I = 0; I < ParamIdx; ++I)
1181 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001182 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001183 }
1184
1185 // Add this parameter to the list of parameters (it or may
1186 // not have a default argument).
1187 LateMethod->DefaultArgs.push_back(
1188 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1189 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1190 }
1191 }
1192}
1193
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001194/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1195///
1196/// member-declaration:
1197/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1198/// function-definition ';'[opt]
1199/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1200/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001201/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001202/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001203/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001204///
1205/// member-declarator-list:
1206/// member-declarator
1207/// member-declarator-list ',' member-declarator
1208///
1209/// member-declarator:
1210/// declarator pure-specifier[opt]
1211/// declarator constant-initializer[opt]
1212/// identifier[opt] ':' constant-expression
1213///
Sebastian Redle2b68332009-04-12 17:16:29 +00001214/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001215/// '= 0'
1216///
1217/// constant-initializer:
1218/// '=' constant-expression
1219///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001220void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001221 const ParsedTemplateInfo &TemplateInfo,
1222 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001223 // Access declarations.
1224 if (!TemplateInfo.Kind &&
1225 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001226 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001227 Tok.is(tok::annot_cxxscope)) {
1228 bool isAccessDecl = false;
1229 if (NextToken().is(tok::identifier))
1230 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1231 else
1232 isAccessDecl = NextToken().is(tok::kw_operator);
1233
1234 if (isAccessDecl) {
1235 // Collect the scope specifier token we annotated earlier.
1236 CXXScopeSpec SS;
1237 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1238
1239 // Try to parse an unqualified-id.
1240 UnqualifiedId Name;
1241 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1242 SkipUntil(tok::semi);
1243 return;
1244 }
1245
1246 // TODO: recover from mistakenly-qualified operator declarations.
1247 if (ExpectAndConsume(tok::semi,
1248 diag::err_expected_semi_after,
1249 "access declaration",
1250 tok::semi))
1251 return;
1252
Douglas Gregor23c94db2010-07-02 17:43:08 +00001253 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001254 false, SourceLocation(),
1255 SS, Name,
1256 /* AttrList */ 0,
1257 /* IsTypeName */ false,
1258 SourceLocation());
1259 return;
1260 }
1261 }
1262
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001263 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001264 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001265 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001266 SourceLocation DeclEnd;
1267 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001268 return;
1269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Chris Lattner682bf922009-03-29 16:50:03 +00001271 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001272 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001273 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001274 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001275 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001276 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001277 return;
1278 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001279
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001280 // Handle: member-declaration ::= '__extension__' member-declaration
1281 if (Tok.is(tok::kw___extension__)) {
1282 // __extension__ silences extension warnings in the subexpression.
1283 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1284 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001285 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001286 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001287
Chris Lattner4ed5d912010-02-02 01:23:29 +00001288 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1289 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001290 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001291
Sean Huntbbd37c62009-11-21 08:43:09 +00001292 CXX0XAttributeList AttrList;
1293 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001294 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001295 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001296
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001297 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001298 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001299
Sean Huntbbd37c62009-11-21 08:43:09 +00001300 if (AttrList.HasAttr)
1301 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1302 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001304 // Eat 'using'.
1305 SourceLocation UsingLoc = ConsumeToken();
1306
1307 if (Tok.is(tok::kw_namespace)) {
1308 Diag(UsingLoc, diag::err_using_namespace_in_class);
1309 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001310 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001311 SourceLocation DeclEnd;
1312 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001313 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001314 }
1315 return;
1316 }
1317
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001318 SourceLocation DSStart = Tok.getLocation();
1319 // decl-specifier-seq:
1320 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001321 ParsingDeclSpec DS(*this, TemplateDiags);
Sean Huntbbd37c62009-11-21 08:43:09 +00001322 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001323 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001324
John McCalldd4a3b02009-09-16 22:47:08 +00001325 Action::MultiTemplateParamsArg TemplateParams(Actions,
1326 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1327 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1328
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001329 if (Tok.is(tok::semi)) {
1330 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001331 DeclPtrTy TheDecl =
1332 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1333 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001334 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001335 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001336
John McCall54abf7d2009-11-04 02:18:39 +00001337 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001338
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001339 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001340 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1341 ColonProtectionRAIIObject X(*this);
1342
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001343 // Parse the first declarator.
1344 ParseDeclarator(DeclaratorInfo);
1345 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001346 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001347 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001348 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001349 if (Tok.is(tok::semi))
1350 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001351 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001352 }
1353
John Thompson1b2fc0f2009-11-25 22:58:06 +00001354 // If attributes exist after the declarator, but before an '{', parse them.
1355 if (Tok.is(tok::kw___attribute)) {
1356 SourceLocation Loc;
1357 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1358 DeclaratorInfo.AddAttributes(AttrList, Loc);
1359 }
1360
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001361 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001362 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001363 || (DeclaratorInfo.isFunctionDeclarator() &&
1364 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001365 if (!DeclaratorInfo.isFunctionDeclarator()) {
1366 Diag(Tok, diag::err_func_def_no_params);
1367 ConsumeBrace();
1368 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001369 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001370 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001371
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001372 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1373 Diag(Tok, diag::err_function_declared_typedef);
1374 // This recovery skips the entire function body. It would be nice
1375 // to simply call ParseCXXInlineMethodDef() below, however Sema
1376 // assumes the declarator represents a function, not a typedef.
1377 ConsumeBrace();
1378 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001379 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001380 }
1381
Douglas Gregor37b372b2009-08-20 22:52:58 +00001382 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001383 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001384 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001385 }
1386
1387 // member-declarator-list:
1388 // member-declarator
1389 // member-declarator-list ',' member-declarator
1390
Chris Lattner682bf922009-03-29 16:50:03 +00001391 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001392 OwningExprResult BitfieldSize(Actions);
1393 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001394 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001395
1396 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001397 // member-declarator:
1398 // declarator pure-specifier[opt]
1399 // declarator constant-initializer[opt]
1400 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001401 if (Tok.is(tok::colon)) {
1402 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001403 BitfieldSize = ParseConstantExpression();
1404 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001405 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001408 // pure-specifier:
1409 // '= 0'
1410 //
1411 // constant-initializer:
1412 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001413 //
1414 // defaulted/deleted function-definition:
1415 // '=' 'default' [TODO]
1416 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001417 if (Tok.is(tok::equal)) {
1418 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001419 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1420 ConsumeToken();
1421 Deleted = true;
1422 } else {
1423 Init = ParseInitializer();
1424 if (Init.isInvalid())
1425 SkipUntil(tok::comma, true, true);
1426 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001427 }
1428
Chris Lattnere6563252010-06-13 05:34:18 +00001429 // If a simple-asm-expr is present, parse it.
1430 if (Tok.is(tok::kw_asm)) {
1431 SourceLocation Loc;
1432 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
1433 if (AsmLabel.isInvalid())
1434 SkipUntil(tok::comma, true, true);
1435
1436 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1437 DeclaratorInfo.SetRangeEnd(Loc);
1438 }
1439
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001440 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001441 if (Tok.is(tok::kw___attribute)) {
1442 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001443 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001444 DeclaratorInfo.AddAttributes(AttrList, Loc);
1445 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001446
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001447 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001448 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001449 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001450
1451 DeclPtrTy ThisDecl;
1452 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001453 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001455 /*IsDefinition*/ false,
1456 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001457 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001458 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001459 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001460 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001461 BitfieldSize.release(),
1462 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001463 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001464 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001465 }
Chris Lattner682bf922009-03-29 16:50:03 +00001466 if (ThisDecl)
1467 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001468
Douglas Gregor72b505b2008-12-16 21:30:33 +00001469 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001470 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001471 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001472 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001473 }
1474
John McCall54abf7d2009-11-04 02:18:39 +00001475 DeclaratorInfo.complete(ThisDecl);
1476
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001477 // If we don't have a comma, it is either the end of the list (a ';')
1478 // or an error, bail out.
1479 if (Tok.isNot(tok::comma))
1480 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001482 // Consume the comma.
1483 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001485 // Parse the next declarator.
1486 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001487 BitfieldSize = 0;
1488 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001489 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001491 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001492 if (Tok.is(tok::kw___attribute)) {
1493 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001494 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001495 DeclaratorInfo.AddAttributes(AttrList, Loc);
1496 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001497
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001498 if (Tok.isNot(tok::colon))
1499 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001500 }
1501
Chris Lattnerae50d502010-02-02 00:43:15 +00001502 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1503 // Skip to end of block or statement.
1504 SkipUntil(tok::r_brace, true, true);
1505 // If we stopped at a ';', eat it.
1506 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001507 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001508 }
1509
Douglas Gregor23c94db2010-07-02 17:43:08 +00001510 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001511 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001512}
1513
1514/// ParseCXXMemberSpecification - Parse the class definition.
1515///
1516/// member-specification:
1517/// member-declaration member-specification[opt]
1518/// access-specifier ':' member-specification[opt]
1519///
1520void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001521 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001522 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001523 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001524 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001525
Chris Lattner49f28ca2009-03-05 08:00:35 +00001526 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1527 PP.getSourceManager(),
1528 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Douglas Gregor26997fd2010-01-16 20:52:59 +00001530 // Determine whether this is a non-nested class. Note that local
1531 // classes are *not* considered to be nested classes.
1532 bool NonNestedClass = true;
1533 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001534 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001535 if (S->isClassScope()) {
1536 // We're inside a class scope, so this is a nested class.
1537 NonNestedClass = false;
1538 break;
1539 }
1540
1541 if ((S->getFlags() & Scope::FnScope)) {
1542 // If we're in a function or function template declared in the
1543 // body of a class, then this is a local class rather than a
1544 // nested class.
1545 const Scope *Parent = S->getParent();
1546 if (Parent->isTemplateParamScope())
1547 Parent = Parent->getParent();
1548 if (Parent->isClassScope())
1549 break;
1550 }
1551 }
1552 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001553
1554 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001555 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001556
Douglas Gregor6569d682009-05-27 23:11:45 +00001557 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001558 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001559
Douglas Gregorddc29e12009-02-06 22:42:48 +00001560 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001561 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001562
1563 if (Tok.is(tok::colon)) {
1564 ParseBaseClause(TagDecl);
1565
1566 if (!Tok.is(tok::l_brace)) {
1567 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001568
1569 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001570 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001571 return;
1572 }
1573 }
1574
1575 assert(Tok.is(tok::l_brace));
1576
1577 SourceLocation LBraceLoc = ConsumeBrace();
1578
John McCall42a4f662010-05-28 08:11:17 +00001579 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001580 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001581
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001582 // C++ 11p3: Members of a class defined with the keyword class are private
1583 // by default. Members of a class defined with the keywords struct or union
1584 // are public by default.
1585 AccessSpecifier CurAS;
1586 if (TagType == DeclSpec::TST_class)
1587 CurAS = AS_private;
1588 else
1589 CurAS = AS_public;
1590
Douglas Gregor07976d22010-06-21 22:31:09 +00001591 SourceLocation RBraceLoc;
1592 if (TagDecl) {
1593 // While we still have something to read, read the member-declarations.
1594 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1595 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Douglas Gregor07976d22010-06-21 22:31:09 +00001597 // Check for extraneous top-level semicolon.
1598 if (Tok.is(tok::semi)) {
1599 Diag(Tok, diag::ext_extra_struct_semi)
1600 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1601 << FixItHint::CreateRemoval(Tok.getLocation());
1602 ConsumeToken();
1603 continue;
1604 }
1605
1606 AccessSpecifier AS = getAccessSpecifierIfPresent();
1607 if (AS != AS_none) {
1608 // Current token is a C++ access specifier.
1609 CurAS = AS;
1610 SourceLocation ASLoc = Tok.getLocation();
1611 ConsumeToken();
1612 if (Tok.is(tok::colon))
1613 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1614 else
1615 Diag(Tok, diag::err_expected_colon);
1616 ConsumeToken();
1617 continue;
1618 }
1619
1620 // FIXME: Make sure we don't have a template here.
1621
1622 // Parse all the comma separated declarators.
1623 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001624 }
1625
Douglas Gregor07976d22010-06-21 22:31:09 +00001626 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1627 } else {
1628 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001629 }
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001631 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001632 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001633 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001634 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001635
John McCall42a4f662010-05-28 08:11:17 +00001636 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001637 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001638 LBraceLoc, RBraceLoc,
1639 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001640
1641 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1642 // complete within function bodies, default arguments,
1643 // exception-specifications, and constructor ctor-initializers (including
1644 // such things in nested classes).
1645 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001646 // FIXME: Only function bodies and constructor ctor-initializers are
1647 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001648 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001649 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001650 // are complete and we can parse the delayed portions of method
1651 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001652 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001653 ParseLexedMethodDeclarations(getCurrentClass());
1654 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001655 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001656 }
1657
John McCall42a4f662010-05-28 08:11:17 +00001658 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001659 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001660
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001661 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001662 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001663 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001664}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001665
1666/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1667/// which explicitly initializes the members or base classes of a
1668/// class (C++ [class.base.init]). For example, the three initializers
1669/// after the ':' in the Derived constructor below:
1670///
1671/// @code
1672/// class Base { };
1673/// class Derived : Base {
1674/// int x;
1675/// float f;
1676/// public:
1677/// Derived(float f) : Base(), x(17), f(f) { }
1678/// };
1679/// @endcode
1680///
Mike Stump1eb44332009-09-09 15:08:12 +00001681/// [C++] ctor-initializer:
1682/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001683///
Mike Stump1eb44332009-09-09 15:08:12 +00001684/// [C++] mem-initializer-list:
1685/// mem-initializer
1686/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001687void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001688 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1689
1690 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Douglas Gregor7ad83902008-11-05 04:29:56 +00001692 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001693 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001694
Douglas Gregor7ad83902008-11-05 04:29:56 +00001695 do {
1696 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001697 if (!MemInit.isInvalid())
1698 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001699 else
1700 AnyErrors = true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001701
Douglas Gregor7ad83902008-11-05 04:29:56 +00001702 if (Tok.is(tok::comma))
1703 ConsumeToken();
1704 else if (Tok.is(tok::l_brace))
1705 break;
1706 else {
1707 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001708 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001709 SkipUntil(tok::l_brace, true, true);
1710 break;
1711 }
1712 } while (true);
1713
Mike Stump1eb44332009-09-09 15:08:12 +00001714 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001715 MemInitializers.data(), MemInitializers.size(),
1716 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001717}
1718
1719/// ParseMemInitializer - Parse a C++ member initializer, which is
1720/// part of a constructor initializer that explicitly initializes one
1721/// member or base class (C++ [class.base.init]). See
1722/// ParseConstructorInitializer for an example.
1723///
1724/// [C++] mem-initializer:
1725/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001726///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001727/// [C++] mem-initializer-id:
1728/// '::'[opt] nested-name-specifier[opt] class-name
1729/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001730Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001731 // parse '::'[opt] nested-name-specifier[opt]
1732 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001733 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001734 TypeTy *TemplateTypeTy = 0;
1735 if (Tok.is(tok::annot_template_id)) {
1736 TemplateIdAnnotation *TemplateId
1737 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001738 if (TemplateId->Kind == TNK_Type_template ||
1739 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001740 AnnotateTemplateIdTokenAsType(&SS);
1741 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1742 TemplateTypeTy = Tok.getAnnotationValue();
1743 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001744 }
1745 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001746 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001747 return true;
1748 }
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Douglas Gregor7ad83902008-11-05 04:29:56 +00001750 // Get the identifier. This may be a member name or a class name,
1751 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001752 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001753 SourceLocation IdLoc = ConsumeToken();
1754
1755 // Parse the '('.
1756 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001757 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001758 return true;
1759 }
1760 SourceLocation LParenLoc = ConsumeParen();
1761
1762 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001763 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001764 CommaLocsTy CommaLocs;
1765 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1766 SkipUntil(tok::r_paren);
1767 return true;
1768 }
1769
1770 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1771
Douglas Gregor23c94db2010-07-02 17:43:08 +00001772 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001773 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001774 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001775 ArgExprs.size(), CommaLocs.data(),
1776 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001777}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001778
1779/// ParseExceptionSpecification - Parse a C++ exception-specification
1780/// (C++ [except.spec]).
1781///
Douglas Gregora4745612008-12-01 18:00:20 +00001782/// exception-specification:
1783/// 'throw' '(' type-id-list [opt] ')'
1784/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001785///
Douglas Gregora4745612008-12-01 18:00:20 +00001786/// type-id-list:
1787/// type-id
1788/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001789///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001790bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001791 llvm::SmallVector<TypeTy*, 2>
1792 &Exceptions,
1793 llvm::SmallVector<SourceRange, 2>
1794 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001795 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001796 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001798 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001800 if (!Tok.is(tok::l_paren)) {
1801 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1802 }
1803 SourceLocation LParenLoc = ConsumeParen();
1804
Douglas Gregora4745612008-12-01 18:00:20 +00001805 // Parse throw(...), a Microsoft extension that means "this function
1806 // can throw anything".
1807 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001808 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001809 SourceLocation EllipsisLoc = ConsumeToken();
1810 if (!getLang().Microsoft)
1811 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001812 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001813 return false;
1814 }
1815
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001816 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001817 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001818 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001819 TypeResult Res(ParseTypeName(&Range));
1820 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001821 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001822 Ranges.push_back(Range);
1823 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001824 if (Tok.is(tok::comma))
1825 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001826 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001827 break;
1828 }
1829
Sebastian Redlab197ba2009-02-09 18:23:29 +00001830 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001831 return false;
1832}
Douglas Gregor6569d682009-05-27 23:11:45 +00001833
1834/// \brief We have just started parsing the definition of a new class,
1835/// so push that class onto our stack of classes that is currently
1836/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001837void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1838 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001839 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001840 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001841}
1842
1843/// \brief Deallocate the given parsed class and all of its nested
1844/// classes.
1845void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1846 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1847 DeallocateParsedClasses(Class->NestedClasses[I]);
1848 delete Class;
1849}
1850
1851/// \brief Pop the top class of the stack of classes that are
1852/// currently being parsed.
1853///
1854/// This routine should be called when we have finished parsing the
1855/// definition of a class, but have not yet popped the Scope
1856/// associated with the class's definition.
1857///
1858/// \returns true if the class we've popped is a top-level class,
1859/// false otherwise.
1860void Parser::PopParsingClass() {
1861 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Douglas Gregor6569d682009-05-27 23:11:45 +00001863 ParsingClass *Victim = ClassStack.top();
1864 ClassStack.pop();
1865 if (Victim->TopLevelClass) {
1866 // Deallocate all of the nested classes of this class,
1867 // recursively: we don't need to keep any of this information.
1868 DeallocateParsedClasses(Victim);
1869 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001870 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001871 assert(!ClassStack.empty() && "Missing top-level class?");
1872
1873 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1874 Victim->NestedClasses.empty()) {
1875 // The victim is a nested class, but we will not need to perform
1876 // any processing after the definition of this class since it has
1877 // no members whose handling was delayed. Therefore, we can just
1878 // remove this nested class.
1879 delete Victim;
1880 return;
1881 }
1882
1883 // This nested class has some members that will need to be processed
1884 // after the top-level class is completely defined. Therefore, add
1885 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001886 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregor6569d682009-05-27 23:11:45 +00001887 ClassStack.top()->NestedClasses.push_back(Victim);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001888 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00001889}
Sean Huntbbd37c62009-11-21 08:43:09 +00001890
1891/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1892/// parses standard attributes.
1893///
1894/// [C++0x] attribute-specifier:
1895/// '[' '[' attribute-list ']' ']'
1896///
1897/// [C++0x] attribute-list:
1898/// attribute[opt]
1899/// attribute-list ',' attribute[opt]
1900///
1901/// [C++0x] attribute:
1902/// attribute-token attribute-argument-clause[opt]
1903///
1904/// [C++0x] attribute-token:
1905/// identifier
1906/// attribute-scoped-token
1907///
1908/// [C++0x] attribute-scoped-token:
1909/// attribute-namespace '::' identifier
1910///
1911/// [C++0x] attribute-namespace:
1912/// identifier
1913///
1914/// [C++0x] attribute-argument-clause:
1915/// '(' balanced-token-seq ')'
1916///
1917/// [C++0x] balanced-token-seq:
1918/// balanced-token
1919/// balanced-token-seq balanced-token
1920///
1921/// [C++0x] balanced-token:
1922/// '(' balanced-token-seq ')'
1923/// '[' balanced-token-seq ']'
1924/// '{' balanced-token-seq '}'
1925/// any token but '(', ')', '[', ']', '{', or '}'
1926CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1927 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1928 && "Not a C++0x attribute list");
1929
1930 SourceLocation StartLoc = Tok.getLocation(), Loc;
1931 AttributeList *CurrAttr = 0;
1932
1933 ConsumeBracket();
1934 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001935
Sean Huntbbd37c62009-11-21 08:43:09 +00001936 if (Tok.is(tok::comma)) {
1937 Diag(Tok.getLocation(), diag::err_expected_ident);
1938 ConsumeToken();
1939 }
1940
1941 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1942 // attribute not present
1943 if (Tok.is(tok::comma)) {
1944 ConsumeToken();
1945 continue;
1946 }
1947
1948 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1949 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001950
Sean Huntbbd37c62009-11-21 08:43:09 +00001951 // scoped attribute
1952 if (Tok.is(tok::coloncolon)) {
1953 ConsumeToken();
1954
1955 if (!Tok.is(tok::identifier)) {
1956 Diag(Tok.getLocation(), diag::err_expected_ident);
1957 SkipUntil(tok::r_square, tok::comma, true, true);
1958 continue;
1959 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001960
Sean Huntbbd37c62009-11-21 08:43:09 +00001961 ScopeName = AttrName;
1962 ScopeLoc = AttrLoc;
1963
1964 AttrName = Tok.getIdentifierInfo();
1965 AttrLoc = ConsumeToken();
1966 }
1967
1968 bool AttrParsed = false;
1969 // No scoped names are supported; ideally we could put all non-standard
1970 // attributes into namespaces.
1971 if (!ScopeName) {
1972 switch(AttributeList::getKind(AttrName))
1973 {
1974 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001975 case AttributeList::AT_base_check:
1976 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001977 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001978 case AttributeList::AT_hiding:
1979 case AttributeList::AT_noreturn:
1980 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001981 if (Tok.is(tok::l_paren)) {
1982 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1983 << AttrName->getName();
1984 break;
1985 }
1986
1987 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1988 SourceLocation(), 0, 0, CurrAttr, false,
1989 true);
1990 AttrParsed = true;
1991 break;
1992 }
1993
1994 // One argument; must be a type-id or assignment-expression
1995 case AttributeList::AT_aligned: {
1996 if (Tok.isNot(tok::l_paren)) {
1997 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1998 << AttrName->getName();
1999 break;
2000 }
2001 SourceLocation ParamLoc = ConsumeParen();
2002
2003 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
2004
2005 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2006
2007 ExprVector ArgExprs(Actions);
2008 ArgExprs.push_back(ArgExpr.release());
2009 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
2010 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
2011 false, true);
2012
2013 AttrParsed = true;
2014 break;
2015 }
2016
2017 // Silence warnings
2018 default: break;
2019 }
2020 }
2021
2022 // Skip the entire parameter clause, if any
2023 if (!AttrParsed && Tok.is(tok::l_paren)) {
2024 ConsumeParen();
2025 // SkipUntil maintains the balancedness of tokens.
2026 SkipUntil(tok::r_paren, false);
2027 }
2028 }
2029
2030 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2031 SkipUntil(tok::r_square, false);
2032 Loc = Tok.getLocation();
2033 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2034 SkipUntil(tok::r_square, false);
2035
2036 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
2037 return Attr;
2038}
2039
2040/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2041/// attribute.
2042///
2043/// FIXME: Simply returns an alignof() expression if the argument is a
2044/// type. Ideally, the type should be propagated directly into Sema.
2045///
2046/// [C++0x] 'align' '(' type-id ')'
2047/// [C++0x] 'align' '(' assignment-expression ')'
2048Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
2049 if (isTypeIdInParens()) {
2050 EnterExpressionEvaluationContext Unevaluated(Actions,
2051 Action::Unevaluated);
2052 SourceLocation TypeLoc = Tok.getLocation();
2053 TypeTy *Ty = ParseTypeName().get();
2054 SourceRange TypeRange(Start, Tok.getLocation());
2055 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
2056 TypeRange);
2057 } else
2058 return ParseConstantExpression();
2059}