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