Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1 | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -------------*- C++ -*-===// |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the C++ Declaration portions of the Parser interfaces. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Douglas Gregor | 423984d | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 13 | #include "clang/Parse/Parser.h" |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 757fcd6 | 2014-03-04 10:05:20 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 16 | #include "clang/AST/PrettyDeclStackTrace.h" |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Attributes.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 18 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Basic/OperatorKinds.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 21 | #include "clang/Parse/ParseDiagnostic.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 22 | #include "clang/Parse/RAIIObjectsForParser.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/ParsedTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Scope.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/TimeProfiler.h" |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 28 | |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
| 31 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 32 | /// may either be a top level namespace or a block-level namespace alias. If |
| 33 | /// there was an inline keyword, it has already been parsed. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 34 | /// |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 35 | /// namespace-definition: [C++: namespace.def] |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 36 | /// named-namespace-definition |
| 37 | /// unnamed-namespace-definition |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 38 | /// nested-namespace-definition |
| 39 | /// |
| 40 | /// named-namespace-definition: |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 41 | /// 'inline'[opt] 'namespace' attributes[opt] identifier '{' |
| 42 | /// namespace-body '}' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 43 | /// |
| 44 | /// unnamed-namespace-definition: |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 45 | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 46 | /// |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 47 | /// nested-namespace-definition: |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 48 | /// 'namespace' enclosing-namespace-specifier '::' 'inline'[opt] |
| 49 | /// identifier '{' namespace-body '}' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 50 | /// |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 51 | /// enclosing-namespace-specifier: |
| 52 | /// identifier |
| 53 | /// enclosing-namespace-specifier '::' 'inline'[opt] identifier |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | /// |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 55 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 56 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 57 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 58 | Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context, |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 59 | SourceLocation &DeclEnd, |
| 60 | SourceLocation InlineLoc) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 61 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 62 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Fariborz Jahanian | 4bf8262 | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 63 | ObjCDeclContextSwitch ObjCDC(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 65 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 66 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 67 | cutOffParsing(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 68 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 69 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 70 | |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 71 | SourceLocation IdentLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 72 | IdentifierInfo *Ident = nullptr; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 73 | InnerNamespaceInfoList ExtraNSs; |
| 74 | SourceLocation FirstNestedInlineLoc; |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 75 | |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 76 | ParsedAttributesWithRange attrs(AttrFactory); |
| 77 | SourceLocation attrLoc; |
| 78 | if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 79 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
Richard Smith | 40e202f | 2017-10-14 00:56:24 +0000 | [diff] [blame] | 80 | ? diag::warn_cxx14_compat_ns_enum_attribute |
| 81 | : diag::ext_ns_enum_attribute) |
| 82 | << 0 /*namespace*/; |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 83 | attrLoc = Tok.getLocation(); |
| 84 | ParseCXX11Attributes(attrs); |
| 85 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 87 | if (Tok.is(tok::identifier)) { |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 88 | Ident = Tok.getIdentifierInfo(); |
| 89 | IdentLoc = ConsumeToken(); // eat the identifier. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 90 | while (Tok.is(tok::coloncolon) && |
| 91 | (NextToken().is(tok::identifier) || |
| 92 | (NextToken().is(tok::kw_inline) && |
| 93 | GetLookAheadToken(2).is(tok::identifier)))) { |
| 94 | |
| 95 | InnerNamespaceInfo Info; |
| 96 | Info.NamespaceLoc = ConsumeToken(); |
| 97 | |
| 98 | if (Tok.is(tok::kw_inline)) { |
| 99 | Info.InlineLoc = ConsumeToken(); |
| 100 | if (FirstNestedInlineLoc.isInvalid()) |
| 101 | FirstNestedInlineLoc = Info.InlineLoc; |
| 102 | } |
| 103 | |
| 104 | Info.Ident = Tok.getIdentifierInfo(); |
| 105 | Info.IdentLoc = ConsumeToken(); |
| 106 | |
| 107 | ExtraNSs.push_back(Info); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 108 | } |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 109 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Aaron Ballman | c0ae7df | 2014-11-08 17:07:15 +0000 | [diff] [blame] | 111 | // A nested namespace definition cannot have attributes. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 112 | if (!ExtraNSs.empty() && attrLoc.isValid()) |
Aaron Ballman | c0ae7df | 2014-11-08 17:07:15 +0000 | [diff] [blame] | 113 | Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute); |
| 114 | |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 115 | // Read label attributes, if present. |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 116 | if (Tok.is(tok::kw___attribute)) { |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 117 | attrLoc = Tok.getLocation(); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 118 | ParseGNUAttributes(attrs); |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 119 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 121 | if (Tok.is(tok::equal)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 122 | if (!Ident) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 123 | Diag(Tok, diag::err_expected) << tok::identifier; |
Nico Weber | 729f1e2 | 2012-10-27 23:44:27 +0000 | [diff] [blame] | 124 | // Skip to end of the definition and eat the ';'. |
| 125 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 126 | return nullptr; |
Nico Weber | 729f1e2 | 2012-10-27 23:44:27 +0000 | [diff] [blame] | 127 | } |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 128 | if (attrLoc.isValid()) |
| 129 | Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 130 | if (InlineLoc.isValid()) |
| 131 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
| 132 | << FixItHint::CreateRemoval(InlineLoc); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 133 | Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
| 134 | return Actions.ConvertDeclToDeclGroup(NSAlias); |
| 135 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 137 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 138 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 139 | if (Ident) |
| 140 | Diag(Tok, diag::err_expected) << tok::l_brace; |
| 141 | else |
| 142 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 143 | return nullptr; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 144 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 146 | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
| 147 | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 148 | getCurScope()->getFnParent()) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 149 | Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 150 | SkipUntil(tok::r_brace); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 151 | return nullptr; |
Douglas Gregor | 05cfc29 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 154 | if (ExtraNSs.empty()) { |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 155 | // Normal namespace definition, not a nested-namespace-definition. |
| 156 | } else if (InlineLoc.isValid()) { |
| 157 | Diag(InlineLoc, diag::err_inline_nested_namespace_definition); |
Aaron Ballman | 6a30894 | 2020-04-21 15:37:19 -0400 | [diff] [blame] | 158 | } else if (getLangOpts().CPlusPlus20) { |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 159 | Diag(ExtraNSs[0].NamespaceLoc, |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 160 | diag::warn_cxx14_compat_nested_namespace_definition); |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 161 | if (FirstNestedInlineLoc.isValid()) |
| 162 | Diag(FirstNestedInlineLoc, |
| 163 | diag::warn_cxx17_compat_inline_nested_namespace_definition); |
| 164 | } else if (getLangOpts().CPlusPlus17) { |
| 165 | Diag(ExtraNSs[0].NamespaceLoc, |
| 166 | diag::warn_cxx14_compat_nested_namespace_definition); |
| 167 | if (FirstNestedInlineLoc.isValid()) |
| 168 | Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 169 | } else { |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 170 | TentativeParsingAction TPA(*this); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 171 | SkipUntil(tok::r_brace, StopBeforeMatch); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 172 | Token rBraceToken = Tok; |
| 173 | TPA.Revert(); |
| 174 | |
| 175 | if (!rBraceToken.is(tok::r_brace)) { |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 176 | Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) |
| 177 | << SourceRange(ExtraNSs.front().NamespaceLoc, |
| 178 | ExtraNSs.back().IdentLoc); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 179 | } else { |
Benjamin Kramer | f546f41 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 180 | std::string NamespaceFix; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 181 | for (const auto &ExtraNS : ExtraNSs) { |
Erich Keane | a946acd | 2018-11-12 21:08:41 +0000 | [diff] [blame] | 182 | NamespaceFix += " { "; |
| 183 | if (ExtraNS.InlineLoc.isValid()) |
| 184 | NamespaceFix += "inline "; |
| 185 | NamespaceFix += "namespace "; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 186 | NamespaceFix += ExtraNS.Ident->getName(); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 187 | } |
Benjamin Kramer | f546f41 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 188 | |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 189 | std::string RBraces; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 190 | for (unsigned i = 0, e = ExtraNSs.size(); i != e; ++i) |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 191 | RBraces += "} "; |
Benjamin Kramer | f546f41 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 192 | |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 193 | Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) |
| 194 | << FixItHint::CreateReplacement( |
| 195 | SourceRange(ExtraNSs.front().NamespaceLoc, |
| 196 | ExtraNSs.back().IdentLoc), |
| 197 | NamespaceFix) |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 198 | << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces); |
| 199 | } |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 200 | |
| 201 | // Warn about nested inline namespaces. |
| 202 | if (FirstNestedInlineLoc.isValid()) |
| 203 | Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 206 | // If we're still good, complain about inline namespaces in non-C++0x now. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 207 | if (InlineLoc.isValid()) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 208 | Diag(InlineLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 209 | diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace); |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 4de55aa | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 211 | // Enter a scope for the namespace. |
| 212 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 213 | |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 214 | UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 215 | Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( |
| 216 | getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident, |
| 217 | T.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl); |
Chris Lattner | 4de55aa | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 218 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 219 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl, |
| 220 | NamespaceLoc, "parsing namespace"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 222 | // Parse the contents of the namespace. This includes parsing recovery on |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 223 | // any improperly nested namespaces. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 224 | ParseInnerNamespace(ExtraNSs, 0, InlineLoc, attrs, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 4de55aa | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 226 | // Leave the namespace scope. |
| 227 | NamespaceScope.Exit(); |
| 228 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 229 | DeclEnd = T.getCloseLocation(); |
| 230 | Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 231 | |
| 232 | return Actions.ConvertDeclToDeclGroup(NamespcDecl, |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 233 | ImplicitUsingDirectiveDecl); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 235 | |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 236 | /// ParseInnerNamespace - Parse the contents of a namespace. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 237 | void Parser::ParseInnerNamespace(const InnerNamespaceInfoList &InnerNSs, |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 238 | unsigned int index, SourceLocation &InlineLoc, |
| 239 | ParsedAttributes &attrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 240 | BalancedDelimiterTracker &Tracker) { |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 241 | if (index == InnerNSs.size()) { |
Richard Smith | 752ada8 | 2015-11-17 23:32:01 +0000 | [diff] [blame] | 242 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 243 | Tok.isNot(tok::eof)) { |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 244 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 245 | MaybeParseCXX11Attributes(attrs); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 246 | ParseExternalDeclaration(attrs); |
| 247 | } |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 248 | |
| 249 | // The caller is what called check -- we are simply calling |
| 250 | // the close for it. |
| 251 | Tracker.consumeClose(); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 252 | |
| 253 | return; |
| 254 | } |
| 255 | |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 256 | // Handle a nested namespace definition. |
| 257 | // FIXME: Preserve the source information through to the AST rather than |
| 258 | // desugaring it here. |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 259 | ParseScope NamespaceScope(this, Scope::DeclScope); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 260 | UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 261 | Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 262 | getCurScope(), InnerNSs[index].InlineLoc, InnerNSs[index].NamespaceLoc, |
| 263 | InnerNSs[index].IdentLoc, InnerNSs[index].Ident, |
| 264 | Tracker.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 265 | assert(!ImplicitUsingDirectiveDecl && |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 266 | "nested namespace definition cannot define anonymous namespace"); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 267 | |
Erich Keane | de6480a3 | 2018-11-13 15:48:08 +0000 | [diff] [blame] | 268 | ParseInnerNamespace(InnerNSs, ++index, InlineLoc, attrs, Tracker); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 269 | |
| 270 | NamespaceScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 271 | Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation()); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 274 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 275 | /// alias definition. |
| 276 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 277 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 278 | SourceLocation AliasLoc, |
| 279 | IdentifierInfo *Alias, |
| 280 | SourceLocation &DeclEnd) { |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 281 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 283 | ConsumeToken(); // eat the '='. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 285 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 286 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 287 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 288 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 289 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 290 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 291 | CXXScopeSpec SS; |
| 292 | // Parse (optional) nested-name-specifier. |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 293 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
| 294 | /*ObjectHadErrors=*/false, |
| 295 | /*EnteringContext=*/false, |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 296 | /*MayBePseudoDestructor=*/nullptr, |
| 297 | /*IsTypename=*/false, |
| 298 | /*LastII=*/nullptr, |
| 299 | /*OnlyNamespace=*/true); |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 300 | |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 301 | if (Tok.isNot(tok::identifier)) { |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 302 | Diag(Tok, diag::err_expected_namespace_name); |
| 303 | // Skip to end of the definition and eat the ';'. |
| 304 | SkipUntil(tok::semi); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 305 | return nullptr; |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 308 | if (SS.isInvalid()) { |
| 309 | // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. |
| 310 | // Skip to end of the definition and eat the ';'. |
| 311 | SkipUntil(tok::semi); |
| 312 | return nullptr; |
| 313 | } |
| 314 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 315 | // Parse identifier. |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 316 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 317 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 319 | // Eat the ';'. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 320 | DeclEnd = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 321 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name)) |
| 322 | SkipUntil(tok::semi); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Craig Topper | ff35428 | 2015-11-14 18:16:00 +0000 | [diff] [blame] | 324 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, |
| 325 | Alias, SS, IdentLoc, Ident); |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 328 | /// ParseLinkage - We know that the current token is a string_literal |
| 329 | /// and just before that, that extern was seen. |
| 330 | /// |
| 331 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 332 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 333 | /// 'extern' string-literal declaration |
| 334 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 335 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) { |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 336 | assert(isTokenStringLiteral() && "Not a string literal!"); |
| 337 | ExprResult Lang = ParseStringLiteralExpression(false); |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 338 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 339 | ParseScope LinkageScope(this, Scope::DeclScope); |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 340 | Decl *LinkageSpec = |
| 341 | Lang.isInvalid() |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 342 | ? nullptr |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 343 | : Actions.ActOnStartLinkageSpecification( |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 344 | getCurScope(), DS.getSourceRange().getBegin(), Lang.get(), |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 345 | Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation()); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 346 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 347 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 348 | MaybeParseCXX11Attributes(attrs); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 349 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 350 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | 4d42399 | 2011-05-01 16:25:54 +0000 | [diff] [blame] | 351 | // Reset the source range in DS, as the leading "extern" |
| 352 | // does not really belong to the inner declaration ... |
| 353 | DS.SetRangeStart(SourceLocation()); |
| 354 | DS.SetRangeEnd(SourceLocation()); |
| 355 | // ... but anyway remember that such an "extern" was seen. |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 356 | DS.setExternInLinkageSpec(true); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 357 | ParseExternalDeclaration(attrs, &DS); |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 358 | return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( |
| 359 | getCurScope(), LinkageSpec, SourceLocation()) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 360 | : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | } |
Douglas Gregor | 29ff7d0 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | b65a913 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 363 | DS.abort(); |
| 364 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 365 | ProhibitAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 367 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 368 | T.consumeOpen(); |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 369 | |
| 370 | unsigned NestedModules = 0; |
| 371 | while (true) { |
| 372 | switch (Tok.getKind()) { |
| 373 | case tok::annot_module_begin: |
| 374 | ++NestedModules; |
| 375 | ParseTopLevelDecl(); |
| 376 | continue; |
| 377 | |
| 378 | case tok::annot_module_end: |
| 379 | if (!NestedModules) |
| 380 | break; |
| 381 | --NestedModules; |
| 382 | ParseTopLevelDecl(); |
| 383 | continue; |
| 384 | |
| 385 | case tok::annot_module_include: |
| 386 | ParseTopLevelDecl(); |
| 387 | continue; |
| 388 | |
| 389 | case tok::eof: |
| 390 | break; |
| 391 | |
| 392 | case tok::r_brace: |
| 393 | if (!NestedModules) |
| 394 | break; |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 395 | LLVM_FALLTHROUGH; |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 396 | default: |
| 397 | ParsedAttributesWithRange attrs(AttrFactory); |
| 398 | MaybeParseCXX11Attributes(attrs); |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 399 | ParseExternalDeclaration(attrs); |
| 400 | continue; |
| 401 | } |
| 402 | |
| 403 | break; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 406 | T.consumeClose(); |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 407 | return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( |
| 408 | getCurScope(), LinkageSpec, T.getCloseLocation()) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 409 | : nullptr; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 410 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 411 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 412 | /// Parse a C++ Modules TS export-declaration. |
| 413 | /// |
| 414 | /// export-declaration: |
| 415 | /// 'export' declaration |
| 416 | /// 'export' '{' declaration-seq[opt] '}' |
| 417 | /// |
| 418 | Decl *Parser::ParseExportDeclaration() { |
| 419 | assert(Tok.is(tok::kw_export)); |
| 420 | SourceLocation ExportLoc = ConsumeToken(); |
| 421 | |
| 422 | ParseScope ExportScope(this, Scope::DeclScope); |
| 423 | Decl *ExportDecl = Actions.ActOnStartExportDecl( |
| 424 | getCurScope(), ExportLoc, |
| 425 | Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation()); |
| 426 | |
| 427 | if (Tok.isNot(tok::l_brace)) { |
| 428 | // FIXME: Factor out a ParseExternalDeclarationWithAttrs. |
| 429 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 430 | MaybeParseCXX11Attributes(Attrs); |
| 431 | MaybeParseMicrosoftAttributes(Attrs); |
| 432 | ParseExternalDeclaration(Attrs); |
| 433 | return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, |
| 434 | SourceLocation()); |
| 435 | } |
| 436 | |
| 437 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 438 | T.consumeOpen(); |
| 439 | |
| 440 | // The Modules TS draft says "An export-declaration shall declare at least one |
| 441 | // entity", but the intent is that it shall contain at least one declaration. |
Richard Smith | e181de7 | 2019-04-22 22:50:11 +0000 | [diff] [blame] | 442 | if (Tok.is(tok::r_brace) && getLangOpts().ModulesTS) { |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 443 | Diag(ExportLoc, diag::err_export_empty) |
| 444 | << SourceRange(ExportLoc, Tok.getLocation()); |
Richard Smith | e181de7 | 2019-04-22 22:50:11 +0000 | [diff] [blame] | 445 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 446 | |
| 447 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 448 | Tok.isNot(tok::eof)) { |
| 449 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 450 | MaybeParseCXX11Attributes(Attrs); |
| 451 | MaybeParseMicrosoftAttributes(Attrs); |
| 452 | ParseExternalDeclaration(Attrs); |
| 453 | } |
| 454 | |
| 455 | T.consumeClose(); |
| 456 | return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, |
| 457 | T.getCloseLocation()); |
| 458 | } |
| 459 | |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 460 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 461 | /// using-directive. Assumes that current token is 'using'. |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 462 | Parser::DeclGroupPtrTy |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 463 | Parser::ParseUsingDirectiveOrDeclaration(DeclaratorContext Context, |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 464 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 465 | SourceLocation &DeclEnd, |
| 466 | ParsedAttributesWithRange &attrs) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 467 | assert(Tok.is(tok::kw_using) && "Not using token"); |
Fariborz Jahanian | 4bf8262 | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 468 | ObjCDeclContextSwitch ObjCDC(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 470 | // Eat 'using'. |
| 471 | SourceLocation UsingLoc = ConsumeToken(); |
| 472 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 473 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 474 | Actions.CodeCompleteUsing(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 475 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 476 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 477 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 478 | |
Richard Trieu | 2efd305 | 2019-05-01 23:33:49 +0000 | [diff] [blame] | 479 | // Consume unexpected 'template' keywords. |
| 480 | while (Tok.is(tok::kw_template)) { |
| 481 | SourceLocation TemplateLoc = ConsumeToken(); |
| 482 | Diag(TemplateLoc, diag::err_unexpected_template_after_using) |
| 483 | << FixItHint::CreateRemoval(TemplateLoc); |
| 484 | } |
| 485 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 486 | // 'using namespace' means this is a using-directive. |
| 487 | if (Tok.is(tok::kw_namespace)) { |
| 488 | // Template parameters are always an error here. |
| 489 | if (TemplateInfo.Kind) { |
| 490 | SourceRange R = TemplateInfo.getSourceRange(); |
Craig Topper | 54a6a68 | 2015-11-14 18:16:08 +0000 | [diff] [blame] | 491 | Diag(UsingLoc, diag::err_templated_using_directive_declaration) |
| 492 | << 0 /* directive */ << R << FixItHint::CreateRemoval(R); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 493 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 494 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 495 | Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
| 496 | return Actions.ConvertDeclToDeclGroup(UsingDir); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 499 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 500 | |
| 501 | // Using declarations can't have attributes. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 502 | ProhibitAttributes(attrs); |
Chris Lattner | 9b01ca1 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 503 | |
Fariborz Jahanian | 4bf8262 | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 504 | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 505 | AS_none); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 509 | /// that current token is 'namespace' and 'using' was already parsed. |
| 510 | /// |
| 511 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 512 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 513 | /// namespace-name ; |
| 514 | /// [GNU] using-directive: |
| 515 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 516 | /// namespace-name attributes[opt] ; |
| 517 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 518 | Decl *Parser::ParseUsingDirective(DeclaratorContext Context, |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 519 | SourceLocation UsingLoc, |
| 520 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 521 | ParsedAttributes &attrs) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 522 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 523 | |
| 524 | // Eat 'namespace'. |
| 525 | SourceLocation NamespcLoc = ConsumeToken(); |
| 526 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 527 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 528 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 529 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 530 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 531 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 533 | CXXScopeSpec SS; |
| 534 | // Parse (optional) nested-name-specifier. |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 535 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
| 536 | /*ObjectHadErrors=*/false, |
| 537 | /*EnteringContext=*/false, |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 538 | /*MayBePseudoDestructor=*/nullptr, |
| 539 | /*IsTypename=*/false, |
| 540 | /*LastII=*/nullptr, |
| 541 | /*OnlyNamespace=*/true); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 542 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 543 | IdentifierInfo *NamespcName = nullptr; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 544 | SourceLocation IdentLoc = SourceLocation(); |
| 545 | |
| 546 | // Parse namespace-name. |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 547 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 548 | Diag(Tok, diag::err_expected_namespace_name); |
| 549 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 550 | SkipUntil(tok::semi); |
| 551 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 552 | return nullptr; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 553 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 555 | if (SS.isInvalid()) { |
| 556 | // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. |
| 557 | // Skip to end of the definition and eat the ';'. |
| 558 | SkipUntil(tok::semi); |
| 559 | return nullptr; |
| 560 | } |
| 561 | |
Chris Lattner | ce1da2c | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 562 | // Parse identifier. |
| 563 | NamespcName = Tok.getIdentifierInfo(); |
| 564 | IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Chris Lattner | ce1da2c | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 566 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 567 | bool GNUAttr = false; |
| 568 | if (Tok.is(tok::kw___attribute)) { |
| 569 | GNUAttr = true; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 570 | ParseGNUAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 571 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Chris Lattner | ce1da2c | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 573 | // Eat ';'. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 574 | DeclEnd = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 575 | if (ExpectAndConsume(tok::semi, |
| 576 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
| 577 | : diag::err_expected_semi_after_namespace_name)) |
| 578 | SkipUntil(tok::semi); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 579 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 580 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 581 | IdentLoc, NamespcName, attrs); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 584 | /// Parse a using-declarator (or the identifier in a C++11 alias-declaration). |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 585 | /// |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 586 | /// using-declarator: |
| 587 | /// 'typename'[opt] nested-name-specifier unqualified-id |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 588 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 589 | bool Parser::ParseUsingDeclarator(DeclaratorContext Context, |
| 590 | UsingDeclarator &D) { |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 591 | D.clear(); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 592 | |
| 593 | // Ignore optional 'typename'. |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 594 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 595 | TryConsumeToken(tok::kw_typename, D.TypenameLoc); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 596 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 597 | if (Tok.is(tok::kw___super)) { |
| 598 | Diag(Tok.getLocation(), diag::err_super_in_using_declaration); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 599 | return true; |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 602 | // Parse nested-name-specifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 603 | IdentifierInfo *LastII = nullptr; |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 604 | if (ParseOptionalCXXScopeSpecifier(D.SS, /*ObjectType=*/nullptr, |
| 605 | /*ObjectHadErrors=*/false, |
| 606 | /*EnteringContext=*/false, |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 607 | /*MayBePseudoDtor=*/nullptr, |
| 608 | /*IsTypename=*/false, |
Ilya Biryukov | d9971d0 | 2019-10-28 09:34:21 +0100 | [diff] [blame] | 609 | /*LastII=*/&LastII, |
| 610 | /*OnlyNamespace=*/false, |
| 611 | /*InUsingDeclaration=*/true)) |
| 612 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 613 | return true; |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 614 | if (D.SS.isInvalid()) |
| 615 | return true; |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 616 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 617 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 618 | // destructor names and allow the action module to diagnose any semantic |
| 619 | // errors. |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 620 | // |
| 621 | // C++11 [class.qual]p2: |
| 622 | // [...] in a using-declaration that is a member-declaration, if the name |
| 623 | // specified after the nested-name-specifier is the same as the identifier |
| 624 | // or the simple-template-id's template-name in the last component of the |
| 625 | // nested-name-specifier, the name is [...] considered to name the |
| 626 | // constructor. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 627 | if (getLangOpts().CPlusPlus11 && |
| 628 | Context == DeclaratorContext::MemberContext && |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 629 | Tok.is(tok::identifier) && |
| 630 | (NextToken().is(tok::semi) || NextToken().is(tok::comma) || |
| 631 | NextToken().is(tok::ellipsis)) && |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 632 | D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() && |
| 633 | !D.SS.getScopeRep()->getAsNamespace() && |
| 634 | !D.SS.getScopeRep()->getAsNamespaceAlias()) { |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 635 | SourceLocation IdLoc = ConsumeToken(); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 636 | ParsedType Type = |
| 637 | Actions.getInheritingConstructorName(D.SS, IdLoc, *LastII); |
| 638 | D.Name.setConstructorName(Type, IdLoc, IdLoc); |
| 639 | } else { |
| 640 | if (ParseUnqualifiedId( |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 641 | D.SS, /*ObjectType=*/nullptr, |
| 642 | /*ObjectHadErrors=*/false, /*EnteringContext=*/false, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 643 | /*AllowDestructorName=*/true, |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 644 | /*AllowConstructorName=*/ |
| 645 | !(Tok.is(tok::identifier) && NextToken().is(tok::equal)), |
| 646 | /*AllowDeductionGuide=*/false, nullptr, D.Name)) |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 647 | return true; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 648 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 649 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 650 | if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc)) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 651 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ? |
Richard Smith | b115e5d | 2017-08-13 23:37:29 +0000 | [diff] [blame] | 652 | diag::warn_cxx17_compat_using_declaration_pack : |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 653 | diag::ext_using_declaration_pack); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 654 | |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration. |
| 659 | /// Assumes that 'using' was already seen. |
| 660 | /// |
| 661 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 662 | /// 'using' using-declarator-list[opt] ; |
| 663 | /// |
| 664 | /// using-declarator-list: [C++1z] |
| 665 | /// using-declarator '...'[opt] |
| 666 | /// using-declarator-list ',' using-declarator '...'[opt] |
| 667 | /// |
| 668 | /// using-declarator-list: [C++98-14] |
| 669 | /// using-declarator |
| 670 | /// |
| 671 | /// alias-declaration: C++11 [dcl.dcl]p1 |
| 672 | /// 'using' identifier attribute-specifier-seq[opt] = type-id ; |
| 673 | /// |
| 674 | Parser::DeclGroupPtrTy |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 675 | Parser::ParseUsingDeclaration(DeclaratorContext Context, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 676 | const ParsedTemplateInfo &TemplateInfo, |
| 677 | SourceLocation UsingLoc, SourceLocation &DeclEnd, |
| 678 | AccessSpecifier AS) { |
| 679 | // Check for misplaced attributes before the identifier in an |
| 680 | // alias-declaration. |
| 681 | ParsedAttributesWithRange MisplacedAttrs(AttrFactory); |
| 682 | MaybeParseCXX11Attributes(MisplacedAttrs); |
| 683 | |
| 684 | UsingDeclarator D; |
| 685 | bool InvalidDeclarator = ParseUsingDeclarator(Context, D); |
| 686 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 687 | ParsedAttributesWithRange Attrs(AttrFactory); |
Richard Smith | 37a45dd | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 688 | MaybeParseGNUAttributes(Attrs); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 689 | MaybeParseCXX11Attributes(Attrs); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 690 | |
| 691 | // Maybe this is an alias-declaration. |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 692 | if (Tok.is(tok::equal)) { |
| 693 | if (InvalidDeclarator) { |
| 694 | SkipUntil(tok::semi); |
| 695 | return nullptr; |
| 696 | } |
| 697 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 698 | // If we had any misplaced attributes from earlier, this is where they |
| 699 | // should have been written. |
| 700 | if (MisplacedAttrs.Range.isValid()) { |
| 701 | Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 702 | << FixItHint::CreateInsertionFromRange( |
| 703 | Tok.getLocation(), |
| 704 | CharSourceRange::getTokenRange(MisplacedAttrs.Range)) |
| 705 | << FixItHint::CreateRemoval(MisplacedAttrs.Range); |
| 706 | Attrs.takeAllFrom(MisplacedAttrs); |
| 707 | } |
| 708 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 709 | Decl *DeclFromDeclSpec = nullptr; |
| 710 | Decl *AD = ParseAliasDeclarationAfterDeclarator( |
| 711 | TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec); |
| 712 | return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 715 | // C++11 attributes are not allowed on a using-declaration, but GNU ones |
| 716 | // are. |
| 717 | ProhibitAttributes(MisplacedAttrs); |
| 718 | ProhibitAttributes(Attrs); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 719 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 720 | // Diagnose an attempt to declare a templated using-declaration. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 721 | // In C++11, alias-declarations can be templates: |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 722 | // template <...> using id = type; |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 723 | if (TemplateInfo.Kind) { |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 724 | SourceRange R = TemplateInfo.getSourceRange(); |
Craig Topper | 54a6a68 | 2015-11-14 18:16:08 +0000 | [diff] [blame] | 725 | Diag(UsingLoc, diag::err_templated_using_directive_declaration) |
| 726 | << 1 /* declaration */ << R << FixItHint::CreateRemoval(R); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 727 | |
| 728 | // Unfortunately, we have to bail out instead of recovering by |
| 729 | // ignoring the parameters, just in case the nested name specifier |
| 730 | // depends on the parameters. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 731 | return nullptr; |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 734 | SmallVector<Decl *, 8> DeclsInGroup; |
| 735 | while (true) { |
| 736 | // Parse (optional) attributes (most likely GNU strong-using extension). |
| 737 | MaybeParseGNUAttributes(Attrs); |
| 738 | |
| 739 | if (InvalidDeclarator) |
| 740 | SkipUntil(tok::comma, tok::semi, StopBeforeMatch); |
| 741 | else { |
| 742 | // "typename" keyword is allowed for identifiers only, |
| 743 | // because it may be a type definition. |
| 744 | if (D.TypenameLoc.isValid() && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 745 | D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 746 | Diag(D.Name.getSourceRange().getBegin(), |
| 747 | diag::err_typename_identifiers_only) |
| 748 | << FixItHint::CreateRemoval(SourceRange(D.TypenameLoc)); |
| 749 | // Proceed parsing, but discard the typename keyword. |
| 750 | D.TypenameLoc = SourceLocation(); |
| 751 | } |
| 752 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 753 | Decl *UD = Actions.ActOnUsingDeclaration(getCurScope(), AS, UsingLoc, |
| 754 | D.TypenameLoc, D.SS, D.Name, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 755 | D.EllipsisLoc, Attrs); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 756 | if (UD) |
| 757 | DeclsInGroup.push_back(UD); |
| 758 | } |
| 759 | |
| 760 | if (!TryConsumeToken(tok::comma)) |
| 761 | break; |
| 762 | |
| 763 | // Parse another using-declarator. |
| 764 | Attrs.clear(); |
| 765 | InvalidDeclarator = ParseUsingDeclarator(Context, D); |
Douglas Gregor | 882a61a | 2011-09-26 14:30:28 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 768 | if (DeclsInGroup.size() > 1) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 769 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ? |
Richard Smith | b115e5d | 2017-08-13 23:37:29 +0000 | [diff] [blame] | 770 | diag::warn_cxx17_compat_multi_using_declaration : |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 771 | diag::ext_multi_using_declaration); |
| 772 | |
| 773 | // Eat ';'. |
| 774 | DeclEnd = Tok.getLocation(); |
| 775 | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
| 776 | !Attrs.empty() ? "attributes list" |
| 777 | : "using declaration")) |
| 778 | SkipUntil(tok::semi); |
| 779 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 780 | return Actions.BuildDeclaratorGroup(DeclsInGroup); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 783 | Decl *Parser::ParseAliasDeclarationAfterDeclarator( |
| 784 | const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc, |
| 785 | UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS, |
| 786 | ParsedAttributes &Attrs, Decl **OwnedType) { |
| 787 | if (ExpectAndConsume(tok::equal)) { |
| 788 | SkipUntil(tok::semi); |
| 789 | return nullptr; |
| 790 | } |
| 791 | |
| 792 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ? |
| 793 | diag::warn_cxx98_compat_alias_declaration : |
| 794 | diag::ext_alias_declaration); |
| 795 | |
| 796 | // Type alias templates cannot be specialized. |
| 797 | int SpecKind = -1; |
| 798 | if (TemplateInfo.Kind == ParsedTemplateInfo::Template && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 799 | D.Name.getKind() == UnqualifiedIdKind::IK_TemplateId) |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 800 | SpecKind = 0; |
| 801 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) |
| 802 | SpecKind = 1; |
| 803 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 804 | SpecKind = 2; |
| 805 | if (SpecKind != -1) { |
| 806 | SourceRange Range; |
| 807 | if (SpecKind == 0) |
| 808 | Range = SourceRange(D.Name.TemplateId->LAngleLoc, |
| 809 | D.Name.TemplateId->RAngleLoc); |
| 810 | else |
| 811 | Range = TemplateInfo.getSourceRange(); |
| 812 | Diag(Range.getBegin(), diag::err_alias_declaration_specialization) |
| 813 | << SpecKind << Range; |
| 814 | SkipUntil(tok::semi); |
| 815 | return nullptr; |
| 816 | } |
| 817 | |
| 818 | // Name must be an identifier. |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 819 | if (D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 820 | Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier); |
| 821 | // No removal fixit: can't recover from this. |
| 822 | SkipUntil(tok::semi); |
| 823 | return nullptr; |
| 824 | } else if (D.TypenameLoc.isValid()) |
| 825 | Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier) |
| 826 | << FixItHint::CreateRemoval(SourceRange( |
| 827 | D.TypenameLoc, |
| 828 | D.SS.isNotEmpty() ? D.SS.getEndLoc() : D.TypenameLoc)); |
| 829 | else if (D.SS.isNotEmpty()) |
| 830 | Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier) |
| 831 | << FixItHint::CreateRemoval(D.SS.getRange()); |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 832 | if (D.EllipsisLoc.isValid()) |
| 833 | Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion) |
| 834 | << FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc)); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 835 | |
| 836 | Decl *DeclFromDeclSpec = nullptr; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 837 | TypeResult TypeAlias = ParseTypeName( |
| 838 | nullptr, |
| 839 | TemplateInfo.Kind ? DeclaratorContext::AliasTemplateContext |
| 840 | : DeclaratorContext::AliasDeclContext, |
| 841 | AS, &DeclFromDeclSpec, &Attrs); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 842 | if (OwnedType) |
| 843 | *OwnedType = DeclFromDeclSpec; |
| 844 | |
| 845 | // Eat ';'. |
| 846 | DeclEnd = Tok.getLocation(); |
| 847 | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
| 848 | !Attrs.empty() ? "attributes list" |
| 849 | : "alias declaration")) |
| 850 | SkipUntil(tok::semi); |
| 851 | |
| 852 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 853 | MultiTemplateParamsArg TemplateParamsArg( |
| 854 | TemplateParams ? TemplateParams->data() : nullptr, |
| 855 | TemplateParams ? TemplateParams->size() : 0); |
| 856 | return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 857 | UsingLoc, D.Name, Attrs, TypeAlias, |
| 858 | DeclFromDeclSpec); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 861 | /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 862 | /// |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 863 | /// [C++0x] static_assert-declaration: |
| 864 | /// static_assert ( constant-expression , string-literal ) ; |
| 865 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 866 | /// [C11] static_assert-declaration: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 867 | /// _Static_assert ( constant-expression , string-literal ) ; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 868 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 869 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 870 | assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) && |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 871 | "Not a static_assert declaration"); |
| 872 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 873 | if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11) |
Aaron Ballman | 1d93522 | 2019-08-27 14:41:39 +0000 | [diff] [blame] | 874 | Diag(Tok, diag::ext_c11_feature) << Tok.getName(); |
Richard Smith | b15c11c | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 875 | if (Tok.is(tok::kw_static_assert)) |
| 876 | Diag(Tok, diag::warn_cxx98_compat_static_assert); |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 877 | |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 878 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 880 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 881 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 882 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Richard Smith | 7696571 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 883 | SkipMalformedDecl(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 884 | return nullptr; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 885 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
Richard Smith | b301806 | 2017-06-06 01:34:24 +0000 | [diff] [blame] | 887 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 888 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 889 | ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext()); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 890 | if (AssertExpr.isInvalid()) { |
Richard Smith | 7696571 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 891 | SkipMalformedDecl(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 892 | return nullptr; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 893 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 895 | ExprResult AssertMessage; |
| 896 | if (Tok.is(tok::r_paren)) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 897 | Diag(Tok, getLangOpts().CPlusPlus17 |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 898 | ? diag::warn_cxx14_compat_static_assert_no_message |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 899 | : diag::ext_static_assert_no_message) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 900 | << (getLangOpts().CPlusPlus17 |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 901 | ? FixItHint() |
| 902 | : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\"")); |
| 903 | } else { |
| 904 | if (ExpectAndConsume(tok::comma)) { |
| 905 | SkipUntil(tok::semi); |
| 906 | return nullptr; |
| 907 | } |
Anders Carlsson | b4cf3ad | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 908 | |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 909 | if (!isTokenStringLiteral()) { |
| 910 | Diag(Tok, diag::err_expected_string_literal) |
| 911 | << /*Source='static_assert'*/1; |
| 912 | SkipMalformedDecl(); |
| 913 | return nullptr; |
| 914 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 916 | AssertMessage = ParseStringLiteralExpression(); |
| 917 | if (AssertMessage.isInvalid()) { |
| 918 | SkipMalformedDecl(); |
| 919 | return nullptr; |
| 920 | } |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 921 | } |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 923 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 925 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 45d6bdf | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 926 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 927 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 928 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 929 | AssertExpr.get(), |
| 930 | AssertMessage.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 931 | T.getCloseLocation()); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 934 | /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier. |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 935 | /// |
| 936 | /// 'decltype' ( expression ) |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 937 | /// 'decltype' ( 'auto' ) [C++1y] |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 938 | /// |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 939 | SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 940 | assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype) |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 941 | && "Not a decltype specifier"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 942 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 943 | ExprResult Result; |
| 944 | SourceLocation StartLoc = Tok.getLocation(); |
| 945 | SourceLocation EndLoc; |
| 946 | |
| 947 | if (Tok.is(tok::annot_decltype)) { |
| 948 | Result = getExprAnnotation(Tok); |
| 949 | EndLoc = Tok.getAnnotationEndLoc(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 950 | ConsumeAnnotationToken(); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 951 | if (Result.isInvalid()) { |
| 952 | DS.SetTypeSpecError(); |
| 953 | return EndLoc; |
| 954 | } |
| 955 | } else { |
Richard Smith | 324df55 | 2012-02-24 22:30:04 +0000 | [diff] [blame] | 956 | if (Tok.getIdentifierInfo()->isStr("decltype")) |
| 957 | Diag(Tok, diag::warn_cxx98_compat_decltype); |
Richard Smith | fd3da93 | 2012-02-24 18:10:23 +0000 | [diff] [blame] | 958 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 959 | ConsumeToken(); |
| 960 | |
| 961 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 962 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 963 | "decltype", tok::r_paren)) { |
| 964 | DS.SetTypeSpecError(); |
| 965 | return T.getOpenLocation() == Tok.getLocation() ? |
| 966 | StartLoc : T.getOpenLocation(); |
| 967 | } |
| 968 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 969 | // Check for C++1y 'decltype(auto)'. |
| 970 | if (Tok.is(tok::kw_auto)) { |
| 971 | // No need to disambiguate here: an expression can't start with 'auto', |
| 972 | // because the typename-specifier in a function-style cast operation can't |
| 973 | // be 'auto'. |
| 974 | Diag(Tok.getLocation(), |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 975 | getLangOpts().CPlusPlus14 |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 976 | ? diag::warn_cxx11_compat_decltype_auto_type_specifier |
| 977 | : diag::ext_decltype_auto_type_specifier); |
| 978 | ConsumeToken(); |
| 979 | } else { |
| 980 | // Parse the expression |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 981 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 982 | // C++11 [dcl.type.simple]p4: |
| 983 | // The operand of the decltype specifier is an unevaluated operand. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 984 | EnterExpressionEvaluationContext Unevaluated( |
| 985 | Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr, |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 986 | Sema::ExpressionEvaluationContextRecord::EK_Decltype); |
Kaelyn Takata | 5cc8535 | 2015-04-10 19:16:46 +0000 | [diff] [blame] | 987 | Result = |
| 988 | Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) { |
| 989 | return E->hasPlaceholderType() ? ExprError() : E; |
| 990 | }); |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 991 | if (Result.isInvalid()) { |
| 992 | DS.SetTypeSpecError(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 993 | if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 994 | EndLoc = ConsumeParen(); |
Argyrios Kyrtzidis | c38395a | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 995 | } else { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 996 | if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) { |
| 997 | // Backtrack to get the location of the last token before the semi. |
| 998 | PP.RevertCachedTokens(2); |
| 999 | ConsumeToken(); // the semi. |
| 1000 | EndLoc = ConsumeAnyToken(); |
| 1001 | assert(Tok.is(tok::semi)); |
| 1002 | } else { |
| 1003 | EndLoc = Tok.getLocation(); |
| 1004 | } |
Argyrios Kyrtzidis | c38395a | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 1005 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1006 | return EndLoc; |
Argyrios Kyrtzidis | c38395a | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 1007 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1008 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1009 | Result = Actions.ActOnDecltypeExpression(Result.get()); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | // Match the ')' |
| 1013 | T.consumeClose(); |
| 1014 | if (T.getCloseLocation().isInvalid()) { |
| 1015 | DS.SetTypeSpecError(); |
| 1016 | // FIXME: this should return the location of the last token |
| 1017 | // that was consumed (by "consumeClose()") |
| 1018 | return T.getCloseLocation(); |
| 1019 | } |
| 1020 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 1021 | if (Result.isInvalid()) { |
| 1022 | DS.SetTypeSpecError(); |
| 1023 | return T.getCloseLocation(); |
| 1024 | } |
| 1025 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1026 | EndLoc = T.getCloseLocation(); |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1027 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1028 | assert(!Result.isInvalid()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1030 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1031 | unsigned DiagID; |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1032 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1033 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1034 | if (Result.get() |
| 1035 | ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1036 | DiagID, Result.get(), Policy) |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1037 | : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1038 | DiagID, Policy)) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1039 | Diag(StartLoc, DiagID) << PrevSpec; |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1040 | DS.SetTypeSpecError(); |
| 1041 | } |
| 1042 | return EndLoc; |
| 1043 | } |
| 1044 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1045 | void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS, |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1046 | SourceLocation StartLoc, |
| 1047 | SourceLocation EndLoc) { |
| 1048 | // make sure we have a token we can turn into an annotation token |
| 1049 | if (PP.isBacktrackEnabled()) |
| 1050 | PP.RevertCachedTokens(1); |
| 1051 | else |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 1052 | PP.EnterToken(Tok, /*IsReinject*/true); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1053 | |
| 1054 | Tok.setKind(tok::annot_decltype); |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 1055 | setExprAnnotation(Tok, |
| 1056 | DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() : |
| 1057 | DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() : |
| 1058 | ExprError()); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1059 | Tok.setAnnotationEndLoc(EndLoc); |
| 1060 | Tok.setLocation(StartLoc); |
| 1061 | PP.AnnotateCachedTokens(Tok); |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1064 | void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) { |
| 1065 | assert(Tok.is(tok::kw___underlying_type) && |
| 1066 | "Not an underlying type specifier"); |
| 1067 | |
| 1068 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1069 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1070 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 1071 | "__underlying_type", tok::r_paren)) { |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | TypeResult Result = ParseTypeName(); |
| 1076 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1077 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1082 | T.consumeClose(); |
| 1083 | if (T.getCloseLocation().isInvalid()) |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1084 | return; |
| 1085 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1086 | const char *PrevSpec = nullptr; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1087 | unsigned DiagID; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1088 | if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1089 | DiagID, Result.get(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1090 | Actions.getASTContext().getPrintingPolicy())) |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1091 | Diag(StartLoc, DiagID) << PrevSpec; |
Enea Zaffanella | a90af72 | 2013-07-06 18:54:58 +0000 | [diff] [blame] | 1092 | DS.setTypeofParensRange(T.getRange()); |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
David Blaikie | 00ee7a08 | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1095 | /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1096 | /// class name or decltype-specifier. Note that we only check that the result |
| 1097 | /// names a type; semantic analysis will need to verify that the type names a |
| 1098 | /// class. The result is either a type or null, depending on whether a type |
David Blaikie | 00ee7a08 | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1099 | /// name was found. |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1100 | /// |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1101 | /// base-type-specifier: [C++11 class.derived] |
David Blaikie | 00ee7a08 | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1102 | /// class-or-decltype |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1103 | /// class-or-decltype: [C++11 class.derived] |
David Blaikie | 00ee7a08 | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1104 | /// nested-name-specifier[opt] class-name |
| 1105 | /// decltype-specifier |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1106 | /// class-name: [C++ class.name] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1107 | /// identifier |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1108 | /// simple-template-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | /// |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1110 | /// In C++98, instead of base-type-specifier, we have: |
| 1111 | /// |
| 1112 | /// ::[opt] nested-name-specifier[opt] class-name |
Craig Topper | 9ad7e26 | 2014-10-31 06:57:07 +0000 | [diff] [blame] | 1113 | TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc, |
| 1114 | SourceLocation &EndLocation) { |
David Blaikie | dd58d4c | 2011-10-25 18:46:41 +0000 | [diff] [blame] | 1115 | // Ignore attempts to use typename |
| 1116 | if (Tok.is(tok::kw_typename)) { |
| 1117 | Diag(Tok, diag::err_expected_class_name_not_template) |
| 1118 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1119 | ConsumeToken(); |
| 1120 | } |
| 1121 | |
David Blaikie | afa155f | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 1122 | // Parse optional nested-name-specifier |
| 1123 | CXXScopeSpec SS; |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 1124 | if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
| 1125 | /*ObjectHadErrors=*/false, |
| 1126 | /*EnteringContext=*/false)) |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1127 | return true; |
David Blaikie | afa155f | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 1128 | |
| 1129 | BaseLoc = Tok.getLocation(); |
| 1130 | |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1131 | // Parse decltype-specifier |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1132 | // tok == kw_decltype is just error recovery, it can only happen when SS |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1133 | // isn't empty |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1134 | if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) { |
David Blaikie | afa155f | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 1135 | if (SS.isNotEmpty()) |
| 1136 | Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype) |
| 1137 | << FixItHint::CreateRemoval(SS.getRange()); |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1138 | // Fake up a Declarator to use with ActOnTypeName. |
| 1139 | DeclSpec DS(AttrFactory); |
| 1140 | |
David Blaikie | 7491e73 | 2011-12-08 04:53:15 +0000 | [diff] [blame] | 1141 | EndLocation = ParseDecltypeSpecifier(DS); |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1142 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1143 | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext); |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1144 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 1145 | } |
| 1146 | |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1147 | // Check whether we have a template-id that names a type. |
| 1148 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1149 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 1150 | if (TemplateId->mightBeType()) { |
Richard Smith | a42fd84 | 2020-01-17 15:42:11 -0800 | [diff] [blame] | 1151 | AnnotateTemplateIdTokenAsType(SS, /*IsClassName*/true); |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1152 | |
| 1153 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 1154 | TypeResult Type = getTypeAnnotation(Tok); |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1155 | EndLocation = Tok.getAnnotationEndLoc(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1156 | ConsumeAnnotationToken(); |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 1157 | return Type; |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | // Fall through to produce an error below. |
| 1161 | } |
| 1162 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1163 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1164 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1165 | return true; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1168 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 1169 | SourceLocation IdLoc = ConsumeToken(); |
| 1170 | |
| 1171 | if (Tok.is(tok::less)) { |
| 1172 | // It looks the user intended to write a template-id here, but the |
| 1173 | // template-name was wrong. Try to fix that. |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 1174 | // FIXME: Invoke ParseOptionalCXXScopeSpecifier in a "'template' is neither |
| 1175 | // required nor permitted" mode, and do this there. |
| 1176 | TemplateNameKind TNK = TNK_Non_template; |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1177 | TemplateTy Template; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1178 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1179 | &SS, Template, TNK)) { |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1180 | Diag(IdLoc, diag::err_unknown_template_name) |
| 1181 | << Id; |
| 1182 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1183 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1184 | // Form the template name |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1185 | UnqualifiedId TemplateName; |
| 1186 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1187 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1188 | // Parse the full template-id, then turn it into a type. |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1189 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 1190 | TemplateName)) |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1191 | return true; |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 1192 | if (Tok.is(tok::annot_template_id) && |
| 1193 | takeTemplateIdAnnotation(Tok)->mightBeType()) |
Richard Smith | a42fd84 | 2020-01-17 15:42:11 -0800 | [diff] [blame] | 1194 | AnnotateTemplateIdTokenAsType(SS, /*IsClassName*/true); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1196 | // If we didn't end up with a typename token, there's nothing more we |
| 1197 | // can do. |
| 1198 | if (Tok.isNot(tok::annot_typename)) |
| 1199 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1201 | // Retrieve the type from the annotation token, consume that token, and |
| 1202 | // return. |
| 1203 | EndLocation = Tok.getAnnotationEndLoc(); |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 1204 | TypeResult Type = getTypeAnnotation(Tok); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1205 | ConsumeAnnotationToken(); |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1206 | return Type; |
| 1207 | } |
| 1208 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1209 | // We have an identifier; check whether it is actually a type. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1210 | IdentifierInfo *CorrectedII = nullptr; |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1211 | ParsedType Type = Actions.getTypeName( |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1212 | *Id, IdLoc, getCurScope(), &SS, /*isClassName=*/true, false, nullptr, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1213 | /*IsCtorOrDtorName=*/false, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1214 | /*WantNontrivialTypeSourceInfo=*/true, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1215 | /*IsClassTemplateDeductionContext*/ false, &CorrectedII); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1216 | if (!Type) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 1217 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1218 | return true; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // Consume the identifier. |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1222 | EndLocation = IdLoc; |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1225 | DeclSpec DS(AttrFactory); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1226 | DS.SetRangeStart(IdLoc); |
| 1227 | DS.SetRangeEnd(EndLocation); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1228 | DS.getTypeSpecScope() = SS; |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1229 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1230 | const char *PrevSpec = nullptr; |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1231 | unsigned DiagID; |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 1232 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type, |
| 1233 | Actions.getASTContext().getPrintingPolicy()); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1234 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1235 | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1236 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1239 | void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1240 | while (Tok.isOneOf(tok::kw___single_inheritance, |
| 1241 | tok::kw___multiple_inheritance, |
| 1242 | tok::kw___virtual_inheritance)) { |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1243 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 1244 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1245 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1246 | ParsedAttr::AS_Keyword); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1250 | /// Determine whether the following tokens are valid after a type-specifier |
| 1251 | /// which could be a standalone declaration. This will conservatively return |
| 1252 | /// true if there's any doubt, and is appropriate for insert-';' fixits. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1253 | bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1254 | // This switch enumerates the valid "follow" set for type-specifiers. |
| 1255 | switch (Tok.getKind()) { |
| 1256 | default: break; |
| 1257 | case tok::semi: // struct foo {...} ; |
| 1258 | case tok::star: // struct foo {...} * P; |
| 1259 | case tok::amp: // struct foo {...} & R = ... |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1260 | case tok::ampamp: // struct foo {...} && R = ... |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1261 | case tok::identifier: // struct foo {...} V ; |
| 1262 | case tok::r_paren: //(struct foo {...} ) {4} |
Richard Smith | 1600e24 | 2019-04-16 00:47:45 +0000 | [diff] [blame] | 1263 | case tok::coloncolon: // struct foo {...} :: a::b; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1264 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1265 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1266 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
Richard Smith | 1600e24 | 2019-04-16 00:47:45 +0000 | [diff] [blame] | 1267 | case tok::kw_decltype: // struct foo {...} decltype (a)::b; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1268 | case tok::l_paren: // struct foo {...} ( x); |
| 1269 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1270 | case tok::kw_operator: // struct foo operator ++() {...} |
Alp Toker | d3f79c5 | 2013-11-24 20:24:54 +0000 | [diff] [blame] | 1271 | case tok::kw___declspec: // struct foo {...} __declspec(...) |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 1272 | case tok::l_square: // void f(struct f [ 3]) |
| 1273 | case tok::ellipsis: // void f(struct f ... [Ns]) |
Abramo Bagnara | 152eb39 | 2014-08-16 08:29:27 +0000 | [diff] [blame] | 1274 | // FIXME: we should emit semantic diagnostic when declaration |
| 1275 | // attribute is in type attribute position. |
| 1276 | case tok::kw___attribute: // struct foo __attribute__((used)) x; |
David Majnemer | 15b311c | 2016-06-14 03:20:28 +0000 | [diff] [blame] | 1277 | case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop)); |
| 1278 | // struct foo {...} _Pragma(section(...)); |
| 1279 | case tok::annot_pragma_ms_pragma: |
| 1280 | // struct foo {...} _Pragma(vtordisp(pop)); |
| 1281 | case tok::annot_pragma_ms_vtordisp: |
| 1282 | // struct foo {...} _Pragma(pointers_to_members(...)); |
| 1283 | case tok::annot_pragma_ms_pointers_to_members: |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1284 | return true; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1285 | case tok::colon: |
| 1286 | return CouldBeBitfield; // enum E { ... } : 2; |
Reid Kleckner | cfa9155 | 2016-03-21 16:08:49 +0000 | [diff] [blame] | 1287 | // Microsoft compatibility |
| 1288 | case tok::kw___cdecl: // struct foo {...} __cdecl x; |
| 1289 | case tok::kw___fastcall: // struct foo {...} __fastcall x; |
| 1290 | case tok::kw___stdcall: // struct foo {...} __stdcall x; |
| 1291 | case tok::kw___thiscall: // struct foo {...} __thiscall x; |
| 1292 | case tok::kw___vectorcall: // struct foo {...} __vectorcall x; |
| 1293 | // We will diagnose these calling-convention specifiers on non-function |
| 1294 | // declarations later, so claim they are valid after a type specifier. |
| 1295 | return getLangOpts().MicrosoftExt; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1296 | // Type qualifiers |
| 1297 | case tok::kw_const: // struct foo {...} const x; |
| 1298 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1299 | case tok::kw_restrict: // struct foo {...} restrict x; |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 1300 | case tok::kw__Atomic: // struct foo {...} _Atomic x; |
Nico Rieck | 3e1ee83 | 2014-12-04 23:30:25 +0000 | [diff] [blame] | 1301 | case tok::kw___unaligned: // struct foo {...} __unaligned *x; |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1302 | // Function specifiers |
| 1303 | // Note, no 'explicit'. An explicit function must be either a conversion |
| 1304 | // operator or a constructor. Either way, it can't have a return type. |
| 1305 | case tok::kw_inline: // struct foo inline f(); |
| 1306 | case tok::kw_virtual: // struct foo virtual f(); |
| 1307 | case tok::kw_friend: // struct foo friend f(); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1308 | // Storage-class specifiers |
| 1309 | case tok::kw_static: // struct foo {...} static x; |
| 1310 | case tok::kw_extern: // struct foo {...} extern x; |
| 1311 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1312 | case tok::kw_register: // struct foo {...} register x; |
| 1313 | case tok::kw_auto: // struct foo {...} auto x; |
| 1314 | case tok::kw_mutable: // struct foo {...} mutable x; |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1315 | case tok::kw_thread_local: // struct foo {...} thread_local x; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1316 | case tok::kw_constexpr: // struct foo {...} constexpr x; |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 1317 | case tok::kw_consteval: // struct foo {...} consteval x; |
| 1318 | case tok::kw_constinit: // struct foo {...} constinit x; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1319 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1320 | // can occur after class specifiers according to the grammar. However, |
| 1321 | // almost no one actually writes code like this. If we see one of these, |
| 1322 | // it is much more likely that someone missed a semi colon and the |
| 1323 | // type/storage class specifier we're seeing is part of the *next* |
| 1324 | // intended declaration, as in: |
| 1325 | // |
| 1326 | // struct foo { ... } |
| 1327 | // typedef int X; |
| 1328 | // |
| 1329 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1330 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1331 | // the same declaration of X. Because of this, we look ahead past this |
| 1332 | // token to see if it's a type specifier. If so, we know the code is |
| 1333 | // otherwise invalid, so we can produce the expected semi error. |
| 1334 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1335 | return true; |
| 1336 | break; |
| 1337 | case tok::r_brace: // struct bar { struct foo {...} } |
| 1338 | // Missing ';' at end of struct is accepted as an extension in C mode. |
| 1339 | if (!getLangOpts().CPlusPlus) |
| 1340 | return true; |
| 1341 | break; |
Richard Smith | 52c5b87 | 2013-01-29 04:13:32 +0000 | [diff] [blame] | 1342 | case tok::greater: |
| 1343 | // template<class T = class X> |
| 1344 | return getLangOpts().CPlusPlus; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1345 | } |
| 1346 | return false; |
| 1347 | } |
| 1348 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1349 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 1350 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 1351 | /// until we reach the start of a definition or see a token that |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1352 | /// cannot start a definition. |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1353 | /// |
| 1354 | /// class-specifier: [C++ class] |
| 1355 | /// class-head '{' member-specification[opt] '}' |
| 1356 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 1357 | /// class-head: |
| 1358 | /// class-key identifier[opt] base-clause[opt] |
| 1359 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 1360 | /// class-key nested-name-specifier[opt] simple-template-id |
| 1361 | /// base-clause[opt] |
| 1362 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1364 | /// identifier base-clause[opt] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1366 | /// simple-template-id base-clause[opt] |
| 1367 | /// class-key: |
| 1368 | /// 'class' |
| 1369 | /// 'struct' |
| 1370 | /// 'union' |
| 1371 | /// |
| 1372 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 1374 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 1375 | /// simple-template-id |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1376 | /// |
| 1377 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 1378 | /// together, subsume the C99 struct-or-union-specifier: |
| 1379 | /// |
| 1380 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 1381 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 1382 | /// struct-or-union identifier |
| 1383 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 1384 | /// '}' attributes[opt] |
| 1385 | /// [GNU] struct-or-union attributes[opt] identifier |
| 1386 | /// struct-or-union: |
| 1387 | /// 'struct' |
| 1388 | /// 'union' |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1389 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 1390 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1391 | const ParsedTemplateInfo &TemplateInfo, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1392 | AccessSpecifier AS, |
| 1393 | bool EnteringContext, DeclSpecContext DSC, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1394 | ParsedAttributesWithRange &Attributes) { |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1395 | DeclSpec::TST TagType; |
| 1396 | if (TagTokKind == tok::kw_struct) |
| 1397 | TagType = DeclSpec::TST_struct; |
| 1398 | else if (TagTokKind == tok::kw___interface) |
| 1399 | TagType = DeclSpec::TST_interface; |
| 1400 | else if (TagTokKind == tok::kw_class) |
| 1401 | TagType = DeclSpec::TST_class; |
| 1402 | else { |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1403 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 1404 | TagType = DeclSpec::TST_union; |
| 1405 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1406 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1407 | if (Tok.is(tok::code_completion)) { |
| 1408 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1409 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1410 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1411 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1412 | |
Chandler Carruth | 2d69ec7 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1413 | // C++03 [temp.explicit] 14.7.2/8: |
| 1414 | // The usual access checking rules do not apply to names used to specify |
| 1415 | // explicit instantiations. |
| 1416 | // |
| 1417 | // As an extension we do not perform access checking on the names used to |
| 1418 | // specify explicit specializations either. This is important to allow |
| 1419 | // specializing traits classes for private types. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1420 | // |
| 1421 | // Note that we don't suppress if this turns out to be an elaborated |
| 1422 | // type specifier. |
| 1423 | bool shouldDelayDiagsInTag = |
| 1424 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 1425 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 1426 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Chandler Carruth | 2d69ec7 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1427 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1428 | ParsedAttributesWithRange attrs(AttrFactory); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1429 | // If attributes exist after tag, parse them. |
Richard Smith | 37a45dd | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 1430 | MaybeParseGNUAttributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 1431 | MaybeParseMicrosoftDeclSpecs(attrs); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1432 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1433 | // Parse inheritance specifiers. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1434 | if (Tok.isOneOf(tok::kw___single_inheritance, |
| 1435 | tok::kw___multiple_inheritance, |
| 1436 | tok::kw___virtual_inheritance)) |
Richard Smith | 37a45dd | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 1437 | ParseMicrosoftInheritanceClassAttributes(attrs); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1438 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1439 | // If C++0x attributes exist here, parse them. |
| 1440 | // FIXME: Are we consistent with the ordering of parsing of different |
| 1441 | // styles of attributes? |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1442 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1444 | // Source location used by FIXIT to insert misplaced |
| 1445 | // C++11 attributes |
| 1446 | SourceLocation AttrFixitLoc = Tok.getLocation(); |
| 1447 | |
Nico Weber | 7c3c5be | 2014-09-23 04:09:56 +0000 | [diff] [blame] | 1448 | if (TagType == DeclSpec::TST_struct && |
David Majnemer | 86330af | 2014-12-29 02:14:26 +0000 | [diff] [blame] | 1449 | Tok.isNot(tok::identifier) && |
| 1450 | !Tok.isAnnotation() && |
Nico Weber | 7c3c5be | 2014-09-23 04:09:56 +0000 | [diff] [blame] | 1451 | Tok.getIdentifierInfo() && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1452 | Tok.isOneOf(tok::kw___is_abstract, |
Eric Fiselier | 0736066 | 2017-04-12 22:12:15 +0000 | [diff] [blame] | 1453 | tok::kw___is_aggregate, |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1454 | tok::kw___is_arithmetic, |
| 1455 | tok::kw___is_array, |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 1456 | tok::kw___is_assignable, |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1457 | tok::kw___is_base_of, |
| 1458 | tok::kw___is_class, |
| 1459 | tok::kw___is_complete_type, |
| 1460 | tok::kw___is_compound, |
| 1461 | tok::kw___is_const, |
| 1462 | tok::kw___is_constructible, |
| 1463 | tok::kw___is_convertible, |
| 1464 | tok::kw___is_convertible_to, |
| 1465 | tok::kw___is_destructible, |
| 1466 | tok::kw___is_empty, |
| 1467 | tok::kw___is_enum, |
| 1468 | tok::kw___is_floating_point, |
| 1469 | tok::kw___is_final, |
| 1470 | tok::kw___is_function, |
| 1471 | tok::kw___is_fundamental, |
| 1472 | tok::kw___is_integral, |
| 1473 | tok::kw___is_interface_class, |
| 1474 | tok::kw___is_literal, |
| 1475 | tok::kw___is_lvalue_expr, |
| 1476 | tok::kw___is_lvalue_reference, |
| 1477 | tok::kw___is_member_function_pointer, |
| 1478 | tok::kw___is_member_object_pointer, |
| 1479 | tok::kw___is_member_pointer, |
| 1480 | tok::kw___is_nothrow_assignable, |
| 1481 | tok::kw___is_nothrow_constructible, |
| 1482 | tok::kw___is_nothrow_destructible, |
| 1483 | tok::kw___is_object, |
| 1484 | tok::kw___is_pod, |
| 1485 | tok::kw___is_pointer, |
| 1486 | tok::kw___is_polymorphic, |
| 1487 | tok::kw___is_reference, |
| 1488 | tok::kw___is_rvalue_expr, |
| 1489 | tok::kw___is_rvalue_reference, |
| 1490 | tok::kw___is_same, |
| 1491 | tok::kw___is_scalar, |
| 1492 | tok::kw___is_sealed, |
| 1493 | tok::kw___is_signed, |
| 1494 | tok::kw___is_standard_layout, |
| 1495 | tok::kw___is_trivial, |
| 1496 | tok::kw___is_trivially_assignable, |
| 1497 | tok::kw___is_trivially_constructible, |
| 1498 | tok::kw___is_trivially_copyable, |
| 1499 | tok::kw___is_union, |
| 1500 | tok::kw___is_unsigned, |
| 1501 | tok::kw___is_void, |
| 1502 | tok::kw___is_volatile)) |
Nico Weber | 7c3c5be | 2014-09-23 04:09:56 +0000 | [diff] [blame] | 1503 | // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the |
| 1504 | // name of struct templates, but some are keywords in GCC >= 4.3 |
| 1505 | // and Clang. Therefore, when we see the token sequence "struct |
| 1506 | // X", make X into a normal identifier rather than a keyword, to |
| 1507 | // allow libstdc++ 4.2 and libc++ to work properly. |
| 1508 | TryKeywordIdentFallback(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 1510 | struct PreserveAtomicIdentifierInfoRAII { |
| 1511 | PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled) |
| 1512 | : AtomicII(nullptr) { |
| 1513 | if (!Enabled) |
| 1514 | return; |
| 1515 | assert(Tok.is(tok::kw__Atomic)); |
| 1516 | AtomicII = Tok.getIdentifierInfo(); |
| 1517 | AtomicII->revertTokenIDToIdentifier(); |
| 1518 | Tok.setKind(tok::identifier); |
| 1519 | } |
| 1520 | ~PreserveAtomicIdentifierInfoRAII() { |
| 1521 | if (!AtomicII) |
| 1522 | return; |
| 1523 | AtomicII->revertIdentifierToTokenID(tok::kw__Atomic); |
| 1524 | } |
| 1525 | IdentifierInfo *AtomicII; |
| 1526 | }; |
| 1527 | |
| 1528 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 1529 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 1530 | // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider |
| 1531 | // '_Atomic' to be a keyword. We are careful to undo this so that clang can |
| 1532 | // use '_Atomic' in its own header files. |
| 1533 | bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat && |
| 1534 | Tok.is(tok::kw__Atomic) && |
| 1535 | TagType == DeclSpec::TST_struct; |
| 1536 | PreserveAtomicIdentifierInfoRAII AtomicTokenGuard( |
| 1537 | Tok, ShouldChangeAtomicToIdentifier); |
| 1538 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1539 | // Parse the (optional) nested-name-specifier. |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1540 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1541 | if (getLangOpts().CPlusPlus) { |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 1542 | // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it |
| 1543 | // is a base-specifier-list. |
Chris Lattner | d5c1c9d | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1544 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1545 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 1546 | CXXScopeSpec Spec; |
| 1547 | bool HasValidSpec = true; |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 1548 | if (ParseOptionalCXXScopeSpecifier(Spec, /*ObjectType=*/nullptr, |
| 1549 | /*ObjectHadErrors=*/false, |
| 1550 | EnteringContext)) { |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1551 | DS.SetTypeSpecError(); |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 1552 | HasValidSpec = false; |
| 1553 | } |
| 1554 | if (Spec.isSet()) |
| 1555 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1556 | Diag(Tok, diag::err_expected) << tok::identifier; |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 1557 | HasValidSpec = false; |
| 1558 | } |
| 1559 | if (HasValidSpec) |
| 1560 | SS = Spec; |
Chris Lattner | d5c1c9d | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1561 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1562 | |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1563 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 1564 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1565 | auto RecoverFromUndeclaredTemplateName = [&](IdentifierInfo *Name, |
| 1566 | SourceLocation NameLoc, |
| 1567 | SourceRange TemplateArgRange, |
| 1568 | bool KnownUndeclared) { |
| 1569 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
| 1570 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 1571 | << TagTokKind << Name << TemplateArgRange << KnownUndeclared; |
| 1572 | |
| 1573 | // Strip off the last template parameter list if it was empty, since |
| 1574 | // we've removed its template argument list. |
| 1575 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
| 1576 | if (TemplateParams->size() > 1) { |
| 1577 | TemplateParams->pop_back(); |
| 1578 | } else { |
| 1579 | TemplateParams = nullptr; |
| 1580 | const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind = |
| 1581 | ParsedTemplateInfo::NonTemplate; |
| 1582 | } |
| 1583 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1584 | // Pretend this is just a forward declaration. |
| 1585 | TemplateParams = nullptr; |
| 1586 | const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind = |
| 1587 | ParsedTemplateInfo::NonTemplate; |
| 1588 | const_cast<ParsedTemplateInfo &>(TemplateInfo).TemplateLoc = |
| 1589 | SourceLocation(); |
| 1590 | const_cast<ParsedTemplateInfo &>(TemplateInfo).ExternLoc = |
| 1591 | SourceLocation(); |
| 1592 | } |
| 1593 | }; |
| 1594 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1595 | // Parse the (optional) class name or simple-template-id. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1596 | IdentifierInfo *Name = nullptr; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1597 | SourceLocation NameLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1598 | TemplateIdAnnotation *TemplateId = nullptr; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1599 | if (Tok.is(tok::identifier)) { |
| 1600 | Name = Tok.getIdentifierInfo(); |
| 1601 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1602 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1603 | if (Tok.is(tok::less) && getLangOpts().CPlusPlus) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1604 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1605 | // Eat the template argument list and try to continue parsing this as |
| 1606 | // a class (or template thereof). |
| 1607 | TemplateArgList TemplateArgs; |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1608 | SourceLocation LAngleLoc, RAngleLoc; |
Richard Smith | 9a420f9 | 2017-05-10 21:47:30 +0000 | [diff] [blame] | 1609 | if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs, |
| 1610 | RAngleLoc)) { |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1611 | // We couldn't parse the template argument list at all, so don't |
| 1612 | // try to give any location information for the list. |
| 1613 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 1614 | } |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1615 | RecoverFromUndeclaredTemplateName( |
| 1616 | Name, NameLoc, SourceRange(LAngleLoc, RAngleLoc), false); |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1617 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1618 | } else if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1619 | TemplateId = takeTemplateIdAnnotation(Tok); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1620 | NameLoc = ConsumeAnnotationToken(); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1621 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1622 | if (TemplateId->Kind == TNK_Undeclared_template) { |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 1623 | // Try to resolve the template name to a type template. May update Kind. |
| 1624 | Actions.ActOnUndeclaredTypeTemplateName( |
| 1625 | getCurScope(), TemplateId->Template, TemplateId->Kind, NameLoc, Name); |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1626 | if (TemplateId->Kind == TNK_Undeclared_template) { |
| 1627 | RecoverFromUndeclaredTemplateName( |
| 1628 | Name, NameLoc, |
| 1629 | SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc), true); |
| 1630 | TemplateId = nullptr; |
| 1631 | } |
| 1632 | } |
| 1633 | |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 1634 | if (TemplateId && !TemplateId->mightBeType()) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1635 | // The template-name in the simple-template-id refers to |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 1636 | // something other than a type template. Give an appropriate |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1637 | // error message and skip to the ';'. |
| 1638 | SourceRange Range(NameLoc); |
| 1639 | if (SS.isNotEmpty()) |
| 1640 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1641 | |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 1642 | // FIXME: Name may be null here. |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1643 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1644 | << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1646 | DS.SetTypeSpecError(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1647 | SkipUntil(tok::semi, StopBeforeMatch); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1648 | return; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1649 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1652 | // There are four options here. |
| 1653 | // - If we are in a trailing return type, this is always just a reference, |
| 1654 | // and we must not try to parse a definition. For instance, |
| 1655 | // [] () -> struct S { }; |
| 1656 | // does not define a type. |
| 1657 | // - If we have 'struct foo {...', 'struct foo :...', |
| 1658 | // 'struct foo final :' or 'struct foo final {', then this is a definition. |
| 1659 | // - If we have 'struct foo;', then this is either a forward declaration |
| 1660 | // or a friend declaration, which have to be treated differently. |
| 1661 | // - Otherwise we have something like 'struct foo xyz', a reference. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1662 | // |
| 1663 | // We also detect these erroneous cases to provide better diagnostic for |
| 1664 | // C++11 attributes parsing. |
| 1665 | // - attributes follow class name: |
| 1666 | // struct foo [[]] {}; |
| 1667 | // - attributes appear before or after 'final': |
| 1668 | // struct foo [[]] final [[]] {}; |
| 1669 | // |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1670 | // However, in type-specifier-seq's, things look like declarations but are |
| 1671 | // just references, e.g. |
| 1672 | // new struct s; |
Sebastian Redl | 2b37272 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1673 | // or |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1674 | // &T::operator struct s; |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1675 | // For these, DSC is DeclSpecContext::DSC_type_specifier or |
| 1676 | // DeclSpecContext::DSC_alias_declaration. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1677 | |
| 1678 | // If there are attributes after class name, parse them. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1679 | MaybeParseCXX11Attributes(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1680 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1681 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1682 | Sema::TagUseKind TUK; |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1683 | if (DSC == DeclSpecContext::DSC_trailing) |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1684 | TUK = Sema::TUK_Reference; |
| 1685 | else if (Tok.is(tok::l_brace) || |
| 1686 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1687 | (isCXX11FinalKeyword() && |
David Blaikie | 9933a5a | 2012-03-12 15:39:49 +0000 | [diff] [blame] | 1688 | (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1689 | if (DS.isFriendSpecified()) { |
| 1690 | // C++ [class.friend]p2: |
| 1691 | // A class shall not be defined in a friend declaration. |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1692 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1693 | << SourceRange(DS.getFriendSpecLoc()); |
| 1694 | |
| 1695 | // Skip everything up to the semicolon, so that this looks like a proper |
| 1696 | // friend class (or template thereof) declaration. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1697 | SkipUntil(tok::semi, StopBeforeMatch); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1698 | TUK = Sema::TUK_Friend; |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1699 | } else { |
| 1700 | // Okay, this is a class definition. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1701 | TUK = Sema::TUK_Definition; |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1702 | } |
Richard Smith | 434516c | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1703 | } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) || |
| 1704 | NextToken().is(tok::kw_alignas))) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1705 | // We can't tell if this is a definition or reference |
| 1706 | // until we skipped the 'final' and C++11 attribute specifiers. |
| 1707 | TentativeParsingAction PA(*this); |
| 1708 | |
| 1709 | // Skip the 'final' keyword. |
| 1710 | ConsumeToken(); |
| 1711 | |
| 1712 | // Skip C++11 attribute specifiers. |
| 1713 | while (true) { |
| 1714 | if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) { |
| 1715 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1716 | if (!SkipUntil(tok::r_square, StopAtSemi)) |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1717 | break; |
Richard Smith | 434516c | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1718 | } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1719 | ConsumeToken(); |
| 1720 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1721 | if (!SkipUntil(tok::r_paren, StopAtSemi)) |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1722 | break; |
| 1723 | } else { |
| 1724 | break; |
| 1725 | } |
| 1726 | } |
| 1727 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1728 | if (Tok.isOneOf(tok::l_brace, tok::colon)) |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1729 | TUK = Sema::TUK_Definition; |
| 1730 | else |
| 1731 | TUK = Sema::TUK_Reference; |
| 1732 | |
| 1733 | PA.Revert(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 1734 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1735 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1736 | (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1737 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1738 | if (Tok.isNot(tok::semi)) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1739 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1740 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 1741 | ExpectAndConsume(tok::semi, diag::err_expected_after, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1742 | DeclSpec::getSpecifierName(TagType, PPol)); |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 1743 | PP.EnterToken(Tok, /*IsReinject*/true); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1744 | Tok.setKind(tok::semi); |
| 1745 | } |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1746 | } else |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1747 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1748 | |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1749 | // Forbid misplaced attributes. In cases of a reference, we pass attributes |
| 1750 | // to caller to handle. |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1751 | if (TUK != Sema::TUK_Reference) { |
| 1752 | // If this is not a reference, then the only possible |
| 1753 | // valid place for C++11 attributes to appear here |
| 1754 | // is between class-key and class-name. If there are |
| 1755 | // any attributes after class-name, we try a fixit to move |
| 1756 | // them to the right place. |
| 1757 | SourceRange AttrRange = Attributes.Range; |
| 1758 | if (AttrRange.isValid()) { |
| 1759 | Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed) |
| 1760 | << AttrRange |
| 1761 | << FixItHint::CreateInsertionFromRange(AttrFixitLoc, |
| 1762 | CharSourceRange(AttrRange, true)) |
| 1763 | << FixItHint::CreateRemoval(AttrRange); |
| 1764 | |
| 1765 | // Recover by adding misplaced attributes to the attribute list |
| 1766 | // of the class so they can be applied on the class later. |
| 1767 | attrs.takeAllFrom(Attributes); |
| 1768 | } |
| 1769 | } |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1770 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1771 | // If this is an elaborated type specifier, and we delayed |
| 1772 | // diagnostics before, just merge them into the current pool. |
| 1773 | if (shouldDelayDiagsInTag) { |
| 1774 | diagsFromTag.done(); |
| 1775 | if (TUK == Sema::TUK_Reference) |
| 1776 | diagsFromTag.redelay(); |
| 1777 | } |
| 1778 | |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1779 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1780 | TUK != Sema::TUK_Definition)) { |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1781 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 1782 | // We have a declaration or reference to an anonymous class. |
| 1783 | Diag(StartLoc, diag::err_anon_type_definition) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1784 | << DeclSpec::getSpecifierName(TagType, Policy); |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1785 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1786 | |
David Majnemer | 3252fd0 | 2013-12-05 01:36:53 +0000 | [diff] [blame] | 1787 | // If we are parsing a definition and stop at a base-clause, continue on |
| 1788 | // until the semicolon. Continuing from the comma will just trick us into |
| 1789 | // thinking we are seeing a variable declaration. |
| 1790 | if (TUK == Sema::TUK_Definition && Tok.is(tok::colon)) |
| 1791 | SkipUntil(tok::semi, StopBeforeMatch); |
| 1792 | else |
| 1793 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1797 | // Create the tag portion of the class or class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1798 | DeclResult TagOrTempResult = true; // invalid |
| 1799 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1801 | bool Owned = false; |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1802 | Sema::SkipBodyInfo SkipBody; |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1803 | if (TemplateId) { |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1804 | // Explicit specialization, class template partial specialization, |
| 1805 | // or explicit instantiation. |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1806 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1807 | TemplateId->NumArgs); |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 1808 | if (TemplateId->isInvalid()) { |
| 1809 | // Can't build the declaration. |
| 1810 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1811 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1812 | // This is an explicit instantiation of a class template. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1813 | ProhibitAttributes(attrs); |
| 1814 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1815 | TagOrTempResult = Actions.ActOnExplicitInstantiation( |
| 1816 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, |
| 1817 | TagType, StartLoc, SS, TemplateId->Template, |
| 1818 | TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, TemplateArgsPtr, |
| 1819 | TemplateId->RAngleLoc, attrs); |
John McCall | b7c5c27 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1820 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1821 | // Friend template-ids are treated as references unless |
| 1822 | // they have template headers, in which case they're ill-formed |
| 1823 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 1824 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1825 | } else if (TUK == Sema::TUK_Reference || |
| 1826 | (TUK == Sema::TUK_Friend && |
John McCall | b7c5c27 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1827 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1828 | ProhibitAttributes(attrs); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1829 | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc, |
Richard Smith | a42fd84 | 2020-01-17 15:42:11 -0800 | [diff] [blame] | 1830 | SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1831 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1832 | TemplateId->Template, |
| 1833 | TemplateId->TemplateNameLoc, |
| 1834 | TemplateId->LAngleLoc, |
| 1835 | TemplateArgsPtr, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1836 | TemplateId->RAngleLoc); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1837 | } else { |
| 1838 | // This is an explicit specialization or a class template |
| 1839 | // partial specialization. |
| 1840 | TemplateParameterLists FakedParamLists; |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1841 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1842 | // This looks like an explicit instantiation, because we have |
| 1843 | // something like |
| 1844 | // |
| 1845 | // template class Foo<X> |
| 1846 | // |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1847 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1848 | // meant to be an explicit specialization, but the user forgot |
| 1849 | // the '<>' after 'template'. |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1850 | // It this is friend declaration however, since it cannot have a |
| 1851 | // template header, it is most likely that the user meant to |
| 1852 | // remove the 'template' keyword. |
Larisse Voufo | b9bbaba | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 1853 | assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) && |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1854 | "Expected a definition here"); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1855 | |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1856 | if (TUK == Sema::TUK_Friend) { |
| 1857 | Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1858 | TemplateParams = nullptr; |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1859 | } else { |
| 1860 | SourceLocation LAngleLoc = |
| 1861 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1862 | Diag(TemplateId->TemplateNameLoc, |
| 1863 | diag::err_explicit_instantiation_with_definition) |
| 1864 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1865 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1866 | |
| 1867 | // Create a fake template parameter list that contains only |
| 1868 | // "template<>", so that we treat this construct as a class |
| 1869 | // template specialization. |
| 1870 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 1871 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1872 | LAngleLoc, nullptr)); |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1873 | TemplateParams = &FakedParamLists; |
| 1874 | } |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | // Build the class template specialization. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1878 | TagOrTempResult = Actions.ActOnClassTemplateSpecialization( |
| 1879 | getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(), |
Richard Smith | a42fd84 | 2020-01-17 15:42:11 -0800 | [diff] [blame] | 1880 | SS, *TemplateId, attrs, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1881 | MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] |
| 1882 | : nullptr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 1883 | TemplateParams ? TemplateParams->size() : 0), |
| 1884 | &SkipBody); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1885 | } |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1886 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1887 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1888 | // Explicit instantiation of a member of a class template |
| 1889 | // specialization, e.g., |
| 1890 | // |
| 1891 | // template struct Outer<int>::Inner; |
| 1892 | // |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1893 | ProhibitAttributes(attrs); |
| 1894 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1895 | TagOrTempResult = Actions.ActOnExplicitInstantiation( |
| 1896 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, |
| 1897 | TagType, StartLoc, SS, Name, NameLoc, attrs); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1898 | } else if (TUK == Sema::TUK_Friend && |
| 1899 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1900 | ProhibitAttributes(attrs); |
| 1901 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1902 | TagOrTempResult = Actions.ActOnTemplatedFriendTag( |
| 1903 | getCurScope(), DS.getFriendSpecLoc(), TagType, StartLoc, SS, Name, |
| 1904 | NameLoc, attrs, |
| 1905 | MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] : nullptr, |
| 1906 | TemplateParams ? TemplateParams->size() : 0)); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1907 | } else { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1908 | if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition) |
| 1909 | ProhibitAttributes(attrs); |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1910 | |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1911 | if (TUK == Sema::TUK_Definition && |
| 1912 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1913 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1914 | // recover by ignoring the 'template' keyword. |
| 1915 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1916 | << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1917 | TemplateParams = nullptr; |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1918 | } |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1919 | |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1920 | bool IsDependent = false; |
| 1921 | |
John McCall | 32723e9 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 1922 | // Don't pass down template parameter lists if this is just a tag |
| 1923 | // reference. For example, we don't need the template parameters here: |
| 1924 | // template <class T> class A *makeA(T t); |
| 1925 | MultiTemplateParamsArg TParams; |
| 1926 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 1927 | TParams = |
| 1928 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 1929 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1930 | stripTypeAttributesOffDeclSpec(attrs, DS, TUK); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1931 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1932 | // Declaration or definition of a class type |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1933 | TagOrTempResult = Actions.ActOnTag( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1934 | getCurScope(), TagType, TUK, StartLoc, SS, Name, NameLoc, attrs, AS, |
| 1935 | DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent, |
| 1936 | SourceLocation(), false, clang::TypeResult(), |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1937 | DSC == DeclSpecContext::DSC_type_specifier, |
| 1938 | DSC == DeclSpecContext::DSC_template_param || |
| 1939 | DSC == DeclSpecContext::DSC_template_type_arg, |
| 1940 | &SkipBody); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1941 | |
| 1942 | // If ActOnTag said the type was dependent, try again with the |
| 1943 | // less common call. |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1944 | if (IsDependent) { |
| 1945 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1946 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1947 | SS, Name, StartLoc, NameLoc); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1948 | } |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1949 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1950 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1951 | // If there is a body, parse it and inform the actions module. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1952 | if (TUK == Sema::TUK_Definition) { |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1953 | assert(Tok.is(tok::l_brace) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1954 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1955 | isCXX11FinalKeyword()); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1956 | if (SkipBody.ShouldSkip) |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 1957 | SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType, |
| 1958 | TagOrTempResult.get()); |
| 1959 | else if (getLangOpts().CPlusPlus) |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1960 | ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType, |
| 1961 | TagOrTempResult.get()); |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 1962 | else { |
| 1963 | Decl *D = |
| 1964 | SkipBody.CheckSameAsPrevious ? SkipBody.New : TagOrTempResult.get(); |
| 1965 | // Parse the definition body. |
| 1966 | ParseStructUnionBody(StartLoc, TagType, D); |
| 1967 | if (SkipBody.CheckSameAsPrevious && |
| 1968 | !Actions.ActOnDuplicateDefinition(DS, TagOrTempResult.get(), |
| 1969 | SkipBody)) { |
| 1970 | DS.SetTypeSpecError(); |
| 1971 | return; |
| 1972 | } |
| 1973 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 1976 | if (!TagOrTempResult.isInvalid()) |
Hiroshi Inoue | 939d932 | 2017-06-30 05:40:31 +0000 | [diff] [blame] | 1977 | // Delayed processing of attributes. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1978 | Actions.ProcessDeclAttributeDelayed(TagOrTempResult.get(), attrs); |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 1979 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1980 | const char *PrevSpec = nullptr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1981 | unsigned DiagID; |
| 1982 | bool Result; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1983 | if (!TypeResult.isInvalid()) { |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1984 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 1985 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1986 | PrevSpec, DiagID, TypeResult.get(), Policy); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1987 | } else if (!TagOrTempResult.isInvalid()) { |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1988 | Result = DS.SetTypeSpecType(TagType, StartLoc, |
| 1989 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1990 | PrevSpec, DiagID, TagOrTempResult.get(), Owned, |
| 1991 | Policy); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1992 | } else { |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1993 | DS.SetTypeSpecError(); |
Anders Carlsson | f83c9fa | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1994 | return; |
| 1995 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1997 | if (Result) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1998 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | cf25141 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 2000 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 2001 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 2002 | // going to look at what comes after it to improve error recovery. If an |
| 2003 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 2004 | // the end of the declaration and recover that way. |
| 2005 | // |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 2006 | // Also enforce C++ [temp]p3: |
| 2007 | // In a template-declaration which defines a class, no declarator |
| 2008 | // is permitted. |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 2009 | // |
| 2010 | // After a type-specifier, we don't expect a semicolon. This only happens in |
| 2011 | // C, since definitions are not permitted in this context in C++. |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 2012 | if (TUK == Sema::TUK_Definition && |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 2013 | (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) && |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 2014 | (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) { |
Argyrios Kyrtzidis | e6f6913 | 2012-12-17 20:10:43 +0000 | [diff] [blame] | 2015 | if (Tok.isNot(tok::semi)) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2016 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2017 | ExpectAndConsume(tok::semi, diag::err_expected_after, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2018 | DeclSpec::getSpecifierName(TagType, PPol)); |
Argyrios Kyrtzidis | e6f6913 | 2012-12-17 20:10:43 +0000 | [diff] [blame] | 2019 | // Push this token back into the preprocessor and change our current token |
| 2020 | // to ';' so that the rest of the code recovers as though there were an |
| 2021 | // ';' after the definition. |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 2022 | PP.EnterToken(Tok, /*IsReinject=*/true); |
Argyrios Kyrtzidis | e6f6913 | 2012-12-17 20:10:43 +0000 | [diff] [blame] | 2023 | Tok.setKind(tok::semi); |
| 2024 | } |
Chris Lattner | cf25141 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 2025 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2029 | /// |
| 2030 | /// base-clause : [C++ class.derived] |
| 2031 | /// ':' base-specifier-list |
| 2032 | /// base-specifier-list: |
| 2033 | /// base-specifier '...'[opt] |
| 2034 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2035 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2036 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 2037 | ConsumeToken(); |
| 2038 | |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2039 | // Build up an array of parsed base specifiers. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2040 | SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2041 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2042 | while (true) { |
| 2043 | // Parse a base-specifier. |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2044 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | f829825 | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 2045 | if (Result.isInvalid()) { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2046 | // Skip the rest of this base specifier, up until the comma or |
| 2047 | // opening brace. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2048 | SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2049 | } else { |
| 2050 | // Add this to our array of base specifiers. |
Douglas Gregor | f829825 | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 2051 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | // If the next token is a comma, consume it and keep reading |
| 2055 | // base-specifiers. |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2056 | if (!TryConsumeToken(tok::comma)) |
| 2057 | break; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2058 | } |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2059 | |
| 2060 | // Attach the base specifiers |
Craig Topper | aa700cb | 2015-12-27 21:55:19 +0000 | [diff] [blame] | 2061 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 2065 | /// one entry in the base class list of a class specifier, for example: |
| 2066 | /// class foo : public bar, virtual private baz { |
| 2067 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 2068 | /// |
| 2069 | /// base-specifier: [C++ class.derived] |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2070 | /// attribute-specifier-seq[opt] base-type-specifier |
| 2071 | /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt] |
| 2072 | /// base-type-specifier |
| 2073 | /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt] |
| 2074 | /// base-type-specifier |
Craig Topper | 9ad7e26 | 2014-10-31 06:57:07 +0000 | [diff] [blame] | 2075 | BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2076 | bool IsVirtual = false; |
| 2077 | SourceLocation StartLoc = Tok.getLocation(); |
| 2078 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2079 | ParsedAttributesWithRange Attributes(AttrFactory); |
| 2080 | MaybeParseCXX11Attributes(Attributes); |
| 2081 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2082 | // Parse the 'virtual' keyword. |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2083 | if (TryConsumeToken(tok::kw_virtual)) |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2084 | IsVirtual = true; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2085 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2086 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 2087 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2088 | // Parse an (optional) access specifier. |
| 2089 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 2090 | if (Access != AS_none) |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2091 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2093 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 2094 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2095 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 2096 | // access specifier. |
| 2097 | if (Tok.is(tok::kw_virtual)) { |
| 2098 | SourceLocation VirtualLoc = ConsumeToken(); |
| 2099 | if (IsVirtual) { |
| 2100 | // Complain about duplicate 'virtual' |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2101 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2102 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | IsVirtual = true; |
| 2106 | } |
| 2107 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2108 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 2109 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2110 | // Parse the class-name. |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 2111 | |
| 2112 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 2113 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 2114 | // classes in <atomic>. Treat '_Atomic' to be an identifier when we are |
| 2115 | // parsing the class-name for a base specifier. |
| 2116 | if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && |
| 2117 | NextToken().is(tok::less)) |
| 2118 | Tok.setKind(tok::identifier); |
| 2119 | |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 2120 | SourceLocation EndLocation; |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 2121 | SourceLocation BaseLoc; |
| 2122 | TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2123 | if (BaseType.isInvalid()) |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2124 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2126 | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2127 | // actually part of the base-specifier-list grammar productions, but we |
| 2128 | // parse it here for convenience. |
| 2129 | SourceLocation EllipsisLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2130 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
| 2131 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | // Find the complete source range for the base-specifier. |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 2133 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2135 | // Notify semantic analysis that we have parsed a complete |
| 2136 | // base-specifier. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2137 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual, |
| 2138 | Access, BaseType.get(), BaseLoc, |
| 2139 | EllipsisLoc); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 2143 | /// a C++ access-specifier. |
| 2144 | /// |
| 2145 | /// access-specifier: [C++ class.derived] |
| 2146 | /// 'private' |
| 2147 | /// 'protected' |
| 2148 | /// 'public' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2150 | switch (Tok.getKind()) { |
| 2151 | default: return AS_none; |
| 2152 | case tok::kw_private: return AS_private; |
| 2153 | case tok::kw_protected: return AS_protected; |
| 2154 | case tok::kw_public: return AS_public; |
| 2155 | } |
| 2156 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2157 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2158 | /// If the given declarator has any parts for which parsing has to be |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2159 | /// delayed, e.g., default arguments or an exception-specification, create a |
| 2160 | /// late-parsed method declaration record to handle the parsing at the end of |
| 2161 | /// the class definition. |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2162 | void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo, |
| 2163 | Decl *ThisDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | DeclaratorChunk::FunctionTypeInfo &FTI |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2165 | = DeclaratorInfo.getFunctionTypeInfo(); |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2166 | // If there was a late-parsed exception-specification, we'll need a |
| 2167 | // late parse |
| 2168 | bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed; |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2169 | |
Nathan Sidwell | 5bb231c | 2015-02-19 14:03:22 +0000 | [diff] [blame] | 2170 | if (!NeedLateParse) { |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2171 | // Look ahead to see if there are any default args |
Nathan Sidwell | 5bb231c | 2015-02-19 14:03:22 +0000 | [diff] [blame] | 2172 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) { |
| 2173 | auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param); |
| 2174 | if (Param->hasUnparsedDefaultArg()) { |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2175 | NeedLateParse = true; |
| 2176 | break; |
| 2177 | } |
Nathan Sidwell | 5bb231c | 2015-02-19 14:03:22 +0000 | [diff] [blame] | 2178 | } |
| 2179 | } |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2180 | |
| 2181 | if (NeedLateParse) { |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2182 | // Push this method onto the stack of late-parsed method |
| 2183 | // declarations. |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2184 | auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2185 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
| 2186 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
| 2187 | |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2188 | // Stash the exception-specification tokens in the late-pased method. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2189 | LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2190 | FTI.ExceptionSpecTokens = nullptr; |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2191 | |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2192 | // Push tokens for each parameter. Those that do not have |
| 2193 | // defaults will be NULL. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2194 | LateMethod->DefaultArgs.reserve(FTI.NumParams); |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2195 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) |
Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 2196 | LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument( |
Malcolm Parsons | ca9d834 | 2016-11-17 21:00:09 +0000 | [diff] [blame] | 2197 | FTI.Params[ParamIdx].Param, |
| 2198 | std::move(FTI.Params[ParamIdx].DefaultArgTokens))); |
Eli Friedman | 3af2a77 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2202 | /// isCXX11VirtSpecifier - Determine whether the given token is a C++11 |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2203 | /// virt-specifier. |
| 2204 | /// |
| 2205 | /// virt-specifier: |
| 2206 | /// override |
| 2207 | /// final |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2208 | /// __final |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2209 | VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const { |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2210 | if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier)) |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 2211 | return VirtSpecifiers::VS_None; |
| 2212 | |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2213 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2214 | |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2215 | // Initialize the contextual keywords. |
| 2216 | if (!Ident_final) { |
| 2217 | Ident_final = &PP.getIdentifierTable().get("final"); |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2218 | if (getLangOpts().GNUKeywords) |
| 2219 | Ident_GNU_final = &PP.getIdentifierTable().get("__final"); |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2220 | if (getLangOpts().MicrosoftExt) |
| 2221 | Ident_sealed = &PP.getIdentifierTable().get("sealed"); |
| 2222 | Ident_override = &PP.getIdentifierTable().get("override"); |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2225 | if (II == Ident_override) |
| 2226 | return VirtSpecifiers::VS_Override; |
| 2227 | |
| 2228 | if (II == Ident_sealed) |
| 2229 | return VirtSpecifiers::VS_Sealed; |
| 2230 | |
| 2231 | if (II == Ident_final) |
| 2232 | return VirtSpecifiers::VS_Final; |
| 2233 | |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2234 | if (II == Ident_GNU_final) |
| 2235 | return VirtSpecifiers::VS_GNU_Final; |
| 2236 | |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2237 | return VirtSpecifiers::VS_None; |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2240 | /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq. |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2241 | /// |
| 2242 | /// virt-specifier-seq: |
| 2243 | /// virt-specifier |
| 2244 | /// virt-specifier-seq virt-specifier |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2245 | void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS, |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2246 | bool IsInterface, |
| 2247 | SourceLocation FriendLoc) { |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2248 | while (true) { |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2249 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2250 | if (Specifier == VirtSpecifiers::VS_None) |
| 2251 | return; |
| 2252 | |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2253 | if (FriendLoc.isValid()) { |
| 2254 | Diag(Tok.getLocation(), diag::err_friend_decl_spec) |
| 2255 | << VirtSpecifiers::getSpecifierName(Specifier) |
| 2256 | << FixItHint::CreateRemoval(Tok.getLocation()) |
| 2257 | << SourceRange(FriendLoc, FriendLoc); |
| 2258 | ConsumeToken(); |
| 2259 | continue; |
| 2260 | } |
| 2261 | |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2262 | // C++ [class.mem]p8: |
| 2263 | // A virt-specifier-seq shall contain at most one of each virt-specifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2264 | const char *PrevSpec = nullptr; |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 2265 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2266 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
| 2267 | << PrevSpec |
| 2268 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2269 | |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2270 | if (IsInterface && (Specifier == VirtSpecifiers::VS_Final || |
| 2271 | Specifier == VirtSpecifiers::VS_Sealed)) { |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2272 | Diag(Tok.getLocation(), diag::err_override_control_interface) |
| 2273 | << VirtSpecifiers::getSpecifierName(Specifier); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2274 | } else if (Specifier == VirtSpecifiers::VS_Sealed) { |
| 2275 | Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword); |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2276 | } else if (Specifier == VirtSpecifiers::VS_GNU_Final) { |
| 2277 | Diag(Tok.getLocation(), diag::ext_warn_gnu_final); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2278 | } else { |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2279 | Diag(Tok.getLocation(), |
| 2280 | getLangOpts().CPlusPlus11 |
| 2281 | ? diag::warn_cxx98_compat_override_control_keyword |
| 2282 | : diag::ext_override_control_keyword) |
| 2283 | << VirtSpecifiers::getSpecifierName(Specifier); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2284 | } |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2285 | ConsumeToken(); |
| 2286 | } |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2289 | /// isCXX11FinalKeyword - Determine whether the next token is a C++11 |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2290 | /// 'final' or Microsoft 'sealed' contextual keyword. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2291 | bool Parser::isCXX11FinalKeyword() const { |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2292 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
| 2293 | return Specifier == VirtSpecifiers::VS_Final || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2294 | Specifier == VirtSpecifiers::VS_GNU_Final || |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2295 | Specifier == VirtSpecifiers::VS_Sealed; |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2298 | /// Parse a C++ member-declarator up to, but not including, the optional |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2299 | /// brace-or-equal-initializer or pure-specifier. |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2300 | bool Parser::ParseCXXMemberDeclaratorBeforeInitializer( |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2301 | Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize, |
| 2302 | LateParsedAttrList &LateParsedAttrs) { |
| 2303 | // member-declarator: |
| 2304 | // declarator pure-specifier[opt] |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2305 | // declarator requires-clause |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2306 | // declarator brace-or-equal-initializer[opt] |
| 2307 | // identifier[opt] ':' constant-expression |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 2308 | if (Tok.isNot(tok::colon)) |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2309 | ParseDeclarator(DeclaratorInfo); |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2310 | else |
| 2311 | DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2312 | |
| 2313 | if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) { |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2314 | assert(DeclaratorInfo.isPastIdentifier() && |
| 2315 | "don't know where identifier would go yet?"); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2316 | BitfieldSize = ParseConstantExpression(); |
| 2317 | if (BitfieldSize.isInvalid()) |
| 2318 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2319 | } else if (Tok.is(tok::kw_requires)) { |
| 2320 | ParseTrailingRequiresClause(DeclaratorInfo); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2321 | } else { |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2322 | ParseOptionalCXX11VirtSpecifierSeq( |
| 2323 | VS, getCurrentClass().IsInterface, |
| 2324 | DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2325 | if (!VS.isUnset()) |
| 2326 | MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS); |
| 2327 | } |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2328 | |
| 2329 | // If a simple-asm-expr is present, parse it. |
| 2330 | if (Tok.is(tok::kw_asm)) { |
| 2331 | SourceLocation Loc; |
Aaron Ballman | 55a51e1 | 2020-01-08 08:38:02 -0500 | [diff] [blame] | 2332 | ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc)); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2333 | if (AsmLabel.isInvalid()) |
| 2334 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
| 2335 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2336 | DeclaratorInfo.setAsmLabel(AsmLabel.get()); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2337 | DeclaratorInfo.SetRangeEnd(Loc); |
| 2338 | } |
| 2339 | |
| 2340 | // If attributes exist after the declarator, but before an '{', parse them. |
| 2341 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
Richard Smith | 4b5a949 | 2014-01-24 22:34:35 +0000 | [diff] [blame] | 2342 | |
| 2343 | // For compatibility with code written to older Clang, also accept a |
| 2344 | // virt-specifier *after* the GNU attributes. |
Aaron Ballman | 5d153e3 | 2014-08-04 17:03:51 +0000 | [diff] [blame] | 2345 | if (BitfieldSize.isUnset() && VS.isUnset()) { |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2346 | ParseOptionalCXX11VirtSpecifierSeq( |
| 2347 | VS, getCurrentClass().IsInterface, |
| 2348 | DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); |
Aaron Ballman | 5d153e3 | 2014-08-04 17:03:51 +0000 | [diff] [blame] | 2349 | if (!VS.isUnset()) { |
| 2350 | // If we saw any GNU-style attributes that are known to GCC followed by a |
| 2351 | // virt-specifier, issue a GCC-compat warning. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2352 | for (const ParsedAttr &AL : DeclaratorInfo.getAttributes()) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2353 | if (AL.isKnownToGCC() && !AL.isCXX11Attribute()) |
| 2354 | Diag(AL.getLoc(), diag::warn_gcc_attribute_location); |
| 2355 | |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2356 | MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS); |
Aaron Ballman | 5d153e3 | 2014-08-04 17:03:51 +0000 | [diff] [blame] | 2357 | } |
| 2358 | } |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2359 | |
| 2360 | // If this has neither a name nor a bit width, something has gone seriously |
| 2361 | // wrong. Skip until the semi-colon or }. |
| 2362 | if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) { |
| 2363 | // If so, skip until the semi-colon or a }. |
| 2364 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 2365 | return true; |
| 2366 | } |
| 2367 | return false; |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2370 | /// Look for declaration specifiers possibly occurring after C++11 |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2371 | /// virt-specifier-seq and diagnose them. |
| 2372 | void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq( |
| 2373 | Declarator &D, |
| 2374 | VirtSpecifiers &VS) { |
| 2375 | DeclSpec DS(AttrFactory); |
| 2376 | |
| 2377 | // GNU-style and C++11 attributes are not allowed here, but they will be |
| 2378 | // handled by the caller. Diagnose everything else. |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 2379 | ParseTypeQualifierListOpt( |
| 2380 | DS, AR_NoAttributesParsed, false, |
| 2381 | /*IdentifierRequired=*/false, llvm::function_ref<void()>([&]() { |
| 2382 | Actions.CodeCompleteFunctionQualifiers(DS, D, &VS); |
| 2383 | })); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2384 | D.ExtendWithDeclSpec(DS); |
| 2385 | |
| 2386 | if (D.isFunctionDeclarator()) { |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 2387 | auto &Function = D.getFunctionTypeInfo(); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2388 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 2389 | auto DeclSpecCheck = [&](DeclSpec::TQ TypeQual, StringRef FixItName, |
| 2390 | SourceLocation SpecLoc) { |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2391 | FixItHint Insertion; |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 2392 | auto &MQ = Function.getOrCreateMethodQualifiers(); |
| 2393 | if (!(MQ.getTypeQualifiers() & TypeQual)) { |
| 2394 | std::string Name(FixItName.data()); |
| 2395 | Name += " "; |
| 2396 | Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name); |
| 2397 | MQ.SetTypeQual(TypeQual, SpecLoc); |
| 2398 | } |
| 2399 | Diag(SpecLoc, diag::err_declspec_after_virtspec) |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2400 | << FixItName |
| 2401 | << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 2402 | << FixItHint::CreateRemoval(SpecLoc) << Insertion; |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2403 | }; |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 2404 | DS.forEachQualifier(DeclSpecCheck); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2405 | } |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 2406 | |
| 2407 | // Parse ref-qualifiers. |
| 2408 | bool RefQualifierIsLValueRef = true; |
| 2409 | SourceLocation RefQualifierLoc; |
| 2410 | if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) { |
| 2411 | const char *Name = (RefQualifierIsLValueRef ? "& " : "&& "); |
| 2412 | FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name); |
| 2413 | Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef; |
| 2414 | Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding(); |
| 2415 | |
| 2416 | Diag(RefQualifierLoc, diag::err_declspec_after_virtspec) |
| 2417 | << (RefQualifierIsLValueRef ? "&" : "&&") |
| 2418 | << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) |
| 2419 | << FixItHint::CreateRemoval(RefQualifierLoc) |
| 2420 | << Insertion; |
| 2421 | D.SetRangeEnd(RefQualifierLoc); |
| 2422 | } |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2426 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 2427 | /// |
| 2428 | /// member-declaration: |
| 2429 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 2430 | /// function-definition ';'[opt] |
| 2431 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 2432 | /// using-declaration [TODO] |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 2433 | /// [C++0x] static_assert-declaration |
Anders Carlsson | dfbbdf6 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 2434 | /// template-declaration |
Chris Lattner | d19c1c0 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2435 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2436 | /// |
| 2437 | /// member-declarator-list: |
| 2438 | /// member-declarator |
| 2439 | /// member-declarator-list ',' member-declarator |
| 2440 | /// |
| 2441 | /// member-declarator: |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2442 | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2443 | /// [C++2a] declarator requires-clause |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2444 | /// declarator constant-initializer[opt] |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2445 | /// [C++11] declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2446 | /// identifier[opt] ':' constant-expression |
| 2447 | /// |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2448 | /// virt-specifier-seq: |
| 2449 | /// virt-specifier |
| 2450 | /// virt-specifier-seq virt-specifier |
| 2451 | /// |
| 2452 | /// virt-specifier: |
| 2453 | /// override |
| 2454 | /// final |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2455 | /// [MS] sealed |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2456 | /// |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2457 | /// pure-specifier: |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2458 | /// '= 0' |
| 2459 | /// |
| 2460 | /// constant-initializer: |
| 2461 | /// '=' constant-expression |
| 2462 | /// |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2463 | Parser::DeclGroupPtrTy |
| 2464 | Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2465 | ParsedAttributes &AccessAttrs, |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2466 | const ParsedTemplateInfo &TemplateInfo, |
| 2467 | ParsingDeclRAIIObject *TemplateDiags) { |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2468 | if (Tok.is(tok::at)) { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2469 | if (getLangOpts().ObjC && NextToken().isObjCAtKeyword(tok::objc_defs)) |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2470 | Diag(Tok, diag::err_at_defs_cxx); |
| 2471 | else |
| 2472 | Diag(Tok, diag::err_at_in_class); |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2473 | |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2474 | ConsumeToken(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2475 | SkipUntil(tok::r_brace, StopAtSemi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2476 | return nullptr; |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2477 | } |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2478 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 2479 | // Turn on colon protection early, while parsing declspec, although there is |
| 2480 | // nothing to protect there. It prevents from false errors if error recovery |
| 2481 | // incorrectly determines where the declspec ends, as in the example: |
| 2482 | // struct A { enum class B { C }; }; |
| 2483 | // const int C = 4; |
| 2484 | // struct D { A::B : C; }; |
| 2485 | ColonProtectionRAIIObject X(*this); |
| 2486 | |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2487 | // Access declarations. |
Richard Smith | 45855df | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2488 | bool MalformedTypeSpec = false; |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2489 | if (!TemplateInfo.Kind && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2490 | Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) { |
Richard Smith | 45855df | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2491 | if (TryAnnotateCXXScopeToken()) |
| 2492 | MalformedTypeSpec = true; |
| 2493 | |
| 2494 | bool isAccessDecl; |
| 2495 | if (Tok.isNot(tok::annot_cxxscope)) |
| 2496 | isAccessDecl = false; |
| 2497 | else if (NextToken().is(tok::identifier)) |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2498 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 2499 | else |
| 2500 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 2501 | |
| 2502 | if (isAccessDecl) { |
| 2503 | // Collect the scope specifier token we annotated earlier. |
| 2504 | CXXScopeSpec SS; |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 2505 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
| 2506 | /*ObjectHadErrors=*/false, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2507 | /*EnteringContext=*/false); |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2508 | |
Nico Weber | ef03e70 | 2014-09-10 00:59:37 +0000 | [diff] [blame] | 2509 | if (SS.isInvalid()) { |
| 2510 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2511 | return nullptr; |
Nico Weber | ef03e70 | 2014-09-10 00:59:37 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2514 | // Try to parse an unqualified-id. |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2515 | SourceLocation TemplateKWLoc; |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2516 | UnqualifiedId Name; |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 2517 | if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, |
| 2518 | /*ObjectHadErrors=*/false, false, true, true, |
| 2519 | false, &TemplateKWLoc, Name)) { |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2520 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2521 | return nullptr; |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | // TODO: recover from mistakenly-qualified operator declarations. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2525 | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
| 2526 | "access declaration")) { |
| 2527 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2528 | return nullptr; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2529 | } |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2530 | |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2531 | // FIXME: We should do something with the 'template' keyword here. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2532 | return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration( |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2533 | getCurScope(), AS, /*UsingLoc*/ SourceLocation(), |
| 2534 | /*TypenameLoc*/ SourceLocation(), SS, Name, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2535 | /*EllipsisLoc*/ SourceLocation(), |
| 2536 | /*AttrList*/ ParsedAttributesView()))); |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
Aaron Ballman | e7c544d | 2014-08-04 20:28:35 +0000 | [diff] [blame] | 2540 | // static_assert-declaration. A templated static_assert declaration is |
| 2541 | // diagnosed in Parser::ParseSingleDeclarationAfterTemplate. |
| 2542 | if (!TemplateInfo.Kind && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2543 | Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) { |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 2544 | SourceLocation DeclEnd; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2545 | return DeclGroupPtrTy::make( |
| 2546 | DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd))); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2547 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2549 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2551 | "Nested template improperly parsed?"); |
Richard Smith | 3af7009 | 2017-02-09 22:14:25 +0000 | [diff] [blame] | 2552 | ObjCDeclContextSwitch ObjCDC(*this); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 2553 | SourceLocation DeclEnd; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2554 | return DeclGroupPtrTy::make( |
Richard Smith | 3af7009 | 2017-02-09 22:14:25 +0000 | [diff] [blame] | 2555 | DeclGroupRef(ParseTemplateDeclarationOrSpecialization( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2556 | DeclaratorContext::MemberContext, DeclEnd, AccessAttrs, AS))); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2557 | } |
Anders Carlsson | dfbbdf6 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 2558 | |
Chris Lattner | d19c1c0 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2559 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 2560 | if (Tok.is(tok::kw___extension__)) { |
| 2561 | // __extension__ silences extension warnings in the subexpression. |
| 2562 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 2563 | ConsumeToken(); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2564 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs, |
| 2565 | TemplateInfo, TemplateDiags); |
Chris Lattner | d19c1c0 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2566 | } |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2567 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2568 | ParsedAttributesWithRange attrs(AttrFactory); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2569 | ParsedAttributesViewWithRange FnAttrs; |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2570 | // Optional C++11 attribute-specifier |
| 2571 | MaybeParseCXX11Attributes(attrs); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2572 | // We need to keep these attributes for future diagnostic |
| 2573 | // before they are taken over by declaration specifier. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2574 | FnAttrs.addAll(attrs.begin(), attrs.end()); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2575 | FnAttrs.Range = attrs.Range; |
| 2576 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2577 | MaybeParseMicrosoftAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2579 | if (Tok.is(tok::kw_using)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2580 | ProhibitAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2582 | // Eat 'using'. |
| 2583 | SourceLocation UsingLoc = ConsumeToken(); |
| 2584 | |
Richard Trieu | 2efd305 | 2019-05-01 23:33:49 +0000 | [diff] [blame] | 2585 | // Consume unexpected 'template' keywords. |
| 2586 | while (Tok.is(tok::kw_template)) { |
| 2587 | SourceLocation TemplateLoc = ConsumeToken(); |
| 2588 | Diag(TemplateLoc, diag::err_unexpected_template_after_using) |
| 2589 | << FixItHint::CreateRemoval(TemplateLoc); |
| 2590 | } |
| 2591 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2592 | if (Tok.is(tok::kw_namespace)) { |
| 2593 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2594 | SkipUntil(tok::semi, StopBeforeMatch); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2595 | return nullptr; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2596 | } |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2597 | SourceLocation DeclEnd; |
| 2598 | // Otherwise, it must be a using-declaration or an alias-declaration. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2599 | return ParseUsingDeclaration(DeclaratorContext::MemberContext, TemplateInfo, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 2600 | UsingLoc, DeclEnd, AS); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2603 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2604 | LateParsedAttrList CommonLateParsedAttrs; |
| 2605 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2606 | // decl-specifier-seq: |
| 2607 | // Parse the common declaration-specifiers piece. |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2608 | ParsingDeclSpec DS(*this, TemplateDiags); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2609 | DS.takeAttributesFrom(attrs); |
Richard Smith | 45855df | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2610 | if (MalformedTypeSpec) |
| 2611 | DS.SetTypeSpecError(); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2612 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2613 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DeclSpecContext::DSC_class, |
| 2614 | &CommonLateParsedAttrs); |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 2615 | |
| 2616 | // Turn off colon protection that was set for declspec. |
| 2617 | X.restore(); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2618 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2619 | // If we had a free-standing type definition with a missing semicolon, we |
| 2620 | // may get this far before the problem becomes obvious. |
| 2621 | if (DS.hasTagDefinition() && |
| 2622 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate && |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2623 | DiagnoseMissingSemiAfterTagDefinition(DS, AS, DeclSpecContext::DSC_class, |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2624 | &CommonLateParsedAttrs)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2625 | return nullptr; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2626 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2627 | MultiTemplateParamsArg TemplateParams( |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2628 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() |
| 2629 | : nullptr, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 2630 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 2631 | |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 2632 | if (TryConsumeToken(tok::semi)) { |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2633 | if (DS.isFriendSpecified()) |
| 2634 | ProhibitAttributes(FnAttrs); |
| 2635 | |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 2636 | RecordDecl *AnonRecord = nullptr; |
| 2637 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec( |
| 2638 | getCurScope(), AS, DS, TemplateParams, false, AnonRecord); |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2639 | DS.complete(TheDecl); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 2640 | if (AnonRecord) { |
| 2641 | Decl* decls[] = {AnonRecord, TheDecl}; |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2642 | return Actions.BuildDeclaratorGroup(decls); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 2643 | } |
| 2644 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2645 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2646 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2647 | ParsingDeclarator DeclaratorInfo(*this, DS, DeclaratorContext::MemberContext); |
Saar Raz | b481f02 | 2020-01-22 02:03:05 +0200 | [diff] [blame] | 2648 | if (TemplateInfo.TemplateParams) |
| 2649 | DeclaratorInfo.setTemplateParameterLists(TemplateParams); |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2650 | VirtSpecifiers VS; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2651 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2652 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2653 | LateParsedAttrList LateParsedAttrs; |
| 2654 | |
Douglas Gregor | 50cefbf | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2655 | SourceLocation EqualLoc; |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2656 | SourceLocation PureSpecLoc; |
| 2657 | |
Yaron Keren | 180c167 | 2015-06-30 07:35:19 +0000 | [diff] [blame] | 2658 | auto TryConsumePureSpecifier = [&] (bool AllowDefinition) { |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2659 | if (Tok.isNot(tok::equal)) |
| 2660 | return false; |
| 2661 | |
| 2662 | auto &Zero = NextToken(); |
| 2663 | SmallString<8> Buffer; |
Richard Smith | ced7617 | 2020-03-20 18:44:13 -0700 | [diff] [blame] | 2664 | if (Zero.isNot(tok::numeric_constant) || |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2665 | PP.getSpelling(Zero, Buffer) != "0") |
| 2666 | return false; |
| 2667 | |
| 2668 | auto &After = GetLookAheadToken(2); |
| 2669 | if (!After.isOneOf(tok::semi, tok::comma) && |
| 2670 | !(AllowDefinition && |
| 2671 | After.isOneOf(tok::l_brace, tok::colon, tok::kw_try))) |
| 2672 | return false; |
| 2673 | |
| 2674 | EqualLoc = ConsumeToken(); |
| 2675 | PureSpecLoc = ConsumeToken(); |
| 2676 | return true; |
| 2677 | }; |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2678 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2679 | SmallVector<Decl *, 8> DeclsInGroup; |
| 2680 | ExprResult BitfieldSize; |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2681 | ExprResult TrailingRequiresClause; |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2682 | bool ExpectSemi = true; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2683 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2684 | // Parse the first declarator. |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2685 | if (ParseCXXMemberDeclaratorBeforeInitializer( |
| 2686 | DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) { |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2687 | TryConsumeToken(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2688 | return nullptr; |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2689 | } |
John Thompson | 5bc5cbe | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 2690 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2691 | // Check for a member function definition. |
Richard Smith | 4b5a949 | 2014-01-24 22:34:35 +0000 | [diff] [blame] | 2692 | if (BitfieldSize.isUnset()) { |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2693 | // MSVC permits pure specifier on inline functions defined at class scope. |
Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2694 | // Hence check for =0 before checking for function definition. |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2695 | if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction()) |
| 2696 | TryConsumePureSpecifier(/*AllowDefinition*/ true); |
Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2697 | |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2698 | FunctionDefinitionKind DefinitionKind = FDK_Declaration; |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2699 | // function-definition: |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2700 | // |
| 2701 | // In C++11, a non-function declarator followed by an open brace is a |
| 2702 | // braced-init-list for an in-class member initialization, not an |
| 2703 | // erroneous function definition. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2704 | if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2705 | DefinitionKind = FDK_Definition; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2706 | } else if (DeclaratorInfo.isFunctionDeclarator()) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2707 | if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) { |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2708 | DefinitionKind = FDK_Definition; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2709 | } else if (Tok.is(tok::equal)) { |
| 2710 | const Token &KW = NextToken(); |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2711 | if (KW.is(tok::kw_default)) |
| 2712 | DefinitionKind = FDK_Defaulted; |
| 2713 | else if (KW.is(tok::kw_delete)) |
| 2714 | DefinitionKind = FDK_Deleted; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2715 | } |
| 2716 | } |
Eli Bendersky | 4184222 | 2015-03-23 23:49:41 +0000 | [diff] [blame] | 2717 | DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2718 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2719 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2720 | // to a friend declaration, that declaration shall be a definition. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2721 | if (DeclaratorInfo.isFunctionDeclarator() && |
Richard Smith | 5ae6554 | 2020-01-30 17:42:17 -0800 | [diff] [blame] | 2722 | DefinitionKind == FDK_Declaration && DS.isFriendSpecified()) { |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2723 | // Diagnose attributes that appear before decl specifier: |
| 2724 | // [[]] friend int foo(); |
| 2725 | ProhibitAttributes(FnAttrs); |
| 2726 | } |
| 2727 | |
Nico Weber | a7f137d | 2015-01-16 19:35:01 +0000 | [diff] [blame] | 2728 | if (DefinitionKind != FDK_Declaration) { |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2729 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
Richard Trieu | 0d73054 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2730 | Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params); |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2731 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2732 | SkipUntil(tok::r_brace); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | 8a4db83 | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2734 | // Consume the optional ';' |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 2735 | TryConsumeToken(tok::semi); |
| 2736 | |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2737 | return nullptr; |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2738 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2739 | |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2740 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Richard Trieu | 0d73054 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2741 | Diag(DeclaratorInfo.getIdentifierLoc(), |
| 2742 | diag::err_function_declared_typedef); |
Douglas Gregor | 8a4db83 | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2743 | |
Richard Smith | 2603b09 | 2012-11-15 22:54:20 +0000 | [diff] [blame] | 2744 | // Recover by treating the 'typedef' as spurious. |
| 2745 | DS.ClearStorageClassSpecs(); |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2748 | Decl *FunDecl = |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2749 | ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo, |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2750 | VS, PureSpecLoc); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2751 | |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2752 | if (FunDecl) { |
| 2753 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) { |
| 2754 | CommonLateParsedAttrs[i]->addDecl(FunDecl); |
| 2755 | } |
| 2756 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) { |
| 2757 | LateParsedAttrs[i]->addDecl(FunDecl); |
| 2758 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2759 | } |
| 2760 | LateParsedAttrs.clear(); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2761 | |
| 2762 | // Consume the ';' - it's optional unless we have a delete or default |
Richard Trieu | 2f7dc46 | 2012-05-16 19:04:59 +0000 | [diff] [blame] | 2763 | if (Tok.is(tok::semi)) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 2764 | ConsumeExtraSemi(AfterMemberFunctionDefinition); |
Douglas Gregor | 8a4db83 | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2765 | |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2766 | return DeclGroupPtrTy::make(DeclGroupRef(FunDecl)); |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2767 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | // member-declarator-list: |
| 2771 | // member-declarator |
| 2772 | // member-declarator-list ',' member-declarator |
| 2773 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2774 | while (1) { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2775 | InClassInitStyle HasInClassInit = ICIS_NoInit; |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2776 | bool HasStaticInitializer = false; |
| 2777 | if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) { |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 2778 | if (DeclaratorInfo.isDeclarationOfFunction()) { |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2779 | // It's a pure-specifier. |
| 2780 | if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false)) |
| 2781 | // Parse it as an expression so that Sema can diagnose it. |
| 2782 | HasStaticInitializer = true; |
| 2783 | } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2784 | DeclSpec::SCS_static && |
| 2785 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2786 | DeclSpec::SCS_typedef && |
| 2787 | !DS.isFriendSpecified()) { |
| 2788 | // It's a default member initializer. |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 2789 | if (BitfieldSize.get()) |
Aaron Ballman | 6a30894 | 2020-04-21 15:37:19 -0400 | [diff] [blame] | 2790 | Diag(Tok, getLangOpts().CPlusPlus20 |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 2791 | ? diag::warn_cxx17_compat_bitfield_member_init |
| 2792 | : diag::ext_bitfield_member_init); |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2793 | HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2794 | } else { |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2795 | HasStaticInitializer = true; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2799 | // NOTE: If Sema is the Action module and declarator is an instance field, |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2800 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2801 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2802 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2803 | NamedDecl *ThisDecl = nullptr; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2804 | if (DS.isFriendSpecified()) { |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2805 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2806 | // to a friend declaration, that declaration shall be a definition. |
| 2807 | // |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2808 | // Diagnose attributes that appear in a friend member function declarator: |
| 2809 | // friend int foo [[]] (); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2810 | SmallVector<SourceRange, 4> Ranges; |
| 2811 | DeclaratorInfo.getCXX11AttributeRanges(Ranges); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2812 | for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(), |
| 2813 | E = Ranges.end(); I != E; ++I) |
| 2814 | Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I; |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2816 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2817 | TemplateParams); |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2818 | } else { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2819 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2820 | DeclaratorInfo, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2821 | TemplateParams, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2822 | BitfieldSize.get(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2823 | VS, HasInClassInit); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2824 | |
| 2825 | if (VarTemplateDecl *VT = |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2826 | ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2827 | // Re-direct this decl to refer to the templated decl so that we can |
| 2828 | // initialize it. |
| 2829 | ThisDecl = VT->getTemplatedDecl(); |
| 2830 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2831 | if (ThisDecl) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 2832 | Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs); |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2833 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2834 | |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2835 | // Error recovery might have converted a non-static member into a static |
| 2836 | // member. |
David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2837 | if (HasInClassInit != ICIS_NoInit && |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2838 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() == |
| 2839 | DeclSpec::SCS_static) { |
| 2840 | HasInClassInit = ICIS_NoInit; |
| 2841 | HasStaticInitializer = true; |
| 2842 | } |
| 2843 | |
| 2844 | if (ThisDecl && PureSpecLoc.isValid()) |
| 2845 | Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc); |
| 2846 | |
| 2847 | // Handle the initializer. |
| 2848 | if (HasInClassInit != ICIS_NoInit) { |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2849 | // The initializer was deferred; parse it and cache the tokens. |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2850 | Diag(Tok, getLangOpts().CPlusPlus11 |
| 2851 | ? diag::warn_cxx98_compat_nonstatic_member_init |
| 2852 | : diag::ext_nonstatic_member_init); |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2853 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2854 | if (DeclaratorInfo.isArrayOfUnknownBound()) { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2855 | // C++11 [dcl.array]p3: An array bound may also be omitted when the |
| 2856 | // declarator is followed by an initializer. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2857 | // |
| 2858 | // A brace-or-equal-initializer for a member-declarator is not an |
David Blaikie | cdd91db | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2859 | // initializer in the grammar, so this is ill-formed. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2860 | Diag(Tok, diag::err_incomplete_array_member_init); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2861 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2862 | |
| 2863 | // Avoid later warnings about a class member of incomplete type. |
David Blaikie | cdd91db | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2864 | if (ThisDecl) |
David Blaikie | cdd91db | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2865 | ThisDecl->setInvalidDecl(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2866 | } else |
| 2867 | ParseCXXNonStaticMemberInitializer(ThisDecl); |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2868 | } else if (HasStaticInitializer) { |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2869 | // Normal initializer. |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2870 | ExprResult Init = ParseCXXMemberInitializer( |
| 2871 | ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2872 | |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2873 | if (Init.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2874 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2875 | else if (ThisDecl) |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2876 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid()); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2877 | } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2878 | // No initializer. |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2879 | Actions.ActOnUninitializedDecl(ThisDecl); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2880 | |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2881 | if (ThisDecl) { |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2882 | if (!ThisDecl->isInvalidDecl()) { |
| 2883 | // Set the Decl for any late parsed attributes |
| 2884 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) |
| 2885 | CommonLateParsedAttrs[i]->addDecl(ThisDecl); |
| 2886 | |
| 2887 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) |
| 2888 | LateParsedAttrs[i]->addDecl(ThisDecl); |
| 2889 | } |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2890 | Actions.FinalizeDeclaration(ThisDecl); |
| 2891 | DeclsInGroup.push_back(ThisDecl); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2892 | |
| 2893 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 2894 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2895 | DeclSpec::SCS_typedef) |
| 2896 | HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2897 | } |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2898 | LateParsedAttrs.clear(); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2899 | |
| 2900 | DeclaratorInfo.complete(ThisDecl); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2901 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2902 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2903 | // or an error, bail out. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 2904 | SourceLocation CommaLoc; |
| 2905 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2906 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2908 | if (Tok.isAtStartOfLine() && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2909 | !MightBeDeclarator(DeclaratorContext::MemberContext)) { |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2910 | // This comma was followed by a line-break and something which can't be |
| 2911 | // the start of a declarator. The comma was probably a typo for a |
| 2912 | // semicolon. |
| 2913 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2914 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2915 | ExpectSemi = false; |
| 2916 | break; |
| 2917 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2919 | // Parse the next declarator. |
| 2920 | DeclaratorInfo.clear(); |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2921 | VS.clear(); |
Nico Weber | f56c85b | 2015-01-17 02:26:40 +0000 | [diff] [blame] | 2922 | BitfieldSize = ExprResult(/*Invalid=*/false); |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2923 | EqualLoc = PureSpecLoc = SourceLocation(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2924 | DeclaratorInfo.setCommaLoc(CommaLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2926 | // GNU attributes are allowed before the second and subsequent declarator. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2927 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2928 | |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2929 | if (ParseCXXMemberDeclaratorBeforeInitializer( |
| 2930 | DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) |
| 2931 | break; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2934 | if (ExpectSemi && |
| 2935 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
Chris Lattner | 916dbf1 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2936 | // Skip to end of block or statement. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2937 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 916dbf1 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2938 | // If we stopped at a ';', eat it. |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 2939 | TryConsumeToken(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2940 | return nullptr; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2943 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2946 | /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer. |
| 2947 | /// Also detect and reject any attempted defaulted/deleted function definition. |
| 2948 | /// The location of the '=', if any, will be placed in EqualLoc. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2949 | /// |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2950 | /// This does not check for a pure-specifier; that's handled elsewhere. |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2951 | /// |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2952 | /// brace-or-equal-initializer: |
| 2953 | /// '=' initializer-expression |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2954 | /// braced-init-list |
| 2955 | /// |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2956 | /// initializer-clause: |
| 2957 | /// assignment-expression |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2958 | /// braced-init-list |
| 2959 | /// |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2960 | /// defaulted/deleted function-definition: |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2961 | /// '=' 'default' |
| 2962 | /// '=' 'delete' |
| 2963 | /// |
| 2964 | /// Prior to C++0x, the assignment-expression in an initializer-clause must |
| 2965 | /// be a constant-expression. |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2966 | ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2967 | SourceLocation &EqualLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2968 | assert(Tok.isOneOf(tok::equal, tok::l_brace) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2969 | && "Data member initializer not starting with '=' or '{'"); |
| 2970 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2971 | EnterExpressionEvaluationContext Context( |
| 2972 | Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, D); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 2973 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2974 | if (Tok.is(tok::kw_delete)) { |
| 2975 | // In principle, an initializer of '= delete p;' is legal, but it will |
| 2976 | // never type-check. It's better to diagnose it as an ill-formed expression |
| 2977 | // than as an ill-formed deleted non-function member. |
| 2978 | // An initializer of '= delete p, foo' will never be parsed, because |
| 2979 | // a top-level comma always ends the initializer expression. |
| 2980 | const Token &Next = NextToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2981 | if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2982 | if (IsFunction) |
| 2983 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2984 | << 1 /* delete */; |
| 2985 | else |
| 2986 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Richard Smith | edcb26e | 2014-06-11 00:49:52 +0000 | [diff] [blame] | 2987 | return ExprError(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2988 | } |
| 2989 | } else if (Tok.is(tok::kw_default)) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2990 | if (IsFunction) |
| 2991 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
| 2992 | << 0 /* default */; |
| 2993 | else |
Richard Smith | d052a578 | 2019-10-22 17:44:08 -0700 | [diff] [blame] | 2994 | Diag(ConsumeToken(), diag::err_default_special_members) |
Aaron Ballman | 6a30894 | 2020-04-21 15:37:19 -0400 | [diff] [blame] | 2995 | << getLangOpts().CPlusPlus20; |
Richard Smith | edcb26e | 2014-06-11 00:49:52 +0000 | [diff] [blame] | 2996 | return ExprError(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2997 | } |
David Majnemer | 87ff66c | 2014-12-13 11:34:16 +0000 | [diff] [blame] | 2998 | } |
| 2999 | if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) { |
| 3000 | Diag(Tok, diag::err_ms_property_initializer) << PD; |
| 3001 | return ExprError(); |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3002 | } |
| 3003 | return ParseInitializer(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 3006 | void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc, |
| 3007 | SourceLocation AttrFixitLoc, |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3008 | unsigned TagType, Decl *TagDecl) { |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 3009 | // Skip the optional 'final' keyword. |
| 3010 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
| 3011 | assert(isCXX11FinalKeyword() && "not a class definition"); |
| 3012 | ConsumeToken(); |
| 3013 | |
| 3014 | // Diagnose any C++11 attributes after 'final' keyword. |
| 3015 | // We deliberately discard these attributes. |
| 3016 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3017 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
| 3018 | |
| 3019 | // This can only happen if we had malformed misplaced attributes; |
| 3020 | // we only get called if there is a colon or left-brace after the |
| 3021 | // attributes. |
| 3022 | if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace)) |
| 3023 | return; |
| 3024 | } |
| 3025 | |
| 3026 | // Skip the base clauses. This requires actually parsing them, because |
| 3027 | // otherwise we can't be sure where they end (a left brace may appear |
| 3028 | // within a template argument). |
| 3029 | if (Tok.is(tok::colon)) { |
| 3030 | // Enter the scope of the class so that we can correctly parse its bases. |
| 3031 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
| 3032 | ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true, |
| 3033 | TagType == DeclSpec::TST_interface); |
Richard Smith | 0f192e8 | 2015-06-11 22:48:25 +0000 | [diff] [blame] | 3034 | auto OldContext = |
| 3035 | Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 3036 | |
| 3037 | // Parse the bases but don't attach them to the class. |
| 3038 | ParseBaseClause(nullptr); |
| 3039 | |
Richard Smith | 0f192e8 | 2015-06-11 22:48:25 +0000 | [diff] [blame] | 3040 | Actions.ActOnTagFinishSkippedDefinition(OldContext); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 3041 | |
| 3042 | if (!Tok.is(tok::l_brace)) { |
| 3043 | Diag(PP.getLocForEndOfToken(PrevTokLocation), |
| 3044 | diag::err_expected_lbrace_after_base_specifiers); |
| 3045 | return; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | // Skip the body. |
| 3050 | assert(Tok.is(tok::l_brace)); |
| 3051 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3052 | T.consumeOpen(); |
| 3053 | T.skipToEnd(); |
Richard Smith | 04c6c1f | 2015-07-01 18:56:50 +0000 | [diff] [blame] | 3054 | |
| 3055 | // Parse and discard any trailing attributes. |
| 3056 | ParsedAttributes Attrs(AttrFactory); |
| 3057 | if (Tok.is(tok::kw___attribute)) |
| 3058 | MaybeParseGNUAttributes(Attrs); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3061 | Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas( |
| 3062 | AccessSpecifier &AS, ParsedAttributesWithRange &AccessAttrs, |
| 3063 | DeclSpec::TST TagType, Decl *TagDecl) { |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 3064 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 3065 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3066 | switch (Tok.getKind()) { |
| 3067 | case tok::kw___if_exists: |
| 3068 | case tok::kw___if_not_exists: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3069 | ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, AS); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3070 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3071 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3072 | case tok::semi: |
| 3073 | // Check for extraneous top-level semicolon. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3074 | ConsumeExtraSemi(InsideStruct, TagType); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3075 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3076 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3077 | // Handle pragmas that can appear as member declarations. |
| 3078 | case tok::annot_pragma_vis: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3079 | HandlePragmaVisibility(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3080 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3081 | case tok::annot_pragma_pack: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3082 | HandlePragmaPack(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3083 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3084 | case tok::annot_pragma_align: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3085 | HandlePragmaAlign(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3086 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3087 | case tok::annot_pragma_ms_pointers_to_members: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3088 | HandlePragmaMSPointersToMembers(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3089 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3090 | case tok::annot_pragma_ms_pragma: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3091 | HandlePragmaMSPragma(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3092 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3093 | case tok::annot_pragma_ms_vtordisp: |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 3094 | HandlePragmaMSVtorDisp(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3095 | return nullptr; |
Richard Smith | b256d30 | 2017-01-28 01:20:57 +0000 | [diff] [blame] | 3096 | case tok::annot_pragma_dump: |
| 3097 | HandlePragmaDump(); |
| 3098 | return nullptr; |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 3099 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3100 | case tok::kw_namespace: |
| 3101 | // If we see a namespace here, a close brace was missing somewhere. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3102 | DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl)); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3103 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3104 | |
Anastasia Stulova | 948e37c | 2019-03-25 11:54:02 +0000 | [diff] [blame] | 3105 | case tok::kw_private: |
| 3106 | // FIXME: We don't accept GNU attributes on access specifiers in OpenCL mode |
| 3107 | // yet. |
| 3108 | if (getLangOpts().OpenCL && !NextToken().is(tok::colon)) |
| 3109 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs); |
| 3110 | LLVM_FALLTHROUGH; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3111 | case tok::kw_public: |
Anastasia Stulova | 948e37c | 2019-03-25 11:54:02 +0000 | [diff] [blame] | 3112 | case tok::kw_protected: { |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3113 | AccessSpecifier NewAS = getAccessSpecifierIfPresent(); |
| 3114 | assert(NewAS != AS_none); |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3115 | // Current token is a C++ access specifier. |
| 3116 | AS = NewAS; |
| 3117 | SourceLocation ASLoc = Tok.getLocation(); |
| 3118 | unsigned TokLength = Tok.getLength(); |
| 3119 | ConsumeToken(); |
| 3120 | AccessAttrs.clear(); |
| 3121 | MaybeParseGNUAttributes(AccessAttrs); |
| 3122 | |
| 3123 | SourceLocation EndLoc; |
| 3124 | if (TryConsumeToken(tok::colon, EndLoc)) { |
| 3125 | } else if (TryConsumeToken(tok::semi, EndLoc)) { |
| 3126 | Diag(EndLoc, diag::err_expected) |
| 3127 | << tok::colon << FixItHint::CreateReplacement(EndLoc, ":"); |
| 3128 | } else { |
| 3129 | EndLoc = ASLoc.getLocWithOffset(TokLength); |
| 3130 | Diag(EndLoc, diag::err_expected) |
| 3131 | << tok::colon << FixItHint::CreateInsertion(EndLoc, ":"); |
| 3132 | } |
| 3133 | |
| 3134 | // The Microsoft extension __interface does not permit non-public |
| 3135 | // access specifiers. |
| 3136 | if (TagType == DeclSpec::TST_interface && AS != AS_public) { |
| 3137 | Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected); |
| 3138 | } |
| 3139 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3140 | if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc, AccessAttrs)) { |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3141 | // found another attribute than only annotations |
| 3142 | AccessAttrs.clear(); |
| 3143 | } |
| 3144 | |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3145 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3148 | case tok::annot_pragma_openmp: |
Alexey Bataev | c972f6f | 2020-01-07 13:39:18 -0500 | [diff] [blame] | 3149 | return ParseOpenMPDeclarativeDirectiveWithExtDecl( |
| 3150 | AS, AccessAttrs, /*Delayed=*/true, TagType, TagDecl); |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3151 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3152 | default: |
Serge Pavlov | 037861b | 2019-08-04 10:08:51 +0000 | [diff] [blame] | 3153 | if (tok::isPragmaAnnotation(Tok.getKind())) { |
| 3154 | Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl) |
| 3155 | << DeclSpec::getSpecifierName(TagType, |
| 3156 | Actions.getASTContext().getPrintingPolicy()); |
| 3157 | ConsumeAnnotationToken(); |
| 3158 | return nullptr; |
| 3159 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3160 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs); |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3161 | } |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3164 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 3165 | /// |
| 3166 | /// member-specification: |
| 3167 | /// member-declaration member-specification[opt] |
| 3168 | /// access-specifier ':' member-specification[opt] |
| 3169 | /// |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 3170 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 3171 | SourceLocation AttrFixitLoc, |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 3172 | ParsedAttributesWithRange &Attrs, |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3173 | unsigned TagType, Decl *TagDecl) { |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 3174 | assert((TagType == DeclSpec::TST_struct || |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3175 | TagType == DeclSpec::TST_interface || |
| 3176 | TagType == DeclSpec::TST_union || |
| 3177 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 3178 | |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 3179 | llvm::TimeTraceScope TimeScope("ParseClass", [&]() { |
| 3180 | if (auto *TD = dyn_cast_or_null<NamedDecl>(TagDecl)) |
| 3181 | return TD->getQualifiedNameAsString(); |
| 3182 | return std::string("<anonymous>"); |
| 3183 | }); |
| 3184 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 3185 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3186 | "parsing struct/union/class body"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3188 | // Determine whether this is a non-nested class. Note that local |
| 3189 | // classes are *not* considered to be nested classes. |
| 3190 | bool NonNestedClass = true; |
| 3191 | if (!ClassStack.empty()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3192 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3193 | if (S->isClassScope()) { |
| 3194 | // We're inside a class scope, so this is a nested class. |
| 3195 | NonNestedClass = false; |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3196 | |
| 3197 | // The Microsoft extension __interface does not permit nested classes. |
| 3198 | if (getCurrentClass().IsInterface) { |
| 3199 | Diag(RecordLoc, diag::err_invalid_member_in_interface) |
| 3200 | << /*ErrorType=*/6 |
| 3201 | << (isa<NamedDecl>(TagDecl) |
| 3202 | ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString() |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 3203 | : "(anonymous)"); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3204 | } |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3205 | break; |
| 3206 | } |
| 3207 | |
Serge Pavlov | d9c0bcf | 2015-07-14 10:02:10 +0000 | [diff] [blame] | 3208 | if ((S->getFlags() & Scope::FnScope)) |
| 3209 | // If we're in a function or function template then this is a local |
| 3210 | // class rather than a nested class. |
| 3211 | break; |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3212 | } |
| 3213 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3214 | |
| 3215 | // Enter a scope for the class. |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3216 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3217 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3218 | // Note that we are parsing a new (potentially-nested) class definition. |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3219 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass, |
| 3220 | TagType == DeclSpec::TST_interface); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3221 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3222 | if (TagDecl) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3223 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3224 | |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3225 | SourceLocation FinalLoc; |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3226 | bool IsFinalSpelledSealed = false; |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3227 | |
| 3228 | // Parse the optional 'final' keyword. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3229 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3230 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok); |
| 3231 | assert((Specifier == VirtSpecifiers::VS_Final || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3232 | Specifier == VirtSpecifiers::VS_GNU_Final || |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3233 | Specifier == VirtSpecifiers::VS_Sealed) && |
| 3234 | "not a class definition"); |
Richard Smith | da26111 | 2011-10-15 04:21:46 +0000 | [diff] [blame] | 3235 | FinalLoc = ConsumeToken(); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3236 | IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed; |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3237 | |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3238 | if (TagType == DeclSpec::TST_interface) |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3239 | Diag(FinalLoc, diag::err_override_control_interface) |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3240 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 3241 | else if (Specifier == VirtSpecifiers::VS_Final) |
| 3242 | Diag(FinalLoc, getLangOpts().CPlusPlus11 |
| 3243 | ? diag::warn_cxx98_compat_override_control_keyword |
| 3244 | : diag::ext_override_control_keyword) |
| 3245 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 3246 | else if (Specifier == VirtSpecifiers::VS_Sealed) |
| 3247 | Diag(FinalLoc, diag::ext_ms_sealed_keyword); |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 3248 | else if (Specifier == VirtSpecifiers::VS_GNU_Final) |
| 3249 | Diag(FinalLoc, diag::ext_warn_gnu_final); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3250 | |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 3251 | // Parse any C++11 attributes after 'final' keyword. |
| 3252 | // These attributes are not allowed to appear here, |
| 3253 | // and the only possible place for them to appertain |
| 3254 | // to the class would be between class-key and class-name. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 3255 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
Nico Weber | 4b4be84 | 2014-12-29 06:56:50 +0000 | [diff] [blame] | 3256 | |
| 3257 | // ParseClassSpecifier() does only a superficial check for attributes before |
| 3258 | // deciding to call this method. For example, for |
| 3259 | // `class C final alignas ([l) {` it will decide that this looks like a |
| 3260 | // misplaced attribute since it sees `alignas '(' ')'`. But the actual |
| 3261 | // attribute parsing code will try to parse the '[' as a constexpr lambda |
| 3262 | // and consume enough tokens that the alignas parsing code will eat the |
| 3263 | // opening '{'. So bail out if the next token isn't one we expect. |
Nico Weber | 36de3a2 | 2014-12-29 21:56:22 +0000 | [diff] [blame] | 3264 | if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) { |
| 3265 | if (TagDecl) |
| 3266 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
Nico Weber | 4b4be84 | 2014-12-29 06:56:50 +0000 | [diff] [blame] | 3267 | return; |
Nico Weber | 36de3a2 | 2014-12-29 21:56:22 +0000 | [diff] [blame] | 3268 | } |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3269 | } |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 3270 | |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3271 | if (Tok.is(tok::colon)) { |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 3272 | ParseScope InheritanceScope(this, getCurScope()->getFlags() | |
| 3273 | Scope::ClassInheritanceScope); |
| 3274 | |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3275 | ParseBaseClause(TagDecl); |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3276 | if (!Tok.is(tok::l_brace)) { |
Ismail Pazarbasi | 129c44c | 2014-09-25 21:13:02 +0000 | [diff] [blame] | 3277 | bool SuggestFixIt = false; |
| 3278 | SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3279 | if (Tok.isAtStartOfLine()) { |
| 3280 | switch (Tok.getKind()) { |
| 3281 | case tok::kw_private: |
| 3282 | case tok::kw_protected: |
| 3283 | case tok::kw_public: |
| 3284 | SuggestFixIt = NextToken().getKind() == tok::colon; |
| 3285 | break; |
| 3286 | case tok::kw_static_assert: |
| 3287 | case tok::r_brace: |
| 3288 | case tok::kw_using: |
| 3289 | // base-clause can have simple-template-id; 'template' can't be there |
| 3290 | case tok::kw_template: |
| 3291 | SuggestFixIt = true; |
| 3292 | break; |
| 3293 | case tok::identifier: |
| 3294 | SuggestFixIt = isConstructorDeclarator(true); |
| 3295 | break; |
| 3296 | default: |
| 3297 | SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); |
| 3298 | break; |
| 3299 | } |
| 3300 | } |
| 3301 | DiagnosticBuilder LBraceDiag = |
| 3302 | Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers); |
| 3303 | if (SuggestFixIt) { |
| 3304 | LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {"); |
| 3305 | // Try recovering from missing { after base-clause. |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 3306 | PP.EnterToken(Tok, /*IsReinject*/true); |
Ismail Pazarbasi | 129c44c | 2014-09-25 21:13:02 +0000 | [diff] [blame] | 3307 | Tok.setKind(tok::l_brace); |
| 3308 | } else { |
| 3309 | if (TagDecl) |
| 3310 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
| 3311 | return; |
| 3312 | } |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | assert(Tok.is(tok::l_brace)); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3317 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3318 | T.consumeOpen(); |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3319 | |
John McCall | 08bede4 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 3320 | if (TagDecl) |
Anders Carlsson | 30f2944 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 3321 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3322 | IsFinalSpelledSealed, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3323 | T.getOpenLocation()); |
John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 3324 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3325 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 3326 | // by default. Members of a class defined with the keywords struct or union |
| 3327 | // are public by default. |
| 3328 | AccessSpecifier CurAS; |
| 3329 | if (TagType == DeclSpec::TST_class) |
| 3330 | CurAS = AS_private; |
| 3331 | else |
| 3332 | CurAS = AS_public; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3333 | ParsedAttributesWithRange AccessAttrs(AttrFactory); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3335 | if (TagDecl) { |
| 3336 | // While we still have something to read, read the member-declarations. |
Richard Smith | 752ada8 | 2015-11-17 23:32:01 +0000 | [diff] [blame] | 3337 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 3338 | Tok.isNot(tok::eof)) { |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3339 | // Each iteration of this loop reads one member-declaration. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3340 | ParseCXXClassMemberDeclarationWithPragmas( |
| 3341 | CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl); |
Richard Smith | 6163aa9 | 2020-04-05 23:10:08 -0700 | [diff] [blame] | 3342 | MaybeDestroyTemplateIds(); |
Serge Pavlov | c4e04a2 | 2015-09-19 05:32:57 +0000 | [diff] [blame] | 3343 | } |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3344 | T.consumeClose(); |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3345 | } else { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3346 | SkipUntil(tok::r_brace); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3347 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3349 | // If attributes exist after class contents, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3350 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3351 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3352 | |
John McCall | 08bede4 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 3353 | if (TagDecl) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3354 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3355 | T.getOpenLocation(), |
| 3356 | T.getCloseLocation(), attrs); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 3358 | // C++11 [class.mem]p2: |
| 3359 | // Within the class member-specification, the class is regarded as complete |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3360 | // within function bodies, default arguments, exception-specifications, and |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 3361 | // brace-or-equal-initializers for non-static data members (including such |
| 3362 | // things in nested classes). |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3363 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3364 | // We are not inside a nested class. This class and its nested classes |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3365 | // are complete and we can parse the delayed portions of method |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 3366 | // declarations and the lexed inline method definitions, along with any |
| 3367 | // delayed attributes. |
Melanie Blower | f5360d4 | 2020-05-01 10:32:06 -0700 | [diff] [blame] | 3368 | |
| 3369 | // Save the state of Sema.FPFeatures, and change the setting |
| 3370 | // to the levels specified on the command line. Previous level |
| 3371 | // will be restored when the RAII object is destroyed. |
| 3372 | Sema::FPFeaturesStateRAII SaveFPFeaturesState(Actions); |
| 3373 | FPOptions fpOptions(getLangOpts()); |
| 3374 | Actions.CurFPFeatures.getFromOpaqueInt(fpOptions.getAsOpaqueInt()); |
| 3375 | |
Douglas Gregor | 428119e | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 3376 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Alexey Bataev | c972f6f | 2020-01-07 13:39:18 -0500 | [diff] [blame] | 3377 | ParseLexedPragmas(getCurrentClass()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 3378 | ParseLexedAttributes(getCurrentClass()); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3379 | ParseLexedMethodDeclarations(getCurrentClass()); |
Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 3380 | |
| 3381 | // We've finished with all pending member declarations. |
| 3382 | Actions.ActOnFinishCXXMemberDecls(); |
| 3383 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3384 | ParseLexedMemberInitializers(getCurrentClass()); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3385 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | 428119e | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 3386 | PrevTokLocation = SavedPrevTokLocation; |
Reid Kleckner | bba3cb9 | 2015-03-17 19:00:50 +0000 | [diff] [blame] | 3387 | |
| 3388 | // We've finished parsing everything, including default argument |
| 3389 | // initializers. |
Hans Wennborg | 92ce2af | 2019-12-02 16:25:23 +0100 | [diff] [blame] | 3390 | Actions.ActOnFinishCXXNonNestedClass(); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
John McCall | 08bede4 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 3393 | if (TagDecl) |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3394 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); |
John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 3395 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3396 | // Leave the class scope. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3397 | ParsingDef.Pop(); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 3398 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3399 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3400 | |
Richard Smith | 2ac43ad | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 3401 | void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) { |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3402 | assert(Tok.is(tok::kw_namespace)); |
| 3403 | |
| 3404 | // FIXME: Suggest where the close brace should have gone by looking |
| 3405 | // at indentation changes within the definition body. |
Richard Smith | 2ac43ad | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 3406 | Diag(D->getLocation(), |
| 3407 | diag::err_missing_end_of_definition) << D; |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3408 | Diag(Tok.getLocation(), |
Richard Smith | 2ac43ad | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 3409 | diag::note_missing_end_of_definition_before) << D; |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3410 | |
| 3411 | // Push '};' onto the token stream to recover. |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 3412 | PP.EnterToken(Tok, /*IsReinject*/ true); |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3413 | |
| 3414 | Tok.startToken(); |
| 3415 | Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation)); |
| 3416 | Tok.setKind(tok::semi); |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 3417 | PP.EnterToken(Tok, /*IsReinject*/ true); |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3418 | |
| 3419 | Tok.setKind(tok::r_brace); |
| 3420 | } |
| 3421 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3422 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 3423 | /// which explicitly initializes the members or base classes of a |
| 3424 | /// class (C++ [class.base.init]). For example, the three initializers |
| 3425 | /// after the ':' in the Derived constructor below: |
| 3426 | /// |
| 3427 | /// @code |
| 3428 | /// class Base { }; |
| 3429 | /// class Derived : Base { |
| 3430 | /// int x; |
| 3431 | /// float f; |
| 3432 | /// public: |
| 3433 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 3434 | /// }; |
| 3435 | /// @endcode |
| 3436 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | /// [C++] ctor-initializer: |
| 3438 | /// ':' mem-initializer-list |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3439 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3440 | /// [C++] mem-initializer-list: |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3441 | /// mem-initializer ...[opt] |
| 3442 | /// mem-initializer ...[opt] , mem-initializer-list |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3443 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Nico Weber | 3b00fdc | 2015-03-07 19:52:39 +0000 | [diff] [blame] | 3444 | assert(Tok.is(tok::colon) && |
| 3445 | "Constructor initializer always starts with ':'"); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3446 | |
Nico Weber | 3b00fdc | 2015-03-07 19:52:39 +0000 | [diff] [blame] | 3447 | // Poison the SEH identifiers so they are flagged as illegal in constructor |
| 3448 | // initializers. |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3449 | PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3450 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3452 | SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3453 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3455 | do { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3456 | if (Tok.is(tok::code_completion)) { |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 3457 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 3458 | MemInitializers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3459 | return cutOffParsing(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3460 | } |
Alexey Bataev | 79de17d | 2016-01-20 05:25:51 +0000 | [diff] [blame] | 3461 | |
| 3462 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 3463 | if (!MemInit.isInvalid()) |
| 3464 | MemInitializers.push_back(MemInit.get()); |
| 3465 | else |
| 3466 | AnyErrors = true; |
| 3467 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3468 | if (Tok.is(tok::comma)) |
| 3469 | ConsumeToken(); |
| 3470 | else if (Tok.is(tok::l_brace)) |
| 3471 | break; |
Alexey Bataev | 79de17d | 2016-01-20 05:25:51 +0000 | [diff] [blame] | 3472 | // If the previous initializer was valid and the next token looks like a |
| 3473 | // base or member initializer, assume that we're just missing a comma. |
| 3474 | else if (!MemInit.isInvalid() && |
| 3475 | Tok.isOneOf(tok::identifier, tok::coloncolon)) { |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3476 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3477 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 3478 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3479 | } else { |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3480 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Alexey Bataev | 79de17d | 2016-01-20 05:25:51 +0000 | [diff] [blame] | 3481 | if (!MemInit.isInvalid()) |
| 3482 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace |
| 3483 | << tok::comma; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3484 | SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3485 | break; |
| 3486 | } |
| 3487 | } while (true); |
| 3488 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3489 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3490 | AnyErrors); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 3494 | /// part of a constructor initializer that explicitly initializes one |
| 3495 | /// member or base class (C++ [class.base.init]). See |
| 3496 | /// ParseConstructorInitializer for an example. |
| 3497 | /// |
| 3498 | /// [C++] mem-initializer: |
| 3499 | /// mem-initializer-id '(' expression-list[opt] ')' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3500 | /// [C++0x] mem-initializer-id braced-init-list |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3501 | /// |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3502 | /// [C++] mem-initializer-id: |
| 3503 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 3504 | /// identifier |
Craig Topper | 9ad7e26 | 2014-10-31 06:57:07 +0000 | [diff] [blame] | 3505 | MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 3506 | // parse '::'[opt] nested-name-specifier[opt] |
| 3507 | CXXScopeSpec SS; |
Haojian Wu | 0dd0b10 | 2020-03-19 09:12:29 +0100 | [diff] [blame] | 3508 | if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, |
| 3509 | /*ObjectHadErrors=*/false, |
| 3510 | /*EnteringContext=*/false)) |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 3511 | return true; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3512 | |
| 3513 | // : identifier |
| 3514 | IdentifierInfo *II = nullptr; |
| 3515 | SourceLocation IdLoc = Tok.getLocation(); |
| 3516 | // : declype(...) |
| 3517 | DeclSpec DS(AttrFactory); |
| 3518 | // : template_name<...> |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3519 | TypeResult TemplateTypeTy; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3520 | |
| 3521 | if (Tok.is(tok::identifier)) { |
| 3522 | // Get the identifier. This may be a member name or a class name, |
| 3523 | // but we'll let the semantic analysis determine which it is. |
| 3524 | II = Tok.getIdentifierInfo(); |
| 3525 | ConsumeToken(); |
| 3526 | } else if (Tok.is(tok::annot_decltype)) { |
| 3527 | // Get the decltype expression, if there is one. |
| 3528 | // Uses of decltype will already have been converted to annot_decltype by |
| 3529 | // ParseOptionalCXXScopeSpecifier at this point. |
| 3530 | // FIXME: Can we get here with a scope specifier? |
| 3531 | ParseDecltypeSpecifier(DS); |
| 3532 | } else { |
| 3533 | TemplateIdAnnotation *TemplateId = Tok.is(tok::annot_template_id) |
| 3534 | ? takeTemplateIdAnnotation(Tok) |
| 3535 | : nullptr; |
Richard Smith | b3f6e3d | 2020-03-27 17:08:26 -0700 | [diff] [blame] | 3536 | if (TemplateId && TemplateId->mightBeType()) { |
Richard Smith | a42fd84 | 2020-01-17 15:42:11 -0800 | [diff] [blame] | 3537 | AnnotateTemplateIdTokenAsType(SS, /*IsClassName*/true); |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 3538 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3539 | TemplateTypeTy = getTypeAnnotation(Tok); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3540 | ConsumeAnnotationToken(); |
| 3541 | } else { |
| 3542 | Diag(Tok, diag::err_expected_member_or_base_name); |
| 3543 | return true; |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 3544 | } |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 3545 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3546 | |
| 3547 | // Parse the '('. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3548 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3549 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 3550 | |
Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 3551 | // FIXME: Add support for signature help inside initializer lists. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3552 | ExprResult InitList = ParseBraceInitializer(); |
| 3553 | if (InitList.isInvalid()) |
| 3554 | return true; |
| 3555 | |
| 3556 | SourceLocation EllipsisLoc; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 3557 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3558 | |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3559 | if (TemplateTypeTy.isInvalid()) |
| 3560 | return true; |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3561 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3562 | TemplateTypeTy.get(), DS, IdLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3563 | InitList.get(), EllipsisLoc); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3564 | } else if(Tok.is(tok::l_paren)) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3565 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3566 | T.consumeOpen(); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3567 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3568 | // Parse the optional expression-list. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3569 | ExprVector ArgExprs; |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3570 | CommaLocsTy CommaLocs; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 3571 | auto RunSignatureHelp = [&] { |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3572 | if (TemplateTypeTy.isInvalid()) |
| 3573 | return QualType(); |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 3574 | QualType PreferredType = Actions.ProduceCtorInitMemberSignatureHelp( |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3575 | getCurScope(), ConstructorDecl, SS, TemplateTypeTy.get(), ArgExprs, II, |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 3576 | T.getOpenLocation()); |
| 3577 | CalledSignatureHelp = true; |
| 3578 | return PreferredType; |
| 3579 | }; |
Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 3580 | if (Tok.isNot(tok::r_paren) && |
| 3581 | ParseExpressionList(ArgExprs, CommaLocs, [&] { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 3582 | PreferredType.enterFunctionArgument(Tok.getLocation(), |
| 3583 | RunSignatureHelp); |
Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 3584 | })) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 3585 | if (PP.isCodeCompletionReached() && !CalledSignatureHelp) |
| 3586 | RunSignatureHelp(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3587 | SkipUntil(tok::r_paren, StopAtSemi); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3588 | return true; |
| 3589 | } |
| 3590 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3591 | T.consumeClose(); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3592 | |
| 3593 | SourceLocation EllipsisLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 3594 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3595 | |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3596 | if (TemplateTypeTy.isInvalid()) |
| 3597 | return true; |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3598 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3599 | TemplateTypeTy.get(), DS, IdLoc, |
Dmitri Gribenko | 139474d | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 3600 | T.getOpenLocation(), ArgExprs, |
| 3601 | T.getCloseLocation(), EllipsisLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Richard Smith | 3308732 | 2020-03-30 17:19:30 -0700 | [diff] [blame] | 3604 | if (TemplateTypeTy.isInvalid()) |
| 3605 | return true; |
| 3606 | |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3607 | if (getLangOpts().CPlusPlus11) |
| 3608 | return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace; |
| 3609 | else |
| 3610 | return Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3611 | } |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3612 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3613 | /// Parse a C++ exception-specification if present (C++0x [except.spec]). |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3614 | /// |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3615 | /// exception-specification: |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3616 | /// dynamic-exception-specification |
| 3617 | /// noexcept-specification |
| 3618 | /// |
| 3619 | /// noexcept-specification: |
| 3620 | /// 'noexcept' |
| 3621 | /// 'noexcept' '(' constant-expression ')' |
| 3622 | ExceptionSpecificationType |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3623 | Parser::tryParseExceptionSpecification(bool Delayed, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 3624 | SourceRange &SpecificationRange, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3625 | SmallVectorImpl<ParsedType> &DynamicExceptions, |
| 3626 | SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3627 | ExprResult &NoexceptExpr, |
| 3628 | CachedTokens *&ExceptionSpecTokens) { |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3629 | ExceptionSpecificationType Result = EST_None; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 3630 | ExceptionSpecTokens = nullptr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3631 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3632 | // Handle delayed parsing of exception-specifications. |
| 3633 | if (Delayed) { |
| 3634 | if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept)) |
| 3635 | return EST_None; |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3636 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3637 | // Consume and cache the starting token. |
| 3638 | bool IsNoexcept = Tok.is(tok::kw_noexcept); |
| 3639 | Token StartTok = Tok; |
| 3640 | SpecificationRange = SourceRange(ConsumeToken()); |
| 3641 | |
| 3642 | // Check for a '('. |
| 3643 | if (!Tok.is(tok::l_paren)) { |
| 3644 | // If this is a bare 'noexcept', we're done. |
| 3645 | if (IsNoexcept) { |
| 3646 | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 3647 | NoexceptExpr = nullptr; |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3648 | return EST_BasicNoexcept; |
| 3649 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3650 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3651 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 3652 | return EST_DynamicNone; |
| 3653 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3654 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3655 | // Cache the tokens for the exception-specification. |
| 3656 | ExceptionSpecTokens = new CachedTokens; |
| 3657 | ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept' |
| 3658 | ExceptionSpecTokens->push_back(Tok); // '(' |
| 3659 | SpecificationRange.setEnd(ConsumeParen()); // '(' |
Richard Smith | b1c217e | 2015-01-13 02:24:58 +0000 | [diff] [blame] | 3660 | |
| 3661 | ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens, |
| 3662 | /*StopAtSemi=*/true, |
| 3663 | /*ConsumeFinalToken=*/true); |
Aaron Ballman | 580ccaf | 2016-01-12 21:04:22 +0000 | [diff] [blame] | 3664 | SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation()); |
| 3665 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3666 | return EST_Unparsed; |
| 3667 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3668 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3669 | // See if there's a dynamic specification. |
| 3670 | if (Tok.is(tok::kw_throw)) { |
| 3671 | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
| 3672 | DynamicExceptions, |
| 3673 | DynamicExceptionRanges); |
| 3674 | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
| 3675 | "Produced different number of exception types and ranges."); |
| 3676 | } |
| 3677 | |
| 3678 | // If there's no noexcept specification, we're done. |
| 3679 | if (Tok.isNot(tok::kw_noexcept)) |
| 3680 | return Result; |
| 3681 | |
Richard Smith | b15c11c | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 3682 | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
| 3683 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3684 | // If we already had a dynamic specification, parse the noexcept for, |
| 3685 | // recovery, but emit a diagnostic and don't store the results. |
| 3686 | SourceRange NoexceptRange; |
| 3687 | ExceptionSpecificationType NoexceptType = EST_None; |
| 3688 | |
| 3689 | SourceLocation KeywordLoc = ConsumeToken(); |
| 3690 | if (Tok.is(tok::l_paren)) { |
| 3691 | // There is an argument. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3692 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3693 | T.consumeOpen(); |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3694 | NoexceptExpr = ParseConstantExpression(); |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3695 | T.consumeClose(); |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3696 | if (!NoexceptExpr.isInvalid()) { |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3697 | NoexceptExpr = Actions.ActOnNoexceptSpec(KeywordLoc, NoexceptExpr.get(), |
| 3698 | NoexceptType); |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3699 | NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation()); |
| 3700 | } else { |
Malcolm Parsons | a3220ce | 2017-01-12 16:11:28 +0000 | [diff] [blame] | 3701 | NoexceptType = EST_BasicNoexcept; |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3702 | } |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3703 | } else { |
| 3704 | // There is no argument. |
| 3705 | NoexceptType = EST_BasicNoexcept; |
| 3706 | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
| 3707 | } |
| 3708 | |
| 3709 | if (Result == EST_None) { |
| 3710 | SpecificationRange = NoexceptRange; |
| 3711 | Result = NoexceptType; |
| 3712 | |
| 3713 | // If there's a dynamic specification after a noexcept specification, |
| 3714 | // parse that and ignore the results. |
| 3715 | if (Tok.is(tok::kw_throw)) { |
| 3716 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 3717 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
| 3718 | DynamicExceptionRanges); |
| 3719 | } |
| 3720 | } else { |
| 3721 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 3722 | } |
| 3723 | |
| 3724 | return Result; |
| 3725 | } |
| 3726 | |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3727 | static void diagnoseDynamicExceptionSpecification( |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 3728 | Parser &P, SourceRange Range, bool IsNoexcept) { |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3729 | if (P.getLangOpts().CPlusPlus11) { |
| 3730 | const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)"; |
Richard Smith | 82da19d | 2016-12-08 02:49:07 +0000 | [diff] [blame] | 3731 | P.Diag(Range.getBegin(), |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3732 | P.getLangOpts().CPlusPlus17 && !IsNoexcept |
Richard Smith | 82da19d | 2016-12-08 02:49:07 +0000 | [diff] [blame] | 3733 | ? diag::ext_dynamic_exception_spec |
| 3734 | : diag::warn_exception_spec_deprecated) |
| 3735 | << Range; |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3736 | P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated) |
| 3737 | << Replacement << FixItHint::CreateReplacement(Range, Replacement); |
| 3738 | } |
| 3739 | } |
| 3740 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3741 | /// ParseDynamicExceptionSpecification - Parse a C++ |
| 3742 | /// dynamic-exception-specification (C++ [except.spec]). |
| 3743 | /// |
| 3744 | /// dynamic-exception-specification: |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3745 | /// 'throw' '(' type-id-list [opt] ')' |
| 3746 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | /// |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3748 | /// type-id-list: |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3749 | /// type-id ... [opt] |
| 3750 | /// type-id-list ',' type-id ... [opt] |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3751 | /// |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3752 | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
| 3753 | SourceRange &SpecificationRange, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3754 | SmallVectorImpl<ParsedType> &Exceptions, |
| 3755 | SmallVectorImpl<SourceRange> &Ranges) { |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3756 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3758 | SpecificationRange.setBegin(ConsumeToken()); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3759 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3760 | if (T.consumeOpen()) { |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3761 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 3762 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3763 | return EST_DynamicNone; |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3764 | } |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3765 | |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3766 | // Parse throw(...), a Microsoft extension that means "this function |
| 3767 | // can throw anything". |
| 3768 | if (Tok.is(tok::ellipsis)) { |
| 3769 | SourceLocation EllipsisLoc = ConsumeToken(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3770 | if (!getLangOpts().MicrosoftExt) |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3771 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3772 | T.consumeClose(); |
| 3773 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3774 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3775 | return EST_MSAny; |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3778 | // Parse the sequence of type-ids. |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3779 | SourceRange Range; |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3780 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3781 | TypeResult Res(ParseTypeName(&Range)); |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3782 | |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3783 | if (Tok.is(tok::ellipsis)) { |
| 3784 | // C++0x [temp.variadic]p5: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3785 | // - In a dynamic-exception-specification (15.4); the pattern is a |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3786 | // type-id. |
| 3787 | SourceLocation Ellipsis = ConsumeToken(); |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3788 | Range.setEnd(Ellipsis); |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3789 | if (!Res.isInvalid()) |
| 3790 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
| 3791 | } |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3792 | |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3793 | if (!Res.isInvalid()) { |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3794 | Exceptions.push_back(Res.get()); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3795 | Ranges.push_back(Range); |
| 3796 | } |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 3797 | |
| 3798 | if (!TryConsumeToken(tok::comma)) |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | } |
| 3801 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3802 | T.consumeClose(); |
| 3803 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3804 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, |
| 3805 | Exceptions.empty()); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3806 | return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic; |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3807 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3808 | |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3809 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 3810 | /// function declaration. |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 3811 | TypeResult Parser::ParseTrailingReturnType(SourceRange &Range, |
| 3812 | bool MayBeFollowedByDirectInit) { |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3813 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 3814 | |
| 3815 | ConsumeToken(); |
| 3816 | |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 3817 | return ParseTypeName(&Range, MayBeFollowedByDirectInit |
| 3818 | ? DeclaratorContext::TrailingReturnVarContext |
| 3819 | : DeclaratorContext::TrailingReturnContext); |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 3822 | /// Parse a requires-clause as part of a function declaration. |
| 3823 | void Parser::ParseTrailingRequiresClause(Declarator &D) { |
| 3824 | assert(Tok.is(tok::kw_requires) && "expected requires"); |
| 3825 | |
| 3826 | SourceLocation RequiresKWLoc = ConsumeToken(); |
| 3827 | |
| 3828 | ExprResult TrailingRequiresClause; |
| 3829 | ParseScope ParamScope(this, |
| 3830 | Scope::DeclScope | |
| 3831 | Scope::FunctionDeclarationScope | |
| 3832 | Scope::FunctionPrototypeScope); |
| 3833 | |
| 3834 | Actions.ActOnStartTrailingRequiresClause(getCurScope(), D); |
| 3835 | |
| 3836 | llvm::Optional<Sema::CXXThisScopeRAII> ThisScope; |
| 3837 | InitCXXThisScopeForDeclaratorIfRelevant(D, D.getDeclSpec(), ThisScope); |
| 3838 | |
| 3839 | TrailingRequiresClause = |
| 3840 | ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true); |
| 3841 | |
| 3842 | TrailingRequiresClause = |
| 3843 | Actions.ActOnFinishTrailingRequiresClause(TrailingRequiresClause); |
| 3844 | |
| 3845 | if (!D.isDeclarationOfFunction()) { |
| 3846 | Diag(RequiresKWLoc, |
| 3847 | diag::err_requires_clause_on_declarator_not_declaring_a_function); |
| 3848 | return; |
| 3849 | } |
| 3850 | |
| 3851 | if (TrailingRequiresClause.isInvalid()) |
| 3852 | SkipUntil({tok::l_brace, tok::arrow, tok::kw_try, tok::comma, tok::colon}, |
| 3853 | StopAtSemi | StopBeforeMatch); |
| 3854 | else |
| 3855 | D.setTrailingRequiresClause(TrailingRequiresClause.get()); |
| 3856 | |
| 3857 | // Did the user swap the trailing return type and requires clause? |
| 3858 | if (D.isFunctionDeclarator() && Tok.is(tok::arrow) && |
| 3859 | D.getDeclSpec().getTypeSpecType() == TST_auto) { |
| 3860 | SourceLocation ArrowLoc = Tok.getLocation(); |
| 3861 | SourceRange Range; |
| 3862 | TypeResult TrailingReturnType = |
| 3863 | ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit=*/false); |
| 3864 | |
| 3865 | if (!TrailingReturnType.isInvalid()) { |
| 3866 | Diag(ArrowLoc, |
| 3867 | diag::err_requires_clause_must_appear_after_trailing_return) |
| 3868 | << Range; |
| 3869 | auto &FunctionChunk = D.getFunctionTypeInfo(); |
| 3870 | FunctionChunk.HasTrailingReturnType = TrailingReturnType.isUsable(); |
| 3871 | FunctionChunk.TrailingReturnType = TrailingReturnType.get(); |
| 3872 | } else |
| 3873 | SkipUntil({tok::equal, tok::l_brace, tok::arrow, tok::kw_try, tok::comma}, |
| 3874 | StopAtSemi | StopBeforeMatch); |
| 3875 | } |
| 3876 | } |
| 3877 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3878 | /// We have just started parsing the definition of a new class, |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3879 | /// so push that class onto our stack of classes that is currently |
| 3880 | /// being parsed. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3881 | Sema::ParsingClassState |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3882 | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass, |
| 3883 | bool IsInterface) { |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3884 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3885 | "Nested class without outer class"); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3886 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface)); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3887 | return Actions.PushParsingClass(); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3890 | /// Deallocate the given parsed class and all of its nested |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3891 | /// classes. |
| 3892 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3893 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 3894 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3895 | delete Class; |
| 3896 | } |
| 3897 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3898 | /// Pop the top class of the stack of classes that are |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3899 | /// currently being parsed. |
| 3900 | /// |
| 3901 | /// This routine should be called when we have finished parsing the |
| 3902 | /// definition of a class, but have not yet popped the Scope |
| 3903 | /// associated with the class's definition. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3904 | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3905 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3907 | Actions.PopParsingClass(state); |
| 3908 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3909 | ParsingClass *Victim = ClassStack.top(); |
| 3910 | ClassStack.pop(); |
| 3911 | if (Victim->TopLevelClass) { |
| 3912 | // Deallocate all of the nested classes of this class, |
| 3913 | // recursively: we don't need to keep any of this information. |
| 3914 | DeallocateParsedClasses(Victim); |
| 3915 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3917 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 3918 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3919 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3920 | // The victim is a nested class, but we will not need to perform |
| 3921 | // any processing after the definition of this class since it has |
| 3922 | // no members whose handling was delayed. Therefore, we can just |
| 3923 | // remove this nested class. |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3924 | DeallocateParsedClasses(Victim); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3925 | return; |
| 3926 | } |
| 3927 | |
| 3928 | // This nested class has some members that will need to be processed |
| 3929 | // after the top-level class is completely defined. Therefore, add |
| 3930 | // it to the list of nested classes within its parent. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3931 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3932 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3933 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3934 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3935 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3936 | /// Try to parse an 'identifier' which appears within an attribute-token. |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3937 | /// |
| 3938 | /// \return the parsed identifier on success, and 0 if the next token is not an |
| 3939 | /// attribute-token. |
| 3940 | /// |
| 3941 | /// C++11 [dcl.attr.grammar]p3: |
| 3942 | /// If a keyword or an alternative token that satisfies the syntactic |
| 3943 | /// requirements of an identifier is contained in an attribute-token, |
| 3944 | /// it is considered an identifier. |
| 3945 | IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) { |
| 3946 | switch (Tok.getKind()) { |
| 3947 | default: |
| 3948 | // Identifiers and keywords have identifier info attached. |
David Majnemer | d527199 | 2015-01-09 18:09:39 +0000 | [diff] [blame] | 3949 | if (!Tok.isAnnotation()) { |
| 3950 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 3951 | Loc = ConsumeToken(); |
| 3952 | return II; |
| 3953 | } |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3954 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3955 | return nullptr; |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3956 | |
Aaron Ballman | c44c1742 | 2018-11-09 17:19:45 +0000 | [diff] [blame] | 3957 | case tok::numeric_constant: { |
| 3958 | // If we got a numeric constant, check to see if it comes from a macro that |
| 3959 | // corresponds to the predefined __clang__ macro. If it does, warn the user |
| 3960 | // and recover by pretending they said _Clang instead. |
| 3961 | if (Tok.getLocation().isMacroID()) { |
| 3962 | SmallString<8> ExpansionBuf; |
| 3963 | SourceLocation ExpansionLoc = |
| 3964 | PP.getSourceManager().getExpansionLoc(Tok.getLocation()); |
| 3965 | StringRef Spelling = PP.getSpelling(ExpansionLoc, ExpansionBuf); |
| 3966 | if (Spelling == "__clang__") { |
| 3967 | SourceRange TokRange( |
| 3968 | ExpansionLoc, |
| 3969 | PP.getSourceManager().getExpansionLoc(Tok.getEndLoc())); |
| 3970 | Diag(Tok, diag::warn_wrong_clang_attr_namespace) |
| 3971 | << FixItHint::CreateReplacement(TokRange, "_Clang"); |
| 3972 | Loc = ConsumeToken(); |
| 3973 | return &PP.getIdentifierTable().get("_Clang"); |
| 3974 | } |
| 3975 | } |
| 3976 | return nullptr; |
| 3977 | } |
| 3978 | |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3979 | case tok::ampamp: // 'and' |
| 3980 | case tok::pipe: // 'bitor' |
| 3981 | case tok::pipepipe: // 'or' |
| 3982 | case tok::caret: // 'xor' |
| 3983 | case tok::tilde: // 'compl' |
| 3984 | case tok::amp: // 'bitand' |
| 3985 | case tok::ampequal: // 'and_eq' |
| 3986 | case tok::pipeequal: // 'or_eq' |
| 3987 | case tok::caretequal: // 'xor_eq' |
| 3988 | case tok::exclaim: // 'not' |
| 3989 | case tok::exclaimequal: // 'not_eq' |
| 3990 | // Alternative tokens do not have identifier info, but their spelling |
| 3991 | // starts with an alphabetical character. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3992 | SmallString<8> SpellingBuf; |
Benjamin Kramer | 60be563 | 2015-03-29 19:25:07 +0000 | [diff] [blame] | 3993 | SourceLocation SpellingLoc = |
| 3994 | PP.getSourceManager().getSpellingLoc(Tok.getLocation()); |
| 3995 | StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf); |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 3996 | if (isLetter(Spelling[0])) { |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3997 | Loc = ConsumeToken(); |
Benjamin Kramer | 5c17f9c | 2012-04-22 20:43:30 +0000 | [diff] [blame] | 3998 | return &PP.getIdentifierTable().get(Spelling); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3999 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4000 | return nullptr; |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4004 | static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4005 | IdentifierInfo *ScopeName) { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 4006 | switch ( |
| 4007 | ParsedAttr::getParsedKind(AttrName, ScopeName, ParsedAttr::AS_CXX11)) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4008 | case ParsedAttr::AT_CarriesDependency: |
| 4009 | case ParsedAttr::AT_Deprecated: |
| 4010 | case ParsedAttr::AT_FallThrough: |
| 4011 | case ParsedAttr::AT_CXX11NoReturn: |
Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 4012 | case ParsedAttr::AT_NoUniqueAddress: |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4013 | return true; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4014 | case ParsedAttr::AT_WarnUnusedResult: |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 4015 | return !ScopeName && AttrName->getName().equals("nodiscard"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4016 | case ParsedAttr::AT_Unused: |
Nico Weber | ac03bce | 2016-08-23 19:59:55 +0000 | [diff] [blame] | 4017 | return !ScopeName && AttrName->getName().equals("maybe_unused"); |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4018 | default: |
| 4019 | return false; |
| 4020 | } |
| 4021 | } |
| 4022 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4023 | /// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause. |
| 4024 | /// |
| 4025 | /// [C++11] attribute-argument-clause: |
| 4026 | /// '(' balanced-token-seq ')' |
| 4027 | /// |
| 4028 | /// [C++11] balanced-token-seq: |
| 4029 | /// balanced-token |
| 4030 | /// balanced-token-seq balanced-token |
| 4031 | /// |
| 4032 | /// [C++11] balanced-token: |
| 4033 | /// '(' balanced-token-seq ')' |
| 4034 | /// '[' balanced-token-seq ']' |
| 4035 | /// '{' balanced-token-seq '}' |
| 4036 | /// any token but '(', ')', '[', ']', '{', or '}' |
| 4037 | bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName, |
| 4038 | SourceLocation AttrNameLoc, |
| 4039 | ParsedAttributes &Attrs, |
| 4040 | SourceLocation *EndLoc, |
| 4041 | IdentifierInfo *ScopeName, |
| 4042 | SourceLocation ScopeLoc) { |
| 4043 | assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list"); |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 4044 | SourceLocation LParenLoc = Tok.getLocation(); |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4045 | const LangOptions &LO = getLangOpts(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4046 | ParsedAttr::Syntax Syntax = |
| 4047 | LO.CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x; |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4048 | |
| 4049 | // If the attribute isn't known, we will not attempt to parse any |
| 4050 | // arguments. |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4051 | if (!hasAttribute(LO.CPlusPlus ? AttrSyntax::CXX : AttrSyntax::C, ScopeName, |
| 4052 | AttrName, getTargetInfo(), getLangOpts())) { |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4053 | // Eat the left paren, then skip to the ending right paren. |
| 4054 | ConsumeParen(); |
| 4055 | SkipUntil(tok::r_paren); |
Aaron Ballman | c44c1742 | 2018-11-09 17:19:45 +0000 | [diff] [blame] | 4056 | return false; |
| 4057 | } |
| 4058 | |
| 4059 | if (ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"))) { |
| 4060 | // GNU-scoped attributes have some special cases to handle GNU-specific |
| 4061 | // behaviors. |
| 4062 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4063 | ScopeLoc, Syntax, nullptr); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 4064 | return true; |
| 4065 | } |
| 4066 | |
| 4067 | unsigned NumArgs; |
| 4068 | // Some Clang-scoped attributes have some special parsing behavior. |
Aaron Ballman | c44c1742 | 2018-11-09 17:19:45 +0000 | [diff] [blame] | 4069 | if (ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"))) |
| 4070 | NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, |
| 4071 | ScopeName, ScopeLoc, Syntax); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 4072 | else |
| 4073 | NumArgs = |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 4074 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4075 | ScopeName, ScopeLoc, Syntax); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 4076 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4077 | if (!Attrs.empty() && |
| 4078 | IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) { |
Michael Kruse | dc5ce72 | 2018-08-03 01:21:16 +0000 | [diff] [blame] | 4079 | ParsedAttr &Attr = Attrs.back(); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 4080 | // If the attribute is a standard or built-in attribute and we are |
| 4081 | // parsing an argument list, we need to determine whether this attribute |
| 4082 | // was allowed to have an argument list (such as [[deprecated]]), and how |
| 4083 | // many arguments were parsed (so we can diagnose on [[deprecated()]]). |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4084 | if (Attr.getMaxArgs() && !NumArgs) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 4085 | // The attribute was allowed to have arguments, but none were provided |
| 4086 | // even though the attribute parsed successfully. This is an error. |
| 4087 | Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4088 | Attr.setInvalid(true); |
| 4089 | } else if (!Attr.getMaxArgs()) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 4090 | // The attribute parsed successfully, but was not allowed to have any |
| 4091 | // arguments. It doesn't matter whether any were provided -- the |
| 4092 | // presence of the argument list (even if empty) is diagnosed. |
| 4093 | Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments) |
| 4094 | << AttrName |
| 4095 | << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc)); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4096 | Attr.setInvalid(true); |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 4097 | } |
| 4098 | } |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4099 | return true; |
| 4100 | } |
| 4101 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4102 | /// ParseCXX11AttributeSpecifier - Parse a C++11 or C2x attribute-specifier. |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4103 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4104 | /// [C++11] attribute-specifier: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4105 | /// '[' '[' attribute-list ']' ']' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 4106 | /// alignment-specifier |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4107 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4108 | /// [C++11] attribute-list: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4109 | /// attribute[opt] |
| 4110 | /// attribute-list ',' attribute[opt] |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4111 | /// attribute '...' |
| 4112 | /// attribute-list ',' attribute '...' |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4113 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4114 | /// [C++11] attribute: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4115 | /// attribute-token attribute-argument-clause[opt] |
| 4116 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4117 | /// [C++11] attribute-token: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4118 | /// identifier |
| 4119 | /// attribute-scoped-token |
| 4120 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4121 | /// [C++11] attribute-scoped-token: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4122 | /// attribute-namespace '::' identifier |
| 4123 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4124 | /// [C++11] attribute-namespace: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4125 | /// identifier |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4126 | void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs, |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4127 | SourceLocation *endLoc) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 4128 | if (Tok.is(tok::kw_alignas)) { |
Richard Smith | f679b5b | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 4129 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 4130 | ParseAlignmentSpecifier(attrs, endLoc); |
| 4131 | return; |
| 4132 | } |
| 4133 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4134 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) && |
| 4135 | "Not a double square bracket attribute list"); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4136 | |
Richard Smith | f679b5b | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 4137 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute); |
| 4138 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4139 | ConsumeBracket(); |
| 4140 | ConsumeBracket(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4141 | |
Richard Smith | b7d7a04 | 2016-06-24 12:15:12 +0000 | [diff] [blame] | 4142 | SourceLocation CommonScopeLoc; |
| 4143 | IdentifierInfo *CommonScopeName = nullptr; |
| 4144 | if (Tok.is(tok::kw_using)) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4145 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
Richard Smith | b7d7a04 | 2016-06-24 12:15:12 +0000 | [diff] [blame] | 4146 | ? diag::warn_cxx14_compat_using_attribute_ns |
| 4147 | : diag::ext_using_attribute_ns); |
| 4148 | ConsumeToken(); |
| 4149 | |
| 4150 | CommonScopeName = TryParseCXX11AttributeIdentifier(CommonScopeLoc); |
| 4151 | if (!CommonScopeName) { |
| 4152 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 4153 | SkipUntil(tok::r_square, tok::colon, StopBeforeMatch); |
| 4154 | } |
| 4155 | if (!TryConsumeToken(tok::colon) && CommonScopeName) |
| 4156 | Diag(Tok.getLocation(), diag::err_expected) << tok::colon; |
| 4157 | } |
| 4158 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4159 | llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs; |
| 4160 | |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4161 | while (Tok.isNot(tok::r_square)) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4162 | // attribute not present |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 4163 | if (TryConsumeToken(tok::comma)) |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4164 | continue; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4165 | |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4166 | SourceLocation ScopeLoc, AttrLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4167 | IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr; |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4168 | |
| 4169 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 4170 | if (!AttrName) |
| 4171 | // Break out to the "expected ']'" diagnostic. |
| 4172 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4173 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4174 | // scoped attribute |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 4175 | if (TryConsumeToken(tok::coloncolon)) { |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4176 | ScopeName = AttrName; |
| 4177 | ScopeLoc = AttrLoc; |
| 4178 | |
| 4179 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 4180 | if (!AttrName) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4181 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4182 | SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4183 | continue; |
| 4184 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Richard Smith | b7d7a04 | 2016-06-24 12:15:12 +0000 | [diff] [blame] | 4187 | if (CommonScopeName) { |
| 4188 | if (ScopeName) { |
| 4189 | Diag(ScopeLoc, diag::err_using_attribute_ns_conflict) |
| 4190 | << SourceRange(CommonScopeLoc); |
| 4191 | } else { |
| 4192 | ScopeName = CommonScopeName; |
| 4193 | ScopeLoc = CommonScopeLoc; |
| 4194 | } |
| 4195 | } |
| 4196 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4197 | bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4198 | bool AttrParsed = false; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4199 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4200 | if (StandardAttr && |
| 4201 | !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second) |
| 4202 | Diag(AttrLoc, diag::err_cxx11_attribute_repeated) |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4203 | << AttrName << SourceRange(SeenAttrs[AttrName]); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4204 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4205 | // Parse attribute arguments |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 4206 | if (Tok.is(tok::l_paren)) |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4207 | AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc, |
| 4208 | ScopeName, ScopeLoc); |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4209 | |
| 4210 | if (!AttrParsed) |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4211 | attrs.addNew( |
| 4212 | AttrName, |
| 4213 | SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, AttrLoc), |
| 4214 | ScopeName, ScopeLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4215 | getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4216 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 4217 | if (TryConsumeToken(tok::ellipsis)) |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4218 | Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 4219 | << AttrName; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4222 | if (ExpectAndConsume(tok::r_square)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4223 | SkipUntil(tok::r_square); |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4224 | if (endLoc) |
| 4225 | *endLoc = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4226 | if (ExpectAndConsume(tok::r_square)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4227 | SkipUntil(tok::r_square); |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4228 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4229 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4230 | /// ParseCXX11Attributes - Parse a C++11 or C2x attribute-specifier-seq. |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4231 | /// |
| 4232 | /// attribute-specifier-seq: |
| 4233 | /// attribute-specifier-seq[opt] attribute-specifier |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4234 | void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs, |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4235 | SourceLocation *endLoc) { |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4236 | assert(standardAttributesAllowed()); |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 4237 | |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4238 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
| 4239 | if (!endLoc) |
| 4240 | endLoc = &Loc; |
| 4241 | |
Douglas Gregor | 6f98100 | 2011-10-07 20:35:25 +0000 | [diff] [blame] | 4242 | do { |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4243 | ParseCXX11AttributeSpecifier(attrs, endLoc); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4244 | } while (isCXX11AttributeSpecifier()); |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4245 | |
| 4246 | attrs.Range = SourceRange(StartLoc, *endLoc); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4249 | void Parser::DiagnoseAndSkipCXX11Attributes() { |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4250 | // Start and end location of an attribute or an attribute list. |
| 4251 | SourceLocation StartLoc = Tok.getLocation(); |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 4252 | SourceLocation EndLoc = SkipCXX11Attributes(); |
| 4253 | |
| 4254 | if (EndLoc.isValid()) { |
| 4255 | SourceRange Range(StartLoc, EndLoc); |
| 4256 | Diag(StartLoc, diag::err_attributes_not_allowed) |
| 4257 | << Range; |
| 4258 | } |
| 4259 | } |
| 4260 | |
| 4261 | SourceLocation Parser::SkipCXX11Attributes() { |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4262 | SourceLocation EndLoc; |
| 4263 | |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 4264 | if (!isCXX11AttributeSpecifier()) |
| 4265 | return EndLoc; |
| 4266 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4267 | do { |
| 4268 | if (Tok.is(tok::l_square)) { |
| 4269 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4270 | T.consumeOpen(); |
| 4271 | T.skipToEnd(); |
| 4272 | EndLoc = T.getCloseLocation(); |
| 4273 | } else { |
| 4274 | assert(Tok.is(tok::kw_alignas) && "not an attribute specifier"); |
| 4275 | ConsumeToken(); |
| 4276 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4277 | if (!T.consumeOpen()) |
| 4278 | T.skipToEnd(); |
| 4279 | EndLoc = T.getCloseLocation(); |
| 4280 | } |
| 4281 | } while (isCXX11AttributeSpecifier()); |
| 4282 | |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 4283 | return EndLoc; |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4286 | /// Parse uuid() attribute when it appears in a [] Microsoft attribute. |
| 4287 | void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) { |
| 4288 | assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list"); |
| 4289 | IdentifierInfo *UuidIdent = Tok.getIdentifierInfo(); |
| 4290 | assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list"); |
| 4291 | |
| 4292 | SourceLocation UuidLoc = Tok.getLocation(); |
| 4293 | ConsumeToken(); |
| 4294 | |
| 4295 | // Ignore the left paren location for now. |
| 4296 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4297 | if (T.consumeOpen()) { |
| 4298 | Diag(Tok, diag::err_expected) << tok::l_paren; |
| 4299 | return; |
| 4300 | } |
| 4301 | |
| 4302 | ArgsVector ArgExprs; |
| 4303 | if (Tok.is(tok::string_literal)) { |
| 4304 | // Easy case: uuid("...") -- quoted string. |
| 4305 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 4306 | if (StringResult.isInvalid()) |
| 4307 | return; |
| 4308 | ArgExprs.push_back(StringResult.get()); |
| 4309 | } else { |
| 4310 | // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no |
| 4311 | // quotes in the parens. Just append the spelling of all tokens encountered |
| 4312 | // until the closing paren. |
| 4313 | |
| 4314 | SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul |
| 4315 | StrBuffer += "\""; |
| 4316 | |
| 4317 | // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace, |
| 4318 | // tok::r_brace, tok::minus, tok::identifier (think C000) and |
| 4319 | // tok::numeric_constant (0000) should be enough. But the spelling of the |
| 4320 | // uuid argument is checked later anyways, so there's no harm in accepting |
| 4321 | // almost anything here. |
| 4322 | // cl is very strict about whitespace in this form and errors out if any |
| 4323 | // is present, so check the space flags on the tokens. |
| 4324 | SourceLocation StartLoc = Tok.getLocation(); |
| 4325 | while (Tok.isNot(tok::r_paren)) { |
| 4326 | if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { |
| 4327 | Diag(Tok, diag::err_attribute_uuid_malformed_guid); |
| 4328 | SkipUntil(tok::r_paren, StopAtSemi); |
| 4329 | return; |
| 4330 | } |
| 4331 | SmallString<16> SpellingBuffer; |
| 4332 | SpellingBuffer.resize(Tok.getLength() + 1); |
| 4333 | bool Invalid = false; |
| 4334 | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); |
| 4335 | if (Invalid) { |
| 4336 | SkipUntil(tok::r_paren, StopAtSemi); |
| 4337 | return; |
| 4338 | } |
| 4339 | StrBuffer += TokSpelling; |
| 4340 | ConsumeAnyToken(); |
| 4341 | } |
| 4342 | StrBuffer += "\""; |
| 4343 | |
| 4344 | if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { |
| 4345 | Diag(Tok, diag::err_attribute_uuid_malformed_guid); |
| 4346 | ConsumeParen(); |
| 4347 | return; |
| 4348 | } |
| 4349 | |
| 4350 | // Pretend the user wrote the appropriate string literal here. |
| 4351 | // ActOnStringLiteral() copies the string data into the literal, so it's |
| 4352 | // ok that the Token points to StrBuffer. |
| 4353 | Token Toks[1]; |
| 4354 | Toks[0].startToken(); |
| 4355 | Toks[0].setKind(tok::string_literal); |
| 4356 | Toks[0].setLocation(StartLoc); |
| 4357 | Toks[0].setLiteralData(StrBuffer.data()); |
| 4358 | Toks[0].setLength(StrBuffer.size()); |
| 4359 | StringLiteral *UuidString = |
| 4360 | cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get()); |
| 4361 | ArgExprs.push_back(UuidString); |
| 4362 | } |
| 4363 | |
| 4364 | if (!T.consumeClose()) { |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4365 | Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr, |
| 4366 | SourceLocation(), ArgExprs.data(), ArgExprs.size(), |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4367 | ParsedAttr::AS_Microsoft); |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4368 | } |
| 4369 | } |
| 4370 | |
David Majnemer | e4752e75 | 2015-07-08 05:55:00 +0000 | [diff] [blame] | 4371 | /// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr] |
Francois Pichet | c2bc5ac | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 4372 | /// |
| 4373 | /// [MS] ms-attribute: |
| 4374 | /// '[' token-seq ']' |
| 4375 | /// |
| 4376 | /// [MS] ms-attribute-seq: |
| 4377 | /// ms-attribute[opt] |
| 4378 | /// ms-attribute ms-attribute-seq |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4379 | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
| 4380 | SourceLocation *endLoc) { |
Francois Pichet | c2bc5ac | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 4381 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 4382 | |
Saleem Abdulrasool | 425efcf | 2015-06-15 20:57:04 +0000 | [diff] [blame] | 4383 | do { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4384 | // FIXME: If this is actually a C++11 attribute, parse it as one. |
Saleem Abdulrasool | 425efcf | 2015-06-15 20:57:04 +0000 | [diff] [blame] | 4385 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4386 | T.consumeOpen(); |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4387 | |
| 4388 | // Skip most ms attributes except for a whitelist. |
| 4389 | while (true) { |
| 4390 | SkipUntil(tok::r_square, tok::identifier, StopAtSemi | StopBeforeMatch); |
| 4391 | if (Tok.isNot(tok::identifier)) // ']', but also eof |
| 4392 | break; |
| 4393 | if (Tok.getIdentifierInfo()->getName() == "uuid") |
| 4394 | ParseMicrosoftUuidAttributeArgs(attrs); |
| 4395 | else |
| 4396 | ConsumeToken(); |
| 4397 | } |
| 4398 | |
Saleem Abdulrasool | 425efcf | 2015-06-15 20:57:04 +0000 | [diff] [blame] | 4399 | T.consumeClose(); |
| 4400 | if (endLoc) |
| 4401 | *endLoc = T.getCloseLocation(); |
| 4402 | } while (Tok.is(tok::l_square)); |
Francois Pichet | c2bc5ac | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 4403 | } |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4404 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4405 | void Parser::ParseMicrosoftIfExistsClassDeclaration( |
| 4406 | DeclSpec::TST TagType, ParsedAttributes &AccessAttrs, |
| 4407 | AccessSpecifier &CurAS) { |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4408 | IfExistsCondition Result; |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4409 | if (ParseMicrosoftIfExistsCondition(Result)) |
| 4410 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4412 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 4413 | if (Braces.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4414 | Diag(Tok, diag::err_expected) << tok::l_brace; |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4415 | return; |
| 4416 | } |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4417 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4418 | switch (Result.Behavior) { |
| 4419 | case IEB_Parse: |
| 4420 | // Parse the declarations below. |
| 4421 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4423 | case IEB_Dependent: |
| 4424 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
| 4425 | << Result.IsIfExists; |
| 4426 | // Fall through to skip. |
Galina Kistanova | d819d5b | 2017-06-01 21:19:06 +0000 | [diff] [blame] | 4427 | LLVM_FALLTHROUGH; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4429 | case IEB_Skip: |
| 4430 | Braces.skipToEnd(); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4431 | return; |
| 4432 | } |
| 4433 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 4434 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4435 | // __if_exists, __if_not_exists can nest. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 4436 | if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) { |
Serge Pavlov | 0c50319 | 2019-08-01 11:46:28 +0000 | [diff] [blame] | 4437 | ParseMicrosoftIfExistsClassDeclaration(TagType, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4438 | AccessAttrs, CurAS); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4439 | continue; |
| 4440 | } |
| 4441 | |
| 4442 | // Check for extraneous top-level semicolon. |
| 4443 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 4444 | ConsumeExtraSemi(InsideStruct, TagType); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4445 | continue; |
| 4446 | } |
| 4447 | |
| 4448 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 4449 | if (AS != AS_none) { |
| 4450 | // Current token is a C++ access specifier. |
| 4451 | CurAS = AS; |
| 4452 | SourceLocation ASLoc = Tok.getLocation(); |
| 4453 | ConsumeToken(); |
| 4454 | if (Tok.is(tok::colon)) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4455 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation(), |
| 4456 | ParsedAttributesView{}); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4457 | else |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 4458 | Diag(Tok, diag::err_expected) << tok::colon; |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4459 | ConsumeToken(); |
| 4460 | continue; |
| 4461 | } |
| 4462 | |
| 4463 | // Parse all the comma separated declarators. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4464 | ParseCXXClassMemberDeclaration(CurAS, AccessAttrs); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4465 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4467 | Braces.consumeClose(); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4468 | } |