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