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