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