Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1 | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anders Carlsson | 0c6139d | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 14 | #include "clang/Basic/OperatorKinds.h" |
Douglas Gregor | 1b7f898 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
| 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 20 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 21 | #include "RAIIObjectsForParser.h" |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 25 | /// may either be a top level namespace or a block-level namespace alias. If |
| 26 | /// there was an inline keyword, it has already been parsed. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 27 | /// |
| 28 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 29 | /// named-namespace-definition |
| 30 | /// unnamed-namespace-definition |
| 31 | /// |
| 32 | /// unnamed-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 33 | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// named-namespace-definition: |
| 36 | /// original-namespace-definition |
| 37 | /// extension-namespace-definition |
| 38 | /// |
| 39 | /// original-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 40 | /// 'inline'[opt] 'namespace' identifier attributes[opt] |
| 41 | /// '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 42 | /// |
| 43 | /// extension-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 44 | /// 'inline'[opt] 'namespace' original-namespace-name |
| 45 | /// '{' namespace-body '}' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | /// |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 47 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 48 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 49 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 50 | Decl *Parser::ParseNamespace(unsigned Context, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 51 | SourceLocation &DeclEnd, |
| 52 | SourceLocation InlineLoc) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 53 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 54 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 56 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 57 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 58 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 59 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 61 | SourceLocation IdentLoc; |
| 62 | IdentifierInfo *Ident = 0; |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 63 | |
| 64 | Token attrTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 66 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 67 | Ident = Tok.getIdentifierInfo(); |
| 68 | IdentLoc = ConsumeToken(); // eat the identifier. |
| 69 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 71 | // Read label attributes, if present. |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 72 | llvm::OwningPtr<AttributeList> AttrList; |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 73 | if (Tok.is(tok::kw___attribute)) { |
| 74 | attrTok = Tok; |
| 75 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 76 | // FIXME: save these somewhere. |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 77 | AttrList.reset(ParseGNUAttributes()); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 78 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 80 | if (Tok.is(tok::equal)) { |
| 81 | if (AttrList) |
| 82 | Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 83 | if (InlineLoc.isValid()) |
| 84 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
| 85 | << FixItHint::CreateRemoval(InlineLoc); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 87 | return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 88 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 90 | if (Tok.isNot(tok::l_brace)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 92 | diag::err_expected_ident_lbrace); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 93 | return 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 94 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 96 | SourceLocation LBrace = ConsumeBrace(); |
| 97 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 98 | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
| 99 | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
| 100 | getCurScope()->getFnParent()) { |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 101 | Diag(LBrace, diag::err_namespace_nonnamespace_scope); |
| 102 | SkipUntil(tok::r_brace, false); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 103 | return 0; |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 106 | // If we're still good, complain about inline namespaces in non-C++0x now. |
| 107 | if (!getLang().CPlusPlus0x && InlineLoc.isValid()) |
| 108 | Diag(InlineLoc, diag::ext_inline_namespace); |
| 109 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 110 | // Enter a scope for the namespace. |
| 111 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 112 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 113 | Decl *NamespcDecl = |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 114 | Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, IdentLoc, Ident, |
| 115 | LBrace, AttrList.get()); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 116 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 117 | PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc, |
| 118 | "parsing namespace"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 120 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 121 | CXX0XAttributeList Attr; |
| 122 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) |
| 123 | Attr = ParseCXX0XAttributes(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 124 | if (getLang().Microsoft && Tok.is(tok::l_square)) |
| 125 | ParseMicrosoftAttributes(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 126 | ParseExternalDeclaration(Attr); |
| 127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 129 | // Leave the namespace scope. |
| 130 | NamespaceScope.Exit(); |
| 131 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 132 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace); |
| 133 | Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 135 | DeclEnd = RBraceLoc; |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 136 | return NamespcDecl; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 139 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 140 | /// alias definition. |
| 141 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 142 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | SourceLocation AliasLoc, |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 144 | IdentifierInfo *Alias, |
| 145 | SourceLocation &DeclEnd) { |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 146 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 148 | ConsumeToken(); // eat the '='. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 150 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 151 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 152 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 153 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 154 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 155 | CXXScopeSpec SS; |
| 156 | // Parse (optional) nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 157 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 158 | |
| 159 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
| 160 | Diag(Tok, diag::err_expected_namespace_name); |
| 161 | // Skip to end of the definition and eat the ';'. |
| 162 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 163 | return 0; |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Parse identifier. |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 167 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 168 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 170 | // Eat the ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 171 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 172 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, |
| 173 | "", tok::semi); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 175 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 176 | SS, IdentLoc, Ident); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 179 | /// ParseLinkage - We know that the current token is a string_literal |
| 180 | /// and just before that, that extern was seen. |
| 181 | /// |
| 182 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 183 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 184 | /// 'extern' string-literal declaration |
| 185 | /// |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame^] | 186 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) { |
Douglas Gregor | c19923d | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 187 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 188 | llvm::SmallString<8> LangBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 189 | bool Invalid = false; |
| 190 | llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid); |
| 191 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 192 | return 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 193 | |
| 194 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 196 | ParseScope LinkageScope(this, Scope::DeclScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 197 | Decl *LinkageSpec |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 198 | = Actions.ActOnStartLinkageSpecification(getCurScope(), |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 199 | /*FIXME: */SourceLocation(), |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 200 | Loc, Lang, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | Tok.is(tok::l_brace)? Tok.getLocation() |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 202 | : SourceLocation()); |
| 203 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 204 | CXX0XAttributeList Attr; |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame^] | 205 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 206 | Attr = ParseCXX0XAttributes(); |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame^] | 207 | |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 208 | if (getLang().Microsoft && Tok.is(tok::l_square)) |
| 209 | ParseMicrosoftAttributes(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 211 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 212 | DS.setExternInLinkageSpec(true); |
Douglas Gregor | 09a63c9 | 2010-08-24 14:14:45 +0000 | [diff] [blame] | 213 | ParseExternalDeclaration(Attr, &DS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 214 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 215 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 63a0113 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 218 | DS.abort(); |
| 219 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 220 | if (Attr.HasAttr) |
| 221 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 222 | << Attr.Range; |
| 223 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 224 | SourceLocation LBrace = ConsumeBrace(); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 225 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 226 | CXX0XAttributeList Attr; |
| 227 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) |
| 228 | Attr = ParseCXX0XAttributes(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 229 | if (getLang().Microsoft && Tok.is(tok::l_square)) |
| 230 | ParseMicrosoftAttributes(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 231 | ParseExternalDeclaration(Attr); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 234 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame^] | 235 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
| 236 | RBrace); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 237 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 238 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 239 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 240 | /// using-directive. Assumes that current token is 'using'. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 241 | Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 242 | SourceLocation &DeclEnd, |
| 243 | CXX0XAttributeList Attr) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 244 | assert(Tok.is(tok::kw_using) && "Not using token"); |
| 245 | |
| 246 | // Eat 'using'. |
| 247 | SourceLocation UsingLoc = ConsumeToken(); |
| 248 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 249 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 250 | Actions.CodeCompleteUsing(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 251 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 252 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 2f27477 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 254 | if (Tok.is(tok::kw_namespace)) |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 255 | // Next token after 'using' is 'namespace' so it must be using-directive |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 256 | return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList); |
| 257 | |
| 258 | if (Attr.HasAttr) |
| 259 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 260 | << Attr.Range; |
Chris Lattner | 2f27477 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 261 | |
| 262 | // Otherwise, it must be using-declaration. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 263 | // Ignore illegal attributes (the caller should already have issued an error. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 264 | return ParseUsingDeclaration(Context, UsingLoc, DeclEnd); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 268 | /// that current token is 'namespace' and 'using' was already parsed. |
| 269 | /// |
| 270 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 271 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 272 | /// namespace-name ; |
| 273 | /// [GNU] using-directive: |
| 274 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 275 | /// namespace-name attributes[opt] ; |
| 276 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 277 | Decl *Parser::ParseUsingDirective(unsigned Context, |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 278 | SourceLocation UsingLoc, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 279 | SourceLocation &DeclEnd, |
| 280 | AttributeList *Attr) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 281 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 282 | |
| 283 | // Eat 'namespace'. |
| 284 | SourceLocation NamespcLoc = ConsumeToken(); |
| 285 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 286 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 287 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 288 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 289 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 291 | CXXScopeSpec SS; |
| 292 | // Parse (optional) nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 293 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 295 | IdentifierInfo *NamespcName = 0; |
| 296 | SourceLocation IdentLoc = SourceLocation(); |
| 297 | |
| 298 | // Parse namespace-name. |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 299 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 300 | Diag(Tok, diag::err_expected_namespace_name); |
| 301 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 302 | SkipUntil(tok::semi); |
| 303 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 304 | return 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 307 | // Parse identifier. |
| 308 | NamespcName = Tok.getIdentifierInfo(); |
| 309 | IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 311 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 312 | bool GNUAttr = false; |
| 313 | if (Tok.is(tok::kw___attribute)) { |
| 314 | GNUAttr = true; |
| 315 | Attr = addAttributeLists(Attr, ParseGNUAttributes()); |
| 316 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 318 | // Eat ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 319 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 320 | ExpectAndConsume(tok::semi, |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 321 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
| 322 | : diag::err_expected_semi_after_namespace_name, |
| 323 | "", tok::semi); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 325 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 326 | IdentLoc, NamespcName, Attr); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that |
| 330 | /// 'using' was already seen. |
| 331 | /// |
| 332 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 333 | /// 'using' 'typename'[opt] ::[opt] nested-name-specifier |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 334 | /// unqualified-id |
| 335 | /// 'using' :: unqualified-id |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 336 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 337 | Decl *Parser::ParseUsingDeclaration(unsigned Context, |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 338 | SourceLocation UsingLoc, |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 339 | SourceLocation &DeclEnd, |
| 340 | AccessSpecifier AS) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 341 | CXXScopeSpec SS; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 342 | SourceLocation TypenameLoc; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 343 | bool IsTypeName; |
| 344 | |
| 345 | // Ignore optional 'typename'. |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 346 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 347 | if (Tok.is(tok::kw_typename)) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 348 | TypenameLoc = Tok.getLocation(); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 349 | ConsumeToken(); |
| 350 | IsTypeName = true; |
| 351 | } |
| 352 | else |
| 353 | IsTypeName = false; |
| 354 | |
| 355 | // Parse nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 356 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 358 | // Check nested-name specifier. |
| 359 | if (SS.isInvalid()) { |
| 360 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 361 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 362 | } |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 363 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 364 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 365 | // destructor names and allow the action module to diagnose any semantic |
| 366 | // errors. |
| 367 | UnqualifiedId Name; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 368 | if (ParseUnqualifiedId(SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 369 | /*EnteringContext=*/false, |
| 370 | /*AllowDestructorName=*/true, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 371 | /*AllowConstructorName=*/true, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 372 | ParsedType(), |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 373 | Name)) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 374 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 375 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 376 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 378 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 379 | llvm::OwningPtr<AttributeList> AttrList; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 380 | if (Tok.is(tok::kw___attribute)) |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 381 | AttrList.reset(ParseGNUAttributes()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 383 | // Eat ';'. |
| 384 | DeclEnd = Tok.getLocation(); |
| 385 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 386 | AttrList ? "attributes list" : "using declaration", |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 387 | tok::semi); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 389 | return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, Name, |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 390 | AttrList.get(), IsTypeName, TypenameLoc); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 393 | /// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion. |
| 394 | /// |
| 395 | /// static_assert-declaration: |
| 396 | /// static_assert ( constant-expression , string-literal ) ; |
| 397 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 398 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 399 | assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration"); |
| 400 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 402 | if (Tok.isNot(tok::l_paren)) { |
| 403 | Diag(Tok, diag::err_expected_lparen); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 404 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 407 | SourceLocation LParenLoc = ConsumeParen(); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 408 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 409 | ExprResult AssertExpr(ParseConstantExpression()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 410 | if (AssertExpr.isInvalid()) { |
| 411 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 412 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 415 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 416 | return 0; |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 417 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 418 | if (Tok.isNot(tok::string_literal)) { |
| 419 | Diag(Tok, diag::err_expected_string_literal); |
| 420 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 421 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 424 | ExprResult AssertMessage(ParseStringLiteralExpression()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | if (AssertMessage.isInvalid()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 426 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 427 | |
Anders Carlsson | 94b15fb | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 428 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 430 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 431 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 432 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 433 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
| 434 | AssertExpr.take(), |
| 435 | AssertMessage.take()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 438 | /// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. |
| 439 | /// |
| 440 | /// 'decltype' ( expression ) |
| 441 | /// |
| 442 | void Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
| 443 | assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier"); |
| 444 | |
| 445 | SourceLocation StartLoc = ConsumeToken(); |
| 446 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
| 448 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 449 | "decltype")) { |
| 450 | SkipUntil(tok::r_paren); |
| 451 | return; |
| 452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 454 | // Parse the expression |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 456 | // C++0x [dcl.type.simple]p4: |
| 457 | // The operand of the decltype specifier is an unevaluated operand. |
| 458 | EnterExpressionEvaluationContext Unevaluated(Actions, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 459 | Sema::Unevaluated); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 460 | ExprResult Result = ParseExpression(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 461 | if (Result.isInvalid()) { |
| 462 | SkipUntil(tok::r_paren); |
| 463 | return; |
| 464 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 466 | // Match the ')' |
| 467 | SourceLocation RParenLoc; |
| 468 | if (Tok.is(tok::r_paren)) |
| 469 | RParenLoc = ConsumeParen(); |
| 470 | else |
| 471 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 473 | if (RParenLoc.isInvalid()) |
| 474 | return; |
| 475 | |
| 476 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 477 | unsigned DiagID; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 478 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 480 | DiagID, Result.release())) |
| 481 | Diag(StartLoc, DiagID) << PrevSpec; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 484 | /// ParseClassName - Parse a C++ class-name, which names a class. Note |
| 485 | /// that we only check that the result names a type; semantic analysis |
| 486 | /// will need to verify that the type names a class. The result is |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 487 | /// either a type or NULL, depending on whether a type name was |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 488 | /// found. |
| 489 | /// |
| 490 | /// class-name: [C++ 9.1] |
| 491 | /// identifier |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 492 | /// simple-template-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | /// |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 494 | Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 495 | CXXScopeSpec *SS) { |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 496 | // Check whether we have a template-id that names a type. |
| 497 | if (Tok.is(tok::annot_template_id)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 499 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 500 | if (TemplateId->Kind == TNK_Type_template || |
| 501 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 502 | AnnotateTemplateIdTokenAsType(SS); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 503 | |
| 504 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 505 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 506 | EndLocation = Tok.getAnnotationEndLoc(); |
| 507 | ConsumeToken(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 508 | |
| 509 | if (Type) |
| 510 | return Type; |
| 511 | return true; |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Fall through to produce an error below. |
| 515 | } |
| 516 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 517 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 518 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 519 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 522 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 523 | SourceLocation IdLoc = ConsumeToken(); |
| 524 | |
| 525 | if (Tok.is(tok::less)) { |
| 526 | // It looks the user intended to write a template-id here, but the |
| 527 | // template-name was wrong. Try to fix that. |
| 528 | TemplateNameKind TNK = TNK_Type_template; |
| 529 | TemplateTy Template; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 530 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 531 | SS, Template, TNK)) { |
| 532 | Diag(IdLoc, diag::err_unknown_template_name) |
| 533 | << Id; |
| 534 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 535 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 536 | if (!Template) |
| 537 | return true; |
| 538 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 539 | // Form the template name |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 540 | UnqualifiedId TemplateName; |
| 541 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 542 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 543 | // Parse the full template-id, then turn it into a type. |
| 544 | if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName, |
| 545 | SourceLocation(), true)) |
| 546 | return true; |
| 547 | if (TNK == TNK_Dependent_template_name) |
| 548 | AnnotateTemplateIdTokenAsType(SS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 550 | // If we didn't end up with a typename token, there's nothing more we |
| 551 | // can do. |
| 552 | if (Tok.isNot(tok::annot_typename)) |
| 553 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 555 | // Retrieve the type from the annotation token, consume that token, and |
| 556 | // return. |
| 557 | EndLocation = Tok.getAnnotationEndLoc(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 558 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 559 | ConsumeToken(); |
| 560 | return Type; |
| 561 | } |
| 562 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 563 | // We have an identifier; check whether it is actually a type. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 564 | ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 565 | if (!Type) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 566 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 567 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // Consume the identifier. |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 571 | EndLocation = IdLoc; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 572 | |
| 573 | // Fake up a Declarator to use with ActOnTypeName. |
| 574 | DeclSpec DS; |
| 575 | DS.SetRangeStart(IdLoc); |
| 576 | DS.SetRangeEnd(EndLocation); |
| 577 | DS.getTypeSpecScope() = *SS; |
| 578 | |
| 579 | const char *PrevSpec = 0; |
| 580 | unsigned DiagID; |
| 581 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type); |
| 582 | |
| 583 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 584 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 587 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 588 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 589 | /// until we reach the start of a definition or see a token that |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 590 | /// cannot start a definition. If SuppressDeclarations is true, we do know. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 591 | /// |
| 592 | /// class-specifier: [C++ class] |
| 593 | /// class-head '{' member-specification[opt] '}' |
| 594 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 595 | /// class-head: |
| 596 | /// class-key identifier[opt] base-clause[opt] |
| 597 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 598 | /// class-key nested-name-specifier[opt] simple-template-id |
| 599 | /// base-clause[opt] |
| 600 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 602 | /// identifier base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 604 | /// simple-template-id base-clause[opt] |
| 605 | /// class-key: |
| 606 | /// 'class' |
| 607 | /// 'struct' |
| 608 | /// 'union' |
| 609 | /// |
| 610 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 612 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 613 | /// simple-template-id |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 614 | /// |
| 615 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 616 | /// together, subsume the C99 struct-or-union-specifier: |
| 617 | /// |
| 618 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 619 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 620 | /// struct-or-union identifier |
| 621 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 622 | /// '}' attributes[opt] |
| 623 | /// [GNU] struct-or-union attributes[opt] identifier |
| 624 | /// struct-or-union: |
| 625 | /// 'struct' |
| 626 | /// 'union' |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 627 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 628 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 629 | const ParsedTemplateInfo &TemplateInfo, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 630 | AccessSpecifier AS, bool SuppressDeclarations){ |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 631 | DeclSpec::TST TagType; |
| 632 | if (TagTokKind == tok::kw_struct) |
| 633 | TagType = DeclSpec::TST_struct; |
| 634 | else if (TagTokKind == tok::kw_class) |
| 635 | TagType = DeclSpec::TST_class; |
| 636 | else { |
| 637 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 638 | TagType = DeclSpec::TST_union; |
| 639 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 640 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 641 | if (Tok.is(tok::code_completion)) { |
| 642 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 643 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 644 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 645 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 646 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 647 | // C++03 [temp.explicit] 14.7.2/8: |
| 648 | // The usual access checking rules do not apply to names used to specify |
| 649 | // explicit instantiations. |
| 650 | // |
| 651 | // As an extension we do not perform access checking on the names used to |
| 652 | // specify explicit specializations either. This is important to allow |
| 653 | // specializing traits classes for private types. |
| 654 | bool SuppressingAccessChecks = false; |
| 655 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 656 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) { |
| 657 | Actions.ActOnStartSuppressingAccessChecks(); |
| 658 | SuppressingAccessChecks = true; |
| 659 | } |
| 660 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 661 | AttributeList *AttrList = 0; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 662 | // If attributes exist after tag, parse them. |
| 663 | if (Tok.is(tok::kw___attribute)) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 664 | AttrList = ParseGNUAttributes(); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 665 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 666 | // If declspecs exist after tag, parse them. |
John McCall | b1d397c | 2010-08-05 17:13:11 +0000 | [diff] [blame] | 667 | while (Tok.is(tok::kw___declspec)) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 668 | AttrList = ParseMicrosoftDeclSpec(AttrList); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 669 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 670 | // If C++0x attributes exist here, parse them. |
| 671 | // FIXME: Are we consistent with the ordering of parsing of different |
| 672 | // styles of attributes? |
| 673 | if (isCXX0XAttributeSpecifier()) |
| 674 | AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 676 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) { |
| 677 | // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but |
| 678 | // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | // token sequence "struct __is_pod", make __is_pod into a normal |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 680 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 681 | // properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 682 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 683 | Tok.setKind(tok::identifier); |
| 684 | } |
| 685 | |
| 686 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) { |
| 687 | // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but |
| 688 | // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | // token sequence "struct __is_empty", make __is_empty into a normal |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 690 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 691 | // properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 692 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 693 | Tok.setKind(tok::identifier); |
| 694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 696 | // Parse the (optional) nested-name-specifier. |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 697 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 698 | if (getLang().CPlusPlus) { |
| 699 | // "FOO : BAR" is not a potential typo for "FOO::BAR". |
| 700 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 701 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 702 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 703 | DS.SetTypeSpecError(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 704 | if (SS.isSet()) |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 705 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
| 706 | Diag(Tok, diag::err_expected_ident); |
| 707 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 708 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 709 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 710 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 711 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 712 | IdentifierInfo *Name = 0; |
| 713 | SourceLocation NameLoc; |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 714 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 715 | if (Tok.is(tok::identifier)) { |
| 716 | Name = Tok.getIdentifierInfo(); |
| 717 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 718 | |
Douglas Gregor | 5ee3734 | 2010-05-30 22:30:21 +0000 | [diff] [blame] | 719 | if (Tok.is(tok::less) && getLang().CPlusPlus) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 720 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 721 | // Eat the template argument list and try to continue parsing this as |
| 722 | // a class (or template thereof). |
| 723 | TemplateArgList TemplateArgs; |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 724 | SourceLocation LAngleLoc, RAngleLoc; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 725 | if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS, |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 726 | true, LAngleLoc, |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 727 | TemplateArgs, RAngleLoc)) { |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 728 | // We couldn't parse the template argument list at all, so don't |
| 729 | // try to give any location information for the list. |
| 730 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 731 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 733 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 734 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 735 | << (TagType == DeclSpec::TST_class? 0 |
| 736 | : TagType == DeclSpec::TST_struct? 1 |
| 737 | : 2) |
| 738 | << Name |
| 739 | << SourceRange(LAngleLoc, RAngleLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 740 | |
| 741 | // Strip off the last template parameter list if it was empty, since |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 742 | // we've removed its template argument list. |
| 743 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
| 744 | if (TemplateParams && TemplateParams->size() > 1) { |
| 745 | TemplateParams->pop_back(); |
| 746 | } else { |
| 747 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 748 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 749 | = ParsedTemplateInfo::NonTemplate; |
| 750 | } |
| 751 | } else if (TemplateInfo.Kind |
| 752 | == ParsedTemplateInfo::ExplicitInstantiation) { |
| 753 | // Pretend this is just a forward declaration. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 754 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 755 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 756 | = ParsedTemplateInfo::NonTemplate; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 757 | const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 758 | = SourceLocation(); |
| 759 | const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc |
| 760 | = SourceLocation(); |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 761 | } |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 762 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 763 | } else if (Tok.is(tok::annot_template_id)) { |
| 764 | TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 765 | NameLoc = ConsumeToken(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 767 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 768 | // The template-name in the simple-template-id refers to |
| 769 | // something other than a class template. Give an appropriate |
| 770 | // error message and skip to the ';'. |
| 771 | SourceRange Range(NameLoc); |
| 772 | if (SS.isNotEmpty()) |
| 773 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 775 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
| 776 | << Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 778 | DS.SetTypeSpecError(); |
| 779 | SkipUntil(tok::semi, false, true); |
| 780 | TemplateId->Destroy(); |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 781 | if (SuppressingAccessChecks) |
| 782 | Actions.ActOnStopSuppressingAccessChecks(); |
| 783 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 784 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 785 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 788 | // As soon as we're finished parsing the class's template-id, turn access |
| 789 | // checking back on. |
| 790 | if (SuppressingAccessChecks) |
| 791 | Actions.ActOnStopSuppressingAccessChecks(); |
| 792 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 793 | // There are four options here. If we have 'struct foo;', then this |
| 794 | // is either a forward declaration or a friend declaration, which |
| 795 | // have to be treated differently. If we have 'struct foo {...' or |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 796 | // 'struct foo :...' then this is a definition. Otherwise we have |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 797 | // something like 'struct foo xyz', a reference. |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 798 | // However, in some contexts, things look like declarations but are just |
| 799 | // references, e.g. |
| 800 | // new struct s; |
| 801 | // or |
| 802 | // &T::operator struct s; |
| 803 | // For these, SuppressDeclarations is true. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 804 | Sema::TagUseKind TUK; |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 805 | if (SuppressDeclarations) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 806 | TUK = Sema::TUK_Reference; |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 807 | else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){ |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 808 | if (DS.isFriendSpecified()) { |
| 809 | // C++ [class.friend]p2: |
| 810 | // A class shall not be defined in a friend declaration. |
| 811 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_class) |
| 812 | << SourceRange(DS.getFriendSpecLoc()); |
| 813 | |
| 814 | // Skip everything up to the semicolon, so that this looks like a proper |
| 815 | // friend class (or template thereof) declaration. |
| 816 | SkipUntil(tok::semi, true, true); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 817 | TUK = Sema::TUK_Friend; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 818 | } else { |
| 819 | // Okay, this is a class definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 820 | TUK = Sema::TUK_Definition; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 821 | } |
| 822 | } else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 823 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 824 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 825 | TUK = Sema::TUK_Reference; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 826 | |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 827 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 828 | TUK != Sema::TUK_Definition)) { |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 829 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 830 | // We have a declaration or reference to an anonymous class. |
| 831 | Diag(StartLoc, diag::err_anon_type_definition) |
| 832 | << DeclSpec::getSpecifierName(TagType); |
| 833 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 835 | SkipUntil(tok::comma, true); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 836 | |
| 837 | if (TemplateId) |
| 838 | TemplateId->Destroy(); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 839 | return; |
| 840 | } |
| 841 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 842 | // Create the tag portion of the class or class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 843 | DeclResult TagOrTempResult = true; // invalid |
| 844 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 846 | bool Owned = false; |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 847 | if (TemplateId) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 848 | // Explicit specialization, class template partial specialization, |
| 849 | // or explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 851 | TemplateId->getTemplateArgs(), |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 852 | TemplateId->NumArgs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 853 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 854 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 855 | // This is an explicit instantiation of a class template. |
| 856 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 857 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 858 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | TemplateInfo.TemplateLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 860 | TagType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | StartLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 862 | SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 863 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | TemplateId->TemplateNameLoc, |
| 865 | TemplateId->LAngleLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 866 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | TemplateId->RAngleLoc, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 868 | AttrList); |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 869 | |
| 870 | // Friend template-ids are treated as references unless |
| 871 | // they have template headers, in which case they're ill-formed |
| 872 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 873 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 874 | } else if (TUK == Sema::TUK_Reference || |
| 875 | (TUK == Sema::TUK_Friend && |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 876 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 877 | TypeResult |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 878 | = Actions.ActOnTemplateIdType(TemplateId->Template, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 879 | TemplateId->TemplateNameLoc, |
| 880 | TemplateId->LAngleLoc, |
| 881 | TemplateArgsPtr, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 882 | TemplateId->RAngleLoc); |
| 883 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 884 | TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK, |
| 885 | TagType, StartLoc); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 886 | } else { |
| 887 | // This is an explicit specialization or a class template |
| 888 | // partial specialization. |
| 889 | TemplateParameterLists FakedParamLists; |
| 890 | |
| 891 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 892 | // This looks like an explicit instantiation, because we have |
| 893 | // something like |
| 894 | // |
| 895 | // template class Foo<X> |
| 896 | // |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 897 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 898 | // meant to be an explicit specialization, but the user forgot |
| 899 | // the '<>' after 'template'. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 900 | assert(TUK == Sema::TUK_Definition && "Expected a definition here"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 901 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | SourceLocation LAngleLoc |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 903 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | Diag(TemplateId->TemplateNameLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 905 | diag::err_explicit_instantiation_with_definition) |
| 906 | << SourceRange(TemplateInfo.TemplateLoc) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 907 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 908 | |
| 909 | // Create a fake template parameter list that contains only |
| 910 | // "template<>", so that we treat this construct as a class |
| 911 | // template specialization. |
| 912 | FakedParamLists.push_back( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | Actions.ActOnTemplateParameterList(0, SourceLocation(), |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 914 | TemplateInfo.TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | LAngleLoc, |
| 916 | 0, 0, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 917 | LAngleLoc)); |
| 918 | TemplateParams = &FakedParamLists; |
| 919 | } |
| 920 | |
| 921 | // Build the class template specialization. |
| 922 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 923 | = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 924 | StartLoc, SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 925 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | TemplateId->TemplateNameLoc, |
| 927 | TemplateId->LAngleLoc, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 928 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | TemplateId->RAngleLoc, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 930 | AttrList, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 931 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 932 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 933 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 934 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 935 | TemplateId->Destroy(); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 936 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 937 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 938 | // Explicit instantiation of a member of a class template |
| 939 | // specialization, e.g., |
| 940 | // |
| 941 | // template struct Outer<int>::Inner; |
| 942 | // |
| 943 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 944 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 945 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | TemplateInfo.TemplateLoc, |
| 947 | TagType, StartLoc, SS, Name, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 948 | NameLoc, AttrList); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 949 | } else if (TUK == Sema::TUK_Friend && |
| 950 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
| 951 | TagOrTempResult = |
| 952 | Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(), |
| 953 | TagType, StartLoc, SS, |
| 954 | Name, NameLoc, AttrList, |
| 955 | MultiTemplateParamsArg(Actions, |
| 956 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 957 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 958 | } else { |
| 959 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 960 | TUK == Sema::TUK_Definition) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 961 | // FIXME: Diagnose this particular error. |
| 962 | } |
| 963 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 964 | bool IsDependent = false; |
| 965 | |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 966 | // Don't pass down template parameter lists if this is just a tag |
| 967 | // reference. For example, we don't need the template parameters here: |
| 968 | // template <class T> class A *makeA(T t); |
| 969 | MultiTemplateParamsArg TParams; |
| 970 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 971 | TParams = |
| 972 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 973 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 974 | // Declaration or definition of a class type |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 975 | TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, |
| 976 | SS, Name, NameLoc, AttrList, AS, |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 977 | TParams, Owned, IsDependent, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 978 | clang::TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 979 | |
| 980 | // If ActOnTag said the type was dependent, try again with the |
| 981 | // less common call. |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 982 | if (IsDependent) { |
| 983 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 984 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 985 | SS, Name, StartLoc, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 986 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 987 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 988 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 989 | // If there is a body, parse it and inform the actions module. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 990 | if (TUK == Sema::TUK_Definition) { |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 991 | assert(Tok.is(tok::l_brace) || |
| 992 | (getLang().CPlusPlus && Tok.is(tok::colon))); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 993 | if (getLang().CPlusPlus) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 994 | ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 995 | else |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 996 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 997 | } |
| 998 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 999 | // FIXME: The DeclSpec should keep the locations of both the keyword and the |
| 1000 | // name (if there is one). |
| 1001 | SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc; |
| 1002 | |
| 1003 | const char *PrevSpec = 0; |
| 1004 | unsigned DiagID; |
| 1005 | bool Result; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1006 | if (!TypeResult.isInvalid()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1007 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, |
| 1008 | PrevSpec, DiagID, TypeResult.get()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1009 | } else if (!TagOrTempResult.isInvalid()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1010 | Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID, |
| 1011 | TagOrTempResult.get(), Owned); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1012 | } else { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1013 | DS.SetTypeSpecError(); |
Anders Carlsson | 66e9977 | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1014 | return; |
| 1015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1017 | if (Result) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1018 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1020 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 1021 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 1022 | // going to look at what comes after it to improve error recovery. If an |
| 1023 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 1024 | // the end of the declaration and recover that way. |
| 1025 | // |
| 1026 | // This switch enumerates the valid "follow" set for definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1027 | if (TUK == Sema::TUK_Definition) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1028 | bool ExpectedSemi = true; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1029 | switch (Tok.getKind()) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1030 | default: break; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1031 | case tok::semi: // struct foo {...} ; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1032 | case tok::star: // struct foo {...} * P; |
| 1033 | case tok::amp: // struct foo {...} & R = ... |
| 1034 | case tok::identifier: // struct foo {...} V ; |
| 1035 | case tok::r_paren: //(struct foo {...} ) {4} |
| 1036 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1037 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1038 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
Chris Lattner | c2e1c1a | 2010-02-03 20:41:24 +0000 | [diff] [blame] | 1039 | case tok::l_paren: // struct foo {...} ( x); |
Chris Lattner | 16acfee | 2010-02-03 01:45:03 +0000 | [diff] [blame] | 1040 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1041 | ExpectedSemi = false; |
| 1042 | break; |
| 1043 | // Type qualifiers |
| 1044 | case tok::kw_const: // struct foo {...} const x; |
| 1045 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1046 | case tok::kw_restrict: // struct foo {...} restrict x; |
| 1047 | case tok::kw_inline: // struct foo {...} inline foo() {}; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1048 | // Storage-class specifiers |
| 1049 | case tok::kw_static: // struct foo {...} static x; |
| 1050 | case tok::kw_extern: // struct foo {...} extern x; |
| 1051 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1052 | case tok::kw_register: // struct foo {...} register x; |
| 1053 | case tok::kw_auto: // struct foo {...} auto x; |
Douglas Gregor | 33f9924 | 2010-05-17 18:19:56 +0000 | [diff] [blame] | 1054 | case tok::kw_mutable: // struct foo {...} mutable x; |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1055 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1056 | // can occur after class specifiers according to the grammar. However, |
| 1057 | // almost noone actually writes code like this. If we see one of these, |
| 1058 | // it is much more likely that someone missed a semi colon and the |
| 1059 | // type/storage class specifier we're seeing is part of the *next* |
| 1060 | // intended declaration, as in: |
| 1061 | // |
| 1062 | // struct foo { ... } |
| 1063 | // typedef int X; |
| 1064 | // |
| 1065 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1066 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1067 | // the same declaration of X. Because of this, we look ahead past this |
| 1068 | // token to see if it's a type specifier. If so, we know the code is |
| 1069 | // otherwise invalid, so we can produce the expected semi error. |
| 1070 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1071 | ExpectedSemi = false; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1072 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1073 | |
| 1074 | case tok::r_brace: // struct bar { struct foo {...} } |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1075 | // Missing ';' at end of struct is accepted as an extension in C mode. |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1076 | if (!getLang().CPlusPlus) |
| 1077 | ExpectedSemi = false; |
| 1078 | break; |
| 1079 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1080 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1081 | if (ExpectedSemi) { |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1082 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1083 | TagType == DeclSpec::TST_class ? "class" |
| 1084 | : TagType == DeclSpec::TST_struct? "struct" : "union"); |
| 1085 | // Push this token back into the preprocessor and change our current token |
| 1086 | // to ';' so that the rest of the code recovers as though there were an |
| 1087 | // ';' after the definition. |
| 1088 | PP.EnterToken(Tok); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1089 | Tok.setKind(tok::semi); |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1090 | } |
| 1091 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1095 | /// |
| 1096 | /// base-clause : [C++ class.derived] |
| 1097 | /// ':' base-specifier-list |
| 1098 | /// base-specifier-list: |
| 1099 | /// base-specifier '...'[opt] |
| 1100 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1101 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1102 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 1103 | ConsumeToken(); |
| 1104 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1105 | // Build up an array of parsed base specifiers. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1106 | llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1108 | while (true) { |
| 1109 | // Parse a base-specifier. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1110 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1111 | if (Result.isInvalid()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1112 | // Skip the rest of this base specifier, up until the comma or |
| 1113 | // opening brace. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1114 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 1115 | } else { |
| 1116 | // Add this to our array of base specifiers. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1117 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | // If the next token is a comma, consume it and keep reading |
| 1121 | // base-specifiers. |
| 1122 | if (Tok.isNot(tok::comma)) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1124 | // Consume the comma. |
| 1125 | ConsumeToken(); |
| 1126 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1127 | |
| 1128 | // Attach the base specifiers |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1129 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 1133 | /// one entry in the base class list of a class specifier, for example: |
| 1134 | /// class foo : public bar, virtual private baz { |
| 1135 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 1136 | /// |
| 1137 | /// base-specifier: [C++ class.derived] |
| 1138 | /// ::[opt] nested-name-specifier[opt] class-name |
| 1139 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
| 1140 | /// class-name |
| 1141 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
| 1142 | /// class-name |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1143 | Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1144 | bool IsVirtual = false; |
| 1145 | SourceLocation StartLoc = Tok.getLocation(); |
| 1146 | |
| 1147 | // Parse the 'virtual' keyword. |
| 1148 | if (Tok.is(tok::kw_virtual)) { |
| 1149 | ConsumeToken(); |
| 1150 | IsVirtual = true; |
| 1151 | } |
| 1152 | |
| 1153 | // Parse an (optional) access specifier. |
| 1154 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1155 | if (Access != AS_none) |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1156 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1158 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 1159 | // access specifier. |
| 1160 | if (Tok.is(tok::kw_virtual)) { |
| 1161 | SourceLocation VirtualLoc = ConsumeToken(); |
| 1162 | if (IsVirtual) { |
| 1163 | // Complain about duplicate 'virtual' |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1164 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1165 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | IsVirtual = true; |
| 1169 | } |
| 1170 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1171 | // Parse optional '::' and optional nested-name-specifier. |
| 1172 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1173 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1175 | // The location of the base class itself. |
| 1176 | SourceLocation BaseLoc = Tok.getLocation(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Parse the class-name. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1179 | SourceLocation EndLocation; |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1180 | TypeResult BaseType = ParseClassName(EndLocation, &SS); |
| 1181 | if (BaseType.isInvalid()) |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1182 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
| 1184 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1185 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1187 | // Notify semantic analysis that we have parsed a complete |
| 1188 | // base-specifier. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1189 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1190 | BaseType.get(), BaseLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 1194 | /// a C++ access-specifier. |
| 1195 | /// |
| 1196 | /// access-specifier: [C++ class.derived] |
| 1197 | /// 'private' |
| 1198 | /// 'protected' |
| 1199 | /// 'public' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1201 | switch (Tok.getKind()) { |
| 1202 | default: return AS_none; |
| 1203 | case tok::kw_private: return AS_private; |
| 1204 | case tok::kw_protected: return AS_protected; |
| 1205 | case tok::kw_public: return AS_public; |
| 1206 | } |
| 1207 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1208 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1209 | void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1210 | Decl *ThisDecl) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1211 | // We just declared a member function. If this member function |
| 1212 | // has any default arguments, we'll need to parse them later. |
| 1213 | LateParsedMethodDeclaration *LateMethod = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | DeclaratorChunk::FunctionTypeInfo &FTI |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1215 | = DeclaratorInfo.getTypeObject(0).Fun; |
| 1216 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 1217 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 1218 | if (!LateMethod) { |
| 1219 | // Push this method onto the stack of late-parsed method |
| 1220 | // declarations. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1221 | LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
| 1222 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1223 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1224 | |
| 1225 | // Add all of the parameters prior to this one (they don't |
| 1226 | // have default arguments). |
| 1227 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 1228 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 1229 | LateMethod->DefaultArgs.push_back( |
Douglas Gregor | 8f8210c | 2010-03-02 01:29:43 +0000 | [diff] [blame] | 1230 | LateParsedDefaultArgument(FTI.ArgInfo[I].Param)); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | // Add this parameter to the list of parameters (it or may |
| 1234 | // not have a default argument). |
| 1235 | LateMethod->DefaultArgs.push_back( |
| 1236 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 1237 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1242 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 1243 | /// |
| 1244 | /// member-declaration: |
| 1245 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 1246 | /// function-definition ';'[opt] |
| 1247 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 1248 | /// using-declaration [TODO] |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1249 | /// [C++0x] static_assert-declaration |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1250 | /// template-declaration |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1251 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1252 | /// |
| 1253 | /// member-declarator-list: |
| 1254 | /// member-declarator |
| 1255 | /// member-declarator-list ',' member-declarator |
| 1256 | /// |
| 1257 | /// member-declarator: |
| 1258 | /// declarator pure-specifier[opt] |
| 1259 | /// declarator constant-initializer[opt] |
| 1260 | /// identifier[opt] ':' constant-expression |
| 1261 | /// |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1262 | /// pure-specifier: |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1263 | /// '= 0' |
| 1264 | /// |
| 1265 | /// constant-initializer: |
| 1266 | /// '=' constant-expression |
| 1267 | /// |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1268 | void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1269 | const ParsedTemplateInfo &TemplateInfo, |
| 1270 | ParsingDeclRAIIObject *TemplateDiags) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1271 | // Access declarations. |
| 1272 | if (!TemplateInfo.Kind && |
| 1273 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1274 | !TryAnnotateCXXScopeToken() && |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1275 | Tok.is(tok::annot_cxxscope)) { |
| 1276 | bool isAccessDecl = false; |
| 1277 | if (NextToken().is(tok::identifier)) |
| 1278 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 1279 | else |
| 1280 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 1281 | |
| 1282 | if (isAccessDecl) { |
| 1283 | // Collect the scope specifier token we annotated earlier. |
| 1284 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1285 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Try to parse an unqualified-id. |
| 1288 | UnqualifiedId Name; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1289 | if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1290 | SkipUntil(tok::semi); |
| 1291 | return; |
| 1292 | } |
| 1293 | |
| 1294 | // TODO: recover from mistakenly-qualified operator declarations. |
| 1295 | if (ExpectAndConsume(tok::semi, |
| 1296 | diag::err_expected_semi_after, |
| 1297 | "access declaration", |
| 1298 | tok::semi)) |
| 1299 | return; |
| 1300 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1301 | Actions.ActOnUsingDeclaration(getCurScope(), AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1302 | false, SourceLocation(), |
| 1303 | SS, Name, |
| 1304 | /* AttrList */ 0, |
| 1305 | /* IsTypeName */ false, |
| 1306 | SourceLocation()); |
| 1307 | return; |
| 1308 | } |
| 1309 | } |
| 1310 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1311 | // static_assert-declaration |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1312 | if (Tok.is(tok::kw_static_assert)) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1313 | // FIXME: Check for templates |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1314 | SourceLocation DeclEnd; |
| 1315 | ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1316 | return; |
| 1317 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1319 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1321 | "Nested template improperly parsed?"); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1322 | SourceLocation DeclEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1324 | AS); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | } |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1328 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 1329 | if (Tok.is(tok::kw___extension__)) { |
| 1330 | // __extension__ silences extension warnings in the subexpression. |
| 1331 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 1332 | ConsumeToken(); |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1333 | return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags); |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1334 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1336 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it |
| 1337 | // is a bitfield. |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1338 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1339 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1340 | CXX0XAttributeList AttrList; |
| 1341 | // Optional C++0x attribute-specifier |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1342 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1343 | AttrList = ParseCXX0XAttributes(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 1344 | if (getLang().Microsoft && Tok.is(tok::l_square)) |
| 1345 | ParseMicrosoftAttributes(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1347 | if (Tok.is(tok::kw_using)) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1348 | // FIXME: Check for template aliases |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1349 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1350 | if (AttrList.HasAttr) |
| 1351 | Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1352 | << AttrList.Range; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1354 | // Eat 'using'. |
| 1355 | SourceLocation UsingLoc = ConsumeToken(); |
| 1356 | |
| 1357 | if (Tok.is(tok::kw_namespace)) { |
| 1358 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
| 1359 | SkipUntil(tok::semi, true, true); |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1360 | } else { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1361 | SourceLocation DeclEnd; |
| 1362 | // Otherwise, it must be using-declaration. |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 1363 | ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1364 | } |
| 1365 | return; |
| 1366 | } |
| 1367 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1368 | SourceLocation DSStart = Tok.getLocation(); |
| 1369 | // decl-specifier-seq: |
| 1370 | // Parse the common declaration-specifiers piece. |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1371 | ParsingDeclSpec DS(*this, TemplateDiags); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1372 | DS.AddAttributes(AttrList.AttrList); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1373 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1374 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1375 | MultiTemplateParamsArg TemplateParams(Actions, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 1376 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, |
| 1377 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 1378 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1379 | if (Tok.is(tok::semi)) { |
| 1380 | ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1381 | Decl *TheDecl = |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1382 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); |
| 1383 | DS.complete(TheDecl); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1384 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1385 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1386 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1387 | ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1388 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1389 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1390 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 1391 | ColonProtectionRAIIObject X(*this); |
| 1392 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1393 | // Parse the first declarator. |
| 1394 | ParseDeclarator(DeclaratorInfo); |
| 1395 | // Error parsing the declarator? |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1396 | if (!DeclaratorInfo.hasName()) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1397 | // If so, skip until the semi-colon or a }. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1398 | SkipUntil(tok::r_brace, true); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1399 | if (Tok.is(tok::semi)) |
| 1400 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1401 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 1404 | // If attributes exist after the declarator, but before an '{', parse them. |
| 1405 | if (Tok.is(tok::kw___attribute)) { |
| 1406 | SourceLocation Loc; |
| 1407 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
| 1408 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 1409 | } |
| 1410 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1411 | // function-definition: |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1412 | if (Tok.is(tok::l_brace) |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1413 | || (DeclaratorInfo.isFunctionDeclarator() && |
| 1414 | (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1415 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
| 1416 | Diag(Tok, diag::err_func_def_no_params); |
| 1417 | ConsumeBrace(); |
| 1418 | SkipUntil(tok::r_brace, true); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1419 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1420 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1421 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1422 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1423 | Diag(Tok, diag::err_function_declared_typedef); |
| 1424 | // This recovery skips the entire function body. It would be nice |
| 1425 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 1426 | // assumes the declarator represents a function, not a typedef. |
| 1427 | ConsumeBrace(); |
| 1428 | SkipUntil(tok::r_brace, true); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1429 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1432 | ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1433 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1434 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | // member-declarator-list: |
| 1438 | // member-declarator |
| 1439 | // member-declarator-list ',' member-declarator |
| 1440 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1441 | llvm::SmallVector<Decl *, 8> DeclsInGroup; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1442 | ExprResult BitfieldSize; |
| 1443 | ExprResult Init; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1444 | bool Deleted = false; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1445 | |
| 1446 | while (1) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1447 | // member-declarator: |
| 1448 | // declarator pure-specifier[opt] |
| 1449 | // declarator constant-initializer[opt] |
| 1450 | // identifier[opt] ':' constant-expression |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1451 | if (Tok.is(tok::colon)) { |
| 1452 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1453 | BitfieldSize = ParseConstantExpression(); |
| 1454 | if (BitfieldSize.isInvalid()) |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1455 | SkipUntil(tok::comma, true, true); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1458 | // pure-specifier: |
| 1459 | // '= 0' |
| 1460 | // |
| 1461 | // constant-initializer: |
| 1462 | // '=' constant-expression |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1463 | // |
| 1464 | // defaulted/deleted function-definition: |
| 1465 | // '=' 'default' [TODO] |
| 1466 | // '=' 'delete' |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1467 | if (Tok.is(tok::equal)) { |
| 1468 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1469 | if (Tok.is(tok::kw_delete)) { |
| 1470 | if (!getLang().CPlusPlus0x) |
| 1471 | Diag(Tok, diag::warn_deleted_function_accepted_as_extension); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1472 | ConsumeToken(); |
| 1473 | Deleted = true; |
| 1474 | } else { |
| 1475 | Init = ParseInitializer(); |
| 1476 | if (Init.isInvalid()) |
| 1477 | SkipUntil(tok::comma, true, true); |
| 1478 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1481 | // If a simple-asm-expr is present, parse it. |
| 1482 | if (Tok.is(tok::kw_asm)) { |
| 1483 | SourceLocation Loc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1484 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1485 | if (AsmLabel.isInvalid()) |
| 1486 | SkipUntil(tok::comma, true, true); |
| 1487 | |
| 1488 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
| 1489 | DeclaratorInfo.SetRangeEnd(Loc); |
| 1490 | } |
| 1491 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1492 | // If attributes exist after the declarator, parse them. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1493 | if (Tok.is(tok::kw___attribute)) { |
| 1494 | SourceLocation Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1495 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1496 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 1497 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1498 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1499 | // NOTE: If Sema is the Action module and declarator is an instance field, |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1500 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1501 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1502 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1503 | Decl *ThisDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1504 | if (DS.isFriendSpecified()) { |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1505 | // TODO: handle initializers, bitfields, 'delete' |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1506 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1507 | /*IsDefinition*/ false, |
| 1508 | move(TemplateParams)); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1509 | } else { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1510 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1511 | DeclaratorInfo, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1512 | move(TemplateParams), |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1513 | BitfieldSize.release(), |
| 1514 | Init.release(), |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 1515 | /*IsDefinition*/Deleted, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1516 | Deleted); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1517 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1518 | if (ThisDecl) |
| 1519 | DeclsInGroup.push_back(ThisDecl); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1521 | if (DeclaratorInfo.isFunctionDeclarator() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1523 | != DeclSpec::SCS_typedef) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1524 | HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1527 | DeclaratorInfo.complete(ThisDecl); |
| 1528 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1529 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1530 | // or an error, bail out. |
| 1531 | if (Tok.isNot(tok::comma)) |
| 1532 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1534 | // Consume the comma. |
| 1535 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1537 | // Parse the next declarator. |
| 1538 | DeclaratorInfo.clear(); |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1539 | BitfieldSize = 0; |
| 1540 | Init = 0; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1541 | Deleted = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1543 | // Attributes are only allowed on the second declarator. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1544 | if (Tok.is(tok::kw___attribute)) { |
| 1545 | SourceLocation Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1546 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1547 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 1548 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1549 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1550 | if (Tok.isNot(tok::colon)) |
| 1551 | ParseDeclarator(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1554 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
| 1555 | // Skip to end of block or statement. |
| 1556 | SkipUntil(tok::r_brace, true, true); |
| 1557 | // If we stopped at a ';', eat it. |
| 1558 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1559 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1562 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(), |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1563 | DeclsInGroup.size()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 1567 | /// |
| 1568 | /// member-specification: |
| 1569 | /// member-declaration member-specification[opt] |
| 1570 | /// access-specifier ':' member-specification[opt] |
| 1571 | /// |
| 1572 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1573 | unsigned TagType, Decl *TagDecl) { |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1574 | assert((TagType == DeclSpec::TST_struct || |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1575 | TagType == DeclSpec::TST_union || |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1576 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1577 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1578 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 1579 | "parsing struct/union/class body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1581 | // Determine whether this is a non-nested class. Note that local |
| 1582 | // classes are *not* considered to be nested classes. |
| 1583 | bool NonNestedClass = true; |
| 1584 | if (!ClassStack.empty()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1585 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1586 | if (S->isClassScope()) { |
| 1587 | // We're inside a class scope, so this is a nested class. |
| 1588 | NonNestedClass = false; |
| 1589 | break; |
| 1590 | } |
| 1591 | |
| 1592 | if ((S->getFlags() & Scope::FnScope)) { |
| 1593 | // If we're in a function or function template declared in the |
| 1594 | // body of a class, then this is a local class rather than a |
| 1595 | // nested class. |
| 1596 | const Scope *Parent = S->getParent(); |
| 1597 | if (Parent->isTemplateParamScope()) |
| 1598 | Parent = Parent->getParent(); |
| 1599 | if (Parent->isClassScope()) |
| 1600 | break; |
| 1601 | } |
| 1602 | } |
| 1603 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1604 | |
| 1605 | // Enter a scope for the class. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1606 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1608 | // Note that we are parsing a new (potentially-nested) class definition. |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1609 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1610 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1611 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1612 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1613 | |
| 1614 | if (Tok.is(tok::colon)) { |
| 1615 | ParseBaseClause(TagDecl); |
| 1616 | |
| 1617 | if (!Tok.is(tok::l_brace)) { |
| 1618 | Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 1619 | |
| 1620 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1621 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1622 | return; |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | assert(Tok.is(tok::l_brace)); |
| 1627 | |
| 1628 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1629 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1630 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1631 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, LBraceLoc); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 1632 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1633 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 1634 | // by default. Members of a class defined with the keywords struct or union |
| 1635 | // are public by default. |
| 1636 | AccessSpecifier CurAS; |
| 1637 | if (TagType == DeclSpec::TST_class) |
| 1638 | CurAS = AS_private; |
| 1639 | else |
| 1640 | CurAS = AS_public; |
| 1641 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1642 | SourceLocation RBraceLoc; |
| 1643 | if (TagDecl) { |
| 1644 | // While we still have something to read, read the member-declarations. |
| 1645 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 1646 | // Each iteration of this loop reads one member-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1648 | // Check for extraneous top-level semicolon. |
| 1649 | if (Tok.is(tok::semi)) { |
| 1650 | Diag(Tok, diag::ext_extra_struct_semi) |
| 1651 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
| 1652 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1653 | ConsumeToken(); |
| 1654 | continue; |
| 1655 | } |
| 1656 | |
| 1657 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 1658 | if (AS != AS_none) { |
| 1659 | // Current token is a C++ access specifier. |
| 1660 | CurAS = AS; |
| 1661 | SourceLocation ASLoc = Tok.getLocation(); |
| 1662 | ConsumeToken(); |
| 1663 | if (Tok.is(tok::colon)) |
| 1664 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation()); |
| 1665 | else |
| 1666 | Diag(Tok, diag::err_expected_colon); |
| 1667 | ConsumeToken(); |
| 1668 | continue; |
| 1669 | } |
| 1670 | |
| 1671 | // FIXME: Make sure we don't have a template here. |
| 1672 | |
| 1673 | // Parse all the comma separated declarators. |
| 1674 | ParseCXXClassMemberDeclaration(CurAS); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1677 | RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 1678 | } else { |
| 1679 | SkipUntil(tok::r_brace, false, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1682 | // If attributes exist after class contents, parse them. |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1683 | llvm::OwningPtr<AttributeList> AttrList; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1684 | if (Tok.is(tok::kw___attribute)) |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 1685 | AttrList.reset(ParseGNUAttributes()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1686 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1687 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1688 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1689 | LBraceLoc, RBraceLoc, |
| 1690 | AttrList.get()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1691 | |
| 1692 | // C++ 9.2p2: Within the class member-specification, the class is regarded as |
| 1693 | // complete within function bodies, default arguments, |
| 1694 | // exception-specifications, and constructor ctor-initializers (including |
| 1695 | // such things in nested classes). |
| 1696 | // |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1697 | // FIXME: Only function bodies and constructor ctor-initializers are |
| 1698 | // parsed correctly, fix the rest. |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1699 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1700 | // We are not inside a nested class. This class and its nested classes |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1701 | // are complete and we can parse the delayed portions of method |
| 1702 | // declarations and the lexed inline method definitions. |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 1703 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1704 | ParseLexedMethodDeclarations(getCurrentClass()); |
| 1705 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 1706 | PrevTokLocation = SavedPrevTokLocation; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1709 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1710 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 1711 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1712 | // Leave the class scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1713 | ParsingDef.Pop(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1714 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1715 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1716 | |
| 1717 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 1718 | /// which explicitly initializes the members or base classes of a |
| 1719 | /// class (C++ [class.base.init]). For example, the three initializers |
| 1720 | /// after the ':' in the Derived constructor below: |
| 1721 | /// |
| 1722 | /// @code |
| 1723 | /// class Base { }; |
| 1724 | /// class Derived : Base { |
| 1725 | /// int x; |
| 1726 | /// float f; |
| 1727 | /// public: |
| 1728 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 1729 | /// }; |
| 1730 | /// @endcode |
| 1731 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | /// [C++] ctor-initializer: |
| 1733 | /// ':' mem-initializer-list |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1734 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | /// [C++] mem-initializer-list: |
| 1736 | /// mem-initializer |
| 1737 | /// mem-initializer , mem-initializer-list |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1738 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1739 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 1740 | |
| 1741 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1743 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 4> MemInitializers; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1744 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1745 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1746 | do { |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 1747 | if (Tok.is(tok::code_completion)) { |
| 1748 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 1749 | MemInitializers.data(), |
| 1750 | MemInitializers.size()); |
| 1751 | ConsumeCodeCompletionToken(); |
| 1752 | } else { |
| 1753 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 1754 | if (!MemInit.isInvalid()) |
| 1755 | MemInitializers.push_back(MemInit.get()); |
| 1756 | else |
| 1757 | AnyErrors = true; |
| 1758 | } |
| 1759 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1760 | if (Tok.is(tok::comma)) |
| 1761 | ConsumeToken(); |
| 1762 | else if (Tok.is(tok::l_brace)) |
| 1763 | break; |
Douglas Gregor | b1f6fa4 | 2010-09-07 14:35:10 +0000 | [diff] [blame] | 1764 | // If the next token looks like a base or member initializer, assume that |
| 1765 | // we're just missing a comma. |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 1766 | else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { |
| 1767 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 1768 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 1769 | << FixItHint::CreateInsertion(Loc, ", "); |
| 1770 | } else { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1771 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1772 | Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1773 | SkipUntil(tok::l_brace, true, true); |
| 1774 | break; |
| 1775 | } |
| 1776 | } while (true); |
| 1777 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1779 | MemInitializers.data(), MemInitializers.size(), |
| 1780 | AnyErrors); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 1784 | /// part of a constructor initializer that explicitly initializes one |
| 1785 | /// member or base class (C++ [class.base.init]). See |
| 1786 | /// ParseConstructorInitializer for an example. |
| 1787 | /// |
| 1788 | /// [C++] mem-initializer: |
| 1789 | /// mem-initializer-id '(' expression-list[opt] ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | /// |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1791 | /// [C++] mem-initializer-id: |
| 1792 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 1793 | /// identifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1794 | Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1795 | // parse '::'[opt] nested-name-specifier[opt] |
| 1796 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1797 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
| 1798 | ParsedType TemplateTypeTy; |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1799 | if (Tok.is(tok::annot_template_id)) { |
| 1800 | TemplateIdAnnotation *TemplateId |
| 1801 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 1802 | if (TemplateId->Kind == TNK_Type_template || |
| 1803 | TemplateId->Kind == TNK_Dependent_template_name) { |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1804 | AnnotateTemplateIdTokenAsType(&SS); |
| 1805 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1806 | TemplateTypeTy = getTypeAnnotation(Tok); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1807 | } |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1808 | } |
| 1809 | if (!TemplateTypeTy && Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1810 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1811 | return true; |
| 1812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1814 | // Get the identifier. This may be a member name or a class name, |
| 1815 | // but we'll let the semantic analysis determine which it is. |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1816 | IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1817 | SourceLocation IdLoc = ConsumeToken(); |
| 1818 | |
| 1819 | // Parse the '('. |
| 1820 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1821 | Diag(Tok, diag::err_expected_lparen); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1822 | return true; |
| 1823 | } |
| 1824 | SourceLocation LParenLoc = ConsumeParen(); |
| 1825 | |
| 1826 | // Parse the optional expression-list. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1827 | ExprVector ArgExprs(Actions); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1828 | CommaLocsTy CommaLocs; |
| 1829 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 1830 | SkipUntil(tok::r_paren); |
| 1831 | return true; |
| 1832 | } |
| 1833 | |
| 1834 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1835 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1836 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1837 | TemplateTypeTy, IdLoc, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1838 | LParenLoc, ArgExprs.take(), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 1839 | ArgExprs.size(), RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1840 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1841 | |
| 1842 | /// ParseExceptionSpecification - Parse a C++ exception-specification |
| 1843 | /// (C++ [except.spec]). |
| 1844 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1845 | /// exception-specification: |
| 1846 | /// 'throw' '(' type-id-list [opt] ')' |
| 1847 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1849 | /// type-id-list: |
| 1850 | /// type-id |
| 1851 | /// type-id-list ',' type-id |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1852 | /// |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1853 | bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1854 | llvm::SmallVectorImpl<ParsedType> |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1855 | &Exceptions, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1856 | llvm::SmallVectorImpl<SourceRange> |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1857 | &Ranges, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1858 | bool &hasAnyExceptionSpec) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1859 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1861 | SourceLocation ThrowLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1863 | if (!Tok.is(tok::l_paren)) { |
| 1864 | return Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 1865 | } |
| 1866 | SourceLocation LParenLoc = ConsumeParen(); |
| 1867 | |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1868 | // Parse throw(...), a Microsoft extension that means "this function |
| 1869 | // can throw anything". |
| 1870 | if (Tok.is(tok::ellipsis)) { |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1871 | hasAnyExceptionSpec = true; |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1872 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 1873 | if (!getLang().Microsoft) |
| 1874 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1875 | EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1876 | return false; |
| 1877 | } |
| 1878 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1879 | // Parse the sequence of type-ids. |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1880 | SourceRange Range; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1881 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1882 | TypeResult Res(ParseTypeName(&Range)); |
| 1883 | if (!Res.isInvalid()) { |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1884 | Exceptions.push_back(Res.get()); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1885 | Ranges.push_back(Range); |
| 1886 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1887 | if (Tok.is(tok::comma)) |
| 1888 | ConsumeToken(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1889 | else |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1890 | break; |
| 1891 | } |
| 1892 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1893 | EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1894 | return false; |
| 1895 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 1897 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 1898 | /// function declaration. |
| 1899 | TypeResult Parser::ParseTrailingReturnType() { |
| 1900 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 1901 | |
| 1902 | ConsumeToken(); |
| 1903 | |
| 1904 | // FIXME: Need to suppress declarations when parsing this typename. |
| 1905 | // Otherwise in this function definition: |
| 1906 | // |
| 1907 | // auto f() -> struct X {} |
| 1908 | // |
| 1909 | // struct X is parsed as class definition because of the trailing |
| 1910 | // brace. |
| 1911 | |
| 1912 | SourceRange Range; |
| 1913 | return ParseTypeName(&Range); |
| 1914 | } |
| 1915 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1916 | /// \brief We have just started parsing the definition of a new class, |
| 1917 | /// so push that class onto our stack of classes that is currently |
| 1918 | /// being parsed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1919 | void Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1920 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1921 | "Nested class without outer class"); |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1922 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass)); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | /// \brief Deallocate the given parsed class and all of its nested |
| 1926 | /// classes. |
| 1927 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1928 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 1929 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1930 | delete Class; |
| 1931 | } |
| 1932 | |
| 1933 | /// \brief Pop the top class of the stack of classes that are |
| 1934 | /// currently being parsed. |
| 1935 | /// |
| 1936 | /// This routine should be called when we have finished parsing the |
| 1937 | /// definition of a class, but have not yet popped the Scope |
| 1938 | /// associated with the class's definition. |
| 1939 | /// |
| 1940 | /// \returns true if the class we've popped is a top-level class, |
| 1941 | /// false otherwise. |
| 1942 | void Parser::PopParsingClass() { |
| 1943 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1945 | ParsingClass *Victim = ClassStack.top(); |
| 1946 | ClassStack.pop(); |
| 1947 | if (Victim->TopLevelClass) { |
| 1948 | // Deallocate all of the nested classes of this class, |
| 1949 | // recursively: we don't need to keep any of this information. |
| 1950 | DeallocateParsedClasses(Victim); |
| 1951 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1953 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 1954 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1955 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1956 | // The victim is a nested class, but we will not need to perform |
| 1957 | // any processing after the definition of this class since it has |
| 1958 | // no members whose handling was delayed. Therefore, we can just |
| 1959 | // remove this nested class. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1960 | DeallocateParsedClasses(Victim); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1961 | return; |
| 1962 | } |
| 1963 | |
| 1964 | // This nested class has some members that will need to be processed |
| 1965 | // after the top-level class is completely defined. Therefore, add |
| 1966 | // it to the list of nested classes within its parent. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1967 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1968 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1969 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1970 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1971 | |
| 1972 | /// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only |
| 1973 | /// parses standard attributes. |
| 1974 | /// |
| 1975 | /// [C++0x] attribute-specifier: |
| 1976 | /// '[' '[' attribute-list ']' ']' |
| 1977 | /// |
| 1978 | /// [C++0x] attribute-list: |
| 1979 | /// attribute[opt] |
| 1980 | /// attribute-list ',' attribute[opt] |
| 1981 | /// |
| 1982 | /// [C++0x] attribute: |
| 1983 | /// attribute-token attribute-argument-clause[opt] |
| 1984 | /// |
| 1985 | /// [C++0x] attribute-token: |
| 1986 | /// identifier |
| 1987 | /// attribute-scoped-token |
| 1988 | /// |
| 1989 | /// [C++0x] attribute-scoped-token: |
| 1990 | /// attribute-namespace '::' identifier |
| 1991 | /// |
| 1992 | /// [C++0x] attribute-namespace: |
| 1993 | /// identifier |
| 1994 | /// |
| 1995 | /// [C++0x] attribute-argument-clause: |
| 1996 | /// '(' balanced-token-seq ')' |
| 1997 | /// |
| 1998 | /// [C++0x] balanced-token-seq: |
| 1999 | /// balanced-token |
| 2000 | /// balanced-token-seq balanced-token |
| 2001 | /// |
| 2002 | /// [C++0x] balanced-token: |
| 2003 | /// '(' balanced-token-seq ')' |
| 2004 | /// '[' balanced-token-seq ']' |
| 2005 | /// '{' balanced-token-seq '}' |
| 2006 | /// any token but '(', ')', '[', ']', '{', or '}' |
| 2007 | CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) { |
| 2008 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) |
| 2009 | && "Not a C++0x attribute list"); |
| 2010 | |
| 2011 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
| 2012 | AttributeList *CurrAttr = 0; |
| 2013 | |
| 2014 | ConsumeBracket(); |
| 2015 | ConsumeBracket(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2016 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2017 | if (Tok.is(tok::comma)) { |
| 2018 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2019 | ConsumeToken(); |
| 2020 | } |
| 2021 | |
| 2022 | while (Tok.is(tok::identifier) || Tok.is(tok::comma)) { |
| 2023 | // attribute not present |
| 2024 | if (Tok.is(tok::comma)) { |
| 2025 | ConsumeToken(); |
| 2026 | continue; |
| 2027 | } |
| 2028 | |
| 2029 | IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo(); |
| 2030 | SourceLocation ScopeLoc, AttrLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2031 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2032 | // scoped attribute |
| 2033 | if (Tok.is(tok::coloncolon)) { |
| 2034 | ConsumeToken(); |
| 2035 | |
| 2036 | if (!Tok.is(tok::identifier)) { |
| 2037 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2038 | SkipUntil(tok::r_square, tok::comma, true, true); |
| 2039 | continue; |
| 2040 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2041 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2042 | ScopeName = AttrName; |
| 2043 | ScopeLoc = AttrLoc; |
| 2044 | |
| 2045 | AttrName = Tok.getIdentifierInfo(); |
| 2046 | AttrLoc = ConsumeToken(); |
| 2047 | } |
| 2048 | |
| 2049 | bool AttrParsed = false; |
| 2050 | // No scoped names are supported; ideally we could put all non-standard |
| 2051 | // attributes into namespaces. |
| 2052 | if (!ScopeName) { |
| 2053 | switch(AttributeList::getKind(AttrName)) |
| 2054 | { |
| 2055 | // No arguments |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2056 | case AttributeList::AT_base_check: |
| 2057 | case AttributeList::AT_carries_dependency: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2058 | case AttributeList::AT_final: |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2059 | case AttributeList::AT_hiding: |
| 2060 | case AttributeList::AT_noreturn: |
| 2061 | case AttributeList::AT_override: { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2062 | if (Tok.is(tok::l_paren)) { |
| 2063 | Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments) |
| 2064 | << AttrName->getName(); |
| 2065 | break; |
| 2066 | } |
| 2067 | |
| 2068 | CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0, |
| 2069 | SourceLocation(), 0, 0, CurrAttr, false, |
| 2070 | true); |
| 2071 | AttrParsed = true; |
| 2072 | break; |
| 2073 | } |
| 2074 | |
| 2075 | // One argument; must be a type-id or assignment-expression |
| 2076 | case AttributeList::AT_aligned: { |
| 2077 | if (Tok.isNot(tok::l_paren)) { |
| 2078 | Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments) |
| 2079 | << AttrName->getName(); |
| 2080 | break; |
| 2081 | } |
| 2082 | SourceLocation ParamLoc = ConsumeParen(); |
| 2083 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2084 | ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2085 | |
| 2086 | MatchRHSPunctuation(tok::r_paren, ParamLoc); |
| 2087 | |
| 2088 | ExprVector ArgExprs(Actions); |
| 2089 | ArgExprs.push_back(ArgExpr.release()); |
| 2090 | CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, |
| 2091 | 0, ParamLoc, ArgExprs.take(), 1, CurrAttr, |
| 2092 | false, true); |
| 2093 | |
| 2094 | AttrParsed = true; |
| 2095 | break; |
| 2096 | } |
| 2097 | |
| 2098 | // Silence warnings |
| 2099 | default: break; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | // Skip the entire parameter clause, if any |
| 2104 | if (!AttrParsed && Tok.is(tok::l_paren)) { |
| 2105 | ConsumeParen(); |
| 2106 | // SkipUntil maintains the balancedness of tokens. |
| 2107 | SkipUntil(tok::r_paren, false); |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2112 | SkipUntil(tok::r_square, false); |
| 2113 | Loc = Tok.getLocation(); |
| 2114 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2115 | SkipUntil(tok::r_square, false); |
| 2116 | |
| 2117 | CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true); |
| 2118 | return Attr; |
| 2119 | } |
| 2120 | |
| 2121 | /// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]] |
| 2122 | /// attribute. |
| 2123 | /// |
| 2124 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2125 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2126 | /// |
| 2127 | /// [C++0x] 'align' '(' type-id ')' |
| 2128 | /// [C++0x] 'align' '(' assignment-expression ')' |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2129 | ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2130 | if (isTypeIdInParens()) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2131 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2132 | SourceLocation TypeLoc = Tok.getLocation(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2133 | ParsedType Ty = ParseTypeName().get(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2134 | SourceRange TypeRange(Start, Tok.getLocation()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2135 | return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, |
| 2136 | Ty.getAsOpaquePtr(), TypeRange); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2137 | } else |
| 2138 | return ParseConstantExpression(); |
| 2139 | } |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2140 | |
| 2141 | /// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr] |
| 2142 | /// |
| 2143 | /// [MS] ms-attribute: |
| 2144 | /// '[' token-seq ']' |
| 2145 | /// |
| 2146 | /// [MS] ms-attribute-seq: |
| 2147 | /// ms-attribute[opt] |
| 2148 | /// ms-attribute ms-attribute-seq |
| 2149 | void Parser::ParseMicrosoftAttributes() { |
| 2150 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 2151 | |
| 2152 | while (Tok.is(tok::l_square)) { |
| 2153 | ConsumeBracket(); |
| 2154 | SkipUntil(tok::r_square, true, true); |
| 2155 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); |
| 2156 | } |
| 2157 | } |