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