blob: 51ee6a44348823142471c6bc897d153f07b57b60 [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,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000467 const CXXScopeSpec *SS,
468 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000469 // Check whether we have a template-id that names a type.
470 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000471 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000472 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000473 if (TemplateId->Kind == TNK_Type_template ||
474 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000475 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000476
477 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
478 TypeTy *Type = Tok.getAnnotationValue();
479 EndLocation = Tok.getAnnotationEndLoc();
480 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000481
482 if (Type)
483 return Type;
484 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000485 }
486
487 // Fall through to produce an error below.
488 }
489
Douglas Gregor42a552f2008-11-05 20:51:48 +0000490 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000491 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000492 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000493 }
494
Douglas Gregor84d0a192010-01-12 21:28:44 +0000495 IdentifierInfo *Id = Tok.getIdentifierInfo();
496 SourceLocation IdLoc = ConsumeToken();
497
498 if (Tok.is(tok::less)) {
499 // It looks the user intended to write a template-id here, but the
500 // template-name was wrong. Try to fix that.
501 TemplateNameKind TNK = TNK_Type_template;
502 TemplateTy Template;
503 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, CurScope,
504 SS, Template, TNK)) {
505 Diag(IdLoc, diag::err_unknown_template_name)
506 << Id;
507 }
508
509 if (!Template)
510 return true;
511
512 // Form the template name
513 UnqualifiedId TemplateName;
514 TemplateName.setIdentifier(Id, IdLoc);
515
516 // Parse the full template-id, then turn it into a type.
517 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
518 SourceLocation(), true))
519 return true;
520 if (TNK == TNK_Dependent_template_name)
521 AnnotateTemplateIdTokenAsType(SS);
522
523 // If we didn't end up with a typename token, there's nothing more we
524 // can do.
525 if (Tok.isNot(tok::annot_typename))
526 return true;
527
528 // Retrieve the type from the annotation token, consume that token, and
529 // return.
530 EndLocation = Tok.getAnnotationEndLoc();
531 TypeTy *Type = Tok.getAnnotationValue();
532 ConsumeToken();
533 return Type;
534 }
535
Douglas Gregor42a552f2008-11-05 20:51:48 +0000536 // We have an identifier; check whether it is actually a type.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000537 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, CurScope, SS, true);
538 if (!Type) {
539 Diag(IdLoc, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000540 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000541 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000542 }
543
544 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000545 EndLocation = IdLoc;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000546 return Type;
547}
548
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000549/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
550/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
551/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000552/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000553///
554/// class-specifier: [C++ class]
555/// class-head '{' member-specification[opt] '}'
556/// class-head '{' member-specification[opt] '}' attributes[opt]
557/// class-head:
558/// class-key identifier[opt] base-clause[opt]
559/// class-key nested-name-specifier identifier base-clause[opt]
560/// class-key nested-name-specifier[opt] simple-template-id
561/// base-clause[opt]
562/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000563/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000564/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000565/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000566/// simple-template-id base-clause[opt]
567/// class-key:
568/// 'class'
569/// 'struct'
570/// 'union'
571///
572/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000573/// class-key ::[opt] nested-name-specifier[opt] identifier
574/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
575/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000576///
577/// Note that the C++ class-specifier and elaborated-type-specifier,
578/// together, subsume the C99 struct-or-union-specifier:
579///
580/// struct-or-union-specifier: [C99 6.7.2.1]
581/// struct-or-union identifier[opt] '{' struct-contents '}'
582/// struct-or-union identifier
583/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
584/// '}' attributes[opt]
585/// [GNU] struct-or-union attributes[opt] identifier
586/// struct-or-union:
587/// 'struct'
588/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000589void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
590 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000591 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000592 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000593 DeclSpec::TST TagType;
594 if (TagTokKind == tok::kw_struct)
595 TagType = DeclSpec::TST_struct;
596 else if (TagTokKind == tok::kw_class)
597 TagType = DeclSpec::TST_class;
598 else {
599 assert(TagTokKind == tok::kw_union && "Not a class specifier");
600 TagType = DeclSpec::TST_union;
601 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000602
Douglas Gregor374929f2009-09-18 15:37:17 +0000603 if (Tok.is(tok::code_completion)) {
604 // Code completion for a struct, class, or union name.
605 Actions.CodeCompleteTag(CurScope, TagType);
606 ConsumeToken();
607 }
608
Sean Huntbbd37c62009-11-21 08:43:09 +0000609 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000610 // If attributes exist after tag, parse them.
611 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000612 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000613
Steve Narofff59e17e2008-12-24 20:59:21 +0000614 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000615 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000616 AttrList = ParseMicrosoftDeclSpec(AttrList);
617
618 // If C++0x attributes exist here, parse them.
619 // FIXME: Are we consistent with the ordering of parsing of different
620 // styles of attributes?
621 if (isCXX0XAttributeSpecifier())
622 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregorb117a602009-09-04 05:53:02 +0000624 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
625 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
626 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000627 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000628 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
629 // properly.
630 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
631 Tok.setKind(tok::identifier);
632 }
633
634 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
635 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
636 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000637 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000638 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
639 // properly.
640 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
641 Tok.setKind(tok::identifier);
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000644 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000645 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000646 if (getLang().CPlusPlus) {
647 // "FOO : BAR" is not a potential typo for "FOO::BAR".
648 ColonProtectionRAIIObject X(*this);
649
650 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
651 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
652 Diag(Tok, diag::err_expected_ident);
653 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000654
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000655 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
656
Douglas Gregorcc636682009-02-17 23:15:12 +0000657 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000658 IdentifierInfo *Name = 0;
659 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000660 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000661 if (Tok.is(tok::identifier)) {
662 Name = Tok.getIdentifierInfo();
663 NameLoc = ConsumeToken();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000664
665 if (Tok.is(tok::less)) {
666 // The name was supposed to refer to a template, but didn't.
667 // Eat the template argument list and try to continue parsing this as
668 // a class (or template thereof).
669 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000670 SourceLocation LAngleLoc, RAngleLoc;
671 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
672 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000673 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000674 // We couldn't parse the template argument list at all, so don't
675 // try to give any location information for the list.
676 LAngleLoc = RAngleLoc = SourceLocation();
677 }
678
679 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000680 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000681 << (TagType == DeclSpec::TST_class? 0
682 : TagType == DeclSpec::TST_struct? 1
683 : 2)
684 << Name
685 << SourceRange(LAngleLoc, RAngleLoc);
686
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000687 // Strip off the last template parameter list if it was empty, since
688 // we've removed its template argument list.
689 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
690 if (TemplateParams && TemplateParams->size() > 1) {
691 TemplateParams->pop_back();
692 } else {
693 TemplateParams = 0;
694 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
695 = ParsedTemplateInfo::NonTemplate;
696 }
697 } else if (TemplateInfo.Kind
698 == ParsedTemplateInfo::ExplicitInstantiation) {
699 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000700 TemplateParams = 0;
701 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
702 = ParsedTemplateInfo::NonTemplate;
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000703 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
704 = SourceLocation();
705 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
706 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000707 }
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000708
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000709
710 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000711 } else if (Tok.is(tok::annot_template_id)) {
712 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
713 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000714
Douglas Gregorc45c2322009-03-31 00:43:58 +0000715 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000716 // The template-name in the simple-template-id refers to
717 // something other than a class template. Give an appropriate
718 // error message and skip to the ';'.
719 SourceRange Range(NameLoc);
720 if (SS.isNotEmpty())
721 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000722
Douglas Gregor39a8de12009-02-25 19:37:18 +0000723 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
724 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Douglas Gregor39a8de12009-02-25 19:37:18 +0000726 DS.SetTypeSpecError();
727 SkipUntil(tok::semi, false, true);
728 TemplateId->Destroy();
729 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000730 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000731 }
732
John McCall67d1a672009-08-06 02:15:43 +0000733 // There are four options here. If we have 'struct foo;', then this
734 // is either a forward declaration or a friend declaration, which
735 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000736 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000737 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000738 // However, in some contexts, things look like declarations but are just
739 // references, e.g.
740 // new struct s;
741 // or
742 // &T::operator struct s;
743 // For these, SuppressDeclarations is true.
John McCall0f434ec2009-07-31 02:45:11 +0000744 Action::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000745 if (SuppressDeclarations)
746 TUK = Action::TUK_Reference;
747 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000748 if (DS.isFriendSpecified()) {
749 // C++ [class.friend]p2:
750 // A class shall not be defined in a friend declaration.
751 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
752 << SourceRange(DS.getFriendSpecLoc());
753
754 // Skip everything up to the semicolon, so that this looks like a proper
755 // friend class (or template thereof) declaration.
756 SkipUntil(tok::semi, true, true);
757 TUK = Action::TUK_Friend;
758 } else {
759 // Okay, this is a class definition.
760 TUK = Action::TUK_Definition;
761 }
762 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000763 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000764 else
John McCall0f434ec2009-07-31 02:45:11 +0000765 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000766
John McCall0f434ec2009-07-31 02:45:11 +0000767 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000768 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000769 Diag(StartLoc, diag::err_anon_type_definition)
770 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000771
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000772 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000773
774 if (TemplateId)
775 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000776 return;
777 }
778
Douglas Gregorddc29e12009-02-06 22:42:48 +0000779 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000780 Action::DeclResult TagOrTempResult = true; // invalid
781 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000782
John McCall0f434ec2009-07-31 02:45:11 +0000783 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000784 // to turn that template-id into a type.
785
Douglas Gregor402abb52009-05-28 23:31:59 +0000786 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000787 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000788 // Explicit specialization, class template partial specialization,
789 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000790 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000791 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000792 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000793 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000794 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000795 // This is an explicit instantiation of a class template.
796 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000797 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000798 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000799 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000800 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000801 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000802 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000803 TemplateTy::make(TemplateId->Template),
804 TemplateId->TemplateNameLoc,
805 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000806 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000807 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000808 AttrList);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000809 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000810 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000811 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
812 TemplateId->TemplateNameLoc,
813 TemplateId->LAngleLoc,
814 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000815 TemplateId->RAngleLoc);
816
John McCallc4e70192009-09-11 04:59:25 +0000817 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
818 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000819 } else {
820 // This is an explicit specialization or a class template
821 // partial specialization.
822 TemplateParameterLists FakedParamLists;
823
824 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
825 // This looks like an explicit instantiation, because we have
826 // something like
827 //
828 // template class Foo<X>
829 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000830 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000831 // meant to be an explicit specialization, but the user forgot
832 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000833 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000834
Mike Stump1eb44332009-09-09 15:08:12 +0000835 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000836 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000837 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000838 diag::err_explicit_instantiation_with_definition)
839 << SourceRange(TemplateInfo.TemplateLoc)
840 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
841
842 // Create a fake template parameter list that contains only
843 // "template<>", so that we treat this construct as a class
844 // template specialization.
845 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000846 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000847 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000848 LAngleLoc,
849 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000850 LAngleLoc));
851 TemplateParams = &FakedParamLists;
852 }
853
854 // Build the class template specialization.
855 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000856 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000857 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000858 TemplateTy::make(TemplateId->Template),
859 TemplateId->TemplateNameLoc,
860 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000861 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000862 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000863 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000864 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000865 TemplateParams? &(*TemplateParams)[0] : 0,
866 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000867 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000868 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000869 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000870 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000871 // Explicit instantiation of a member of a class template
872 // specialization, e.g.,
873 //
874 // template struct Outer<int>::Inner;
875 //
876 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000877 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000878 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000879 TemplateInfo.TemplateLoc,
880 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000881 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000882 } else {
883 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000884 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000885 // FIXME: Diagnose this particular error.
886 }
887
John McCallc4e70192009-09-11 04:59:25 +0000888 bool IsDependent = false;
889
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000890 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000891 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000892 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000893 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000894 TemplateParams? &(*TemplateParams)[0] : 0,
895 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000896 Owned, IsDependent);
897
898 // If ActOnTag said the type was dependent, try again with the
899 // less common call.
900 if (IsDependent)
901 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
902 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000903 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000904
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000905 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000906 if (TUK == Action::TUK_Definition) {
907 assert(Tok.is(tok::l_brace) ||
908 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000909 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000910 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000911 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000912 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000913 }
914
John McCallc4e70192009-09-11 04:59:25 +0000915 void *Result;
916 if (!TypeResult.isInvalid()) {
917 TagType = DeclSpec::TST_typename;
918 Result = TypeResult.get();
919 Owned = false;
920 } else if (!TagOrTempResult.isInvalid()) {
921 Result = TagOrTempResult.get().getAs<void>();
922 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000923 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000924 return;
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
John McCallfec54012009-08-03 20:12:06 +0000927 const char *PrevSpec = 0;
928 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000929
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000930 // FIXME: The DeclSpec should keep the locations of both the keyword and the
931 // name (if there is one).
932 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
933
934 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000935 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000936 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000937
938 // At this point, we've successfully parsed a class-specifier in 'definition'
939 // form (e.g. "struct foo { int x; }". While we could just return here, we're
940 // going to look at what comes after it to improve error recovery. If an
941 // impossible token occurs next, we assume that the programmer forgot a ; at
942 // the end of the declaration and recover that way.
943 //
944 // This switch enumerates the valid "follow" set for definition.
945 if (TUK == Action::TUK_Definition) {
946 switch (Tok.getKind()) {
947 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000948 case tok::star: // struct foo {...} * P;
949 case tok::amp: // struct foo {...} & R = ...
950 case tok::identifier: // struct foo {...} V ;
951 case tok::r_paren: //(struct foo {...} ) {4}
952 case tok::annot_cxxscope: // struct foo {...} a:: b;
953 case tok::annot_typename: // struct foo {...} a ::b;
954 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +0000955 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +0000956 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattner99c95202010-02-02 17:32:27 +0000957 // Storage-class specifiers
958 case tok::kw_static: // struct foo {...} static x;
959 case tok::kw_extern: // struct foo {...} extern x;
960 case tok::kw_typedef: // struct foo {...} typedef x;
961 case tok::kw_register: // struct foo {...} register x;
962 case tok::kw_auto: // struct foo {...} auto x;
963 // Type qualifiers
964 case tok::kw_const: // struct foo {...} const x;
965 case tok::kw_volatile: // struct foo {...} volatile x;
966 case tok::kw_restrict: // struct foo {...} restrict x;
967 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner4ed5d912010-02-02 01:23:29 +0000968 break;
969
970 case tok::r_brace: // struct bar { struct foo {...} }
971 // Missing ';' at end of struct is accepted as an extension in C mode.
972 if (!getLang().CPlusPlus) break;
973 // FALL THROUGH.
974 default:
975 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
976 TagType == DeclSpec::TST_class ? "class"
977 : TagType == DeclSpec::TST_struct? "struct" : "union");
978 // Push this token back into the preprocessor and change our current token
979 // to ';' so that the rest of the code recovers as though there were an
980 // ';' after the definition.
981 PP.EnterToken(Tok);
982 Tok.setKind(tok::semi);
983 break;
984 }
985 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000986}
987
Mike Stump1eb44332009-09-09 15:08:12 +0000988/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000989///
990/// base-clause : [C++ class.derived]
991/// ':' base-specifier-list
992/// base-specifier-list:
993/// base-specifier '...'[opt]
994/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000995void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000996 assert(Tok.is(tok::colon) && "Not a base clause");
997 ConsumeToken();
998
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000999 // Build up an array of parsed base specifiers.
1000 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1001
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001002 while (true) {
1003 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001004 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001005 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001006 // Skip the rest of this base specifier, up until the comma or
1007 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001008 SkipUntil(tok::comma, tok::l_brace, true, true);
1009 } else {
1010 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001011 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001012 }
1013
1014 // If the next token is a comma, consume it and keep reading
1015 // base-specifiers.
1016 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001018 // Consume the comma.
1019 ConsumeToken();
1020 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001021
1022 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001023 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001024}
1025
1026/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1027/// one entry in the base class list of a class specifier, for example:
1028/// class foo : public bar, virtual private baz {
1029/// 'public bar' and 'virtual private baz' are each base-specifiers.
1030///
1031/// base-specifier: [C++ class.derived]
1032/// ::[opt] nested-name-specifier[opt] class-name
1033/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1034/// class-name
1035/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1036/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +00001037Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001038 bool IsVirtual = false;
1039 SourceLocation StartLoc = Tok.getLocation();
1040
1041 // Parse the 'virtual' keyword.
1042 if (Tok.is(tok::kw_virtual)) {
1043 ConsumeToken();
1044 IsVirtual = true;
1045 }
1046
1047 // Parse an (optional) access specifier.
1048 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001049 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001050 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001052 // Parse the 'virtual' keyword (again!), in case it came after the
1053 // access specifier.
1054 if (Tok.is(tok::kw_virtual)) {
1055 SourceLocation VirtualLoc = ConsumeToken();
1056 if (IsVirtual) {
1057 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001058 Diag(VirtualLoc, diag::err_dup_virtual)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001059 << CodeModificationHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001060 }
1061
1062 IsVirtual = true;
1063 }
1064
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001065 // Parse optional '::' and optional nested-name-specifier.
1066 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001067 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001068
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001069 // The location of the base class itself.
1070 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001071
1072 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001073 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001074 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1075 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001076 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001077
1078 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001079 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001081 // Notify semantic analysis that we have parsed a complete
1082 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001083 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001084 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001085}
1086
1087/// getAccessSpecifierIfPresent - Determine whether the next token is
1088/// a C++ access-specifier.
1089///
1090/// access-specifier: [C++ class.derived]
1091/// 'private'
1092/// 'protected'
1093/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001094AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001095 switch (Tok.getKind()) {
1096 default: return AS_none;
1097 case tok::kw_private: return AS_private;
1098 case tok::kw_protected: return AS_protected;
1099 case tok::kw_public: return AS_public;
1100 }
1101}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001102
Eli Friedmand33133c2009-07-22 21:45:50 +00001103void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1104 DeclPtrTy ThisDecl) {
1105 // We just declared a member function. If this member function
1106 // has any default arguments, we'll need to parse them later.
1107 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001108 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001109 = DeclaratorInfo.getTypeObject(0).Fun;
1110 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1111 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1112 if (!LateMethod) {
1113 // Push this method onto the stack of late-parsed method
1114 // declarations.
1115 getCurrentClass().MethodDecls.push_back(
1116 LateParsedMethodDeclaration(ThisDecl));
1117 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001118 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001119
1120 // Add all of the parameters prior to this one (they don't
1121 // have default arguments).
1122 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1123 for (unsigned I = 0; I < ParamIdx; ++I)
1124 LateMethod->DefaultArgs.push_back(
1125 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1126 }
1127
1128 // Add this parameter to the list of parameters (it or may
1129 // not have a default argument).
1130 LateMethod->DefaultArgs.push_back(
1131 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1132 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1133 }
1134 }
1135}
1136
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001137/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1138///
1139/// member-declaration:
1140/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1141/// function-definition ';'[opt]
1142/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1143/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001144/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001145/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001146/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001147///
1148/// member-declarator-list:
1149/// member-declarator
1150/// member-declarator-list ',' member-declarator
1151///
1152/// member-declarator:
1153/// declarator pure-specifier[opt]
1154/// declarator constant-initializer[opt]
1155/// identifier[opt] ':' constant-expression
1156///
Sebastian Redle2b68332009-04-12 17:16:29 +00001157/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001158/// '= 0'
1159///
1160/// constant-initializer:
1161/// '=' constant-expression
1162///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001163void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1164 const ParsedTemplateInfo &TemplateInfo) {
John McCall60fa3cf2009-12-11 02:10:03 +00001165 // Access declarations.
1166 if (!TemplateInfo.Kind &&
1167 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
1168 TryAnnotateCXXScopeToken() &&
1169 Tok.is(tok::annot_cxxscope)) {
1170 bool isAccessDecl = false;
1171 if (NextToken().is(tok::identifier))
1172 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1173 else
1174 isAccessDecl = NextToken().is(tok::kw_operator);
1175
1176 if (isAccessDecl) {
1177 // Collect the scope specifier token we annotated earlier.
1178 CXXScopeSpec SS;
1179 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1180
1181 // Try to parse an unqualified-id.
1182 UnqualifiedId Name;
1183 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1184 SkipUntil(tok::semi);
1185 return;
1186 }
1187
1188 // TODO: recover from mistakenly-qualified operator declarations.
1189 if (ExpectAndConsume(tok::semi,
1190 diag::err_expected_semi_after,
1191 "access declaration",
1192 tok::semi))
1193 return;
1194
1195 Actions.ActOnUsingDeclaration(CurScope, AS,
1196 false, SourceLocation(),
1197 SS, Name,
1198 /* AttrList */ 0,
1199 /* IsTypeName */ false,
1200 SourceLocation());
1201 return;
1202 }
1203 }
1204
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001205 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001206 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001207 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001208 SourceLocation DeclEnd;
1209 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001210 return;
1211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Chris Lattner682bf922009-03-29 16:50:03 +00001213 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001214 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001215 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001216 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001217 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001218 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001219 return;
1220 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001221
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001222 // Handle: member-declaration ::= '__extension__' member-declaration
1223 if (Tok.is(tok::kw___extension__)) {
1224 // __extension__ silences extension warnings in the subexpression.
1225 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1226 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001227 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001228 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001229
Chris Lattner4ed5d912010-02-02 01:23:29 +00001230 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1231 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001232 ColonProtectionRAIIObject X(*this);
1233
Sean Huntbbd37c62009-11-21 08:43:09 +00001234 CXX0XAttributeList AttrList;
1235 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001236 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001237 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001238
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001239 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001240 // FIXME: Check for template aliases
Sean Huntbbd37c62009-11-21 08:43:09 +00001241
1242 if (AttrList.HasAttr)
1243 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1244 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001246 // Eat 'using'.
1247 SourceLocation UsingLoc = ConsumeToken();
1248
1249 if (Tok.is(tok::kw_namespace)) {
1250 Diag(UsingLoc, diag::err_using_namespace_in_class);
1251 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001252 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001253 SourceLocation DeclEnd;
1254 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001255 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001256 }
1257 return;
1258 }
1259
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001260 SourceLocation DSStart = Tok.getLocation();
1261 // decl-specifier-seq:
1262 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001263 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001264 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001265 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001266
John McCalldd4a3b02009-09-16 22:47:08 +00001267 Action::MultiTemplateParamsArg TemplateParams(Actions,
1268 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1269 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1270
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001271 if (Tok.is(tok::semi)) {
1272 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001273 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001274 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001275 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001276
John McCall54abf7d2009-11-04 02:18:39 +00001277 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001278
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001279 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001280 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1281 ColonProtectionRAIIObject X(*this);
1282
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001283 // Parse the first declarator.
1284 ParseDeclarator(DeclaratorInfo);
1285 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001286 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001287 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001288 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001289 if (Tok.is(tok::semi))
1290 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001291 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001292 }
1293
John Thompson1b2fc0f2009-11-25 22:58:06 +00001294 // If attributes exist after the declarator, but before an '{', parse them.
1295 if (Tok.is(tok::kw___attribute)) {
1296 SourceLocation Loc;
1297 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1298 DeclaratorInfo.AddAttributes(AttrList, Loc);
1299 }
1300
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001301 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001302 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001303 || (DeclaratorInfo.isFunctionDeclarator() &&
1304 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001305 if (!DeclaratorInfo.isFunctionDeclarator()) {
1306 Diag(Tok, diag::err_func_def_no_params);
1307 ConsumeBrace();
1308 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001309 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001310 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001311
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001312 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1313 Diag(Tok, diag::err_function_declared_typedef);
1314 // This recovery skips the entire function body. It would be nice
1315 // to simply call ParseCXXInlineMethodDef() below, however Sema
1316 // assumes the declarator represents a function, not a typedef.
1317 ConsumeBrace();
1318 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001319 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001320 }
1321
Douglas Gregor37b372b2009-08-20 22:52:58 +00001322 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001323 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001324 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001325 }
1326
1327 // member-declarator-list:
1328 // member-declarator
1329 // member-declarator-list ',' member-declarator
1330
Chris Lattner682bf922009-03-29 16:50:03 +00001331 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001332 OwningExprResult BitfieldSize(Actions);
1333 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001334 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001335
1336 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001337 // member-declarator:
1338 // declarator pure-specifier[opt]
1339 // declarator constant-initializer[opt]
1340 // identifier[opt] ':' constant-expression
1341
1342 if (Tok.is(tok::colon)) {
1343 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001344 BitfieldSize = ParseConstantExpression();
1345 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001346 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001347 }
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001349 // pure-specifier:
1350 // '= 0'
1351 //
1352 // constant-initializer:
1353 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001354 //
1355 // defaulted/deleted function-definition:
1356 // '=' 'default' [TODO]
1357 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001358
1359 if (Tok.is(tok::equal)) {
1360 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001361 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1362 ConsumeToken();
1363 Deleted = true;
1364 } else {
1365 Init = ParseInitializer();
1366 if (Init.isInvalid())
1367 SkipUntil(tok::comma, true, true);
1368 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001369 }
1370
1371 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001372 if (Tok.is(tok::kw___attribute)) {
1373 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001374 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001375 DeclaratorInfo.AddAttributes(AttrList, Loc);
1376 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001377
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001378 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001379 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001380 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001381
1382 DeclPtrTy ThisDecl;
1383 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001384 // TODO: handle initializers, bitfields, 'delete'
1385 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1386 /*IsDefinition*/ false,
1387 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001388 } else {
John McCall67d1a672009-08-06 02:15:43 +00001389 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1390 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001391 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001392 BitfieldSize.release(),
1393 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001394 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001395 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001396 }
Chris Lattner682bf922009-03-29 16:50:03 +00001397 if (ThisDecl)
1398 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001399
Douglas Gregor72b505b2008-12-16 21:30:33 +00001400 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001401 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001402 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001403 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001404 }
1405
John McCall54abf7d2009-11-04 02:18:39 +00001406 DeclaratorInfo.complete(ThisDecl);
1407
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001408 // If we don't have a comma, it is either the end of the list (a ';')
1409 // or an error, bail out.
1410 if (Tok.isNot(tok::comma))
1411 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001413 // Consume the comma.
1414 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001416 // Parse the next declarator.
1417 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001418 BitfieldSize = 0;
1419 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001420 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001422 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001423 if (Tok.is(tok::kw___attribute)) {
1424 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001425 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001426 DeclaratorInfo.AddAttributes(AttrList, Loc);
1427 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001428
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001429 if (Tok.isNot(tok::colon))
1430 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001431 }
1432
Chris Lattnerae50d502010-02-02 00:43:15 +00001433 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1434 // Skip to end of block or statement.
1435 SkipUntil(tok::r_brace, true, true);
1436 // If we stopped at a ';', eat it.
1437 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001438 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001439 }
1440
Chris Lattnerae50d502010-02-02 00:43:15 +00001441 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
1442 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001443}
1444
1445/// ParseCXXMemberSpecification - Parse the class definition.
1446///
1447/// member-specification:
1448/// member-declaration member-specification[opt]
1449/// access-specifier ':' member-specification[opt]
1450///
1451void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001452 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001453 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001454 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001455 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001456
Chris Lattner49f28ca2009-03-05 08:00:35 +00001457 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1458 PP.getSourceManager(),
1459 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregor26997fd2010-01-16 20:52:59 +00001461 // Determine whether this is a non-nested class. Note that local
1462 // classes are *not* considered to be nested classes.
1463 bool NonNestedClass = true;
1464 if (!ClassStack.empty()) {
1465 for (const Scope *S = CurScope; S; S = S->getParent()) {
1466 if (S->isClassScope()) {
1467 // We're inside a class scope, so this is a nested class.
1468 NonNestedClass = false;
1469 break;
1470 }
1471
1472 if ((S->getFlags() & Scope::FnScope)) {
1473 // If we're in a function or function template declared in the
1474 // body of a class, then this is a local class rather than a
1475 // nested class.
1476 const Scope *Parent = S->getParent();
1477 if (Parent->isTemplateParamScope())
1478 Parent = Parent->getParent();
1479 if (Parent->isClassScope())
1480 break;
1481 }
1482 }
1483 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001484
1485 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001486 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001487
Douglas Gregor6569d682009-05-27 23:11:45 +00001488 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001489 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001490
Douglas Gregorddc29e12009-02-06 22:42:48 +00001491 if (TagDecl)
1492 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001493
1494 if (Tok.is(tok::colon)) {
1495 ParseBaseClause(TagDecl);
1496
1497 if (!Tok.is(tok::l_brace)) {
1498 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
1499 return;
1500 }
1501 }
1502
1503 assert(Tok.is(tok::l_brace));
1504
1505 SourceLocation LBraceLoc = ConsumeBrace();
1506
1507 if (!TagDecl) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001508 SkipUntil(tok::r_brace, false, false);
1509 return;
1510 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001511
John McCallf9368152009-12-20 07:58:13 +00001512 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
1513
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001514 // C++ 11p3: Members of a class defined with the keyword class are private
1515 // by default. Members of a class defined with the keywords struct or union
1516 // are public by default.
1517 AccessSpecifier CurAS;
1518 if (TagType == DeclSpec::TST_class)
1519 CurAS = AS_private;
1520 else
1521 CurAS = AS_public;
1522
1523 // While we still have something to read, read the member-declarations.
1524 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1525 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001527 // Check for extraneous top-level semicolon.
1528 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001529 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001530 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001531 ConsumeToken();
1532 continue;
1533 }
1534
1535 AccessSpecifier AS = getAccessSpecifierIfPresent();
1536 if (AS != AS_none) {
1537 // Current token is a C++ access specifier.
1538 CurAS = AS;
1539 ConsumeToken();
1540 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1541 continue;
1542 }
1543
Douglas Gregor37b372b2009-08-20 22:52:58 +00001544 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001546 // Parse all the comma separated declarators.
1547 ParseCXXClassMemberDeclaration(CurAS);
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001550 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001552 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001553 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001554 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001555 AttrList.reset(ParseGNUAttributes()); // FIXME: where should I put them?
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001556
1557 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1558 LBraceLoc, RBraceLoc);
1559
1560 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1561 // complete within function bodies, default arguments,
1562 // exception-specifications, and constructor ctor-initializers (including
1563 // such things in nested classes).
1564 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001565 // FIXME: Only function bodies and constructor ctor-initializers are
1566 // parsed correctly, fix the rest.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001567 if (NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001568 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001569 // are complete and we can parse the delayed portions of method
1570 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001571 ParseLexedMethodDeclarations(getCurrentClass());
1572 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001573 }
1574
1575 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001576 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001577 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001578
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001579 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001580}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001581
1582/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1583/// which explicitly initializes the members or base classes of a
1584/// class (C++ [class.base.init]). For example, the three initializers
1585/// after the ':' in the Derived constructor below:
1586///
1587/// @code
1588/// class Base { };
1589/// class Derived : Base {
1590/// int x;
1591/// float f;
1592/// public:
1593/// Derived(float f) : Base(), x(17), f(f) { }
1594/// };
1595/// @endcode
1596///
Mike Stump1eb44332009-09-09 15:08:12 +00001597/// [C++] ctor-initializer:
1598/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001599///
Mike Stump1eb44332009-09-09 15:08:12 +00001600/// [C++] mem-initializer-list:
1601/// mem-initializer
1602/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001603void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001604 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1605
1606 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Douglas Gregor7ad83902008-11-05 04:29:56 +00001608 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001609 bool AnyErrors = false;
1610
Douglas Gregor7ad83902008-11-05 04:29:56 +00001611 do {
1612 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001613 if (!MemInit.isInvalid())
1614 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001615 else
1616 AnyErrors = true;
1617
Douglas Gregor7ad83902008-11-05 04:29:56 +00001618 if (Tok.is(tok::comma))
1619 ConsumeToken();
1620 else if (Tok.is(tok::l_brace))
1621 break;
1622 else {
1623 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001624 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001625 SkipUntil(tok::l_brace, true, true);
1626 break;
1627 }
1628 } while (true);
1629
Mike Stump1eb44332009-09-09 15:08:12 +00001630 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001631 MemInitializers.data(), MemInitializers.size(),
1632 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001633}
1634
1635/// ParseMemInitializer - Parse a C++ member initializer, which is
1636/// part of a constructor initializer that explicitly initializes one
1637/// member or base class (C++ [class.base.init]). See
1638/// ParseConstructorInitializer for an example.
1639///
1640/// [C++] mem-initializer:
1641/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001642///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001643/// [C++] mem-initializer-id:
1644/// '::'[opt] nested-name-specifier[opt] class-name
1645/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001646Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001647 // parse '::'[opt] nested-name-specifier[opt]
1648 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001649 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001650 TypeTy *TemplateTypeTy = 0;
1651 if (Tok.is(tok::annot_template_id)) {
1652 TemplateIdAnnotation *TemplateId
1653 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001654 if (TemplateId->Kind == TNK_Type_template ||
1655 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001656 AnnotateTemplateIdTokenAsType(&SS);
1657 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1658 TemplateTypeTy = Tok.getAnnotationValue();
1659 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001660 }
1661 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001662 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001663 return true;
1664 }
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Douglas Gregor7ad83902008-11-05 04:29:56 +00001666 // Get the identifier. This may be a member name or a class name,
1667 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001668 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001669 SourceLocation IdLoc = ConsumeToken();
1670
1671 // Parse the '('.
1672 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001673 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001674 return true;
1675 }
1676 SourceLocation LParenLoc = ConsumeParen();
1677
1678 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001679 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001680 CommaLocsTy CommaLocs;
1681 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1682 SkipUntil(tok::r_paren);
1683 return true;
1684 }
1685
1686 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1687
Fariborz Jahanian96174332009-07-01 19:21:19 +00001688 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1689 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001690 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001691 ArgExprs.size(), CommaLocs.data(),
1692 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001693}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001694
1695/// ParseExceptionSpecification - Parse a C++ exception-specification
1696/// (C++ [except.spec]).
1697///
Douglas Gregora4745612008-12-01 18:00:20 +00001698/// exception-specification:
1699/// 'throw' '(' type-id-list [opt] ')'
1700/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001701///
Douglas Gregora4745612008-12-01 18:00:20 +00001702/// type-id-list:
1703/// type-id
1704/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001705///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001706bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001707 llvm::SmallVector<TypeTy*, 2>
1708 &Exceptions,
1709 llvm::SmallVector<SourceRange, 2>
1710 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001711 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001712 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001714 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001716 if (!Tok.is(tok::l_paren)) {
1717 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1718 }
1719 SourceLocation LParenLoc = ConsumeParen();
1720
Douglas Gregora4745612008-12-01 18:00:20 +00001721 // Parse throw(...), a Microsoft extension that means "this function
1722 // can throw anything".
1723 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001724 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001725 SourceLocation EllipsisLoc = ConsumeToken();
1726 if (!getLang().Microsoft)
1727 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001728 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001729 return false;
1730 }
1731
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001732 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001733 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001734 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001735 TypeResult Res(ParseTypeName(&Range));
1736 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001737 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001738 Ranges.push_back(Range);
1739 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001740 if (Tok.is(tok::comma))
1741 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001742 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001743 break;
1744 }
1745
Sebastian Redlab197ba2009-02-09 18:23:29 +00001746 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001747 return false;
1748}
Douglas Gregor6569d682009-05-27 23:11:45 +00001749
1750/// \brief We have just started parsing the definition of a new class,
1751/// so push that class onto our stack of classes that is currently
1752/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001753void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1754 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001755 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001756 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001757}
1758
1759/// \brief Deallocate the given parsed class and all of its nested
1760/// classes.
1761void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1762 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1763 DeallocateParsedClasses(Class->NestedClasses[I]);
1764 delete Class;
1765}
1766
1767/// \brief Pop the top class of the stack of classes that are
1768/// currently being parsed.
1769///
1770/// This routine should be called when we have finished parsing the
1771/// definition of a class, but have not yet popped the Scope
1772/// associated with the class's definition.
1773///
1774/// \returns true if the class we've popped is a top-level class,
1775/// false otherwise.
1776void Parser::PopParsingClass() {
1777 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregor6569d682009-05-27 23:11:45 +00001779 ParsingClass *Victim = ClassStack.top();
1780 ClassStack.pop();
1781 if (Victim->TopLevelClass) {
1782 // Deallocate all of the nested classes of this class,
1783 // recursively: we don't need to keep any of this information.
1784 DeallocateParsedClasses(Victim);
1785 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001786 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001787 assert(!ClassStack.empty() && "Missing top-level class?");
1788
1789 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1790 Victim->NestedClasses.empty()) {
1791 // The victim is a nested class, but we will not need to perform
1792 // any processing after the definition of this class since it has
1793 // no members whose handling was delayed. Therefore, we can just
1794 // remove this nested class.
1795 delete Victim;
1796 return;
1797 }
1798
1799 // This nested class has some members that will need to be processed
1800 // after the top-level class is completely defined. Therefore, add
1801 // it to the list of nested classes within its parent.
1802 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1803 ClassStack.top()->NestedClasses.push_back(Victim);
1804 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1805}
Sean Huntbbd37c62009-11-21 08:43:09 +00001806
1807/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1808/// parses standard attributes.
1809///
1810/// [C++0x] attribute-specifier:
1811/// '[' '[' attribute-list ']' ']'
1812///
1813/// [C++0x] attribute-list:
1814/// attribute[opt]
1815/// attribute-list ',' attribute[opt]
1816///
1817/// [C++0x] attribute:
1818/// attribute-token attribute-argument-clause[opt]
1819///
1820/// [C++0x] attribute-token:
1821/// identifier
1822/// attribute-scoped-token
1823///
1824/// [C++0x] attribute-scoped-token:
1825/// attribute-namespace '::' identifier
1826///
1827/// [C++0x] attribute-namespace:
1828/// identifier
1829///
1830/// [C++0x] attribute-argument-clause:
1831/// '(' balanced-token-seq ')'
1832///
1833/// [C++0x] balanced-token-seq:
1834/// balanced-token
1835/// balanced-token-seq balanced-token
1836///
1837/// [C++0x] balanced-token:
1838/// '(' balanced-token-seq ')'
1839/// '[' balanced-token-seq ']'
1840/// '{' balanced-token-seq '}'
1841/// any token but '(', ')', '[', ']', '{', or '}'
1842CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1843 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1844 && "Not a C++0x attribute list");
1845
1846 SourceLocation StartLoc = Tok.getLocation(), Loc;
1847 AttributeList *CurrAttr = 0;
1848
1849 ConsumeBracket();
1850 ConsumeBracket();
1851
1852 if (Tok.is(tok::comma)) {
1853 Diag(Tok.getLocation(), diag::err_expected_ident);
1854 ConsumeToken();
1855 }
1856
1857 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1858 // attribute not present
1859 if (Tok.is(tok::comma)) {
1860 ConsumeToken();
1861 continue;
1862 }
1863
1864 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1865 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
1866
1867 // scoped attribute
1868 if (Tok.is(tok::coloncolon)) {
1869 ConsumeToken();
1870
1871 if (!Tok.is(tok::identifier)) {
1872 Diag(Tok.getLocation(), diag::err_expected_ident);
1873 SkipUntil(tok::r_square, tok::comma, true, true);
1874 continue;
1875 }
1876
1877 ScopeName = AttrName;
1878 ScopeLoc = AttrLoc;
1879
1880 AttrName = Tok.getIdentifierInfo();
1881 AttrLoc = ConsumeToken();
1882 }
1883
1884 bool AttrParsed = false;
1885 // No scoped names are supported; ideally we could put all non-standard
1886 // attributes into namespaces.
1887 if (!ScopeName) {
1888 switch(AttributeList::getKind(AttrName))
1889 {
1890 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001891 case AttributeList::AT_base_check:
1892 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001893 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001894 case AttributeList::AT_hiding:
1895 case AttributeList::AT_noreturn:
1896 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001897 if (Tok.is(tok::l_paren)) {
1898 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1899 << AttrName->getName();
1900 break;
1901 }
1902
1903 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1904 SourceLocation(), 0, 0, CurrAttr, false,
1905 true);
1906 AttrParsed = true;
1907 break;
1908 }
1909
1910 // One argument; must be a type-id or assignment-expression
1911 case AttributeList::AT_aligned: {
1912 if (Tok.isNot(tok::l_paren)) {
1913 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1914 << AttrName->getName();
1915 break;
1916 }
1917 SourceLocation ParamLoc = ConsumeParen();
1918
1919 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1920
1921 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1922
1923 ExprVector ArgExprs(Actions);
1924 ArgExprs.push_back(ArgExpr.release());
1925 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1926 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1927 false, true);
1928
1929 AttrParsed = true;
1930 break;
1931 }
1932
1933 // Silence warnings
1934 default: break;
1935 }
1936 }
1937
1938 // Skip the entire parameter clause, if any
1939 if (!AttrParsed && Tok.is(tok::l_paren)) {
1940 ConsumeParen();
1941 // SkipUntil maintains the balancedness of tokens.
1942 SkipUntil(tok::r_paren, false);
1943 }
1944 }
1945
1946 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1947 SkipUntil(tok::r_square, false);
1948 Loc = Tok.getLocation();
1949 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1950 SkipUntil(tok::r_square, false);
1951
1952 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1953 return Attr;
1954}
1955
1956/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1957/// attribute.
1958///
1959/// FIXME: Simply returns an alignof() expression if the argument is a
1960/// type. Ideally, the type should be propagated directly into Sema.
1961///
1962/// [C++0x] 'align' '(' type-id ')'
1963/// [C++0x] 'align' '(' assignment-expression ')'
1964Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1965 if (isTypeIdInParens()) {
1966 EnterExpressionEvaluationContext Unevaluated(Actions,
1967 Action::Unevaluated);
1968 SourceLocation TypeLoc = Tok.getLocation();
1969 TypeTy *Ty = ParseTypeName().get();
1970 SourceRange TypeRange(Start, Tok.getLocation());
1971 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
1972 TypeRange);
1973 } else
1974 return ParseConstantExpression();
1975}