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