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