blob: dbbfb9518b33ffacdacbd26294f641669461224e [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)) {
52 Actions.CodeCompleteNamespaceDecl(CurScope);
53 ConsumeToken();
54 }
55
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
90 // Enter a scope for the namespace.
91 ParseScope NamespaceScope(this, Scope::DeclScope);
92
93 DeclPtrTy NamespcDecl =
Ted Kremenek1e377652010-02-11 02:19:13 +000094 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace,
95 AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +000096
97 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
98 PP.getSourceManager(),
99 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Sean Huntbbd37c62009-11-21 08:43:09 +0000101 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
102 CXX0XAttributeList Attr;
103 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
104 Attr = ParseCXX0XAttributes();
105 ParseExternalDeclaration(Attr);
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner51448322009-03-29 14:02:43 +0000108 // Leave the namespace scope.
109 NamespaceScope.Exit();
110
Chris Lattner97144fc2009-04-02 04:16:50 +0000111 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
112 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000113
Chris Lattner97144fc2009-04-02 04:16:50 +0000114 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000115 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000117
Anders Carlssonf67606a2009-03-28 04:07:16 +0000118/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
119/// alias definition.
120///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000121Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000122 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000123 IdentifierInfo *Alias,
124 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000125 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Anders Carlssonf67606a2009-03-28 04:07:16 +0000127 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000129 if (Tok.is(tok::code_completion)) {
130 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
131 ConsumeToken();
132 }
133
Anders Carlssonf67606a2009-03-28 04:07:16 +0000134 CXXScopeSpec SS;
135 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000136 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137
138 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
139 Diag(Tok, diag::err_expected_namespace_name);
140 // Skip to end of the definition and eat the ';'.
141 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000142 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000143 }
144
145 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000146 IdentifierInfo *Ident = Tok.getIdentifierInfo();
147 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Anders Carlssonf67606a2009-03-28 04:07:16 +0000149 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000150 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000151 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
152 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
154 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000155 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000156}
157
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158/// ParseLinkage - We know that the current token is a string_literal
159/// and just before that, that extern was seen.
160///
161/// linkage-specification: [C++ 7.5p2: dcl.link]
162/// 'extern' string-literal '{' declaration-seq[opt] '}'
163/// 'extern' string-literal declaration
164///
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000165Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
166 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000167 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000168 llvm::SmallVector<char, 8> LangBuffer;
169 // LangBuffer is guaranteed to be big enough.
170 LangBuffer.resize(Tok.getLength());
171 const char *LangBufPtr = &LangBuffer[0];
172 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
173
174 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175
Douglas Gregor074149e2009-01-05 19:45:36 +0000176 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000177 DeclPtrTy LinkageSpec
178 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000179 /*FIXME: */SourceLocation(),
180 Loc, LangBufPtr, StrSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000181 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000182 : SourceLocation());
183
Sean Huntbbd37c62009-11-21 08:43:09 +0000184 CXX0XAttributeList Attr;
185 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
186 Attr = ParseCXX0XAttributes();
187 }
188
Douglas Gregor074149e2009-01-05 19:45:36 +0000189 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000190 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000191 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000192 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000193 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000194
Douglas Gregor63a01132010-02-07 08:38:28 +0000195 DS.abort();
196
Sean Huntbbd37c62009-11-21 08:43:09 +0000197 if (Attr.HasAttr)
198 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
199 << Attr.Range;
200
Douglas Gregorf44515a2008-12-16 22:23:02 +0000201 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000202 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000203 CXX0XAttributeList Attr;
204 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
205 Attr = ParseCXX0XAttributes();
206 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000207 }
208
Douglas Gregorf44515a2008-12-16 22:23:02 +0000209 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000210 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000211}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000212
Douglas Gregorf780abc2008-12-30 03:27:21 +0000213/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
214/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000215Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000216 SourceLocation &DeclEnd,
217 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000218 assert(Tok.is(tok::kw_using) && "Not using token");
219
220 // Eat 'using'.
221 SourceLocation UsingLoc = ConsumeToken();
222
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000223 if (Tok.is(tok::code_completion)) {
224 Actions.CodeCompleteUsing(CurScope);
225 ConsumeToken();
226 }
227
Chris Lattner2f274772009-01-06 06:55:51 +0000228 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000229 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000230 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
231
232 if (Attr.HasAttr)
233 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
234 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000235
236 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000237 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000238 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000239}
240
241/// ParseUsingDirective - Parse C++ using-directive, assumes
242/// that current token is 'namespace' and 'using' was already parsed.
243///
244/// using-directive: [C++ 7.3.p4: namespace.udir]
245/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
246/// namespace-name ;
247/// [GNU] using-directive:
248/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
249/// namespace-name attributes[opt] ;
250///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000251Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000252 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000253 SourceLocation &DeclEnd,
254 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000255 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
256
257 // Eat 'namespace'.
258 SourceLocation NamespcLoc = ConsumeToken();
259
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000260 if (Tok.is(tok::code_completion)) {
261 Actions.CodeCompleteUsingDirective(CurScope);
262 ConsumeToken();
263 }
264
Douglas Gregorf780abc2008-12-30 03:27:21 +0000265 CXXScopeSpec SS;
266 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000267 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000268
Douglas Gregorf780abc2008-12-30 03:27:21 +0000269 IdentifierInfo *NamespcName = 0;
270 SourceLocation IdentLoc = SourceLocation();
271
272 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000273 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000274 Diag(Tok, diag::err_expected_namespace_name);
275 // If there was invalid namespace name, skip to end of decl, and eat ';'.
276 SkipUntil(tok::semi);
277 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000278 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000279 }
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattner823c44e2009-01-06 07:27:21 +0000281 // Parse identifier.
282 NamespcName = Tok.getIdentifierInfo();
283 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chris Lattner823c44e2009-01-06 07:27:21 +0000285 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000286 bool GNUAttr = false;
287 if (Tok.is(tok::kw___attribute)) {
288 GNUAttr = true;
289 Attr = addAttributeLists(Attr, ParseGNUAttributes());
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner823c44e2009-01-06 07:27:21 +0000292 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000293 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000294 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000295 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000296 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000297
298 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000299 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000300}
301
302/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
303/// 'using' was already seen.
304///
305/// using-declaration: [C++ 7.3.p3: namespace.udecl]
306/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000307/// unqualified-id
308/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000309///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000310Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000311 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000312 SourceLocation &DeclEnd,
313 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000314 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000315 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000316 bool IsTypeName;
317
318 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000319 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000320 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000321 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000322 ConsumeToken();
323 IsTypeName = true;
324 }
325 else
326 IsTypeName = false;
327
328 // Parse nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000329 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000330
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000331 // Check nested-name specifier.
332 if (SS.isInvalid()) {
333 SkipUntil(tok::semi);
334 return DeclPtrTy();
335 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000336
337 // Parse the unqualified-id. We allow parsing of both constructor and
338 // destructor names and allow the action module to diagnose any semantic
339 // errors.
340 UnqualifiedId Name;
341 if (ParseUnqualifiedId(SS,
342 /*EnteringContext=*/false,
343 /*AllowDestructorName=*/true,
344 /*AllowConstructorName=*/true,
345 /*ObjectType=*/0,
346 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000347 SkipUntil(tok::semi);
348 return DeclPtrTy();
349 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000350
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000351 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000352 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000353 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000354 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000356 // Eat ';'.
357 DeclEnd = Tok.getLocation();
358 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000359 AttrList ? "attributes list" : "using declaration",
360 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000361
John McCall60fa3cf2009-12-11 02:10:03 +0000362 return Actions.ActOnUsingDeclaration(CurScope, AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000363 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000364}
365
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000366/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
367///
368/// static_assert-declaration:
369/// static_assert ( constant-expression , string-literal ) ;
370///
Chris Lattner97144fc2009-04-02 04:16:50 +0000371Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000372 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
373 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000375 if (Tok.isNot(tok::l_paren)) {
376 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000377 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000380 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000381
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000382 OwningExprResult AssertExpr(ParseConstantExpression());
383 if (AssertExpr.isInvalid()) {
384 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000385 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Anders Carlssonad5f9602009-03-13 23:29:20 +0000388 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000389 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000390
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000391 if (Tok.isNot(tok::string_literal)) {
392 Diag(Tok, diag::err_expected_string_literal);
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 Carlsson511d7ab2009-03-11 16:27:10 +0000397 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000398 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000399 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000400
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000401 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner97144fc2009-04-02 04:16:50 +0000403 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000404 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
405
Mike Stump1eb44332009-09-09 15:08:12 +0000406 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000407 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000408}
409
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000410/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
411///
412/// 'decltype' ( expression )
413///
414void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
415 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
416
417 SourceLocation StartLoc = ConsumeToken();
418 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000419
420 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000421 "decltype")) {
422 SkipUntil(tok::r_paren);
423 return;
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000426 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000428 // C++0x [dcl.type.simple]p4:
429 // The operand of the decltype specifier is an unevaluated operand.
430 EnterExpressionEvaluationContext Unevaluated(Actions,
431 Action::Unevaluated);
432 OwningExprResult Result = ParseExpression();
433 if (Result.isInvalid()) {
434 SkipUntil(tok::r_paren);
435 return;
436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000438 // Match the ')'
439 SourceLocation RParenLoc;
440 if (Tok.is(tok::r_paren))
441 RParenLoc = ConsumeParen();
442 else
443 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000445 if (RParenLoc.isInvalid())
446 return;
447
448 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000449 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000450 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000451 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000452 DiagID, Result.release()))
453 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000454}
455
Douglas Gregor42a552f2008-11-05 20:51:48 +0000456/// ParseClassName - Parse a C++ class-name, which names a class. Note
457/// that we only check that the result names a type; semantic analysis
458/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000459/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000460/// found.
461///
462/// class-name: [C++ 9.1]
463/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000464/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000465///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000466Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregor124b8782010-02-16 19:09:40 +0000467 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000468 // Check whether we have a template-id that names a type.
469 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000470 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000471 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000472 if (TemplateId->Kind == TNK_Type_template ||
473 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000474 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000475
476 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
477 TypeTy *Type = Tok.getAnnotationValue();
478 EndLocation = Tok.getAnnotationEndLoc();
479 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000480
481 if (Type)
482 return Type;
483 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000484 }
485
486 // Fall through to produce an error below.
487 }
488
Douglas Gregor42a552f2008-11-05 20:51:48 +0000489 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000490 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000491 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000492 }
493
Douglas Gregor84d0a192010-01-12 21:28:44 +0000494 IdentifierInfo *Id = Tok.getIdentifierInfo();
495 SourceLocation IdLoc = ConsumeToken();
496
497 if (Tok.is(tok::less)) {
498 // It looks the user intended to write a template-id here, but the
499 // template-name was wrong. Try to fix that.
500 TemplateNameKind TNK = TNK_Type_template;
501 TemplateTy Template;
502 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, CurScope,
503 SS, Template, TNK)) {
504 Diag(IdLoc, diag::err_unknown_template_name)
505 << Id;
506 }
507
508 if (!Template)
509 return true;
510
511 // Form the template name
512 UnqualifiedId TemplateName;
513 TemplateName.setIdentifier(Id, IdLoc);
514
515 // Parse the full template-id, then turn it into a type.
516 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
517 SourceLocation(), true))
518 return true;
519 if (TNK == TNK_Dependent_template_name)
520 AnnotateTemplateIdTokenAsType(SS);
521
522 // If we didn't end up with a typename token, there's nothing more we
523 // can do.
524 if (Tok.isNot(tok::annot_typename))
525 return true;
526
527 // Retrieve the type from the annotation token, consume that token, and
528 // return.
529 EndLocation = Tok.getAnnotationEndLoc();
530 TypeTy *Type = Tok.getAnnotationValue();
531 ConsumeToken();
532 return Type;
533 }
534
Douglas Gregor42a552f2008-11-05 20:51:48 +0000535 // We have an identifier; check whether it is actually a type.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000536 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, CurScope, SS, true);
537 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000538 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000539 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000540 }
541
542 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000543 EndLocation = IdLoc;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000544 return Type;
545}
546
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000547/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
548/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
549/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000550/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000551///
552/// class-specifier: [C++ class]
553/// class-head '{' member-specification[opt] '}'
554/// class-head '{' member-specification[opt] '}' attributes[opt]
555/// class-head:
556/// class-key identifier[opt] base-clause[opt]
557/// class-key nested-name-specifier identifier base-clause[opt]
558/// class-key nested-name-specifier[opt] simple-template-id
559/// base-clause[opt]
560/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000561/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000562/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000563/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000564/// simple-template-id base-clause[opt]
565/// class-key:
566/// 'class'
567/// 'struct'
568/// 'union'
569///
570/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000571/// class-key ::[opt] nested-name-specifier[opt] identifier
572/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
573/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000574///
575/// Note that the C++ class-specifier and elaborated-type-specifier,
576/// together, subsume the C99 struct-or-union-specifier:
577///
578/// struct-or-union-specifier: [C99 6.7.2.1]
579/// struct-or-union identifier[opt] '{' struct-contents '}'
580/// struct-or-union identifier
581/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
582/// '}' attributes[opt]
583/// [GNU] struct-or-union attributes[opt] identifier
584/// struct-or-union:
585/// 'struct'
586/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000587void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
588 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000589 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000590 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000591 DeclSpec::TST TagType;
592 if (TagTokKind == tok::kw_struct)
593 TagType = DeclSpec::TST_struct;
594 else if (TagTokKind == tok::kw_class)
595 TagType = DeclSpec::TST_class;
596 else {
597 assert(TagTokKind == tok::kw_union && "Not a class specifier");
598 TagType = DeclSpec::TST_union;
599 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000600
Douglas Gregor374929f2009-09-18 15:37:17 +0000601 if (Tok.is(tok::code_completion)) {
602 // Code completion for a struct, class, or union name.
603 Actions.CodeCompleteTag(CurScope, TagType);
604 ConsumeToken();
605 }
606
Sean Huntbbd37c62009-11-21 08:43:09 +0000607 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000608 // If attributes exist after tag, parse them.
609 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000610 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000611
Steve Narofff59e17e2008-12-24 20:59:21 +0000612 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000613 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000614 AttrList = ParseMicrosoftDeclSpec(AttrList);
615
616 // If C++0x attributes exist here, parse them.
617 // FIXME: Are we consistent with the ordering of parsing of different
618 // styles of attributes?
619 if (isCXX0XAttributeSpecifier())
620 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Douglas Gregorb117a602009-09-04 05:53:02 +0000622 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
623 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
624 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000625 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000626 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
627 // properly.
628 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
629 Tok.setKind(tok::identifier);
630 }
631
632 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
633 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
634 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000635 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000636 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
637 // properly.
638 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
639 Tok.setKind(tok::identifier);
640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000642 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000643 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000644 if (getLang().CPlusPlus) {
645 // "FOO : BAR" is not a potential typo for "FOO::BAR".
646 ColonProtectionRAIIObject X(*this);
647
John McCall9ba61662010-02-26 08:45:28 +0000648 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
649 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000650 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
651 Diag(Tok, diag::err_expected_ident);
652 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000653
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000654 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
655
Douglas Gregorcc636682009-02-17 23:15:12 +0000656 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000657 IdentifierInfo *Name = 0;
658 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000659 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000660 if (Tok.is(tok::identifier)) {
661 Name = Tok.getIdentifierInfo();
662 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000663
664 if (Tok.is(tok::less)) {
665 // The name was supposed to refer to a template, but didn't.
666 // Eat the template argument list and try to continue parsing this as
667 // a class (or template thereof).
668 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000669 SourceLocation LAngleLoc, RAngleLoc;
670 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
671 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000672 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000673 // We couldn't parse the template argument list at all, so don't
674 // try to give any location information for the list.
675 LAngleLoc = RAngleLoc = SourceLocation();
676 }
677
678 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000679 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000680 << (TagType == DeclSpec::TST_class? 0
681 : TagType == DeclSpec::TST_struct? 1
682 : 2)
683 << Name
684 << SourceRange(LAngleLoc, RAngleLoc);
685
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000686 // Strip off the last template parameter list if it was empty, since
687 // we've removed its template argument list.
688 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
689 if (TemplateParams && TemplateParams->size() > 1) {
690 TemplateParams->pop_back();
691 } else {
692 TemplateParams = 0;
693 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
694 = ParsedTemplateInfo::NonTemplate;
695 }
696 } else if (TemplateInfo.Kind
697 == ParsedTemplateInfo::ExplicitInstantiation) {
698 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000699 TemplateParams = 0;
700 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
701 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000702 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
703 = SourceLocation();
704 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
705 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000706 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000707
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000708
709 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000710 } else if (Tok.is(tok::annot_template_id)) {
711 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
712 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000713
Douglas Gregorc45c2322009-03-31 00:43:58 +0000714 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000715 // The template-name in the simple-template-id refers to
716 // something other than a class template. Give an appropriate
717 // error message and skip to the ';'.
718 SourceRange Range(NameLoc);
719 if (SS.isNotEmpty())
720 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000721
Douglas Gregor39a8de12009-02-25 19:37:18 +0000722 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
723 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregor39a8de12009-02-25 19:37:18 +0000725 DS.SetTypeSpecError();
726 SkipUntil(tok::semi, false, true);
727 TemplateId->Destroy();
728 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000729 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000730 }
731
John McCall67d1a672009-08-06 02:15:43 +0000732 // There are four options here. If we have 'struct foo;', then this
733 // is either a forward declaration or a friend declaration, which
734 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000735 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000736 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000737 // However, in some contexts, things look like declarations but are just
738 // references, e.g.
739 // new struct s;
740 // or
741 // &T::operator struct s;
742 // For these, SuppressDeclarations is true.
John McCall0f434ec2009-07-31 02:45:11 +0000743 Action::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000744 if (SuppressDeclarations)
745 TUK = Action::TUK_Reference;
746 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000747 if (DS.isFriendSpecified()) {
748 // C++ [class.friend]p2:
749 // A class shall not be defined in a friend declaration.
750 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
751 << SourceRange(DS.getFriendSpecLoc());
752
753 // Skip everything up to the semicolon, so that this looks like a proper
754 // friend class (or template thereof) declaration.
755 SkipUntil(tok::semi, true, true);
756 TUK = Action::TUK_Friend;
757 } else {
758 // Okay, this is a class definition.
759 TUK = Action::TUK_Definition;
760 }
761 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000762 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000763 else
John McCall0f434ec2009-07-31 02:45:11 +0000764 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000765
John McCall0f434ec2009-07-31 02:45:11 +0000766 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000767 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000768 Diag(StartLoc, diag::err_anon_type_definition)
769 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000770
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000771 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000772
773 if (TemplateId)
774 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000775 return;
776 }
777
Douglas Gregorddc29e12009-02-06 22:42:48 +0000778 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000779 Action::DeclResult TagOrTempResult = true; // invalid
780 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000781
John McCall0f434ec2009-07-31 02:45:11 +0000782 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000783 // to turn that template-id into a type.
784
Douglas Gregor402abb52009-05-28 23:31:59 +0000785 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000786 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000787 // Explicit specialization, class template partial specialization,
788 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000789 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000790 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000791 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000792 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000793 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000794 // This is an explicit instantiation of a class template.
795 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000796 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000797 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000798 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000799 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000800 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000801 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000802 TemplateTy::make(TemplateId->Template),
803 TemplateId->TemplateNameLoc,
804 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000805 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000806 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000807 AttrList);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000808 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000809 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000810 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
811 TemplateId->TemplateNameLoc,
812 TemplateId->LAngleLoc,
813 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000814 TemplateId->RAngleLoc);
815
John McCallc4e70192009-09-11 04:59:25 +0000816 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
817 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000818 } else {
819 // This is an explicit specialization or a class template
820 // partial specialization.
821 TemplateParameterLists FakedParamLists;
822
823 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
824 // This looks like an explicit instantiation, because we have
825 // something like
826 //
827 // template class Foo<X>
828 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000829 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000830 // meant to be an explicit specialization, but the user forgot
831 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000832 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000833
Mike Stump1eb44332009-09-09 15:08:12 +0000834 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000835 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000836 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000837 diag::err_explicit_instantiation_with_definition)
838 << SourceRange(TemplateInfo.TemplateLoc)
839 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
840
841 // Create a fake template parameter list that contains only
842 // "template<>", so that we treat this construct as a class
843 // template specialization.
844 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000845 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000846 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000847 LAngleLoc,
848 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000849 LAngleLoc));
850 TemplateParams = &FakedParamLists;
851 }
852
853 // Build the class template specialization.
854 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000855 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000856 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000857 TemplateTy::make(TemplateId->Template),
858 TemplateId->TemplateNameLoc,
859 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000860 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000861 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000862 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000863 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000864 TemplateParams? &(*TemplateParams)[0] : 0,
865 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000866 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000867 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000868 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000869 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000870 // Explicit instantiation of a member of a class template
871 // specialization, e.g.,
872 //
873 // template struct Outer<int>::Inner;
874 //
875 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000876 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000877 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000878 TemplateInfo.TemplateLoc,
879 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000880 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000881 } else {
882 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000883 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000884 // FIXME: Diagnose this particular error.
885 }
886
John McCallc4e70192009-09-11 04:59:25 +0000887 bool IsDependent = false;
888
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000889 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000890 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000891 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000892 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000893 TemplateParams? &(*TemplateParams)[0] : 0,
894 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000895 Owned, IsDependent);
896
897 // If ActOnTag said the type was dependent, try again with the
898 // less common call.
899 if (IsDependent)
900 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
901 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000902 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000903
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000904 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000905 if (TUK == Action::TUK_Definition) {
906 assert(Tok.is(tok::l_brace) ||
907 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000908 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000909 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000910 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000911 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000912 }
913
John McCallc4e70192009-09-11 04:59:25 +0000914 void *Result;
915 if (!TypeResult.isInvalid()) {
916 TagType = DeclSpec::TST_typename;
917 Result = TypeResult.get();
918 Owned = false;
919 } else if (!TagOrTempResult.isInvalid()) {
920 Result = TagOrTempResult.get().getAs<void>();
921 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000922 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000923 return;
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
John McCallfec54012009-08-03 20:12:06 +0000926 const char *PrevSpec = 0;
927 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000928
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000929 // FIXME: The DeclSpec should keep the locations of both the keyword and the
930 // name (if there is one).
931 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
932
933 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000934 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000935 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000936
937 // At this point, we've successfully parsed a class-specifier in 'definition'
938 // form (e.g. "struct foo { int x; }". While we could just return here, we're
939 // going to look at what comes after it to improve error recovery. If an
940 // impossible token occurs next, we assume that the programmer forgot a ; at
941 // the end of the declaration and recover that way.
942 //
943 // This switch enumerates the valid "follow" set for definition.
944 if (TUK == Action::TUK_Definition) {
945 switch (Tok.getKind()) {
946 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000947 case tok::star: // struct foo {...} * P;
948 case tok::amp: // struct foo {...} & R = ...
949 case tok::identifier: // struct foo {...} V ;
950 case tok::r_paren: //(struct foo {...} ) {4}
951 case tok::annot_cxxscope: // struct foo {...} a:: b;
952 case tok::annot_typename: // struct foo {...} a ::b;
953 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +0000954 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +0000955 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattner99c95202010-02-02 17:32:27 +0000956 // Storage-class specifiers
957 case tok::kw_static: // struct foo {...} static x;
958 case tok::kw_extern: // struct foo {...} extern x;
959 case tok::kw_typedef: // struct foo {...} typedef x;
960 case tok::kw_register: // struct foo {...} register x;
961 case tok::kw_auto: // struct foo {...} auto x;
962 // Type qualifiers
963 case tok::kw_const: // struct foo {...} const x;
964 case tok::kw_volatile: // struct foo {...} volatile x;
965 case tok::kw_restrict: // struct foo {...} restrict x;
966 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner4ed5d912010-02-02 01:23:29 +0000967 break;
968
969 case tok::r_brace: // struct bar { struct foo {...} }
970 // Missing ';' at end of struct is accepted as an extension in C mode.
971 if (!getLang().CPlusPlus) break;
972 // FALL THROUGH.
973 default:
974 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
975 TagType == DeclSpec::TST_class ? "class"
976 : TagType == DeclSpec::TST_struct? "struct" : "union");
977 // Push this token back into the preprocessor and change our current token
978 // to ';' so that the rest of the code recovers as though there were an
979 // ';' after the definition.
980 PP.EnterToken(Tok);
981 Tok.setKind(tok::semi);
982 break;
983 }
984 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000985}
986
Mike Stump1eb44332009-09-09 15:08:12 +0000987/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000988///
989/// base-clause : [C++ class.derived]
990/// ':' base-specifier-list
991/// base-specifier-list:
992/// base-specifier '...'[opt]
993/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000994void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000995 assert(Tok.is(tok::colon) && "Not a base clause");
996 ConsumeToken();
997
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000998 // Build up an array of parsed base specifiers.
999 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1000
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001001 while (true) {
1002 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001003 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001004 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001005 // Skip the rest of this base specifier, up until the comma or
1006 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001007 SkipUntil(tok::comma, tok::l_brace, true, true);
1008 } else {
1009 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001010 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001011 }
1012
1013 // If the next token is a comma, consume it and keep reading
1014 // base-specifiers.
1015 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001017 // Consume the comma.
1018 ConsumeToken();
1019 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001020
1021 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001022 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001023}
1024
1025/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1026/// one entry in the base class list of a class specifier, for example:
1027/// class foo : public bar, virtual private baz {
1028/// 'public bar' and 'virtual private baz' are each base-specifiers.
1029///
1030/// base-specifier: [C++ class.derived]
1031/// ::[opt] nested-name-specifier[opt] class-name
1032/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1033/// class-name
1034/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1035/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +00001036Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001037 bool IsVirtual = false;
1038 SourceLocation StartLoc = Tok.getLocation();
1039
1040 // Parse the 'virtual' keyword.
1041 if (Tok.is(tok::kw_virtual)) {
1042 ConsumeToken();
1043 IsVirtual = true;
1044 }
1045
1046 // Parse an (optional) access specifier.
1047 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001048 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001049 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001051 // Parse the 'virtual' keyword (again!), in case it came after the
1052 // access specifier.
1053 if (Tok.is(tok::kw_virtual)) {
1054 SourceLocation VirtualLoc = ConsumeToken();
1055 if (IsVirtual) {
1056 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001057 Diag(VirtualLoc, diag::err_dup_virtual)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001058 << CodeModificationHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001059 }
1060
1061 IsVirtual = true;
1062 }
1063
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001064 // Parse optional '::' and optional nested-name-specifier.
1065 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001066 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001067
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001068 // The location of the base class itself.
1069 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001070
1071 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001072 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001073 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1074 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001075 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
1077 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001078 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001080 // Notify semantic analysis that we have parsed a complete
1081 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001082 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001083 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001084}
1085
1086/// getAccessSpecifierIfPresent - Determine whether the next token is
1087/// a C++ access-specifier.
1088///
1089/// access-specifier: [C++ class.derived]
1090/// 'private'
1091/// 'protected'
1092/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001093AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001094 switch (Tok.getKind()) {
1095 default: return AS_none;
1096 case tok::kw_private: return AS_private;
1097 case tok::kw_protected: return AS_protected;
1098 case tok::kw_public: return AS_public;
1099 }
1100}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001101
Eli Friedmand33133c2009-07-22 21:45:50 +00001102void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1103 DeclPtrTy ThisDecl) {
1104 // We just declared a member function. If this member function
1105 // has any default arguments, we'll need to parse them later.
1106 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001107 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001108 = DeclaratorInfo.getTypeObject(0).Fun;
1109 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1110 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1111 if (!LateMethod) {
1112 // Push this method onto the stack of late-parsed method
1113 // declarations.
1114 getCurrentClass().MethodDecls.push_back(
1115 LateParsedMethodDeclaration(ThisDecl));
1116 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001117 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001118
1119 // Add all of the parameters prior to this one (they don't
1120 // have default arguments).
1121 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1122 for (unsigned I = 0; I < ParamIdx; ++I)
1123 LateMethod->DefaultArgs.push_back(
1124 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1125 }
1126
1127 // Add this parameter to the list of parameters (it or may
1128 // not have a default argument).
1129 LateMethod->DefaultArgs.push_back(
1130 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1131 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1132 }
1133 }
1134}
1135
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001136/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1137///
1138/// member-declaration:
1139/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1140/// function-definition ';'[opt]
1141/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1142/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001143/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001144/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001145/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001146///
1147/// member-declarator-list:
1148/// member-declarator
1149/// member-declarator-list ',' member-declarator
1150///
1151/// member-declarator:
1152/// declarator pure-specifier[opt]
1153/// declarator constant-initializer[opt]
1154/// identifier[opt] ':' constant-expression
1155///
Sebastian Redle2b68332009-04-12 17:16:29 +00001156/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001157/// '= 0'
1158///
1159/// constant-initializer:
1160/// '=' constant-expression
1161///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001162void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1163 const ParsedTemplateInfo &TemplateInfo) {
John McCall60fa3cf2009-12-11 02:10:03 +00001164 // Access declarations.
1165 if (!TemplateInfo.Kind &&
1166 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001167 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001168 Tok.is(tok::annot_cxxscope)) {
1169 bool isAccessDecl = false;
1170 if (NextToken().is(tok::identifier))
1171 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1172 else
1173 isAccessDecl = NextToken().is(tok::kw_operator);
1174
1175 if (isAccessDecl) {
1176 // Collect the scope specifier token we annotated earlier.
1177 CXXScopeSpec SS;
1178 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1179
1180 // Try to parse an unqualified-id.
1181 UnqualifiedId Name;
1182 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1183 SkipUntil(tok::semi);
1184 return;
1185 }
1186
1187 // TODO: recover from mistakenly-qualified operator declarations.
1188 if (ExpectAndConsume(tok::semi,
1189 diag::err_expected_semi_after,
1190 "access declaration",
1191 tok::semi))
1192 return;
1193
1194 Actions.ActOnUsingDeclaration(CurScope, AS,
1195 false, SourceLocation(),
1196 SS, Name,
1197 /* AttrList */ 0,
1198 /* IsTypeName */ false,
1199 SourceLocation());
1200 return;
1201 }
1202 }
1203
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001204 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001205 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001206 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001207 SourceLocation DeclEnd;
1208 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001209 return;
1210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Chris Lattner682bf922009-03-29 16:50:03 +00001212 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001213 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001214 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001215 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001216 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001217 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001218 return;
1219 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001220
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001221 // Handle: member-declaration ::= '__extension__' member-declaration
1222 if (Tok.is(tok::kw___extension__)) {
1223 // __extension__ silences extension warnings in the subexpression.
1224 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1225 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001226 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001227 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001228
Chris Lattner4ed5d912010-02-02 01:23:29 +00001229 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1230 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001231 ColonProtectionRAIIObject X(*this);
1232
Sean Huntbbd37c62009-11-21 08:43:09 +00001233 CXX0XAttributeList AttrList;
1234 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001235 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001236 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001237
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001238 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001239 // FIXME: Check for template aliases
Sean Huntbbd37c62009-11-21 08:43:09 +00001240
1241 if (AttrList.HasAttr)
1242 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1243 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001245 // Eat 'using'.
1246 SourceLocation UsingLoc = ConsumeToken();
1247
1248 if (Tok.is(tok::kw_namespace)) {
1249 Diag(UsingLoc, diag::err_using_namespace_in_class);
1250 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001251 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001252 SourceLocation DeclEnd;
1253 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001254 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001255 }
1256 return;
1257 }
1258
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001259 SourceLocation DSStart = Tok.getLocation();
1260 // decl-specifier-seq:
1261 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001262 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001263 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001264 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001265
John McCalldd4a3b02009-09-16 22:47:08 +00001266 Action::MultiTemplateParamsArg TemplateParams(Actions,
1267 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1268 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1269
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001270 if (Tok.is(tok::semi)) {
1271 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001272 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001273 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001274 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001275
John McCall54abf7d2009-11-04 02:18:39 +00001276 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001277
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001278 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001279 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1280 ColonProtectionRAIIObject X(*this);
1281
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001282 // Parse the first declarator.
1283 ParseDeclarator(DeclaratorInfo);
1284 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001285 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001286 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001287 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001288 if (Tok.is(tok::semi))
1289 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001290 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001291 }
1292
John Thompson1b2fc0f2009-11-25 22:58:06 +00001293 // If attributes exist after the declarator, but before an '{', parse them.
1294 if (Tok.is(tok::kw___attribute)) {
1295 SourceLocation Loc;
1296 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1297 DeclaratorInfo.AddAttributes(AttrList, Loc);
1298 }
1299
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001300 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001301 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001302 || (DeclaratorInfo.isFunctionDeclarator() &&
1303 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001304 if (!DeclaratorInfo.isFunctionDeclarator()) {
1305 Diag(Tok, diag::err_func_def_no_params);
1306 ConsumeBrace();
1307 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001308 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001309 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001310
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001311 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1312 Diag(Tok, diag::err_function_declared_typedef);
1313 // This recovery skips the entire function body. It would be nice
1314 // to simply call ParseCXXInlineMethodDef() below, however Sema
1315 // assumes the declarator represents a function, not a typedef.
1316 ConsumeBrace();
1317 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001318 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001319 }
1320
Douglas Gregor37b372b2009-08-20 22:52:58 +00001321 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001322 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001323 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001324 }
1325
1326 // member-declarator-list:
1327 // member-declarator
1328 // member-declarator-list ',' member-declarator
1329
Chris Lattner682bf922009-03-29 16:50:03 +00001330 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001331 OwningExprResult BitfieldSize(Actions);
1332 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001333 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001334
1335 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001336 // member-declarator:
1337 // declarator pure-specifier[opt]
1338 // declarator constant-initializer[opt]
1339 // identifier[opt] ':' constant-expression
1340
1341 if (Tok.is(tok::colon)) {
1342 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001343 BitfieldSize = ParseConstantExpression();
1344 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001345 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001346 }
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001348 // pure-specifier:
1349 // '= 0'
1350 //
1351 // constant-initializer:
1352 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001353 //
1354 // defaulted/deleted function-definition:
1355 // '=' 'default' [TODO]
1356 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001357
1358 if (Tok.is(tok::equal)) {
1359 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001360 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1361 ConsumeToken();
1362 Deleted = true;
1363 } else {
1364 Init = ParseInitializer();
1365 if (Init.isInvalid())
1366 SkipUntil(tok::comma, true, true);
1367 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001368 }
1369
1370 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001371 if (Tok.is(tok::kw___attribute)) {
1372 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001373 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001374 DeclaratorInfo.AddAttributes(AttrList, Loc);
1375 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001376
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001377 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001378 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001379 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001380
1381 DeclPtrTy ThisDecl;
1382 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001383 // TODO: handle initializers, bitfields, 'delete'
1384 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1385 /*IsDefinition*/ false,
1386 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001387 } else {
John McCall67d1a672009-08-06 02:15:43 +00001388 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1389 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001390 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001391 BitfieldSize.release(),
1392 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001393 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001394 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001395 }
Chris Lattner682bf922009-03-29 16:50:03 +00001396 if (ThisDecl)
1397 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001398
Douglas Gregor72b505b2008-12-16 21:30:33 +00001399 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001400 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001401 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001402 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001403 }
1404
John McCall54abf7d2009-11-04 02:18:39 +00001405 DeclaratorInfo.complete(ThisDecl);
1406
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001407 // If we don't have a comma, it is either the end of the list (a ';')
1408 // or an error, bail out.
1409 if (Tok.isNot(tok::comma))
1410 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001412 // Consume the comma.
1413 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001415 // Parse the next declarator.
1416 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001417 BitfieldSize = 0;
1418 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001419 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001421 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001422 if (Tok.is(tok::kw___attribute)) {
1423 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001424 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001425 DeclaratorInfo.AddAttributes(AttrList, Loc);
1426 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001427
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001428 if (Tok.isNot(tok::colon))
1429 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001430 }
1431
Chris Lattnerae50d502010-02-02 00:43:15 +00001432 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1433 // Skip to end of block or statement.
1434 SkipUntil(tok::r_brace, true, true);
1435 // If we stopped at a ';', eat it.
1436 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001437 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001438 }
1439
Chris Lattnerae50d502010-02-02 00:43:15 +00001440 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
1441 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001442}
1443
1444/// ParseCXXMemberSpecification - Parse the class definition.
1445///
1446/// member-specification:
1447/// member-declaration member-specification[opt]
1448/// access-specifier ':' member-specification[opt]
1449///
1450void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001451 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001452 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001453 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001454 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001455
Chris Lattner49f28ca2009-03-05 08:00:35 +00001456 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1457 PP.getSourceManager(),
1458 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Douglas Gregor26997fd2010-01-16 20:52:59 +00001460 // Determine whether this is a non-nested class. Note that local
1461 // classes are *not* considered to be nested classes.
1462 bool NonNestedClass = true;
1463 if (!ClassStack.empty()) {
1464 for (const Scope *S = CurScope; S; S = S->getParent()) {
1465 if (S->isClassScope()) {
1466 // We're inside a class scope, so this is a nested class.
1467 NonNestedClass = false;
1468 break;
1469 }
1470
1471 if ((S->getFlags() & Scope::FnScope)) {
1472 // If we're in a function or function template declared in the
1473 // body of a class, then this is a local class rather than a
1474 // nested class.
1475 const Scope *Parent = S->getParent();
1476 if (Parent->isTemplateParamScope())
1477 Parent = Parent->getParent();
1478 if (Parent->isClassScope())
1479 break;
1480 }
1481 }
1482 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001483
1484 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001485 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001486
Douglas Gregor6569d682009-05-27 23:11:45 +00001487 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001488 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001489
Douglas Gregorddc29e12009-02-06 22:42:48 +00001490 if (TagDecl)
1491 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001492
1493 if (Tok.is(tok::colon)) {
1494 ParseBaseClause(TagDecl);
1495
1496 if (!Tok.is(tok::l_brace)) {
1497 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
1498 return;
1499 }
1500 }
1501
1502 assert(Tok.is(tok::l_brace));
1503
1504 SourceLocation LBraceLoc = ConsumeBrace();
1505
1506 if (!TagDecl) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001507 SkipUntil(tok::r_brace, false, false);
1508 return;
1509 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001510
John McCallf9368152009-12-20 07:58:13 +00001511 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
1512
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001513 // C++ 11p3: Members of a class defined with the keyword class are private
1514 // by default. Members of a class defined with the keywords struct or union
1515 // are public by default.
1516 AccessSpecifier CurAS;
1517 if (TagType == DeclSpec::TST_class)
1518 CurAS = AS_private;
1519 else
1520 CurAS = AS_public;
1521
1522 // While we still have something to read, read the member-declarations.
1523 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1524 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001526 // Check for extraneous top-level semicolon.
1527 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001528 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001529 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001530 ConsumeToken();
1531 continue;
1532 }
1533
1534 AccessSpecifier AS = getAccessSpecifierIfPresent();
1535 if (AS != AS_none) {
1536 // Current token is a C++ access specifier.
1537 CurAS = AS;
1538 ConsumeToken();
1539 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1540 continue;
1541 }
1542
Douglas Gregor37b372b2009-08-20 22:52:58 +00001543 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001545 // Parse all the comma separated declarators.
1546 ParseCXXClassMemberDeclaration(CurAS);
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001549 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001551 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001552 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001553 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001554 AttrList.reset(ParseGNUAttributes()); // FIXME: where should I put them?
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001555
1556 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1557 LBraceLoc, RBraceLoc);
1558
1559 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1560 // complete within function bodies, default arguments,
1561 // exception-specifications, and constructor ctor-initializers (including
1562 // such things in nested classes).
1563 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001564 // FIXME: Only function bodies and constructor ctor-initializers are
1565 // parsed correctly, fix the rest.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001566 if (NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001567 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001568 // are complete and we can parse the delayed portions of method
1569 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001570 ParseLexedMethodDeclarations(getCurrentClass());
1571 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001572 }
1573
1574 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001575 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001576 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001577
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001578 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001579}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001580
1581/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1582/// which explicitly initializes the members or base classes of a
1583/// class (C++ [class.base.init]). For example, the three initializers
1584/// after the ':' in the Derived constructor below:
1585///
1586/// @code
1587/// class Base { };
1588/// class Derived : Base {
1589/// int x;
1590/// float f;
1591/// public:
1592/// Derived(float f) : Base(), x(17), f(f) { }
1593/// };
1594/// @endcode
1595///
Mike Stump1eb44332009-09-09 15:08:12 +00001596/// [C++] ctor-initializer:
1597/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001598///
Mike Stump1eb44332009-09-09 15:08:12 +00001599/// [C++] mem-initializer-list:
1600/// mem-initializer
1601/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001602void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001603 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1604
1605 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Douglas Gregor7ad83902008-11-05 04:29:56 +00001607 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001608 bool AnyErrors = false;
1609
Douglas Gregor7ad83902008-11-05 04:29:56 +00001610 do {
1611 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001612 if (!MemInit.isInvalid())
1613 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001614 else
1615 AnyErrors = true;
1616
Douglas Gregor7ad83902008-11-05 04:29:56 +00001617 if (Tok.is(tok::comma))
1618 ConsumeToken();
1619 else if (Tok.is(tok::l_brace))
1620 break;
1621 else {
1622 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001623 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001624 SkipUntil(tok::l_brace, true, true);
1625 break;
1626 }
1627 } while (true);
1628
Mike Stump1eb44332009-09-09 15:08:12 +00001629 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001630 MemInitializers.data(), MemInitializers.size(),
1631 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001632}
1633
1634/// ParseMemInitializer - Parse a C++ member initializer, which is
1635/// part of a constructor initializer that explicitly initializes one
1636/// member or base class (C++ [class.base.init]). See
1637/// ParseConstructorInitializer for an example.
1638///
1639/// [C++] mem-initializer:
1640/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001641///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001642/// [C++] mem-initializer-id:
1643/// '::'[opt] nested-name-specifier[opt] class-name
1644/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001645Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001646 // parse '::'[opt] nested-name-specifier[opt]
1647 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001648 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001649 TypeTy *TemplateTypeTy = 0;
1650 if (Tok.is(tok::annot_template_id)) {
1651 TemplateIdAnnotation *TemplateId
1652 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001653 if (TemplateId->Kind == TNK_Type_template ||
1654 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001655 AnnotateTemplateIdTokenAsType(&SS);
1656 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1657 TemplateTypeTy = Tok.getAnnotationValue();
1658 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001659 }
1660 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001661 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001662 return true;
1663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Douglas Gregor7ad83902008-11-05 04:29:56 +00001665 // Get the identifier. This may be a member name or a class name,
1666 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001667 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001668 SourceLocation IdLoc = ConsumeToken();
1669
1670 // Parse the '('.
1671 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001672 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001673 return true;
1674 }
1675 SourceLocation LParenLoc = ConsumeParen();
1676
1677 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001678 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001679 CommaLocsTy CommaLocs;
1680 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1681 SkipUntil(tok::r_paren);
1682 return true;
1683 }
1684
1685 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1686
Fariborz Jahanian96174332009-07-01 19:21:19 +00001687 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1688 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001689 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001690 ArgExprs.size(), CommaLocs.data(),
1691 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001692}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001693
1694/// ParseExceptionSpecification - Parse a C++ exception-specification
1695/// (C++ [except.spec]).
1696///
Douglas Gregora4745612008-12-01 18:00:20 +00001697/// exception-specification:
1698/// 'throw' '(' type-id-list [opt] ')'
1699/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001700///
Douglas Gregora4745612008-12-01 18:00:20 +00001701/// type-id-list:
1702/// type-id
1703/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001704///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001705bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001706 llvm::SmallVector<TypeTy*, 2>
1707 &Exceptions,
1708 llvm::SmallVector<SourceRange, 2>
1709 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001710 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001711 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001713 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001715 if (!Tok.is(tok::l_paren)) {
1716 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1717 }
1718 SourceLocation LParenLoc = ConsumeParen();
1719
Douglas Gregora4745612008-12-01 18:00:20 +00001720 // Parse throw(...), a Microsoft extension that means "this function
1721 // can throw anything".
1722 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001723 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001724 SourceLocation EllipsisLoc = ConsumeToken();
1725 if (!getLang().Microsoft)
1726 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001727 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001728 return false;
1729 }
1730
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001731 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001732 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001733 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001734 TypeResult Res(ParseTypeName(&Range));
1735 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001736 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001737 Ranges.push_back(Range);
1738 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001739 if (Tok.is(tok::comma))
1740 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001741 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001742 break;
1743 }
1744
Sebastian Redlab197ba2009-02-09 18:23:29 +00001745 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001746 return false;
1747}
Douglas Gregor6569d682009-05-27 23:11:45 +00001748
1749/// \brief We have just started parsing the definition of a new class,
1750/// so push that class onto our stack of classes that is currently
1751/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001752void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1753 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001754 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001755 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001756}
1757
1758/// \brief Deallocate the given parsed class and all of its nested
1759/// classes.
1760void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1761 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1762 DeallocateParsedClasses(Class->NestedClasses[I]);
1763 delete Class;
1764}
1765
1766/// \brief Pop the top class of the stack of classes that are
1767/// currently being parsed.
1768///
1769/// This routine should be called when we have finished parsing the
1770/// definition of a class, but have not yet popped the Scope
1771/// associated with the class's definition.
1772///
1773/// \returns true if the class we've popped is a top-level class,
1774/// false otherwise.
1775void Parser::PopParsingClass() {
1776 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Douglas Gregor6569d682009-05-27 23:11:45 +00001778 ParsingClass *Victim = ClassStack.top();
1779 ClassStack.pop();
1780 if (Victim->TopLevelClass) {
1781 // Deallocate all of the nested classes of this class,
1782 // recursively: we don't need to keep any of this information.
1783 DeallocateParsedClasses(Victim);
1784 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001785 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001786 assert(!ClassStack.empty() && "Missing top-level class?");
1787
1788 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1789 Victim->NestedClasses.empty()) {
1790 // The victim is a nested class, but we will not need to perform
1791 // any processing after the definition of this class since it has
1792 // no members whose handling was delayed. Therefore, we can just
1793 // remove this nested class.
1794 delete Victim;
1795 return;
1796 }
1797
1798 // This nested class has some members that will need to be processed
1799 // after the top-level class is completely defined. Therefore, add
1800 // it to the list of nested classes within its parent.
1801 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1802 ClassStack.top()->NestedClasses.push_back(Victim);
1803 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1804}
Sean Huntbbd37c62009-11-21 08:43:09 +00001805
1806/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1807/// parses standard attributes.
1808///
1809/// [C++0x] attribute-specifier:
1810/// '[' '[' attribute-list ']' ']'
1811///
1812/// [C++0x] attribute-list:
1813/// attribute[opt]
1814/// attribute-list ',' attribute[opt]
1815///
1816/// [C++0x] attribute:
1817/// attribute-token attribute-argument-clause[opt]
1818///
1819/// [C++0x] attribute-token:
1820/// identifier
1821/// attribute-scoped-token
1822///
1823/// [C++0x] attribute-scoped-token:
1824/// attribute-namespace '::' identifier
1825///
1826/// [C++0x] attribute-namespace:
1827/// identifier
1828///
1829/// [C++0x] attribute-argument-clause:
1830/// '(' balanced-token-seq ')'
1831///
1832/// [C++0x] balanced-token-seq:
1833/// balanced-token
1834/// balanced-token-seq balanced-token
1835///
1836/// [C++0x] balanced-token:
1837/// '(' balanced-token-seq ')'
1838/// '[' balanced-token-seq ']'
1839/// '{' balanced-token-seq '}'
1840/// any token but '(', ')', '[', ']', '{', or '}'
1841CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1842 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1843 && "Not a C++0x attribute list");
1844
1845 SourceLocation StartLoc = Tok.getLocation(), Loc;
1846 AttributeList *CurrAttr = 0;
1847
1848 ConsumeBracket();
1849 ConsumeBracket();
1850
1851 if (Tok.is(tok::comma)) {
1852 Diag(Tok.getLocation(), diag::err_expected_ident);
1853 ConsumeToken();
1854 }
1855
1856 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1857 // attribute not present
1858 if (Tok.is(tok::comma)) {
1859 ConsumeToken();
1860 continue;
1861 }
1862
1863 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1864 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1865
1866 // scoped attribute
1867 if (Tok.is(tok::coloncolon)) {
1868 ConsumeToken();
1869
1870 if (!Tok.is(tok::identifier)) {
1871 Diag(Tok.getLocation(), diag::err_expected_ident);
1872 SkipUntil(tok::r_square, tok::comma, true, true);
1873 continue;
1874 }
1875
1876 ScopeName = AttrName;
1877 ScopeLoc = AttrLoc;
1878
1879 AttrName = Tok.getIdentifierInfo();
1880 AttrLoc = ConsumeToken();
1881 }
1882
1883 bool AttrParsed = false;
1884 // No scoped names are supported; ideally we could put all non-standard
1885 // attributes into namespaces.
1886 if (!ScopeName) {
1887 switch(AttributeList::getKind(AttrName))
1888 {
1889 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001890 case AttributeList::AT_base_check:
1891 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001892 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001893 case AttributeList::AT_hiding:
1894 case AttributeList::AT_noreturn:
1895 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001896 if (Tok.is(tok::l_paren)) {
1897 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1898 << AttrName->getName();
1899 break;
1900 }
1901
1902 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1903 SourceLocation(), 0, 0, CurrAttr, false,
1904 true);
1905 AttrParsed = true;
1906 break;
1907 }
1908
1909 // One argument; must be a type-id or assignment-expression
1910 case AttributeList::AT_aligned: {
1911 if (Tok.isNot(tok::l_paren)) {
1912 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1913 << AttrName->getName();
1914 break;
1915 }
1916 SourceLocation ParamLoc = ConsumeParen();
1917
1918 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1919
1920 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1921
1922 ExprVector ArgExprs(Actions);
1923 ArgExprs.push_back(ArgExpr.release());
1924 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1925 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1926 false, true);
1927
1928 AttrParsed = true;
1929 break;
1930 }
1931
1932 // Silence warnings
1933 default: break;
1934 }
1935 }
1936
1937 // Skip the entire parameter clause, if any
1938 if (!AttrParsed && Tok.is(tok::l_paren)) {
1939 ConsumeParen();
1940 // SkipUntil maintains the balancedness of tokens.
1941 SkipUntil(tok::r_paren, false);
1942 }
1943 }
1944
1945 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1946 SkipUntil(tok::r_square, false);
1947 Loc = Tok.getLocation();
1948 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1949 SkipUntil(tok::r_square, false);
1950
1951 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1952 return Attr;
1953}
1954
1955/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1956/// attribute.
1957///
1958/// FIXME: Simply returns an alignof() expression if the argument is a
1959/// type. Ideally, the type should be propagated directly into Sema.
1960///
1961/// [C++0x] 'align' '(' type-id ')'
1962/// [C++0x] 'align' '(' assignment-expression ')'
1963Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1964 if (isTypeIdInParens()) {
1965 EnterExpressionEvaluationContext Unevaluated(Actions,
1966 Action::Unevaluated);
1967 SourceLocation TypeLoc = Tok.getLocation();
1968 TypeTy *Ty = ParseTypeName().get();
1969 SourceRange TypeRange(Start, Tok.getLocation());
1970 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1971 TypeRange);
1972 } else
1973 return ParseConstantExpression();
1974}