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