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