Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1 | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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 | f7b2e55 | 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 | e8c36f2 | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 14 | #include "clang/Basic/OperatorKinds.h" |
Douglas Gregor | 696be93 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Chris Lattner | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 17 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 18 | #include "clang/Parse/Scope.h" |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 19 | #include "ExtensionRAIIObject.h" |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
| 23 | /// may either be a top level namespace or a block-level namespace alias. |
| 24 | /// |
| 25 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 26 | /// named-namespace-definition |
| 27 | /// unnamed-namespace-definition |
| 28 | /// |
| 29 | /// unnamed-namespace-definition: |
| 30 | /// 'namespace' attributes[opt] '{' namespace-body '}' |
| 31 | /// |
| 32 | /// named-namespace-definition: |
| 33 | /// original-namespace-definition |
| 34 | /// extension-namespace-definition |
| 35 | /// |
| 36 | /// original-namespace-definition: |
| 37 | /// 'namespace' identifier attributes[opt] '{' namespace-body '}' |
| 38 | /// |
| 39 | /// extension-namespace-definition: |
| 40 | /// 'namespace' original-namespace-name '{' namespace-body '}' |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 41 | /// |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 42 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 43 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 44 | /// |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 45 | Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context, |
| 46 | SourceLocation &DeclEnd) { |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 47 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 48 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 49 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 50 | SourceLocation IdentLoc; |
| 51 | IdentifierInfo *Ident = 0; |
Douglas Gregor | b6d226f | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 52 | |
| 53 | Token attrTok; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 54 | |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 55 | if (Tok.is(tok::identifier)) { |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 56 | Ident = Tok.getIdentifierInfo(); |
| 57 | IdentLoc = ConsumeToken(); // eat the identifier. |
| 58 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 59 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 60 | // Read label attributes, if present. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 61 | Action::AttrTy *AttrList = 0; |
Douglas Gregor | b6d226f | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 62 | if (Tok.is(tok::kw___attribute)) { |
| 63 | attrTok = Tok; |
| 64 | |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 65 | // FIXME: save these somewhere. |
| 66 | AttrList = ParseAttributes(); |
Douglas Gregor | b6d226f | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 67 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 68 | |
Douglas Gregor | b6d226f | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 69 | if (Tok.is(tok::equal)) { |
| 70 | if (AttrList) |
| 71 | Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); |
| 72 | |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 73 | return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
Douglas Gregor | b6d226f | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 74 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 75 | |
Chris Lattner | 872b044 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 76 | if (Tok.isNot(tok::l_brace)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 77 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
Chris Lattner | 872b044 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 78 | diag::err_expected_ident_lbrace); |
| 79 | return DeclPtrTy(); |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 80 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 81 | |
Chris Lattner | 872b044 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 82 | SourceLocation LBrace = ConsumeBrace(); |
| 83 | |
| 84 | // Enter a scope for the namespace. |
| 85 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 86 | |
| 87 | DeclPtrTy NamespcDecl = |
| 88 | Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace); |
| 89 | |
| 90 | PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions, |
| 91 | PP.getSourceManager(), |
| 92 | "parsing namespace"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 93 | |
Chris Lattner | 872b044 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 94 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) |
| 95 | ParseExternalDeclaration(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 96 | |
Chris Lattner | 872b044 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 97 | // Leave the namespace scope. |
| 98 | NamespaceScope.Exit(); |
| 99 | |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 100 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace); |
| 101 | Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc); |
Chris Lattner | 872b044 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 103 | DeclEnd = RBraceLoc; |
Chris Lattner | 872b044 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 104 | return NamespcDecl; |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 106 | |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 107 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 108 | /// alias definition. |
| 109 | /// |
Anders Carlsson | 26de788 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 110 | Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 111 | SourceLocation AliasLoc, |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 112 | IdentifierInfo *Alias, |
| 113 | SourceLocation &DeclEnd) { |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 114 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 115 | |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 116 | ConsumeToken(); // eat the '='. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 117 | |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 118 | CXXScopeSpec SS; |
| 119 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | 681d31d | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 120 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 121 | |
| 122 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
| 123 | Diag(Tok, diag::err_expected_namespace_name); |
| 124 | // Skip to end of the definition and eat the ';'. |
| 125 | SkipUntil(tok::semi); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 126 | return DeclPtrTy(); |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Parse identifier. |
Anders Carlsson | 26de788 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 130 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 131 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 132 | |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 133 | // Eat the ';'. |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 134 | DeclEnd = Tok.getLocation(); |
Chris Lattner | cb9057e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 135 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, |
| 136 | "", tok::semi); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 137 | |
| 138 | return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias, |
Anders Carlsson | 26de788 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 139 | SS, IdentLoc, Ident); |
Anders Carlsson | f94cca2 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 142 | /// ParseLinkage - We know that the current token is a string_literal |
| 143 | /// and just before that, that extern was seen. |
| 144 | /// |
| 145 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 146 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 147 | /// 'extern' string-literal declaration |
| 148 | /// |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 149 | Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) { |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 150 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 151 | llvm::SmallVector<char, 8> LangBuffer; |
| 152 | // LangBuffer is guaranteed to be big enough. |
| 153 | LangBuffer.resize(Tok.getLength()); |
| 154 | const char *LangBufPtr = &LangBuffer[0]; |
| 155 | unsigned StrSize = PP.getSpelling(Tok, LangBufPtr); |
| 156 | |
| 157 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 158 | |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 159 | ParseScope LinkageScope(this, Scope::DeclScope); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 160 | DeclPtrTy LinkageSpec |
| 161 | = Actions.ActOnStartLinkageSpecification(CurScope, |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 162 | /*FIXME: */SourceLocation(), |
| 163 | Loc, LangBufPtr, StrSize, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 164 | Tok.is(tok::l_brace)? Tok.getLocation() |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 165 | : SourceLocation()); |
| 166 | |
| 167 | if (Tok.isNot(tok::l_brace)) { |
| 168 | ParseDeclarationOrFunctionDefinition(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 169 | return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 170 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 171 | } |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 172 | |
| 173 | SourceLocation LBrace = ConsumeBrace(); |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 174 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 175 | ParseExternalDeclaration(); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Douglas Gregor | ad17e37 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 178 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Douglas Gregor | d802838 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 179 | return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 180 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 181 | |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 182 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 183 | /// using-directive. Assumes that current token is 'using'. |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 184 | Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, |
| 185 | SourceLocation &DeclEnd) { |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 186 | assert(Tok.is(tok::kw_using) && "Not using token"); |
| 187 | |
| 188 | // Eat 'using'. |
| 189 | SourceLocation UsingLoc = ConsumeToken(); |
| 190 | |
Chris Lattner | 08ab416 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 191 | if (Tok.is(tok::kw_namespace)) |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 192 | // Next token after 'using' is 'namespace' so it must be using-directive |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 193 | return ParseUsingDirective(Context, UsingLoc, DeclEnd); |
Chris Lattner | 08ab416 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 194 | |
| 195 | // Otherwise, it must be using-declaration. |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 196 | return ParseUsingDeclaration(Context, UsingLoc, DeclEnd); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 200 | /// that current token is 'namespace' and 'using' was already parsed. |
| 201 | /// |
| 202 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 203 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 204 | /// namespace-name ; |
| 205 | /// [GNU] using-directive: |
| 206 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 207 | /// namespace-name attributes[opt] ; |
| 208 | /// |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 209 | Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context, |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 210 | SourceLocation UsingLoc, |
| 211 | SourceLocation &DeclEnd) { |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 212 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 213 | |
| 214 | // Eat 'namespace'. |
| 215 | SourceLocation NamespcLoc = ConsumeToken(); |
| 216 | |
| 217 | CXXScopeSpec SS; |
| 218 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | 681d31d | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 219 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 220 | |
| 221 | AttributeList *AttrList = 0; |
| 222 | IdentifierInfo *NamespcName = 0; |
| 223 | SourceLocation IdentLoc = SourceLocation(); |
| 224 | |
| 225 | // Parse namespace-name. |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 226 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 227 | Diag(Tok, diag::err_expected_namespace_name); |
| 228 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 229 | SkipUntil(tok::semi); |
| 230 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 231 | return DeclPtrTy(); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 232 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 233 | |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 234 | // Parse identifier. |
| 235 | NamespcName = Tok.getIdentifierInfo(); |
| 236 | IdentLoc = ConsumeToken(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 237 | |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 238 | // Parse (optional) attributes (most likely GNU strong-using extension). |
| 239 | if (Tok.is(tok::kw___attribute)) |
| 240 | AttrList = ParseAttributes(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 241 | |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 242 | // Eat ';'. |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 243 | DeclEnd = Tok.getLocation(); |
Chris Lattner | cb9057e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 244 | ExpectAndConsume(tok::semi, |
| 245 | AttrList ? diag::err_expected_semi_after_attribute_list : |
| 246 | diag::err_expected_semi_after_namespace_name, "", tok::semi); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 247 | |
| 248 | return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS, |
Chris Lattner | 7898bf6 | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 249 | IdentLoc, NamespcName, AttrList); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that |
| 253 | /// 'using' was already seen. |
| 254 | /// |
| 255 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 256 | /// 'using' 'typename'[opt] ::[opt] nested-name-specifier |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 257 | /// unqualified-id |
| 258 | /// 'using' :: unqualified-id |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 259 | /// |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 260 | Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context, |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 261 | SourceLocation UsingLoc, |
Anders Carlsson | e16b4fe | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 262 | SourceLocation &DeclEnd, |
| 263 | AccessSpecifier AS) { |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 264 | CXXScopeSpec SS; |
| 265 | bool IsTypeName; |
| 266 | |
| 267 | // Ignore optional 'typename'. |
| 268 | if (Tok.is(tok::kw_typename)) { |
| 269 | ConsumeToken(); |
| 270 | IsTypeName = true; |
| 271 | } |
| 272 | else |
| 273 | IsTypeName = false; |
| 274 | |
| 275 | // Parse nested-name-specifier. |
Douglas Gregor | 681d31d | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 276 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 277 | |
| 278 | AttributeList *AttrList = 0; |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 279 | |
| 280 | // Check nested-name specifier. |
| 281 | if (SS.isInvalid()) { |
| 282 | SkipUntil(tok::semi); |
| 283 | return DeclPtrTy(); |
| 284 | } |
| 285 | if (Tok.is(tok::annot_template_id)) { |
Anders Carlsson | 66b082a | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 286 | // C++0x N2914 [namespace.udecl]p5: |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 287 | // A using-declaration shall not name a template-id. |
Anders Carlsson | 66b082a | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 288 | Diag(Tok, diag::err_using_decl_can_not_refer_to_template_spec); |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 289 | SkipUntil(tok::semi); |
| 290 | return DeclPtrTy(); |
| 291 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 292 | |
Anders Carlsson | e8c36f2 | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 293 | IdentifierInfo *TargetName = 0; |
| 294 | OverloadedOperatorKind Op = OO_None; |
| 295 | SourceLocation IdentLoc; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 296 | |
Anders Carlsson | e8c36f2 | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 297 | if (Tok.is(tok::kw_operator)) { |
| 298 | IdentLoc = Tok.getLocation(); |
| 299 | |
| 300 | Op = TryParseOperatorFunctionId(); |
| 301 | if (!Op) { |
| 302 | // If there was an invalid operator, skip to end of decl, and eat ';'. |
| 303 | SkipUntil(tok::semi); |
| 304 | return DeclPtrTy(); |
| 305 | } |
| 306 | } else if (Tok.is(tok::identifier)) { |
| 307 | // Parse identifier. |
| 308 | TargetName = Tok.getIdentifierInfo(); |
| 309 | IdentLoc = ConsumeToken(); |
| 310 | } else { |
| 311 | // FIXME: Use a better diagnostic here. |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 312 | Diag(Tok, diag::err_expected_ident_in_using); |
Anders Carlsson | e8c36f2 | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 313 | |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 314 | // If there was invalid identifier, skip to end of decl, and eat ';'. |
| 315 | SkipUntil(tok::semi); |
| 316 | return DeclPtrTy(); |
| 317 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 318 | |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 319 | // Parse (optional) attributes (most likely GNU strong-using extension). |
| 320 | if (Tok.is(tok::kw___attribute)) |
| 321 | AttrList = ParseAttributes(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 322 | |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 323 | // Eat ';'. |
| 324 | DeclEnd = Tok.getLocation(); |
| 325 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
| 326 | AttrList ? "attributes list" : "namespace name", tok::semi); |
| 327 | |
Anders Carlsson | e16b4fe | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 328 | return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS, |
Anders Carlsson | e8c36f2 | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 329 | IdentLoc, TargetName, Op, |
| 330 | AttrList, IsTypeName); |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 333 | /// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion. |
| 334 | /// |
| 335 | /// static_assert-declaration: |
| 336 | /// static_assert ( constant-expression , string-literal ) ; |
| 337 | /// |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 338 | Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 339 | assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration"); |
| 340 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 341 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 342 | if (Tok.isNot(tok::l_paren)) { |
| 343 | Diag(Tok, diag::err_expected_lparen); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 344 | return DeclPtrTy(); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 345 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 346 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 347 | SourceLocation LParenLoc = ConsumeParen(); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 348 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 349 | OwningExprResult AssertExpr(ParseConstantExpression()); |
| 350 | if (AssertExpr.isInvalid()) { |
| 351 | SkipUntil(tok::semi); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 352 | return DeclPtrTy(); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 353 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 354 | |
Anders Carlsson | a24e8d5 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 355 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 356 | return DeclPtrTy(); |
Anders Carlsson | a24e8d5 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 357 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 358 | if (Tok.isNot(tok::string_literal)) { |
| 359 | Diag(Tok, diag::err_expected_string_literal); |
| 360 | SkipUntil(tok::semi); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 361 | return DeclPtrTy(); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 362 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 363 | |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 364 | OwningExprResult AssertMessage(ParseStringLiteralExpression()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 365 | if (AssertMessage.isInvalid()) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 366 | return DeclPtrTy(); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 367 | |
Anders Carlsson | c45057a | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 368 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 369 | |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 370 | DeclEnd = Tok.getLocation(); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 371 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert); |
| 372 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 373 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr), |
Anders Carlsson | c45057a | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 374 | move(AssertMessage)); |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 377 | /// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. |
| 378 | /// |
| 379 | /// 'decltype' ( expression ) |
| 380 | /// |
| 381 | void Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
| 382 | assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier"); |
| 383 | |
| 384 | SourceLocation StartLoc = ConsumeToken(); |
| 385 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 386 | |
| 387 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 388 | "decltype")) { |
| 389 | SkipUntil(tok::r_paren); |
| 390 | return; |
| 391 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 392 | |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 393 | // Parse the expression |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 394 | |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 395 | // C++0x [dcl.type.simple]p4: |
| 396 | // The operand of the decltype specifier is an unevaluated operand. |
| 397 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 398 | Action::Unevaluated); |
| 399 | OwningExprResult Result = ParseExpression(); |
| 400 | if (Result.isInvalid()) { |
| 401 | SkipUntil(tok::r_paren); |
| 402 | return; |
| 403 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 404 | |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 405 | // Match the ')' |
| 406 | SourceLocation RParenLoc; |
| 407 | if (Tok.is(tok::r_paren)) |
| 408 | RParenLoc = ConsumeParen(); |
| 409 | else |
| 410 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 411 | |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 412 | if (RParenLoc.isInvalid()) |
| 413 | return; |
| 414 | |
| 415 | const char *PrevSpec = 0; |
John McCall | 9f6e097 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 416 | unsigned DiagID; |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 417 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 418 | if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
John McCall | 9f6e097 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 419 | DiagID, Result.release())) |
| 420 | Diag(StartLoc, DiagID) << PrevSpec; |
Anders Carlsson | eed418b | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 423 | /// ParseClassName - Parse a C++ class-name, which names a class. Note |
| 424 | /// that we only check that the result names a type; semantic analysis |
| 425 | /// will need to verify that the type names a class. The result is |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 426 | /// either a type or NULL, depending on whether a type name was |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 427 | /// found. |
| 428 | /// |
| 429 | /// class-name: [C++ 9.1] |
| 430 | /// identifier |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 431 | /// simple-template-id |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 432 | /// |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 433 | Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation, |
Fariborz Jahanian | 1b8fd75 | 2009-07-20 17:43:15 +0000 | [diff] [blame] | 434 | const CXXScopeSpec *SS, |
| 435 | bool DestrExpected) { |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 436 | // Check whether we have a template-id that names a type. |
| 437 | if (Tok.is(tok::annot_template_id)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 438 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 439 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 440 | if (TemplateId->Kind == TNK_Type_template) { |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 441 | AnnotateTemplateIdTokenAsType(SS); |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 442 | |
| 443 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
| 444 | TypeTy *Type = Tok.getAnnotationValue(); |
| 445 | EndLocation = Tok.getAnnotationEndLoc(); |
| 446 | ConsumeToken(); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 447 | |
| 448 | if (Type) |
| 449 | return Type; |
| 450 | return true; |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // Fall through to produce an error below. |
| 454 | } |
| 455 | |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 456 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 457 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 458 | return true; |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // We have an identifier; check whether it is actually a type. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 462 | TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(), |
Douglas Gregor | 64037c8 | 2009-08-26 18:27:52 +0000 | [diff] [blame] | 463 | Tok.getLocation(), CurScope, SS, |
| 464 | true); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 465 | if (!Type) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 466 | Diag(Tok, DestrExpected ? diag::err_destructor_class_name |
Fariborz Jahanian | 1b8fd75 | 2009-07-20 17:43:15 +0000 | [diff] [blame] | 467 | : diag::err_expected_class_name); |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 468 | return true; |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // Consume the identifier. |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 472 | EndLocation = ConsumeToken(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 473 | return Type; |
| 474 | } |
| 475 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 476 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 477 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 478 | /// until we reach the start of a definition or see a token that |
| 479 | /// cannot start a definition. |
| 480 | /// |
| 481 | /// class-specifier: [C++ class] |
| 482 | /// class-head '{' member-specification[opt] '}' |
| 483 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 484 | /// class-head: |
| 485 | /// class-key identifier[opt] base-clause[opt] |
| 486 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 487 | /// class-key nested-name-specifier[opt] simple-template-id |
| 488 | /// base-clause[opt] |
| 489 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 490 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 491 | /// identifier base-clause[opt] |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 492 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 493 | /// simple-template-id base-clause[opt] |
| 494 | /// class-key: |
| 495 | /// 'class' |
| 496 | /// 'struct' |
| 497 | /// 'union' |
| 498 | /// |
| 499 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 500 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 501 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 502 | /// simple-template-id |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 503 | /// |
| 504 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 505 | /// together, subsume the C99 struct-or-union-specifier: |
| 506 | /// |
| 507 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 508 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 509 | /// struct-or-union identifier |
| 510 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 511 | /// '}' attributes[opt] |
| 512 | /// [GNU] struct-or-union attributes[opt] identifier |
| 513 | /// struct-or-union: |
| 514 | /// 'struct' |
| 515 | /// 'union' |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 516 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 517 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 518 | const ParsedTemplateInfo &TemplateInfo, |
Douglas Gregor | 0c793bb | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 519 | AccessSpecifier AS) { |
Chris Lattner | 197b434 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 520 | DeclSpec::TST TagType; |
| 521 | if (TagTokKind == tok::kw_struct) |
| 522 | TagType = DeclSpec::TST_struct; |
| 523 | else if (TagTokKind == tok::kw_class) |
| 524 | TagType = DeclSpec::TST_class; |
| 525 | else { |
| 526 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 527 | TagType = DeclSpec::TST_union; |
| 528 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 529 | |
| 530 | AttributeList *Attr = 0; |
| 531 | // If attributes exist after tag, parse them. |
| 532 | if (Tok.is(tok::kw___attribute)) |
| 533 | Attr = ParseAttributes(); |
| 534 | |
Steve Naroff | c5ab14f | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 535 | // If declspecs exist after tag, parse them. |
Eli Friedman | 891d82f | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 536 | if (Tok.is(tok::kw___declspec)) |
| 537 | Attr = ParseMicrosoftDeclSpec(Attr); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 538 | |
Douglas Gregor | 7d23bf4 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 539 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) { |
| 540 | // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but |
| 541 | // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 542 | // token sequence "struct __is_pod", make __is_pod into a normal |
Douglas Gregor | 7d23bf4 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 543 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 544 | // properly. |
| 545 | Tok.getIdentifierInfo()->setTokenID(tok::identifier); |
| 546 | Tok.setKind(tok::identifier); |
| 547 | } |
| 548 | |
| 549 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) { |
| 550 | // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but |
| 551 | // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 552 | // token sequence "struct __is_empty", make __is_empty into a normal |
Douglas Gregor | 7d23bf4 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 553 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 554 | // properly. |
| 555 | Tok.getIdentifierInfo()->setTokenID(tok::identifier); |
| 556 | Tok.setKind(tok::identifier); |
| 557 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 558 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 559 | // Parse the (optional) nested-name-specifier. |
| 560 | CXXScopeSpec SS; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 561 | if (getLang().CPlusPlus && |
Douglas Gregor | 681d31d | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 562 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 563 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 564 | Diag(Tok, diag::err_expected_ident); |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 565 | |
| 566 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 567 | IdentifierInfo *Name = 0; |
| 568 | SourceLocation NameLoc; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 569 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 570 | if (Tok.is(tok::identifier)) { |
| 571 | Name = Tok.getIdentifierInfo(); |
| 572 | NameLoc = ConsumeToken(); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 573 | } else if (Tok.is(tok::annot_template_id)) { |
| 574 | TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 575 | NameLoc = ConsumeToken(); |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | aabb850 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 577 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 578 | // The template-name in the simple-template-id refers to |
| 579 | // something other than a class template. Give an appropriate |
| 580 | // error message and skip to the ';'. |
| 581 | SourceRange Range(NameLoc); |
| 582 | if (SS.isNotEmpty()) |
| 583 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 585 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
| 586 | << Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 587 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 588 | DS.SetTypeSpecError(); |
| 589 | SkipUntil(tok::semi, false, true); |
| 590 | TemplateId->Destroy(); |
| 591 | return; |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 592 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 593 | } |
| 594 | |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 595 | // There are four options here. If we have 'struct foo;', then this |
| 596 | // is either a forward declaration or a friend declaration, which |
| 597 | // have to be treated differently. If we have 'struct foo {...' or |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 598 | // 'struct foo :...' then this is a definition. Otherwise we have |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 599 | // something like 'struct foo xyz', a reference. |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 600 | Action::TagUseKind TUK; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 601 | if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 602 | TUK = Action::TUK_Definition; |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 603 | else if (Tok.is(tok::semi)) |
| 604 | TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 605 | else |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 606 | TUK = Action::TUK_Reference; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 607 | |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 608 | if (!Name && !TemplateId && TUK != Action::TUK_Definition) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 609 | // We have a declaration or reference to an anonymous class. |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 610 | Diag(StartLoc, diag::err_anon_type_definition) |
| 611 | << DeclSpec::getSpecifierName(TagType); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 612 | |
| 613 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 614 | SkipUntil(tok::comma, true); |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 615 | |
| 616 | if (TemplateId) |
| 617 | TemplateId->Destroy(); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 621 | // Create the tag portion of the class or class template. |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 622 | Action::DeclResult TagOrTempResult; |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 623 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 624 | |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 625 | // FIXME: When TUK == TUK_Reference and we have a template-id, we need |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 626 | // to turn that template-id into a type. |
| 627 | |
Douglas Gregor | 71f0603 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 628 | bool Owned = false; |
John McCall | fd02518 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 629 | if (TemplateId) { |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 630 | // Explicit specialization, class template partial specialization, |
| 631 | // or explicit instantiation. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 632 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 633 | TemplateId->getTemplateArgs(), |
| 634 | TemplateId->getTemplateArgIsType(), |
| 635 | TemplateId->NumArgs); |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 636 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 637 | TUK == Action::TUK_Declaration) { |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 638 | // This is an explicit instantiation of a class template. |
| 639 | TagOrTempResult |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 640 | = Actions.ActOnExplicitInstantiation(CurScope, |
Douglas Gregor | 7a37472 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 641 | TemplateInfo.ExternLoc, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 642 | TemplateInfo.TemplateLoc, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 643 | TagType, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 644 | StartLoc, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 645 | SS, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 646 | TemplateTy::make(TemplateId->Template), |
| 647 | TemplateId->TemplateNameLoc, |
| 648 | TemplateId->LAngleLoc, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 649 | TemplateArgsPtr, |
| 650 | TemplateId->getTemplateArgLocations(), |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 651 | TemplateId->RAngleLoc, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 652 | Attr); |
John McCall | fd02518 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 653 | } else if (TUK == Action::TUK_Reference || TUK == Action::TUK_Friend) { |
John McCall | d321d49 | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 654 | Action::TypeResult Type |
| 655 | = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template), |
| 656 | TemplateId->TemplateNameLoc, |
| 657 | TemplateId->LAngleLoc, |
| 658 | TemplateArgsPtr, |
| 659 | TemplateId->getTemplateArgLocations(), |
| 660 | TemplateId->RAngleLoc); |
| 661 | |
| 662 | Type = Actions.ActOnTagTemplateIdType(Type, TUK, TagType, StartLoc); |
John McCall | fd02518 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 663 | |
| 664 | TemplateId->Destroy(); |
| 665 | |
John McCall | d321d49 | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 666 | if (Type.isInvalid()) { |
John McCall | fd02518 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 667 | DS.SetTypeSpecError(); |
| 668 | return; |
| 669 | } |
John McCall | d321d49 | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 670 | |
John McCall | fd02518 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 671 | const char *PrevSpec = 0; |
| 672 | unsigned DiagID; |
| 673 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, PrevSpec, |
John McCall | d321d49 | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 674 | DiagID, Type.get())) |
John McCall | fd02518 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 675 | Diag(StartLoc, DiagID) << PrevSpec; |
| 676 | |
| 677 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 678 | |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 679 | } else { |
| 680 | // This is an explicit specialization or a class template |
| 681 | // partial specialization. |
| 682 | TemplateParameterLists FakedParamLists; |
| 683 | |
| 684 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 685 | // This looks like an explicit instantiation, because we have |
| 686 | // something like |
| 687 | // |
| 688 | // template class Foo<X> |
| 689 | // |
Douglas Gregor | 96b6df9 | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 690 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 691 | // meant to be an explicit specialization, but the user forgot |
| 692 | // the '<>' after 'template'. |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 693 | assert(TUK == Action::TUK_Definition && "Expected a definition here"); |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 694 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 695 | SourceLocation LAngleLoc |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 696 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 697 | Diag(TemplateId->TemplateNameLoc, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 698 | diag::err_explicit_instantiation_with_definition) |
| 699 | << SourceRange(TemplateInfo.TemplateLoc) |
| 700 | << CodeModificationHint::CreateInsertion(LAngleLoc, "<>"); |
| 701 | |
| 702 | // Create a fake template parameter list that contains only |
| 703 | // "template<>", so that we treat this construct as a class |
| 704 | // template specialization. |
| 705 | FakedParamLists.push_back( |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 706 | Actions.ActOnTemplateParameterList(0, SourceLocation(), |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 707 | TemplateInfo.TemplateLoc, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 708 | LAngleLoc, |
| 709 | 0, 0, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 710 | LAngleLoc)); |
| 711 | TemplateParams = &FakedParamLists; |
| 712 | } |
| 713 | |
| 714 | // Build the class template specialization. |
| 715 | TagOrTempResult |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 716 | = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK, |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 717 | StartLoc, SS, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 718 | TemplateTy::make(TemplateId->Template), |
| 719 | TemplateId->TemplateNameLoc, |
| 720 | TemplateId->LAngleLoc, |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 721 | TemplateArgsPtr, |
| 722 | TemplateId->getTemplateArgLocations(), |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 723 | TemplateId->RAngleLoc, |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 724 | Attr, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 725 | Action::MultiTemplateParamsArg(Actions, |
Douglas Gregor | a08b6c7 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 726 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 727 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 728 | } |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 729 | TemplateId->Destroy(); |
Douglas Gregor | 96b6df9 | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 730 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 731 | TUK == Action::TUK_Declaration) { |
Douglas Gregor | 96b6df9 | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 732 | // Explicit instantiation of a member of a class template |
| 733 | // specialization, e.g., |
| 734 | // |
| 735 | // template struct Outer<int>::Inner; |
| 736 | // |
| 737 | TagOrTempResult |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 738 | = Actions.ActOnExplicitInstantiation(CurScope, |
Douglas Gregor | 7a37472 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 739 | TemplateInfo.ExternLoc, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 740 | TemplateInfo.TemplateLoc, |
| 741 | TagType, StartLoc, SS, Name, |
Douglas Gregor | 96b6df9 | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 742 | NameLoc, Attr); |
| 743 | } else { |
| 744 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 745 | TUK == Action::TUK_Definition) { |
Douglas Gregor | 96b6df9 | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 746 | // FIXME: Diagnose this particular error. |
| 747 | } |
| 748 | |
| 749 | // Declaration or definition of a class type |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 750 | TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS, |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 751 | Name, NameLoc, Attr, AS, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 752 | Action::MultiTemplateParamsArg(Actions, |
Douglas Gregor | 84a2081 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 753 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 754 | TemplateParams? TemplateParams->size() : 0), |
| 755 | Owned); |
Douglas Gregor | 96b6df9 | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 757 | |
| 758 | // Parse the optional base clause (C++ only). |
Chris Lattner | 31ccf0a | 2009-02-16 22:07:16 +0000 | [diff] [blame] | 759 | if (getLang().CPlusPlus && Tok.is(tok::colon)) |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 760 | ParseBaseClause(TagOrTempResult.get()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 761 | |
| 762 | // If there is a body, parse it and inform the actions module. |
| 763 | if (Tok.is(tok::l_brace)) |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 764 | if (getLang().CPlusPlus) |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 765 | ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 766 | else |
Douglas Gregor | c5d6fa7 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 767 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
John McCall | 069c23a | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 768 | else if (TUK == Action::TUK_Definition) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 769 | // FIXME: Complain that we have a base-specifier list but no |
| 770 | // definition. |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 771 | Diag(Tok, diag::err_expected_lbrace); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Anders Carlsson | ef3fa4f | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 774 | if (TagOrTempResult.isInvalid()) { |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 775 | DS.SetTypeSpecError(); |
Anders Carlsson | ef3fa4f | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 776 | return; |
| 777 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 778 | |
John McCall | 9f6e097 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 779 | const char *PrevSpec = 0; |
| 780 | unsigned DiagID; |
| 781 | if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID, |
Douglas Gregor | 71f0603 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 782 | TagOrTempResult.get().getAs<void>(), Owned)) |
John McCall | 9f6e097 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 783 | Diag(StartLoc, DiagID) << PrevSpec; |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 786 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 787 | /// |
| 788 | /// base-clause : [C++ class.derived] |
| 789 | /// ':' base-specifier-list |
| 790 | /// base-specifier-list: |
| 791 | /// base-specifier '...'[opt] |
| 792 | /// base-specifier-list ',' base-specifier '...'[opt] |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 793 | void Parser::ParseBaseClause(DeclPtrTy ClassDecl) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 794 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 795 | ConsumeToken(); |
| 796 | |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 797 | // Build up an array of parsed base specifiers. |
| 798 | llvm::SmallVector<BaseTy *, 8> BaseInfo; |
| 799 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 800 | while (true) { |
| 801 | // Parse a base-specifier. |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 802 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 10a18fc | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 803 | if (Result.isInvalid()) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 804 | // Skip the rest of this base specifier, up until the comma or |
| 805 | // opening brace. |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 806 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 807 | } else { |
| 808 | // Add this to our array of base specifiers. |
Douglas Gregor | 10a18fc | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 809 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // If the next token is a comma, consume it and keep reading |
| 813 | // base-specifiers. |
| 814 | if (Tok.isNot(tok::comma)) break; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 815 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 816 | // Consume the comma. |
| 817 | ConsumeToken(); |
| 818 | } |
Douglas Gregor | abed217 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 819 | |
| 820 | // Attach the base specifiers |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 821 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 825 | /// one entry in the base class list of a class specifier, for example: |
| 826 | /// class foo : public bar, virtual private baz { |
| 827 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 828 | /// |
| 829 | /// base-specifier: [C++ class.derived] |
| 830 | /// ::[opt] nested-name-specifier[opt] class-name |
| 831 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
| 832 | /// class-name |
| 833 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
| 834 | /// class-name |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 835 | Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 836 | bool IsVirtual = false; |
| 837 | SourceLocation StartLoc = Tok.getLocation(); |
| 838 | |
| 839 | // Parse the 'virtual' keyword. |
| 840 | if (Tok.is(tok::kw_virtual)) { |
| 841 | ConsumeToken(); |
| 842 | IsVirtual = true; |
| 843 | } |
| 844 | |
| 845 | // Parse an (optional) access specifier. |
| 846 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
| 847 | if (Access) |
| 848 | ConsumeToken(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 849 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 850 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 851 | // access specifier. |
| 852 | if (Tok.is(tok::kw_virtual)) { |
| 853 | SourceLocation VirtualLoc = ConsumeToken(); |
| 854 | if (IsVirtual) { |
| 855 | // Complain about duplicate 'virtual' |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 856 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 857 | << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc)); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | IsVirtual = true; |
| 861 | } |
| 862 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 863 | // Parse optional '::' and optional nested-name-specifier. |
| 864 | CXXScopeSpec SS; |
Douglas Gregor | 681d31d | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 865 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 867 | // The location of the base class itself. |
| 868 | SourceLocation BaseLoc = Tok.getLocation(); |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 869 | |
| 870 | // Parse the class-name. |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 871 | SourceLocation EndLocation; |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 872 | TypeResult BaseType = ParseClassName(EndLocation, &SS); |
| 873 | if (BaseType.isInvalid()) |
Douglas Gregor | 8210a8e | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 874 | return true; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 875 | |
| 876 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7bbed2a | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 877 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 878 | |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 879 | // Notify semantic analysis that we have parsed a complete |
| 880 | // base-specifier. |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 881 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, |
Douglas Gregor | d7cb037 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 882 | BaseType.get(), BaseLoc); |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 886 | /// a C++ access-specifier. |
| 887 | /// |
| 888 | /// access-specifier: [C++ class.derived] |
| 889 | /// 'private' |
| 890 | /// 'protected' |
| 891 | /// 'public' |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 892 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | ec93f44 | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 893 | switch (Tok.getKind()) { |
| 894 | default: return AS_none; |
| 895 | case tok::kw_private: return AS_private; |
| 896 | case tok::kw_protected: return AS_protected; |
| 897 | case tok::kw_public: return AS_public; |
| 898 | } |
| 899 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 900 | |
Eli Friedman | 40035e2 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 901 | void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo, |
| 902 | DeclPtrTy ThisDecl) { |
| 903 | // We just declared a member function. If this member function |
| 904 | // has any default arguments, we'll need to parse them later. |
| 905 | LateParsedMethodDeclaration *LateMethod = 0; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 906 | DeclaratorChunk::FunctionTypeInfo &FTI |
Eli Friedman | 40035e2 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 907 | = DeclaratorInfo.getTypeObject(0).Fun; |
| 908 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 909 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 910 | if (!LateMethod) { |
| 911 | // Push this method onto the stack of late-parsed method |
| 912 | // declarations. |
| 913 | getCurrentClass().MethodDecls.push_back( |
| 914 | LateParsedMethodDeclaration(ThisDecl)); |
| 915 | LateMethod = &getCurrentClass().MethodDecls.back(); |
Douglas Gregor | 6ee3505 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 916 | LateMethod->TemplateScope = CurScope->isTemplateParamScope(); |
Eli Friedman | 40035e2 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 917 | |
| 918 | // Add all of the parameters prior to this one (they don't |
| 919 | // have default arguments). |
| 920 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 921 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 922 | LateMethod->DefaultArgs.push_back( |
| 923 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param)); |
| 924 | } |
| 925 | |
| 926 | // Add this parameter to the list of parameters (it or may |
| 927 | // not have a default argument). |
| 928 | LateMethod->DefaultArgs.push_back( |
| 929 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 930 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 935 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 936 | /// |
| 937 | /// member-declaration: |
| 938 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 939 | /// function-definition ';'[opt] |
| 940 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 941 | /// using-declaration [TODO] |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 942 | /// [C++0x] static_assert-declaration |
Anders Carlsson | ed20fb9 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 943 | /// template-declaration |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 944 | /// [GNU] '__extension__' member-declaration |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 945 | /// |
| 946 | /// member-declarator-list: |
| 947 | /// member-declarator |
| 948 | /// member-declarator-list ',' member-declarator |
| 949 | /// |
| 950 | /// member-declarator: |
| 951 | /// declarator pure-specifier[opt] |
| 952 | /// declarator constant-initializer[opt] |
| 953 | /// identifier[opt] ':' constant-expression |
| 954 | /// |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 955 | /// pure-specifier: |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 956 | /// '= 0' |
| 957 | /// |
| 958 | /// constant-initializer: |
| 959 | /// '=' constant-expression |
| 960 | /// |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 961 | void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
| 962 | const ParsedTemplateInfo &TemplateInfo) { |
Anders Carlsson | ab04198 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 963 | // static_assert-declaration |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 964 | if (Tok.is(tok::kw_static_assert)) { |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 965 | // FIXME: Check for templates |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 966 | SourceLocation DeclEnd; |
| 967 | ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 968 | return; |
| 969 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 970 | |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 971 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 972 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 973 | "Nested template improperly parsed?"); |
Chris Lattner | 9802a0a | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 974 | SourceLocation DeclEnd; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 975 | ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, |
Douglas Gregor | a9db0fa | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 976 | AS); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 977 | return; |
| 978 | } |
Anders Carlsson | ed20fb9 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 979 | |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 980 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 981 | if (Tok.is(tok::kw___extension__)) { |
| 982 | // __extension__ silences extension warnings in the subexpression. |
| 983 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 984 | ConsumeToken(); |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 985 | return ParseCXXClassMemberDeclaration(AS, TemplateInfo); |
Chris Lattner | f3375de | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 986 | } |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 987 | |
| 988 | if (Tok.is(tok::kw_using)) { |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 989 | // FIXME: Check for template aliases |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 990 | |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 991 | // Eat 'using'. |
| 992 | SourceLocation UsingLoc = ConsumeToken(); |
| 993 | |
| 994 | if (Tok.is(tok::kw_namespace)) { |
| 995 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
| 996 | SkipUntil(tok::semi, true, true); |
| 997 | } |
| 998 | else { |
| 999 | SourceLocation DeclEnd; |
| 1000 | // Otherwise, it must be using-declaration. |
Anders Carlsson | e16b4fe | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 1001 | ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS); |
Douglas Gregor | 683a114 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1002 | } |
| 1003 | return; |
| 1004 | } |
| 1005 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1006 | SourceLocation DSStart = Tok.getLocation(); |
| 1007 | // decl-specifier-seq: |
| 1008 | // Parse the common declaration-specifiers piece. |
| 1009 | DeclSpec DS; |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1010 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1011 | |
| 1012 | if (Tok.is(tok::semi)) { |
| 1013 | ConsumeToken(); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1014 | |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1015 | // FIXME: Friend templates? |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1016 | if (DS.isFriendSpecified()) |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1017 | Actions.ActOnFriendDecl(CurScope, &DS, /*IsDefinition*/ false); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1018 | else |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1019 | Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1020 | |
| 1021 | return; |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1022 | } |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1023 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1024 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1025 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1026 | if (Tok.isNot(tok::colon)) { |
| 1027 | // Parse the first declarator. |
| 1028 | ParseDeclarator(DeclaratorInfo); |
| 1029 | // Error parsing the declarator? |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1030 | if (!DeclaratorInfo.hasName()) { |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1031 | // If so, skip until the semi-colon or a }. |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1032 | SkipUntil(tok::r_brace, true); |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1033 | if (Tok.is(tok::semi)) |
| 1034 | ConsumeToken(); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1035 | return; |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1038 | // function-definition: |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1039 | if (Tok.is(tok::l_brace) |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1040 | || (DeclaratorInfo.isFunctionDeclarator() && |
| 1041 | (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) { |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1042 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
| 1043 | Diag(Tok, diag::err_func_def_no_params); |
| 1044 | ConsumeBrace(); |
| 1045 | SkipUntil(tok::r_brace, true); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1046 | return; |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1047 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1048 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1049 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1050 | Diag(Tok, diag::err_function_declared_typedef); |
| 1051 | // This recovery skips the entire function body. It would be nice |
| 1052 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 1053 | // assumes the declarator represents a function, not a typedef. |
| 1054 | ConsumeBrace(); |
| 1055 | SkipUntil(tok::r_brace, true); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1056 | return; |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1059 | ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1060 | return; |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1061 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | // member-declarator-list: |
| 1065 | // member-declarator |
| 1066 | // member-declarator-list ',' member-declarator |
| 1067 | |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1068 | llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup; |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1069 | OwningExprResult BitfieldSize(Actions); |
| 1070 | OwningExprResult Init(Actions); |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1071 | bool Deleted = false; |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1072 | |
| 1073 | while (1) { |
| 1074 | |
| 1075 | // member-declarator: |
| 1076 | // declarator pure-specifier[opt] |
| 1077 | // declarator constant-initializer[opt] |
| 1078 | // identifier[opt] ':' constant-expression |
| 1079 | |
| 1080 | if (Tok.is(tok::colon)) { |
| 1081 | ConsumeToken(); |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1082 | BitfieldSize = ParseConstantExpression(); |
| 1083 | if (BitfieldSize.isInvalid()) |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1084 | SkipUntil(tok::comma, true, true); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1085 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1086 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1087 | // pure-specifier: |
| 1088 | // '= 0' |
| 1089 | // |
| 1090 | // constant-initializer: |
| 1091 | // '=' constant-expression |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1092 | // |
| 1093 | // defaulted/deleted function-definition: |
| 1094 | // '=' 'default' [TODO] |
| 1095 | // '=' 'delete' |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (Tok.is(tok::equal)) { |
| 1098 | ConsumeToken(); |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1099 | if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) { |
| 1100 | ConsumeToken(); |
| 1101 | Deleted = true; |
| 1102 | } else { |
| 1103 | Init = ParseInitializer(); |
| 1104 | if (Init.isInvalid()) |
| 1105 | SkipUntil(tok::comma, true, true); |
| 1106 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | // If attributes exist after the declarator, parse them. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1110 | if (Tok.is(tok::kw___attribute)) { |
| 1111 | SourceLocation Loc; |
| 1112 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1113 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 1114 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1115 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1116 | // NOTE: If Sema is the Action module and declarator is an instance field, |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1117 | // this call will *not* return the created decl; It will return null. |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1118 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1119 | |
| 1120 | DeclPtrTy ThisDecl; |
| 1121 | if (DS.isFriendSpecified()) { |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1122 | // TODO: handle initializers, bitfields, 'delete', friend templates |
John McCall | 3649308 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 1123 | ThisDecl = Actions.ActOnFriendDecl(CurScope, &DeclaratorInfo, |
| 1124 | /*IsDefinition*/ false); |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1125 | } else { |
| 1126 | Action::MultiTemplateParamsArg TemplateParams(Actions, |
| 1127 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, |
| 1128 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1129 | ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS, |
| 1130 | DeclaratorInfo, |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1131 | move(TemplateParams), |
John McCall | 140607b | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1132 | BitfieldSize.release(), |
| 1133 | Init.release(), |
| 1134 | Deleted); |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1135 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1136 | if (ThisDecl) |
| 1137 | DeclsInGroup.push_back(ThisDecl); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1138 | |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1139 | if (DeclaratorInfo.isFunctionDeclarator() && |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1140 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1141 | != DeclSpec::SCS_typedef) { |
Eli Friedman | 40035e2 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1142 | HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1145 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1146 | // or an error, bail out. |
| 1147 | if (Tok.isNot(tok::comma)) |
| 1148 | break; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1149 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1150 | // Consume the comma. |
| 1151 | ConsumeToken(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1152 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1153 | // Parse the next declarator. |
| 1154 | DeclaratorInfo.clear(); |
Sebastian Redl | 6226104 | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1155 | BitfieldSize = 0; |
| 1156 | Init = 0; |
Sebastian Redl | a55834a | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1157 | Deleted = false; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1158 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1159 | // Attributes are only allowed on the second declarator. |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1160 | if (Tok.is(tok::kw___attribute)) { |
| 1161 | SourceLocation Loc; |
| 1162 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1163 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 1164 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1165 | |
Argiris Kirtzidis | f8009b4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1166 | if (Tok.isNot(tok::colon)) |
| 1167 | ParseDeclarator(DeclaratorInfo); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | if (Tok.is(tok::semi)) { |
| 1171 | ConsumeToken(); |
Eli Friedman | 4d57af2 | 2009-05-29 01:49:24 +0000 | [diff] [blame] | 1172 | Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(), |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1173 | DeclsInGroup.size()); |
| 1174 | return; |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 1178 | // Skip to end of block or statement |
| 1179 | SkipUntil(tok::r_brace, true, true); |
| 1180 | if (Tok.is(tok::semi)) |
| 1181 | ConsumeToken(); |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1182 | return; |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 1186 | /// |
| 1187 | /// member-specification: |
| 1188 | /// member-declaration member-specification[opt] |
| 1189 | /// access-specifier ':' member-specification[opt] |
| 1190 | /// |
| 1191 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1192 | unsigned TagType, DeclPtrTy TagDecl) { |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1193 | assert((TagType == DeclSpec::TST_struct || |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1194 | TagType == DeclSpec::TST_union || |
Sanjiv Gupta | fa45143 | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1195 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | c309ade | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 1197 | PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions, |
| 1198 | PP.getSourceManager(), |
| 1199 | "parsing struct/union/class body"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1200 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1201 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1202 | |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1203 | // Determine whether this is a top-level (non-nested) class. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1204 | bool TopLevelClass = ClassStack.empty() || |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1205 | CurScope->isInCXXInlineMethodScope(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Enter a scope for the class. |
Douglas Gregor | cab994d | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1208 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1209 | |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1210 | // Note that we are parsing a new (potentially-nested) class definition. |
| 1211 | ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass); |
| 1212 | |
Douglas Gregor | d406b03 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1213 | if (TagDecl) |
| 1214 | Actions.ActOnTagStartDefinition(CurScope, TagDecl); |
| 1215 | else { |
| 1216 | SkipUntil(tok::r_brace, false, false); |
| 1217 | return; |
| 1218 | } |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1219 | |
| 1220 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 1221 | // by default. Members of a class defined with the keywords struct or union |
| 1222 | // are public by default. |
| 1223 | AccessSpecifier CurAS; |
| 1224 | if (TagType == DeclSpec::TST_class) |
| 1225 | CurAS = AS_private; |
| 1226 | else |
| 1227 | CurAS = AS_public; |
| 1228 | |
| 1229 | // While we still have something to read, read the member-declarations. |
| 1230 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 1231 | // Each iteration of this loop reads one member-declaration. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1232 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1233 | // Check for extraneous top-level semicolon. |
| 1234 | if (Tok.is(tok::semi)) { |
| 1235 | Diag(Tok, diag::ext_extra_struct_semi); |
| 1236 | ConsumeToken(); |
| 1237 | continue; |
| 1238 | } |
| 1239 | |
| 1240 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 1241 | if (AS != AS_none) { |
| 1242 | // Current token is a C++ access specifier. |
| 1243 | CurAS = AS; |
| 1244 | ConsumeToken(); |
| 1245 | ExpectAndConsume(tok::colon, diag::err_expected_colon); |
| 1246 | continue; |
| 1247 | } |
| 1248 | |
Douglas Gregor | 398a801 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1249 | // FIXME: Make sure we don't have a template here. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1250 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1251 | // Parse all the comma separated declarators. |
| 1252 | ParseCXXClassMemberDeclaration(CurAS); |
| 1253 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1254 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1255 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1256 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1257 | AttributeList *AttrList = 0; |
| 1258 | // If attributes exist after class contents, parse them. |
| 1259 | if (Tok.is(tok::kw___attribute)) |
| 1260 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
| 1261 | |
| 1262 | Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl, |
| 1263 | LBraceLoc, RBraceLoc); |
| 1264 | |
| 1265 | // C++ 9.2p2: Within the class member-specification, the class is regarded as |
| 1266 | // complete within function bodies, default arguments, |
| 1267 | // exception-specifications, and constructor ctor-initializers (including |
| 1268 | // such things in nested classes). |
| 1269 | // |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1270 | // FIXME: Only function bodies and constructor ctor-initializers are |
| 1271 | // parsed correctly, fix the rest. |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1272 | if (TopLevelClass) { |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1273 | // We are not inside a nested class. This class and its nested classes |
Douglas Gregor | 605de8d | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1274 | // are complete and we can parse the delayed portions of method |
| 1275 | // declarations and the lexed inline method definitions. |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1276 | ParseLexedMethodDeclarations(getCurrentClass()); |
| 1277 | ParseLexedMethodDefs(getCurrentClass()); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | // Leave the class scope. |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1281 | ParsingDef.Pop(); |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1282 | ClassScope.Exit(); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1283 | |
Argiris Kirtzidis | eb92564 | 2009-07-14 03:17:52 +0000 | [diff] [blame] | 1284 | Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc); |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1285 | } |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1286 | |
| 1287 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 1288 | /// which explicitly initializes the members or base classes of a |
| 1289 | /// class (C++ [class.base.init]). For example, the three initializers |
| 1290 | /// after the ':' in the Derived constructor below: |
| 1291 | /// |
| 1292 | /// @code |
| 1293 | /// class Base { }; |
| 1294 | /// class Derived : Base { |
| 1295 | /// int x; |
| 1296 | /// float f; |
| 1297 | /// public: |
| 1298 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 1299 | /// }; |
| 1300 | /// @endcode |
| 1301 | /// |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1302 | /// [C++] ctor-initializer: |
| 1303 | /// ':' mem-initializer-list |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1304 | /// |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1305 | /// [C++] mem-initializer-list: |
| 1306 | /// mem-initializer |
| 1307 | /// mem-initializer , mem-initializer-list |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1308 | void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) { |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1309 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 1310 | |
| 1311 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1312 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1313 | llvm::SmallVector<MemInitTy*, 4> MemInitializers; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1314 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1315 | do { |
| 1316 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
Douglas Gregor | 10a18fc | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1317 | if (!MemInit.isInvalid()) |
| 1318 | MemInitializers.push_back(MemInit.get()); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1319 | |
| 1320 | if (Tok.is(tok::comma)) |
| 1321 | ConsumeToken(); |
| 1322 | else if (Tok.is(tok::l_brace)) |
| 1323 | break; |
| 1324 | else { |
| 1325 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1326 | Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1327 | SkipUntil(tok::l_brace, true, true); |
| 1328 | break; |
| 1329 | } |
| 1330 | } while (true); |
| 1331 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1332 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1333 | MemInitializers.data(), MemInitializers.size()); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 1337 | /// part of a constructor initializer that explicitly initializes one |
| 1338 | /// member or base class (C++ [class.base.init]). See |
| 1339 | /// ParseConstructorInitializer for an example. |
| 1340 | /// |
| 1341 | /// [C++] mem-initializer: |
| 1342 | /// mem-initializer-id '(' expression-list[opt] ')' |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1343 | /// |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1344 | /// [C++] mem-initializer-id: |
| 1345 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 1346 | /// identifier |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1347 | Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) { |
Fariborz Jahanian | c37d306 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1348 | // parse '::'[opt] nested-name-specifier[opt] |
| 1349 | CXXScopeSpec SS; |
Douglas Gregor | 681d31d | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1350 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); |
Fariborz Jahanian | 2b2b736 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1351 | TypeTy *TemplateTypeTy = 0; |
| 1352 | if (Tok.is(tok::annot_template_id)) { |
| 1353 | TemplateIdAnnotation *TemplateId |
| 1354 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 1355 | if (TemplateId->Kind == TNK_Type_template) { |
| 1356 | AnnotateTemplateIdTokenAsType(&SS); |
| 1357 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
| 1358 | TemplateTypeTy = Tok.getAnnotationValue(); |
| 1359 | } |
| 1360 | // FIXME. May need to check for TNK_Dependent_template as well. |
| 1361 | } |
| 1362 | if (!TemplateTypeTy && Tok.isNot(tok::identifier)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1363 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1364 | return true; |
| 1365 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1366 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1367 | // Get the identifier. This may be a member name or a class name, |
| 1368 | // but we'll let the semantic analysis determine which it is. |
Fariborz Jahanian | 2b2b736 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1369 | IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0; |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1370 | SourceLocation IdLoc = ConsumeToken(); |
| 1371 | |
| 1372 | // Parse the '('. |
| 1373 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1374 | Diag(Tok, diag::err_expected_lparen); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1375 | return true; |
| 1376 | } |
| 1377 | SourceLocation LParenLoc = ConsumeParen(); |
| 1378 | |
| 1379 | // Parse the optional expression-list. |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1380 | ExprVector ArgExprs(Actions); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1381 | CommaLocsTy CommaLocs; |
| 1382 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 1383 | SkipUntil(tok::r_paren); |
| 1384 | return true; |
| 1385 | } |
| 1386 | |
| 1387 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1388 | |
Fariborz Jahanian | 2b2b736 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1389 | return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II, |
| 1390 | TemplateTypeTy, IdLoc, |
Sebastian Redl | 6008ac3 | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1391 | LParenLoc, ArgExprs.take(), |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1392 | ArgExprs.size(), CommaLocs.data(), |
| 1393 | RParenLoc); |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1394 | } |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1395 | |
| 1396 | /// ParseExceptionSpecification - Parse a C++ exception-specification |
| 1397 | /// (C++ [except.spec]). |
| 1398 | /// |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1399 | /// exception-specification: |
| 1400 | /// 'throw' '(' type-id-list [opt] ')' |
| 1401 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1402 | /// |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1403 | /// type-id-list: |
| 1404 | /// type-id |
| 1405 | /// type-id-list ',' type-id |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1406 | /// |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1407 | bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc, |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1408 | llvm::SmallVector<TypeTy*, 2> |
| 1409 | &Exceptions, |
| 1410 | llvm::SmallVector<SourceRange, 2> |
| 1411 | &Ranges, |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1412 | bool &hasAnyExceptionSpec) { |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1413 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1414 | |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1415 | SourceLocation ThrowLoc = ConsumeToken(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1416 | |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1417 | if (!Tok.is(tok::l_paren)) { |
| 1418 | return Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 1419 | } |
| 1420 | SourceLocation LParenLoc = ConsumeParen(); |
| 1421 | |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1422 | // Parse throw(...), a Microsoft extension that means "this function |
| 1423 | // can throw anything". |
| 1424 | if (Tok.is(tok::ellipsis)) { |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1425 | hasAnyExceptionSpec = true; |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1426 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 1427 | if (!getLang().Microsoft) |
| 1428 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1429 | EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | 9ed9ac8 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
| 1432 | |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1433 | // Parse the sequence of type-ids. |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1434 | SourceRange Range; |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1435 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1436 | TypeResult Res(ParseTypeName(&Range)); |
| 1437 | if (!Res.isInvalid()) { |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1438 | Exceptions.push_back(Res.get()); |
Sebastian Redl | aaacda9 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1439 | Ranges.push_back(Range); |
| 1440 | } |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1441 | if (Tok.is(tok::comma)) |
| 1442 | ConsumeToken(); |
Sebastian Redl | 35f3a5b | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1443 | else |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | } |
| 1446 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1447 | EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | 90a2c97 | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1448 | return false; |
| 1449 | } |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1450 | |
| 1451 | /// \brief We have just started parsing the definition of a new class, |
| 1452 | /// so push that class onto our stack of classes that is currently |
| 1453 | /// being parsed. |
| 1454 | void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1455 | assert((TopLevelClass || !ClassStack.empty()) && |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1456 | "Nested class without outer class"); |
| 1457 | ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass)); |
| 1458 | } |
| 1459 | |
| 1460 | /// \brief Deallocate the given parsed class and all of its nested |
| 1461 | /// classes. |
| 1462 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
| 1463 | for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I) |
| 1464 | DeallocateParsedClasses(Class->NestedClasses[I]); |
| 1465 | delete Class; |
| 1466 | } |
| 1467 | |
| 1468 | /// \brief Pop the top class of the stack of classes that are |
| 1469 | /// currently being parsed. |
| 1470 | /// |
| 1471 | /// This routine should be called when we have finished parsing the |
| 1472 | /// definition of a class, but have not yet popped the Scope |
| 1473 | /// associated with the class's definition. |
| 1474 | /// |
| 1475 | /// \returns true if the class we've popped is a top-level class, |
| 1476 | /// false otherwise. |
| 1477 | void Parser::PopParsingClass() { |
| 1478 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1479 | |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1480 | ParsingClass *Victim = ClassStack.top(); |
| 1481 | ClassStack.pop(); |
| 1482 | if (Victim->TopLevelClass) { |
| 1483 | // Deallocate all of the nested classes of this class, |
| 1484 | // recursively: we don't need to keep any of this information. |
| 1485 | DeallocateParsedClasses(Victim); |
| 1486 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1487 | } |
Douglas Gregor | a376cbd | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1488 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 1489 | |
| 1490 | if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() && |
| 1491 | Victim->NestedClasses.empty()) { |
| 1492 | // The victim is a nested class, but we will not need to perform |
| 1493 | // any processing after the definition of this class since it has |
| 1494 | // no members whose handling was delayed. Therefore, we can just |
| 1495 | // remove this nested class. |
| 1496 | delete Victim; |
| 1497 | return; |
| 1498 | } |
| 1499 | |
| 1500 | // This nested class has some members that will need to be processed |
| 1501 | // after the top-level class is completely defined. Therefore, add |
| 1502 | // it to the list of nested classes within its parent. |
| 1503 | assert(CurScope->isClassScope() && "Nested class outside of class scope?"); |
| 1504 | ClassStack.top()->NestedClasses.push_back(Victim); |
| 1505 | Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope(); |
| 1506 | } |