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