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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | 423984d | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 14 | #include "clang/Parse/Parser.h" |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 757fcd6 | 2014-03-04 10:05:20 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/PrettyDeclStackTrace.h" |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Attributes.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Basic/OperatorKinds.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 22 | #include "clang/Parse/ParseDiagnostic.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 23 | #include "clang/Parse/RAIIObjectsForParser.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/DeclSpec.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/ParsedTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Scope.h" |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 27 | #include "clang/Sema/SemaDiagnostic.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallString.h" |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 29 | |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
| 32 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 33 | /// may either be a top level namespace or a block-level namespace alias. If |
| 34 | /// there was an inline keyword, it has already been parsed. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 35 | /// |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 36 | /// namespace-definition: [C++: namespace.def] |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 37 | /// named-namespace-definition |
| 38 | /// unnamed-namespace-definition |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 39 | /// nested-namespace-definition |
| 40 | /// |
| 41 | /// named-namespace-definition: |
| 42 | /// 'inline'[opt] 'namespace' attributes[opt] identifier '{' 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: |
| 48 | /// 'namespace' enclosing-namespace-specifier '::' 'inline'[opt] identifier '{' namespace-body '}' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 49 | /// |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 50 | /// enclosing-namespace-specifier: |
| 51 | /// identifier |
| 52 | /// enclosing-namespace-specifier '::' 'inline'[opt] identifier |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | /// |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 54 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 55 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 56 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 57 | Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context, |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 58 | SourceLocation &DeclEnd, |
| 59 | SourceLocation InlineLoc) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 60 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 61 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Fariborz Jahanian | 4bf8262 | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 62 | ObjCDeclContextSwitch ObjCDC(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 63 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 64 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 65 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 66 | cutOffParsing(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 67 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 68 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 69 | |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 70 | SourceLocation IdentLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 71 | IdentifierInfo *Ident = nullptr; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 72 | InnerNamespaceInfoList ExtraNSs; |
| 73 | SourceLocation FirstNestedInlineLoc; |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 74 | |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 75 | ParsedAttributesWithRange attrs(AttrFactory); |
| 76 | SourceLocation attrLoc; |
| 77 | if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 78 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
Richard Smith | 40e202f | 2017-10-14 00:56:24 +0000 | [diff] [blame] | 79 | ? diag::warn_cxx14_compat_ns_enum_attribute |
| 80 | : diag::ext_ns_enum_attribute) |
| 81 | << 0 /*namespace*/; |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 82 | attrLoc = Tok.getLocation(); |
| 83 | ParseCXX11Attributes(attrs); |
| 84 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 86 | if (Tok.is(tok::identifier)) { |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 87 | Ident = Tok.getIdentifierInfo(); |
| 88 | IdentLoc = ConsumeToken(); // eat the identifier. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 89 | while (Tok.is(tok::coloncolon) && |
| 90 | (NextToken().is(tok::identifier) || |
| 91 | (NextToken().is(tok::kw_inline) && |
| 92 | GetLookAheadToken(2).is(tok::identifier)))) { |
| 93 | |
| 94 | InnerNamespaceInfo Info; |
| 95 | Info.NamespaceLoc = ConsumeToken(); |
| 96 | |
| 97 | if (Tok.is(tok::kw_inline)) { |
| 98 | Info.InlineLoc = ConsumeToken(); |
| 99 | if (FirstNestedInlineLoc.isInvalid()) |
| 100 | FirstNestedInlineLoc = Info.InlineLoc; |
| 101 | } |
| 102 | |
| 103 | Info.Ident = Tok.getIdentifierInfo(); |
| 104 | Info.IdentLoc = ConsumeToken(); |
| 105 | |
| 106 | ExtraNSs.push_back(Info); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 108 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Aaron Ballman | c0ae7df | 2014-11-08 17:07:15 +0000 | [diff] [blame] | 110 | // A nested namespace definition cannot have attributes. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 111 | if (!ExtraNSs.empty() && attrLoc.isValid()) |
Aaron Ballman | c0ae7df | 2014-11-08 17:07:15 +0000 | [diff] [blame] | 112 | Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute); |
| 113 | |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 114 | // Read label attributes, if present. |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 115 | if (Tok.is(tok::kw___attribute)) { |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 116 | attrLoc = Tok.getLocation(); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 117 | ParseGNUAttributes(attrs); |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | 6b6bba4 | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 120 | if (Tok.is(tok::equal)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 121 | if (!Ident) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 122 | Diag(Tok, diag::err_expected) << tok::identifier; |
Nico Weber | 729f1e2 | 2012-10-27 23:44:27 +0000 | [diff] [blame] | 123 | // Skip to end of the definition and eat the ';'. |
| 124 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 125 | return nullptr; |
Nico Weber | 729f1e2 | 2012-10-27 23:44:27 +0000 | [diff] [blame] | 126 | } |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 127 | if (attrLoc.isValid()) |
| 128 | Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 129 | if (InlineLoc.isValid()) |
| 130 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
| 131 | << FixItHint::CreateRemoval(InlineLoc); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 132 | Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
| 133 | return Actions.ConvertDeclToDeclGroup(NSAlias); |
| 134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 136 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 137 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 138 | if (Ident) |
| 139 | Diag(Tok, diag::err_expected) << tok::l_brace; |
| 140 | else |
| 141 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 142 | return nullptr; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 143 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 145 | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
| 146 | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 147 | getCurScope()->getFnParent()) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 148 | Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 149 | SkipUntil(tok::r_brace); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 150 | return nullptr; |
Douglas Gregor | 05cfc29 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 153 | if (ExtraNSs.empty()) { |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 154 | // Normal namespace definition, not a nested-namespace-definition. |
| 155 | } else if (InlineLoc.isValid()) { |
| 156 | Diag(InlineLoc, diag::err_inline_nested_namespace_definition); |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 157 | } else if (getLangOpts().CPlusPlus2a) { |
| 158 | Diag(ExtraNSs[0].NamespaceLoc, |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 159 | diag::warn_cxx14_compat_nested_namespace_definition); |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 160 | if (FirstNestedInlineLoc.isValid()) |
| 161 | Diag(FirstNestedInlineLoc, |
| 162 | diag::warn_cxx17_compat_inline_nested_namespace_definition); |
| 163 | } else if (getLangOpts().CPlusPlus17) { |
| 164 | Diag(ExtraNSs[0].NamespaceLoc, |
| 165 | diag::warn_cxx14_compat_nested_namespace_definition); |
| 166 | if (FirstNestedInlineLoc.isValid()) |
| 167 | Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 168 | } else { |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 169 | TentativeParsingAction TPA(*this); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 170 | SkipUntil(tok::r_brace, StopBeforeMatch); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 171 | Token rBraceToken = Tok; |
| 172 | TPA.Revert(); |
| 173 | |
| 174 | if (!rBraceToken.is(tok::r_brace)) { |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 175 | Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) |
| 176 | << SourceRange(ExtraNSs.front().NamespaceLoc, |
| 177 | ExtraNSs.back().IdentLoc); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 178 | } else { |
Benjamin Kramer | f546f41 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 179 | std::string NamespaceFix; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 180 | for (const auto &ExtraNS : ExtraNSs) { |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 181 | NamespaceFix += " { namespace "; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 182 | NamespaceFix += ExtraNS.Ident->getName(); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 183 | } |
Benjamin Kramer | f546f41 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 184 | |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 185 | std::string RBraces; |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 186 | for (unsigned i = 0, e = ExtraNSs.size(); i != e; ++i) |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 187 | RBraces += "} "; |
Benjamin Kramer | f546f41 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 188 | |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 189 | Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) |
| 190 | << FixItHint::CreateReplacement( |
| 191 | SourceRange(ExtraNSs.front().NamespaceLoc, |
| 192 | ExtraNSs.back().IdentLoc), |
| 193 | NamespaceFix) |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 194 | << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces); |
| 195 | } |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 196 | |
| 197 | // Warn about nested inline namespaces. |
| 198 | if (FirstNestedInlineLoc.isValid()) |
| 199 | Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 202 | // 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] | 203 | if (InlineLoc.isValid()) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 204 | Diag(InlineLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 205 | diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace); |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 4de55aa | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 207 | // Enter a scope for the namespace. |
| 208 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 209 | |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 210 | UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 211 | Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( |
| 212 | getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident, |
| 213 | T.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl); |
Chris Lattner | 4de55aa | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 214 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 215 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl, |
| 216 | NamespaceLoc, "parsing namespace"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 218 | // Parse the contents of the namespace. This includes parsing recovery on |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 219 | // any improperly nested namespaces. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 220 | ParseInnerNamespace(ExtraNSs, 0, InlineLoc, attrs, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 4de55aa | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 222 | // Leave the namespace scope. |
| 223 | NamespaceScope.Exit(); |
| 224 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 225 | DeclEnd = T.getCloseLocation(); |
| 226 | Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 227 | |
| 228 | return Actions.ConvertDeclToDeclGroup(NamespcDecl, |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 229 | ImplicitUsingDirectiveDecl); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 230 | } |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 231 | |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 232 | /// ParseInnerNamespace - Parse the contents of a namespace. |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 233 | void Parser::ParseInnerNamespace(const InnerNamespaceInfoList &InnerNSs, |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 234 | unsigned int index, SourceLocation &InlineLoc, |
| 235 | ParsedAttributes &attrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 236 | BalancedDelimiterTracker &Tracker) { |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 237 | if (index == InnerNSs.size()) { |
Richard Smith | 752ada8 | 2015-11-17 23:32:01 +0000 | [diff] [blame] | 238 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 239 | Tok.isNot(tok::eof)) { |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 240 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 241 | MaybeParseCXX11Attributes(attrs); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 242 | ParseExternalDeclaration(attrs); |
| 243 | } |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 244 | |
| 245 | // The caller is what called check -- we are simply calling |
| 246 | // the close for it. |
| 247 | Tracker.consumeClose(); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 248 | |
| 249 | return; |
| 250 | } |
| 251 | |
Richard Smith | 13307f5 | 2014-11-08 05:37:34 +0000 | [diff] [blame] | 252 | // Handle a nested namespace definition. |
| 253 | // FIXME: Preserve the source information through to the AST rather than |
| 254 | // desugaring it here. |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 255 | ParseScope NamespaceScope(this, Scope::DeclScope); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 256 | UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 257 | Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 258 | getCurScope(), InnerNSs[index].InlineLoc, InnerNSs[index].NamespaceLoc, |
| 259 | InnerNSs[index].IdentLoc, InnerNSs[index].Ident, |
| 260 | Tracker.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 261 | assert(!ImplicitUsingDirectiveDecl && |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 262 | "nested namespace definition cannot define anonymous namespace"); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 263 | |
Erich Keane | 53f391d | 2018-11-12 17:19:48 +0000 | [diff] [blame] | 264 | ParseInnerNamespace(InnerNSs, ++index, InlineLoc, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 265 | attrs, Tracker); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 266 | |
| 267 | NamespaceScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 268 | Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation()); |
Richard Trieu | 61384cb | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 271 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 272 | /// alias definition. |
| 273 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 274 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 275 | SourceLocation AliasLoc, |
| 276 | IdentifierInfo *Alias, |
| 277 | SourceLocation &DeclEnd) { |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 278 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 280 | ConsumeToken(); // eat the '='. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 282 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 283 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 284 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 285 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 286 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 287 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 288 | CXXScopeSpec SS; |
| 289 | // Parse (optional) nested-name-specifier. |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 290 | ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false, |
| 291 | /*MayBePseudoDestructor=*/nullptr, |
| 292 | /*IsTypename=*/false, |
| 293 | /*LastII=*/nullptr, |
| 294 | /*OnlyNamespace=*/true); |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 295 | |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 296 | if (Tok.isNot(tok::identifier)) { |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 297 | Diag(Tok, diag::err_expected_namespace_name); |
| 298 | // Skip to end of the definition and eat the ';'. |
| 299 | SkipUntil(tok::semi); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 300 | return nullptr; |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 303 | if (SS.isInvalid()) { |
| 304 | // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. |
| 305 | // Skip to end of the definition and eat the ';'. |
| 306 | SkipUntil(tok::semi); |
| 307 | return nullptr; |
| 308 | } |
| 309 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 310 | // Parse identifier. |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 311 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 312 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 314 | // Eat the ';'. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 315 | DeclEnd = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 316 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name)) |
| 317 | SkipUntil(tok::semi); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Craig Topper | ff35428 | 2015-11-14 18:16:00 +0000 | [diff] [blame] | 319 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, |
| 320 | Alias, SS, IdentLoc, Ident); |
Anders Carlsson | 1894f0d4 | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 323 | /// ParseLinkage - We know that the current token is a string_literal |
| 324 | /// and just before that, that extern was seen. |
| 325 | /// |
| 326 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 327 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 328 | /// 'extern' string-literal declaration |
| 329 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 330 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) { |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 331 | assert(isTokenStringLiteral() && "Not a string literal!"); |
| 332 | ExprResult Lang = ParseStringLiteralExpression(false); |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 333 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 334 | ParseScope LinkageScope(this, Scope::DeclScope); |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 335 | Decl *LinkageSpec = |
| 336 | Lang.isInvalid() |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 337 | ? nullptr |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 338 | : Actions.ActOnStartLinkageSpecification( |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 339 | getCurScope(), DS.getSourceRange().getBegin(), Lang.get(), |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 340 | Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation()); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 341 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 342 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 343 | MaybeParseCXX11Attributes(attrs); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 345 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | 4d42399 | 2011-05-01 16:25:54 +0000 | [diff] [blame] | 346 | // Reset the source range in DS, as the leading "extern" |
| 347 | // does not really belong to the inner declaration ... |
| 348 | DS.SetRangeStart(SourceLocation()); |
| 349 | DS.SetRangeEnd(SourceLocation()); |
| 350 | // ... but anyway remember that such an "extern" was seen. |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 351 | DS.setExternInLinkageSpec(true); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 352 | ParseExternalDeclaration(attrs, &DS); |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 353 | return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( |
| 354 | getCurScope(), LinkageSpec, SourceLocation()) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 355 | : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | } |
Douglas Gregor | 29ff7d0 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | b65a913 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 358 | DS.abort(); |
| 359 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 360 | ProhibitAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 361 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 362 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 363 | T.consumeOpen(); |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 364 | |
| 365 | unsigned NestedModules = 0; |
| 366 | while (true) { |
| 367 | switch (Tok.getKind()) { |
| 368 | case tok::annot_module_begin: |
| 369 | ++NestedModules; |
| 370 | ParseTopLevelDecl(); |
| 371 | continue; |
| 372 | |
| 373 | case tok::annot_module_end: |
| 374 | if (!NestedModules) |
| 375 | break; |
| 376 | --NestedModules; |
| 377 | ParseTopLevelDecl(); |
| 378 | continue; |
| 379 | |
| 380 | case tok::annot_module_include: |
| 381 | ParseTopLevelDecl(); |
| 382 | continue; |
| 383 | |
| 384 | case tok::eof: |
| 385 | break; |
| 386 | |
| 387 | case tok::r_brace: |
| 388 | if (!NestedModules) |
| 389 | break; |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 390 | LLVM_FALLTHROUGH; |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 391 | default: |
| 392 | ParsedAttributesWithRange attrs(AttrFactory); |
| 393 | MaybeParseCXX11Attributes(attrs); |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 394 | ParseExternalDeclaration(attrs); |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | break; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 401 | T.consumeClose(); |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 402 | return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( |
| 403 | getCurScope(), LinkageSpec, T.getCloseLocation()) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 404 | : nullptr; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 405 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 406 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 407 | /// Parse a C++ Modules TS export-declaration. |
| 408 | /// |
| 409 | /// export-declaration: |
| 410 | /// 'export' declaration |
| 411 | /// 'export' '{' declaration-seq[opt] '}' |
| 412 | /// |
| 413 | Decl *Parser::ParseExportDeclaration() { |
| 414 | assert(Tok.is(tok::kw_export)); |
| 415 | SourceLocation ExportLoc = ConsumeToken(); |
| 416 | |
| 417 | ParseScope ExportScope(this, Scope::DeclScope); |
| 418 | Decl *ExportDecl = Actions.ActOnStartExportDecl( |
| 419 | getCurScope(), ExportLoc, |
| 420 | Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation()); |
| 421 | |
| 422 | if (Tok.isNot(tok::l_brace)) { |
| 423 | // FIXME: Factor out a ParseExternalDeclarationWithAttrs. |
| 424 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 425 | MaybeParseCXX11Attributes(Attrs); |
| 426 | MaybeParseMicrosoftAttributes(Attrs); |
| 427 | ParseExternalDeclaration(Attrs); |
| 428 | return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, |
| 429 | SourceLocation()); |
| 430 | } |
| 431 | |
| 432 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 433 | T.consumeOpen(); |
| 434 | |
| 435 | // The Modules TS draft says "An export-declaration shall declare at least one |
| 436 | // entity", but the intent is that it shall contain at least one declaration. |
| 437 | if (Tok.is(tok::r_brace)) |
| 438 | Diag(ExportLoc, diag::err_export_empty) |
| 439 | << SourceRange(ExportLoc, Tok.getLocation()); |
| 440 | |
| 441 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 442 | Tok.isNot(tok::eof)) { |
| 443 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 444 | MaybeParseCXX11Attributes(Attrs); |
| 445 | MaybeParseMicrosoftAttributes(Attrs); |
| 446 | ParseExternalDeclaration(Attrs); |
| 447 | } |
| 448 | |
| 449 | T.consumeClose(); |
| 450 | return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, |
| 451 | T.getCloseLocation()); |
| 452 | } |
| 453 | |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 454 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 455 | /// using-directive. Assumes that current token is 'using'. |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 456 | Parser::DeclGroupPtrTy |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 457 | Parser::ParseUsingDirectiveOrDeclaration(DeclaratorContext Context, |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 458 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 459 | SourceLocation &DeclEnd, |
| 460 | ParsedAttributesWithRange &attrs) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 461 | assert(Tok.is(tok::kw_using) && "Not using token"); |
Fariborz Jahanian | 4bf8262 | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 462 | ObjCDeclContextSwitch ObjCDC(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 464 | // Eat 'using'. |
| 465 | SourceLocation UsingLoc = ConsumeToken(); |
| 466 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 467 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 468 | Actions.CodeCompleteUsing(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 469 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 470 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 471 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 472 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 473 | // 'using namespace' means this is a using-directive. |
| 474 | if (Tok.is(tok::kw_namespace)) { |
| 475 | // Template parameters are always an error here. |
| 476 | if (TemplateInfo.Kind) { |
| 477 | SourceRange R = TemplateInfo.getSourceRange(); |
Craig Topper | 54a6a68 | 2015-11-14 18:16:08 +0000 | [diff] [blame] | 478 | Diag(UsingLoc, diag::err_templated_using_directive_declaration) |
| 479 | << 0 /* directive */ << R << FixItHint::CreateRemoval(R); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 480 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 481 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 482 | Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
| 483 | return Actions.ConvertDeclToDeclGroup(UsingDir); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 486 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 487 | |
| 488 | // Using declarations can't have attributes. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 489 | ProhibitAttributes(attrs); |
Chris Lattner | 9b01ca1 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 490 | |
Fariborz Jahanian | 4bf8262 | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 491 | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 492 | AS_none); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 496 | /// that current token is 'namespace' and 'using' was already parsed. |
| 497 | /// |
| 498 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 499 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 500 | /// namespace-name ; |
| 501 | /// [GNU] using-directive: |
| 502 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 503 | /// namespace-name attributes[opt] ; |
| 504 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 505 | Decl *Parser::ParseUsingDirective(DeclaratorContext Context, |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 506 | SourceLocation UsingLoc, |
| 507 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 508 | ParsedAttributes &attrs) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 509 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 510 | |
| 511 | // Eat 'namespace'. |
| 512 | SourceLocation NamespcLoc = ConsumeToken(); |
| 513 | |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 514 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 515 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 516 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 517 | return nullptr; |
Douglas Gregor | 7e90c6d | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 518 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 519 | |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 520 | CXXScopeSpec SS; |
| 521 | // Parse (optional) nested-name-specifier. |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 522 | ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false, |
| 523 | /*MayBePseudoDestructor=*/nullptr, |
| 524 | /*IsTypename=*/false, |
| 525 | /*LastII=*/nullptr, |
| 526 | /*OnlyNamespace=*/true); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 527 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 528 | IdentifierInfo *NamespcName = nullptr; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 529 | SourceLocation IdentLoc = SourceLocation(); |
| 530 | |
| 531 | // Parse namespace-name. |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 532 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 533 | Diag(Tok, diag::err_expected_namespace_name); |
| 534 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 535 | SkipUntil(tok::semi); |
| 536 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 537 | return nullptr; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 538 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 540 | if (SS.isInvalid()) { |
| 541 | // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. |
| 542 | // Skip to end of the definition and eat the ';'. |
| 543 | SkipUntil(tok::semi); |
| 544 | return nullptr; |
| 545 | } |
| 546 | |
Chris Lattner | ce1da2c | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 547 | // Parse identifier. |
| 548 | NamespcName = Tok.getIdentifierInfo(); |
| 549 | IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | |
Chris Lattner | ce1da2c | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 551 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 552 | bool GNUAttr = false; |
| 553 | if (Tok.is(tok::kw___attribute)) { |
| 554 | GNUAttr = true; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 555 | ParseGNUAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 556 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Chris Lattner | ce1da2c | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 558 | // Eat ';'. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 559 | DeclEnd = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 560 | if (ExpectAndConsume(tok::semi, |
| 561 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
| 562 | : diag::err_expected_semi_after_namespace_name)) |
| 563 | SkipUntil(tok::semi); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 565 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 566 | IdentLoc, NamespcName, attrs); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 569 | /// 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] | 570 | /// |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 571 | /// using-declarator: |
| 572 | /// 'typename'[opt] nested-name-specifier unqualified-id |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 573 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 574 | bool Parser::ParseUsingDeclarator(DeclaratorContext Context, |
| 575 | UsingDeclarator &D) { |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 576 | D.clear(); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 577 | |
| 578 | // Ignore optional 'typename'. |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 579 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 580 | TryConsumeToken(tok::kw_typename, D.TypenameLoc); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 581 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 582 | if (Tok.is(tok::kw___super)) { |
| 583 | Diag(Tok.getLocation(), diag::err_super_in_using_declaration); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 584 | return true; |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 587 | // Parse nested-name-specifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 588 | IdentifierInfo *LastII = nullptr; |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 589 | ParseOptionalCXXScopeSpecifier(D.SS, nullptr, /*EnteringContext=*/false, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 590 | /*MayBePseudoDtor=*/nullptr, |
| 591 | /*IsTypename=*/false, |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 592 | /*LastII=*/&LastII); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 593 | if (D.SS.isInvalid()) |
| 594 | return true; |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 595 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 596 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 597 | // destructor names and allow the action module to diagnose any semantic |
| 598 | // errors. |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 599 | // |
| 600 | // C++11 [class.qual]p2: |
| 601 | // [...] in a using-declaration that is a member-declaration, if the name |
| 602 | // specified after the nested-name-specifier is the same as the identifier |
| 603 | // or the simple-template-id's template-name in the last component of the |
| 604 | // nested-name-specifier, the name is [...] considered to name the |
| 605 | // constructor. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 606 | if (getLangOpts().CPlusPlus11 && |
| 607 | Context == DeclaratorContext::MemberContext && |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 608 | Tok.is(tok::identifier) && |
| 609 | (NextToken().is(tok::semi) || NextToken().is(tok::comma) || |
| 610 | NextToken().is(tok::ellipsis)) && |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 611 | D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() && |
| 612 | !D.SS.getScopeRep()->getAsNamespace() && |
| 613 | !D.SS.getScopeRep()->getAsNamespaceAlias()) { |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 614 | SourceLocation IdLoc = ConsumeToken(); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 615 | ParsedType Type = |
| 616 | Actions.getInheritingConstructorName(D.SS, IdLoc, *LastII); |
| 617 | D.Name.setConstructorName(Type, IdLoc, IdLoc); |
| 618 | } else { |
| 619 | if (ParseUnqualifiedId( |
| 620 | D.SS, /*EnteringContext=*/false, |
| 621 | /*AllowDestructorName=*/true, |
| 622 | /*AllowConstructorName=*/!(Tok.is(tok::identifier) && |
| 623 | NextToken().is(tok::equal)), |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 624 | /*AllowDeductionGuide=*/false, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 625 | nullptr, nullptr, D.Name)) |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 626 | return true; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 627 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 628 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 629 | if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc)) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 630 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ? |
Richard Smith | b115e5d | 2017-08-13 23:37:29 +0000 | [diff] [blame] | 631 | diag::warn_cxx17_compat_using_declaration_pack : |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 632 | diag::ext_using_declaration_pack); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 633 | |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration. |
| 638 | /// Assumes that 'using' was already seen. |
| 639 | /// |
| 640 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 641 | /// 'using' using-declarator-list[opt] ; |
| 642 | /// |
| 643 | /// using-declarator-list: [C++1z] |
| 644 | /// using-declarator '...'[opt] |
| 645 | /// using-declarator-list ',' using-declarator '...'[opt] |
| 646 | /// |
| 647 | /// using-declarator-list: [C++98-14] |
| 648 | /// using-declarator |
| 649 | /// |
| 650 | /// alias-declaration: C++11 [dcl.dcl]p1 |
| 651 | /// 'using' identifier attribute-specifier-seq[opt] = type-id ; |
| 652 | /// |
| 653 | Parser::DeclGroupPtrTy |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 654 | Parser::ParseUsingDeclaration(DeclaratorContext Context, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 655 | const ParsedTemplateInfo &TemplateInfo, |
| 656 | SourceLocation UsingLoc, SourceLocation &DeclEnd, |
| 657 | AccessSpecifier AS) { |
| 658 | // Check for misplaced attributes before the identifier in an |
| 659 | // alias-declaration. |
| 660 | ParsedAttributesWithRange MisplacedAttrs(AttrFactory); |
| 661 | MaybeParseCXX11Attributes(MisplacedAttrs); |
| 662 | |
| 663 | UsingDeclarator D; |
| 664 | bool InvalidDeclarator = ParseUsingDeclarator(Context, D); |
| 665 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 666 | ParsedAttributesWithRange Attrs(AttrFactory); |
Richard Smith | 37a45dd | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 667 | MaybeParseGNUAttributes(Attrs); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 668 | MaybeParseCXX11Attributes(Attrs); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 669 | |
| 670 | // Maybe this is an alias-declaration. |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 671 | if (Tok.is(tok::equal)) { |
| 672 | if (InvalidDeclarator) { |
| 673 | SkipUntil(tok::semi); |
| 674 | return nullptr; |
| 675 | } |
| 676 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 677 | // If we had any misplaced attributes from earlier, this is where they |
| 678 | // should have been written. |
| 679 | if (MisplacedAttrs.Range.isValid()) { |
| 680 | Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 681 | << FixItHint::CreateInsertionFromRange( |
| 682 | Tok.getLocation(), |
| 683 | CharSourceRange::getTokenRange(MisplacedAttrs.Range)) |
| 684 | << FixItHint::CreateRemoval(MisplacedAttrs.Range); |
| 685 | Attrs.takeAllFrom(MisplacedAttrs); |
| 686 | } |
| 687 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 688 | Decl *DeclFromDeclSpec = nullptr; |
| 689 | Decl *AD = ParseAliasDeclarationAfterDeclarator( |
| 690 | TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec); |
| 691 | return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 692 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 694 | // C++11 attributes are not allowed on a using-declaration, but GNU ones |
| 695 | // are. |
| 696 | ProhibitAttributes(MisplacedAttrs); |
| 697 | ProhibitAttributes(Attrs); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 698 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 699 | // Diagnose an attempt to declare a templated using-declaration. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 700 | // In C++11, alias-declarations can be templates: |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 701 | // template <...> using id = type; |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 702 | if (TemplateInfo.Kind) { |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 703 | SourceRange R = TemplateInfo.getSourceRange(); |
Craig Topper | 54a6a68 | 2015-11-14 18:16:08 +0000 | [diff] [blame] | 704 | Diag(UsingLoc, diag::err_templated_using_directive_declaration) |
| 705 | << 1 /* declaration */ << R << FixItHint::CreateRemoval(R); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 706 | |
| 707 | // Unfortunately, we have to bail out instead of recovering by |
| 708 | // ignoring the parameters, just in case the nested name specifier |
| 709 | // depends on the parameters. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 710 | return nullptr; |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 713 | SmallVector<Decl *, 8> DeclsInGroup; |
| 714 | while (true) { |
| 715 | // Parse (optional) attributes (most likely GNU strong-using extension). |
| 716 | MaybeParseGNUAttributes(Attrs); |
| 717 | |
| 718 | if (InvalidDeclarator) |
| 719 | SkipUntil(tok::comma, tok::semi, StopBeforeMatch); |
| 720 | else { |
| 721 | // "typename" keyword is allowed for identifiers only, |
| 722 | // because it may be a type definition. |
| 723 | if (D.TypenameLoc.isValid() && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 724 | D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 725 | Diag(D.Name.getSourceRange().getBegin(), |
| 726 | diag::err_typename_identifiers_only) |
| 727 | << FixItHint::CreateRemoval(SourceRange(D.TypenameLoc)); |
| 728 | // Proceed parsing, but discard the typename keyword. |
| 729 | D.TypenameLoc = SourceLocation(); |
| 730 | } |
| 731 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 732 | Decl *UD = Actions.ActOnUsingDeclaration(getCurScope(), AS, UsingLoc, |
| 733 | D.TypenameLoc, D.SS, D.Name, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 734 | D.EllipsisLoc, Attrs); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 735 | if (UD) |
| 736 | DeclsInGroup.push_back(UD); |
| 737 | } |
| 738 | |
| 739 | if (!TryConsumeToken(tok::comma)) |
| 740 | break; |
| 741 | |
| 742 | // Parse another using-declarator. |
| 743 | Attrs.clear(); |
| 744 | InvalidDeclarator = ParseUsingDeclarator(Context, D); |
Douglas Gregor | 882a61a | 2011-09-26 14:30:28 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 747 | if (DeclsInGroup.size() > 1) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 748 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ? |
Richard Smith | b115e5d | 2017-08-13 23:37:29 +0000 | [diff] [blame] | 749 | diag::warn_cxx17_compat_multi_using_declaration : |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 750 | diag::ext_multi_using_declaration); |
| 751 | |
| 752 | // Eat ';'. |
| 753 | DeclEnd = Tok.getLocation(); |
| 754 | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
| 755 | !Attrs.empty() ? "attributes list" |
| 756 | : "using declaration")) |
| 757 | SkipUntil(tok::semi); |
| 758 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 759 | return Actions.BuildDeclaratorGroup(DeclsInGroup); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 762 | Decl *Parser::ParseAliasDeclarationAfterDeclarator( |
| 763 | const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc, |
| 764 | UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS, |
| 765 | ParsedAttributes &Attrs, Decl **OwnedType) { |
| 766 | if (ExpectAndConsume(tok::equal)) { |
| 767 | SkipUntil(tok::semi); |
| 768 | return nullptr; |
| 769 | } |
| 770 | |
| 771 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ? |
| 772 | diag::warn_cxx98_compat_alias_declaration : |
| 773 | diag::ext_alias_declaration); |
| 774 | |
| 775 | // Type alias templates cannot be specialized. |
| 776 | int SpecKind = -1; |
| 777 | if (TemplateInfo.Kind == ParsedTemplateInfo::Template && |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 778 | D.Name.getKind() == UnqualifiedIdKind::IK_TemplateId) |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 779 | SpecKind = 0; |
| 780 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) |
| 781 | SpecKind = 1; |
| 782 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 783 | SpecKind = 2; |
| 784 | if (SpecKind != -1) { |
| 785 | SourceRange Range; |
| 786 | if (SpecKind == 0) |
| 787 | Range = SourceRange(D.Name.TemplateId->LAngleLoc, |
| 788 | D.Name.TemplateId->RAngleLoc); |
| 789 | else |
| 790 | Range = TemplateInfo.getSourceRange(); |
| 791 | Diag(Range.getBegin(), diag::err_alias_declaration_specialization) |
| 792 | << SpecKind << Range; |
| 793 | SkipUntil(tok::semi); |
| 794 | return nullptr; |
| 795 | } |
| 796 | |
| 797 | // Name must be an identifier. |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 798 | if (D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 799 | Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier); |
| 800 | // No removal fixit: can't recover from this. |
| 801 | SkipUntil(tok::semi); |
| 802 | return nullptr; |
| 803 | } else if (D.TypenameLoc.isValid()) |
| 804 | Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier) |
| 805 | << FixItHint::CreateRemoval(SourceRange( |
| 806 | D.TypenameLoc, |
| 807 | D.SS.isNotEmpty() ? D.SS.getEndLoc() : D.TypenameLoc)); |
| 808 | else if (D.SS.isNotEmpty()) |
| 809 | Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier) |
| 810 | << FixItHint::CreateRemoval(D.SS.getRange()); |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 811 | if (D.EllipsisLoc.isValid()) |
| 812 | Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion) |
| 813 | << FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc)); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 814 | |
| 815 | Decl *DeclFromDeclSpec = nullptr; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 816 | TypeResult TypeAlias = ParseTypeName( |
| 817 | nullptr, |
| 818 | TemplateInfo.Kind ? DeclaratorContext::AliasTemplateContext |
| 819 | : DeclaratorContext::AliasDeclContext, |
| 820 | AS, &DeclFromDeclSpec, &Attrs); |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 821 | if (OwnedType) |
| 822 | *OwnedType = DeclFromDeclSpec; |
| 823 | |
| 824 | // Eat ';'. |
| 825 | DeclEnd = Tok.getLocation(); |
| 826 | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
| 827 | !Attrs.empty() ? "attributes list" |
| 828 | : "alias declaration")) |
| 829 | SkipUntil(tok::semi); |
| 830 | |
| 831 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 832 | MultiTemplateParamsArg TemplateParamsArg( |
| 833 | TemplateParams ? TemplateParams->data() : nullptr, |
| 834 | TemplateParams ? TemplateParams->size() : 0); |
| 835 | return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 836 | UsingLoc, D.Name, Attrs, TypeAlias, |
| 837 | DeclFromDeclSpec); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 840 | /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 841 | /// |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 842 | /// [C++0x] static_assert-declaration: |
| 843 | /// static_assert ( constant-expression , string-literal ) ; |
| 844 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 845 | /// [C11] static_assert-declaration: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 846 | /// _Static_assert ( constant-expression , string-literal ) ; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 847 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 848 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 849 | assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) && |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 850 | "Not a static_assert declaration"); |
| 851 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 852 | if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11) |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 853 | Diag(Tok, diag::ext_c11_static_assert); |
Richard Smith | b15c11c | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 854 | if (Tok.is(tok::kw_static_assert)) |
| 855 | Diag(Tok, diag::warn_cxx98_compat_static_assert); |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 856 | |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 857 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 859 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 860 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 861 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Richard Smith | 7696571 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 862 | SkipMalformedDecl(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 863 | return nullptr; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 864 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Richard Smith | b301806 | 2017-06-06 01:34:24 +0000 | [diff] [blame] | 866 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 867 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 868 | ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext()); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 869 | if (AssertExpr.isInvalid()) { |
Richard Smith | 7696571 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 870 | SkipMalformedDecl(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 871 | return nullptr; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 872 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 874 | ExprResult AssertMessage; |
| 875 | if (Tok.is(tok::r_paren)) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 876 | Diag(Tok, getLangOpts().CPlusPlus17 |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 877 | ? diag::warn_cxx14_compat_static_assert_no_message |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 878 | : diag::ext_static_assert_no_message) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 879 | << (getLangOpts().CPlusPlus17 |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 880 | ? FixItHint() |
| 881 | : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\"")); |
| 882 | } else { |
| 883 | if (ExpectAndConsume(tok::comma)) { |
| 884 | SkipUntil(tok::semi); |
| 885 | return nullptr; |
| 886 | } |
Anders Carlsson | b4cf3ad | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 887 | |
Richard Smith | 085a64f | 2014-06-20 19:57:12 +0000 | [diff] [blame] | 888 | if (!isTokenStringLiteral()) { |
| 889 | Diag(Tok, diag::err_expected_string_literal) |
| 890 | << /*Source='static_assert'*/1; |
| 891 | SkipMalformedDecl(); |
| 892 | return nullptr; |
| 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 | AssertMessage = ParseStringLiteralExpression(); |
| 896 | if (AssertMessage.isInvalid()) { |
| 897 | SkipMalformedDecl(); |
| 898 | return nullptr; |
| 899 | } |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 900 | } |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 902 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 904 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 45d6bdf | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 905 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 906 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 907 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 908 | AssertExpr.get(), |
| 909 | AssertMessage.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 910 | T.getCloseLocation()); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 913 | /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier. |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 914 | /// |
| 915 | /// 'decltype' ( expression ) |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 916 | /// 'decltype' ( 'auto' ) [C++1y] |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 917 | /// |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 918 | SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 919 | assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype) |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 920 | && "Not a decltype specifier"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 921 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 922 | ExprResult Result; |
| 923 | SourceLocation StartLoc = Tok.getLocation(); |
| 924 | SourceLocation EndLoc; |
| 925 | |
| 926 | if (Tok.is(tok::annot_decltype)) { |
| 927 | Result = getExprAnnotation(Tok); |
| 928 | EndLoc = Tok.getAnnotationEndLoc(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 929 | ConsumeAnnotationToken(); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 930 | if (Result.isInvalid()) { |
| 931 | DS.SetTypeSpecError(); |
| 932 | return EndLoc; |
| 933 | } |
| 934 | } else { |
Richard Smith | 324df55 | 2012-02-24 22:30:04 +0000 | [diff] [blame] | 935 | if (Tok.getIdentifierInfo()->isStr("decltype")) |
| 936 | Diag(Tok, diag::warn_cxx98_compat_decltype); |
Richard Smith | fd3da93 | 2012-02-24 18:10:23 +0000 | [diff] [blame] | 937 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 938 | ConsumeToken(); |
| 939 | |
| 940 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 941 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 942 | "decltype", tok::r_paren)) { |
| 943 | DS.SetTypeSpecError(); |
| 944 | return T.getOpenLocation() == Tok.getLocation() ? |
| 945 | StartLoc : T.getOpenLocation(); |
| 946 | } |
| 947 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 948 | // Check for C++1y 'decltype(auto)'. |
| 949 | if (Tok.is(tok::kw_auto)) { |
| 950 | // No need to disambiguate here: an expression can't start with 'auto', |
| 951 | // because the typename-specifier in a function-style cast operation can't |
| 952 | // be 'auto'. |
| 953 | Diag(Tok.getLocation(), |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 954 | getLangOpts().CPlusPlus14 |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 955 | ? diag::warn_cxx11_compat_decltype_auto_type_specifier |
| 956 | : diag::ext_decltype_auto_type_specifier); |
| 957 | ConsumeToken(); |
| 958 | } else { |
| 959 | // Parse the expression |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 960 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 961 | // C++11 [dcl.type.simple]p4: |
| 962 | // The operand of the decltype specifier is an unevaluated operand. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 963 | EnterExpressionEvaluationContext Unevaluated( |
| 964 | Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr, |
Nicolas Lesser | b6d5c58 | 2018-07-12 18:45:41 +0000 | [diff] [blame] | 965 | Sema::ExpressionEvaluationContextRecord::EK_Decltype); |
Kaelyn Takata | 5cc8535 | 2015-04-10 19:16:46 +0000 | [diff] [blame] | 966 | Result = |
| 967 | Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) { |
| 968 | return E->hasPlaceholderType() ? ExprError() : E; |
| 969 | }); |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 970 | if (Result.isInvalid()) { |
| 971 | DS.SetTypeSpecError(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 972 | if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 973 | EndLoc = ConsumeParen(); |
Argyrios Kyrtzidis | c38395a | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 974 | } else { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 975 | if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) { |
| 976 | // Backtrack to get the location of the last token before the semi. |
| 977 | PP.RevertCachedTokens(2); |
| 978 | ConsumeToken(); // the semi. |
| 979 | EndLoc = ConsumeAnyToken(); |
| 980 | assert(Tok.is(tok::semi)); |
| 981 | } else { |
| 982 | EndLoc = Tok.getLocation(); |
| 983 | } |
Argyrios Kyrtzidis | c38395a | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 984 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 985 | return EndLoc; |
Argyrios Kyrtzidis | c38395a | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 986 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 987 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 988 | Result = Actions.ActOnDecltypeExpression(Result.get()); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | // Match the ')' |
| 992 | T.consumeClose(); |
| 993 | if (T.getCloseLocation().isInvalid()) { |
| 994 | DS.SetTypeSpecError(); |
| 995 | // FIXME: this should return the location of the last token |
| 996 | // that was consumed (by "consumeClose()") |
| 997 | return T.getCloseLocation(); |
| 998 | } |
| 999 | |
Richard Smith | fd555f6 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 1000 | if (Result.isInvalid()) { |
| 1001 | DS.SetTypeSpecError(); |
| 1002 | return T.getCloseLocation(); |
| 1003 | } |
| 1004 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1005 | EndLoc = T.getCloseLocation(); |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1006 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1007 | assert(!Result.isInvalid()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1009 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1010 | unsigned DiagID; |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1011 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1012 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1013 | if (Result.get() |
| 1014 | ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1015 | DiagID, Result.get(), Policy) |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1016 | : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1017 | DiagID, Policy)) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1018 | Diag(StartLoc, DiagID) << PrevSpec; |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1019 | DS.SetTypeSpecError(); |
| 1020 | } |
| 1021 | return EndLoc; |
| 1022 | } |
| 1023 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1024 | void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS, |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1025 | SourceLocation StartLoc, |
| 1026 | SourceLocation EndLoc) { |
| 1027 | // make sure we have a token we can turn into an annotation token |
| 1028 | if (PP.isBacktrackEnabled()) |
| 1029 | PP.RevertCachedTokens(1); |
| 1030 | else |
| 1031 | PP.EnterToken(Tok); |
| 1032 | |
| 1033 | Tok.setKind(tok::annot_decltype); |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 1034 | setExprAnnotation(Tok, |
| 1035 | DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() : |
| 1036 | DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() : |
| 1037 | ExprError()); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1038 | Tok.setAnnotationEndLoc(EndLoc); |
| 1039 | Tok.setLocation(StartLoc); |
| 1040 | PP.AnnotateCachedTokens(Tok); |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1043 | void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) { |
| 1044 | assert(Tok.is(tok::kw___underlying_type) && |
| 1045 | "Not an underlying type specifier"); |
| 1046 | |
| 1047 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1048 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1049 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 1050 | "__underlying_type", tok::r_paren)) { |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | TypeResult Result = ParseTypeName(); |
| 1055 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1056 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1061 | T.consumeClose(); |
| 1062 | if (T.getCloseLocation().isInvalid()) |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1063 | return; |
| 1064 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1065 | const char *PrevSpec = nullptr; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1066 | unsigned DiagID; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1067 | if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1068 | DiagID, Result.get(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1069 | Actions.getASTContext().getPrintingPolicy())) |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1070 | Diag(StartLoc, DiagID) << PrevSpec; |
Enea Zaffanella | a90af72 | 2013-07-06 18:54:58 +0000 | [diff] [blame] | 1071 | DS.setTypeofParensRange(T.getRange()); |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
David Blaikie | 00ee7a08 | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1074 | /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1075 | /// class name or decltype-specifier. Note that we only check that the result |
| 1076 | /// names a type; semantic analysis will need to verify that the type names a |
| 1077 | /// 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] | 1078 | /// name was found. |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1079 | /// |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1080 | /// base-type-specifier: [C++11 class.derived] |
David Blaikie | 00ee7a08 | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1081 | /// class-or-decltype |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1082 | /// class-or-decltype: [C++11 class.derived] |
David Blaikie | 00ee7a08 | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1083 | /// nested-name-specifier[opt] class-name |
| 1084 | /// decltype-specifier |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1085 | /// class-name: [C++ class.name] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1086 | /// identifier |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1087 | /// simple-template-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | /// |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1089 | /// In C++98, instead of base-type-specifier, we have: |
| 1090 | /// |
| 1091 | /// ::[opt] nested-name-specifier[opt] class-name |
Craig Topper | 9ad7e26 | 2014-10-31 06:57:07 +0000 | [diff] [blame] | 1092 | TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc, |
| 1093 | SourceLocation &EndLocation) { |
David Blaikie | dd58d4c | 2011-10-25 18:46:41 +0000 | [diff] [blame] | 1094 | // Ignore attempts to use typename |
| 1095 | if (Tok.is(tok::kw_typename)) { |
| 1096 | Diag(Tok, diag::err_expected_class_name_not_template) |
| 1097 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1098 | ConsumeToken(); |
| 1099 | } |
| 1100 | |
David Blaikie | afa155f | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 1101 | // Parse optional nested-name-specifier |
| 1102 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1103 | ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false); |
David Blaikie | afa155f | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 1104 | |
| 1105 | BaseLoc = Tok.getLocation(); |
| 1106 | |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1107 | // Parse decltype-specifier |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1108 | // 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] | 1109 | // isn't empty |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1110 | if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) { |
David Blaikie | afa155f | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 1111 | if (SS.isNotEmpty()) |
| 1112 | Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype) |
| 1113 | << FixItHint::CreateRemoval(SS.getRange()); |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1114 | // Fake up a Declarator to use with ActOnTypeName. |
| 1115 | DeclSpec DS(AttrFactory); |
| 1116 | |
David Blaikie | 7491e73 | 2011-12-08 04:53:15 +0000 | [diff] [blame] | 1117 | EndLocation = ParseDecltypeSpecifier(DS); |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1118 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1119 | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext); |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1120 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 1121 | } |
| 1122 | |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1123 | // Check whether we have a template-id that names a type. |
| 1124 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1125 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | 46c5961 | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 1126 | if (TemplateId->Kind == TNK_Type_template || |
| 1127 | TemplateId->Kind == TNK_Dependent_template_name) { |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 1128 | AnnotateTemplateIdTokenAsType(/*IsClassName*/true); |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1129 | |
| 1130 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1131 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1132 | EndLocation = Tok.getAnnotationEndLoc(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1133 | ConsumeAnnotationToken(); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (Type) |
| 1136 | return Type; |
| 1137 | return true; |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | // Fall through to produce an error below. |
| 1141 | } |
| 1142 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1143 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1144 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1145 | return true; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1148 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 1149 | SourceLocation IdLoc = ConsumeToken(); |
| 1150 | |
| 1151 | if (Tok.is(tok::less)) { |
| 1152 | // It looks the user intended to write a template-id here, but the |
| 1153 | // template-name was wrong. Try to fix that. |
| 1154 | TemplateNameKind TNK = TNK_Type_template; |
| 1155 | TemplateTy Template; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1156 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1157 | &SS, Template, TNK)) { |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1158 | Diag(IdLoc, diag::err_unknown_template_name) |
| 1159 | << Id; |
| 1160 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1161 | |
Serge Pavlov | b716b3c | 2013-08-10 05:54:47 +0000 | [diff] [blame] | 1162 | if (!Template) { |
| 1163 | TemplateArgList TemplateArgs; |
| 1164 | SourceLocation LAngleLoc, RAngleLoc; |
Richard Smith | 9a420f9 | 2017-05-10 21:47:30 +0000 | [diff] [blame] | 1165 | ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs, |
| 1166 | RAngleLoc); |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1167 | return true; |
Serge Pavlov | b716b3c | 2013-08-10 05:54:47 +0000 | [diff] [blame] | 1168 | } |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1169 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1170 | // Form the template name |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1171 | UnqualifiedId TemplateName; |
| 1172 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1174 | // Parse the full template-id, then turn it into a type. |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1175 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 1176 | TemplateName)) |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1177 | return true; |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 1178 | if (TNK == TNK_Type_template || TNK == TNK_Dependent_template_name) |
| 1179 | AnnotateTemplateIdTokenAsType(/*IsClassName*/true); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1180 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1181 | // If we didn't end up with a typename token, there's nothing more we |
| 1182 | // can do. |
| 1183 | if (Tok.isNot(tok::annot_typename)) |
| 1184 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1185 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1186 | // Retrieve the type from the annotation token, consume that token, and |
| 1187 | // return. |
| 1188 | EndLocation = Tok.getAnnotationEndLoc(); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1189 | ParsedType Type = getTypeAnnotation(Tok); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1190 | ConsumeAnnotationToken(); |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1191 | return Type; |
| 1192 | } |
| 1193 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1194 | // We have an identifier; check whether it is actually a type. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1195 | IdentifierInfo *CorrectedII = nullptr; |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1196 | ParsedType Type = Actions.getTypeName( |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 1197 | *Id, IdLoc, getCurScope(), &SS, /*IsClassName=*/true, false, nullptr, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1198 | /*IsCtorOrDtorName=*/false, |
| 1199 | /*NonTrivialTypeSourceInfo=*/true, |
| 1200 | /*IsClassTemplateDeductionContext*/ false, &CorrectedII); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1201 | if (!Type) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 1202 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1203 | return true; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | // Consume the identifier. |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 1207 | EndLocation = IdLoc; |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1210 | DeclSpec DS(AttrFactory); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1211 | DS.SetRangeStart(IdLoc); |
| 1212 | DS.SetRangeEnd(EndLocation); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1213 | DS.getTypeSpecScope() = SS; |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1214 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1215 | const char *PrevSpec = nullptr; |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1216 | unsigned DiagID; |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 1217 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type, |
| 1218 | Actions.getASTContext().getPrintingPolicy()); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1219 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1220 | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1221 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1224 | void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1225 | while (Tok.isOneOf(tok::kw___single_inheritance, |
| 1226 | tok::kw___multiple_inheritance, |
| 1227 | tok::kw___virtual_inheritance)) { |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1228 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 1229 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1230 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1231 | ParsedAttr::AS_Keyword); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1235 | /// Determine whether the following tokens are valid after a type-specifier |
| 1236 | /// which could be a standalone declaration. This will conservatively return |
| 1237 | /// true if there's any doubt, and is appropriate for insert-';' fixits. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1238 | bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1239 | // This switch enumerates the valid "follow" set for type-specifiers. |
| 1240 | switch (Tok.getKind()) { |
| 1241 | default: break; |
| 1242 | case tok::semi: // struct foo {...} ; |
| 1243 | case tok::star: // struct foo {...} * P; |
| 1244 | case tok::amp: // struct foo {...} & R = ... |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1245 | case tok::ampamp: // struct foo {...} && R = ... |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1246 | case tok::identifier: // struct foo {...} V ; |
| 1247 | case tok::r_paren: //(struct foo {...} ) {4} |
| 1248 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1249 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1250 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
| 1251 | case tok::l_paren: // struct foo {...} ( x); |
| 1252 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1253 | case tok::kw_operator: // struct foo operator ++() {...} |
Alp Toker | d3f79c5 | 2013-11-24 20:24:54 +0000 | [diff] [blame] | 1254 | case tok::kw___declspec: // struct foo {...} __declspec(...) |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 1255 | case tok::l_square: // void f(struct f [ 3]) |
| 1256 | case tok::ellipsis: // void f(struct f ... [Ns]) |
Abramo Bagnara | 152eb39 | 2014-08-16 08:29:27 +0000 | [diff] [blame] | 1257 | // FIXME: we should emit semantic diagnostic when declaration |
| 1258 | // attribute is in type attribute position. |
| 1259 | case tok::kw___attribute: // struct foo __attribute__((used)) x; |
David Majnemer | 15b311c | 2016-06-14 03:20:28 +0000 | [diff] [blame] | 1260 | case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop)); |
| 1261 | // struct foo {...} _Pragma(section(...)); |
| 1262 | case tok::annot_pragma_ms_pragma: |
| 1263 | // struct foo {...} _Pragma(vtordisp(pop)); |
| 1264 | case tok::annot_pragma_ms_vtordisp: |
| 1265 | // struct foo {...} _Pragma(pointers_to_members(...)); |
| 1266 | case tok::annot_pragma_ms_pointers_to_members: |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1267 | return true; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1268 | case tok::colon: |
| 1269 | return CouldBeBitfield; // enum E { ... } : 2; |
Reid Kleckner | cfa9155 | 2016-03-21 16:08:49 +0000 | [diff] [blame] | 1270 | // Microsoft compatibility |
| 1271 | case tok::kw___cdecl: // struct foo {...} __cdecl x; |
| 1272 | case tok::kw___fastcall: // struct foo {...} __fastcall x; |
| 1273 | case tok::kw___stdcall: // struct foo {...} __stdcall x; |
| 1274 | case tok::kw___thiscall: // struct foo {...} __thiscall x; |
| 1275 | case tok::kw___vectorcall: // struct foo {...} __vectorcall x; |
| 1276 | // We will diagnose these calling-convention specifiers on non-function |
| 1277 | // declarations later, so claim they are valid after a type specifier. |
| 1278 | return getLangOpts().MicrosoftExt; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1279 | // Type qualifiers |
| 1280 | case tok::kw_const: // struct foo {...} const x; |
| 1281 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1282 | case tok::kw_restrict: // struct foo {...} restrict x; |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 1283 | case tok::kw__Atomic: // struct foo {...} _Atomic x; |
Nico Rieck | 3e1ee83 | 2014-12-04 23:30:25 +0000 | [diff] [blame] | 1284 | case tok::kw___unaligned: // struct foo {...} __unaligned *x; |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1285 | // Function specifiers |
| 1286 | // Note, no 'explicit'. An explicit function must be either a conversion |
| 1287 | // operator or a constructor. Either way, it can't have a return type. |
| 1288 | case tok::kw_inline: // struct foo inline f(); |
| 1289 | case tok::kw_virtual: // struct foo virtual f(); |
| 1290 | case tok::kw_friend: // struct foo friend f(); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1291 | // Storage-class specifiers |
| 1292 | case tok::kw_static: // struct foo {...} static x; |
| 1293 | case tok::kw_extern: // struct foo {...} extern x; |
| 1294 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1295 | case tok::kw_register: // struct foo {...} register x; |
| 1296 | case tok::kw_auto: // struct foo {...} auto x; |
| 1297 | case tok::kw_mutable: // struct foo {...} mutable x; |
Richard Smith | 1ac67d1 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1298 | case tok::kw_thread_local: // struct foo {...} thread_local x; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1299 | case tok::kw_constexpr: // struct foo {...} constexpr x; |
| 1300 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1301 | // can occur after class specifiers according to the grammar. However, |
| 1302 | // almost no one actually writes code like this. If we see one of these, |
| 1303 | // it is much more likely that someone missed a semi colon and the |
| 1304 | // type/storage class specifier we're seeing is part of the *next* |
| 1305 | // intended declaration, as in: |
| 1306 | // |
| 1307 | // struct foo { ... } |
| 1308 | // typedef int X; |
| 1309 | // |
| 1310 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1311 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1312 | // the same declaration of X. Because of this, we look ahead past this |
| 1313 | // token to see if it's a type specifier. If so, we know the code is |
| 1314 | // otherwise invalid, so we can produce the expected semi error. |
| 1315 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1316 | return true; |
| 1317 | break; |
| 1318 | case tok::r_brace: // struct bar { struct foo {...} } |
| 1319 | // Missing ';' at end of struct is accepted as an extension in C mode. |
| 1320 | if (!getLangOpts().CPlusPlus) |
| 1321 | return true; |
| 1322 | break; |
Richard Smith | 52c5b87 | 2013-01-29 04:13:32 +0000 | [diff] [blame] | 1323 | case tok::greater: |
| 1324 | // template<class T = class X> |
| 1325 | return getLangOpts().CPlusPlus; |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1326 | } |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1330 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 1331 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 1332 | /// 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] | 1333 | /// cannot start a definition. |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1334 | /// |
| 1335 | /// class-specifier: [C++ class] |
| 1336 | /// class-head '{' member-specification[opt] '}' |
| 1337 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 1338 | /// class-head: |
| 1339 | /// class-key identifier[opt] base-clause[opt] |
| 1340 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 1341 | /// class-key nested-name-specifier[opt] simple-template-id |
| 1342 | /// base-clause[opt] |
| 1343 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1345 | /// identifier base-clause[opt] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1347 | /// simple-template-id base-clause[opt] |
| 1348 | /// class-key: |
| 1349 | /// 'class' |
| 1350 | /// 'struct' |
| 1351 | /// 'union' |
| 1352 | /// |
| 1353 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 1355 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 1356 | /// simple-template-id |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1357 | /// |
| 1358 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 1359 | /// together, subsume the C99 struct-or-union-specifier: |
| 1360 | /// |
| 1361 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 1362 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 1363 | /// struct-or-union identifier |
| 1364 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 1365 | /// '}' attributes[opt] |
| 1366 | /// [GNU] struct-or-union attributes[opt] identifier |
| 1367 | /// struct-or-union: |
| 1368 | /// 'struct' |
| 1369 | /// 'union' |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1370 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 1371 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1372 | const ParsedTemplateInfo &TemplateInfo, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1373 | AccessSpecifier AS, |
| 1374 | bool EnteringContext, DeclSpecContext DSC, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1375 | ParsedAttributesWithRange &Attributes) { |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1376 | DeclSpec::TST TagType; |
| 1377 | if (TagTokKind == tok::kw_struct) |
| 1378 | TagType = DeclSpec::TST_struct; |
| 1379 | else if (TagTokKind == tok::kw___interface) |
| 1380 | TagType = DeclSpec::TST_interface; |
| 1381 | else if (TagTokKind == tok::kw_class) |
| 1382 | TagType = DeclSpec::TST_class; |
| 1383 | else { |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1384 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 1385 | TagType = DeclSpec::TST_union; |
| 1386 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1388 | if (Tok.is(tok::code_completion)) { |
| 1389 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1390 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1391 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1392 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1393 | |
Chandler Carruth | 2d69ec7 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1394 | // C++03 [temp.explicit] 14.7.2/8: |
| 1395 | // The usual access checking rules do not apply to names used to specify |
| 1396 | // explicit instantiations. |
| 1397 | // |
| 1398 | // As an extension we do not perform access checking on the names used to |
| 1399 | // specify explicit specializations either. This is important to allow |
| 1400 | // specializing traits classes for private types. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1401 | // |
| 1402 | // Note that we don't suppress if this turns out to be an elaborated |
| 1403 | // type specifier. |
| 1404 | bool shouldDelayDiagsInTag = |
| 1405 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 1406 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 1407 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Chandler Carruth | 2d69ec7 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1408 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1409 | ParsedAttributesWithRange attrs(AttrFactory); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1410 | // If attributes exist after tag, parse them. |
Richard Smith | 37a45dd | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 1411 | MaybeParseGNUAttributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 1412 | MaybeParseMicrosoftDeclSpecs(attrs); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1413 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1414 | // Parse inheritance specifiers. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1415 | if (Tok.isOneOf(tok::kw___single_inheritance, |
| 1416 | tok::kw___multiple_inheritance, |
| 1417 | tok::kw___virtual_inheritance)) |
Richard Smith | 37a45dd | 2013-10-24 01:21:09 +0000 | [diff] [blame] | 1418 | ParseMicrosoftInheritanceClassAttributes(attrs); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1419 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1420 | // If C++0x attributes exist here, parse them. |
| 1421 | // FIXME: Are we consistent with the ordering of parsing of different |
| 1422 | // styles of attributes? |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1423 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1425 | // Source location used by FIXIT to insert misplaced |
| 1426 | // C++11 attributes |
| 1427 | SourceLocation AttrFixitLoc = Tok.getLocation(); |
| 1428 | |
Nico Weber | 7c3c5be | 2014-09-23 04:09:56 +0000 | [diff] [blame] | 1429 | if (TagType == DeclSpec::TST_struct && |
David Majnemer | 86330af | 2014-12-29 02:14:26 +0000 | [diff] [blame] | 1430 | Tok.isNot(tok::identifier) && |
| 1431 | !Tok.isAnnotation() && |
Nico Weber | 7c3c5be | 2014-09-23 04:09:56 +0000 | [diff] [blame] | 1432 | Tok.getIdentifierInfo() && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1433 | Tok.isOneOf(tok::kw___is_abstract, |
Eric Fiselier | 0736066 | 2017-04-12 22:12:15 +0000 | [diff] [blame] | 1434 | tok::kw___is_aggregate, |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1435 | tok::kw___is_arithmetic, |
| 1436 | tok::kw___is_array, |
David Majnemer | b3d9688 | 2016-05-23 17:21:55 +0000 | [diff] [blame] | 1437 | tok::kw___is_assignable, |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1438 | tok::kw___is_base_of, |
| 1439 | tok::kw___is_class, |
| 1440 | tok::kw___is_complete_type, |
| 1441 | tok::kw___is_compound, |
| 1442 | tok::kw___is_const, |
| 1443 | tok::kw___is_constructible, |
| 1444 | tok::kw___is_convertible, |
| 1445 | tok::kw___is_convertible_to, |
| 1446 | tok::kw___is_destructible, |
| 1447 | tok::kw___is_empty, |
| 1448 | tok::kw___is_enum, |
| 1449 | tok::kw___is_floating_point, |
| 1450 | tok::kw___is_final, |
| 1451 | tok::kw___is_function, |
| 1452 | tok::kw___is_fundamental, |
| 1453 | tok::kw___is_integral, |
| 1454 | tok::kw___is_interface_class, |
| 1455 | tok::kw___is_literal, |
| 1456 | tok::kw___is_lvalue_expr, |
| 1457 | tok::kw___is_lvalue_reference, |
| 1458 | tok::kw___is_member_function_pointer, |
| 1459 | tok::kw___is_member_object_pointer, |
| 1460 | tok::kw___is_member_pointer, |
| 1461 | tok::kw___is_nothrow_assignable, |
| 1462 | tok::kw___is_nothrow_constructible, |
| 1463 | tok::kw___is_nothrow_destructible, |
| 1464 | tok::kw___is_object, |
| 1465 | tok::kw___is_pod, |
| 1466 | tok::kw___is_pointer, |
| 1467 | tok::kw___is_polymorphic, |
| 1468 | tok::kw___is_reference, |
| 1469 | tok::kw___is_rvalue_expr, |
| 1470 | tok::kw___is_rvalue_reference, |
| 1471 | tok::kw___is_same, |
| 1472 | tok::kw___is_scalar, |
| 1473 | tok::kw___is_sealed, |
| 1474 | tok::kw___is_signed, |
| 1475 | tok::kw___is_standard_layout, |
| 1476 | tok::kw___is_trivial, |
| 1477 | tok::kw___is_trivially_assignable, |
| 1478 | tok::kw___is_trivially_constructible, |
| 1479 | tok::kw___is_trivially_copyable, |
| 1480 | tok::kw___is_union, |
| 1481 | tok::kw___is_unsigned, |
| 1482 | tok::kw___is_void, |
| 1483 | tok::kw___is_volatile)) |
Nico Weber | 7c3c5be | 2014-09-23 04:09:56 +0000 | [diff] [blame] | 1484 | // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the |
| 1485 | // name of struct templates, but some are keywords in GCC >= 4.3 |
| 1486 | // and Clang. Therefore, when we see the token sequence "struct |
| 1487 | // X", make X into a normal identifier rather than a keyword, to |
| 1488 | // allow libstdc++ 4.2 and libc++ to work properly. |
| 1489 | TryKeywordIdentFallback(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 1491 | struct PreserveAtomicIdentifierInfoRAII { |
| 1492 | PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled) |
| 1493 | : AtomicII(nullptr) { |
| 1494 | if (!Enabled) |
| 1495 | return; |
| 1496 | assert(Tok.is(tok::kw__Atomic)); |
| 1497 | AtomicII = Tok.getIdentifierInfo(); |
| 1498 | AtomicII->revertTokenIDToIdentifier(); |
| 1499 | Tok.setKind(tok::identifier); |
| 1500 | } |
| 1501 | ~PreserveAtomicIdentifierInfoRAII() { |
| 1502 | if (!AtomicII) |
| 1503 | return; |
| 1504 | AtomicII->revertIdentifierToTokenID(tok::kw__Atomic); |
| 1505 | } |
| 1506 | IdentifierInfo *AtomicII; |
| 1507 | }; |
| 1508 | |
| 1509 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 1510 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 1511 | // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider |
| 1512 | // '_Atomic' to be a keyword. We are careful to undo this so that clang can |
| 1513 | // use '_Atomic' in its own header files. |
| 1514 | bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat && |
| 1515 | Tok.is(tok::kw__Atomic) && |
| 1516 | TagType == DeclSpec::TST_struct; |
| 1517 | PreserveAtomicIdentifierInfoRAII AtomicTokenGuard( |
| 1518 | Tok, ShouldChangeAtomicToIdentifier); |
| 1519 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1520 | // Parse the (optional) nested-name-specifier. |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1521 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1522 | if (getLangOpts().CPlusPlus) { |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 1523 | // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it |
| 1524 | // is a base-specifier-list. |
Chris Lattner | d5c1c9d | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1525 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1526 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 1527 | CXXScopeSpec Spec; |
| 1528 | bool HasValidSpec = true; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 1529 | if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, EnteringContext)) { |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1530 | DS.SetTypeSpecError(); |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 1531 | HasValidSpec = false; |
| 1532 | } |
| 1533 | if (Spec.isSet()) |
| 1534 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1535 | Diag(Tok, diag::err_expected) << tok::identifier; |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 1536 | HasValidSpec = false; |
| 1537 | } |
| 1538 | if (HasValidSpec) |
| 1539 | SS = Spec; |
Chris Lattner | d5c1c9d | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1540 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1541 | |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1542 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 1543 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1544 | // Parse the (optional) class name or simple-template-id. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1545 | IdentifierInfo *Name = nullptr; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1546 | SourceLocation NameLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1547 | TemplateIdAnnotation *TemplateId = nullptr; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1548 | if (Tok.is(tok::identifier)) { |
| 1549 | Name = Tok.getIdentifierInfo(); |
| 1550 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1551 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1552 | if (Tok.is(tok::less) && getLangOpts().CPlusPlus) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1553 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1554 | // Eat the template argument list and try to continue parsing this as |
| 1555 | // a class (or template thereof). |
| 1556 | TemplateArgList TemplateArgs; |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1557 | SourceLocation LAngleLoc, RAngleLoc; |
Richard Smith | 9a420f9 | 2017-05-10 21:47:30 +0000 | [diff] [blame] | 1558 | if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs, |
| 1559 | RAngleLoc)) { |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1560 | // We couldn't parse the template argument list at all, so don't |
| 1561 | // try to give any location information for the list. |
| 1562 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 1563 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1564 | |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1565 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
Alp Toker | 01d65e1 | 2014-01-06 12:54:41 +0000 | [diff] [blame] | 1566 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 1567 | << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1568 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1569 | // Strip off the last template parameter list if it was empty, since |
Douglas Gregor | 1d0015f | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1570 | // we've removed its template argument list. |
| 1571 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
Hubert Tong | 97b0663 | 2016-04-13 18:41:03 +0000 | [diff] [blame] | 1572 | if (TemplateParams->size() > 1) { |
Douglas Gregor | 1d0015f | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1573 | TemplateParams->pop_back(); |
| 1574 | } else { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1575 | TemplateParams = nullptr; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1576 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 1d0015f | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1577 | = ParsedTemplateInfo::NonTemplate; |
| 1578 | } |
| 1579 | } else if (TemplateInfo.Kind |
| 1580 | == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1581 | // Pretend this is just a forward declaration. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1582 | TemplateParams = nullptr; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1583 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1584 | = ParsedTemplateInfo::NonTemplate; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1585 | const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc |
Douglas Gregor | 1d0015f | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1586 | = SourceLocation(); |
| 1587 | const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc |
| 1588 | = SourceLocation(); |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1589 | } |
Douglas Gregor | 916462b | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1590 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1591 | } else if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1592 | TemplateId = takeTemplateIdAnnotation(Tok); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1593 | NameLoc = ConsumeAnnotationToken(); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1594 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1595 | if (TemplateId->Kind != TNK_Type_template && |
| 1596 | TemplateId->Kind != TNK_Dependent_template_name) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1597 | // The template-name in the simple-template-id refers to |
| 1598 | // something other than a class template. Give an appropriate |
| 1599 | // error message and skip to the ';'. |
| 1600 | SourceRange Range(NameLoc); |
| 1601 | if (SS.isNotEmpty()) |
| 1602 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1603 | |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 1604 | // FIXME: Name may be null here. |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1605 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
Richard Trieu | 30f9385 | 2013-06-19 22:25:01 +0000 | [diff] [blame] | 1606 | << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1608 | DS.SetTypeSpecError(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1609 | SkipUntil(tok::semi, StopBeforeMatch); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1610 | return; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1611 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1614 | // There are four options here. |
| 1615 | // - If we are in a trailing return type, this is always just a reference, |
| 1616 | // and we must not try to parse a definition. For instance, |
| 1617 | // [] () -> struct S { }; |
| 1618 | // does not define a type. |
| 1619 | // - If we have 'struct foo {...', 'struct foo :...', |
| 1620 | // 'struct foo final :' or 'struct foo final {', then this is a definition. |
| 1621 | // - If we have 'struct foo;', then this is either a forward declaration |
| 1622 | // or a friend declaration, which have to be treated differently. |
| 1623 | // - Otherwise we have something like 'struct foo xyz', a reference. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1624 | // |
| 1625 | // We also detect these erroneous cases to provide better diagnostic for |
| 1626 | // C++11 attributes parsing. |
| 1627 | // - attributes follow class name: |
| 1628 | // struct foo [[]] {}; |
| 1629 | // - attributes appear before or after 'final': |
| 1630 | // struct foo [[]] final [[]] {}; |
| 1631 | // |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1632 | // However, in type-specifier-seq's, things look like declarations but are |
| 1633 | // just references, e.g. |
| 1634 | // new struct s; |
Sebastian Redl | 2b37272 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1635 | // or |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1636 | // &T::operator struct s; |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1637 | // For these, DSC is DeclSpecContext::DSC_type_specifier or |
| 1638 | // DeclSpecContext::DSC_alias_declaration. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1639 | |
| 1640 | // If there are attributes after class name, parse them. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1641 | MaybeParseCXX11Attributes(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1642 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1643 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1644 | Sema::TagUseKind TUK; |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1645 | if (DSC == DeclSpecContext::DSC_trailing) |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1646 | TUK = Sema::TUK_Reference; |
| 1647 | else if (Tok.is(tok::l_brace) || |
| 1648 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1649 | (isCXX11FinalKeyword() && |
David Blaikie | 9933a5a | 2012-03-12 15:39:49 +0000 | [diff] [blame] | 1650 | (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1651 | if (DS.isFriendSpecified()) { |
| 1652 | // C++ [class.friend]p2: |
| 1653 | // A class shall not be defined in a friend declaration. |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1654 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1655 | << SourceRange(DS.getFriendSpecLoc()); |
| 1656 | |
| 1657 | // Skip everything up to the semicolon, so that this looks like a proper |
| 1658 | // friend class (or template thereof) declaration. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1659 | SkipUntil(tok::semi, StopBeforeMatch); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1660 | TUK = Sema::TUK_Friend; |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1661 | } else { |
| 1662 | // Okay, this is a class definition. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1663 | TUK = Sema::TUK_Definition; |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1664 | } |
Richard Smith | 434516c | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1665 | } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) || |
| 1666 | NextToken().is(tok::kw_alignas))) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1667 | // We can't tell if this is a definition or reference |
| 1668 | // until we skipped the 'final' and C++11 attribute specifiers. |
| 1669 | TentativeParsingAction PA(*this); |
| 1670 | |
| 1671 | // Skip the 'final' keyword. |
| 1672 | ConsumeToken(); |
| 1673 | |
| 1674 | // Skip C++11 attribute specifiers. |
| 1675 | while (true) { |
| 1676 | if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) { |
| 1677 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1678 | if (!SkipUntil(tok::r_square, StopAtSemi)) |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1679 | break; |
Richard Smith | 434516c | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1680 | } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1681 | ConsumeToken(); |
| 1682 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1683 | if (!SkipUntil(tok::r_paren, StopAtSemi)) |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1684 | break; |
| 1685 | } else { |
| 1686 | break; |
| 1687 | } |
| 1688 | } |
| 1689 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1690 | if (Tok.isOneOf(tok::l_brace, tok::colon)) |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1691 | TUK = Sema::TUK_Definition; |
| 1692 | else |
| 1693 | TUK = Sema::TUK_Reference; |
| 1694 | |
| 1695 | PA.Revert(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 1696 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1697 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1698 | (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1699 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1700 | if (Tok.isNot(tok::semi)) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1701 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1702 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 1703 | ExpectAndConsume(tok::semi, diag::err_expected_after, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1704 | DeclSpec::getSpecifierName(TagType, PPol)); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1705 | PP.EnterToken(Tok); |
| 1706 | Tok.setKind(tok::semi); |
| 1707 | } |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1708 | } else |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1709 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1710 | |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1711 | // Forbid misplaced attributes. In cases of a reference, we pass attributes |
| 1712 | // to caller to handle. |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1713 | if (TUK != Sema::TUK_Reference) { |
| 1714 | // If this is not a reference, then the only possible |
| 1715 | // valid place for C++11 attributes to appear here |
| 1716 | // is between class-key and class-name. If there are |
| 1717 | // any attributes after class-name, we try a fixit to move |
| 1718 | // them to the right place. |
| 1719 | SourceRange AttrRange = Attributes.Range; |
| 1720 | if (AttrRange.isValid()) { |
| 1721 | Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed) |
| 1722 | << AttrRange |
| 1723 | << FixItHint::CreateInsertionFromRange(AttrFixitLoc, |
| 1724 | CharSourceRange(AttrRange, true)) |
| 1725 | << FixItHint::CreateRemoval(AttrRange); |
| 1726 | |
| 1727 | // Recover by adding misplaced attributes to the attribute list |
| 1728 | // of the class so they can be applied on the class later. |
| 1729 | attrs.takeAllFrom(Attributes); |
| 1730 | } |
| 1731 | } |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1732 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1733 | // If this is an elaborated type specifier, and we delayed |
| 1734 | // diagnostics before, just merge them into the current pool. |
| 1735 | if (shouldDelayDiagsInTag) { |
| 1736 | diagsFromTag.done(); |
| 1737 | if (TUK == Sema::TUK_Reference) |
| 1738 | diagsFromTag.redelay(); |
| 1739 | } |
| 1740 | |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1741 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1742 | TUK != Sema::TUK_Definition)) { |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1743 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 1744 | // We have a declaration or reference to an anonymous class. |
| 1745 | Diag(StartLoc, diag::err_anon_type_definition) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1746 | << DeclSpec::getSpecifierName(TagType, Policy); |
John McCall | 413021a | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1747 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1748 | |
David Majnemer | 3252fd0 | 2013-12-05 01:36:53 +0000 | [diff] [blame] | 1749 | // If we are parsing a definition and stop at a base-clause, continue on |
| 1750 | // until the semicolon. Continuing from the comma will just trick us into |
| 1751 | // thinking we are seeing a variable declaration. |
| 1752 | if (TUK == Sema::TUK_Definition && Tok.is(tok::colon)) |
| 1753 | SkipUntil(tok::semi, StopBeforeMatch); |
| 1754 | else |
| 1755 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1756 | return; |
| 1757 | } |
| 1758 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1759 | // Create the tag portion of the class or class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1760 | DeclResult TagOrTempResult = true; // invalid |
| 1761 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1762 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1763 | bool Owned = false; |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1764 | Sema::SkipBodyInfo SkipBody; |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1765 | if (TemplateId) { |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1766 | // Explicit specialization, class template partial specialization, |
| 1767 | // or explicit instantiation. |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1768 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1769 | TemplateId->NumArgs); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1770 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1771 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1772 | // This is an explicit instantiation of a class template. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1773 | ProhibitAttributes(attrs); |
| 1774 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1775 | TagOrTempResult = Actions.ActOnExplicitInstantiation( |
| 1776 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, |
| 1777 | TagType, StartLoc, SS, TemplateId->Template, |
| 1778 | TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, TemplateArgsPtr, |
| 1779 | TemplateId->RAngleLoc, attrs); |
John McCall | b7c5c27 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1780 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1781 | // Friend template-ids are treated as references unless |
| 1782 | // they have template headers, in which case they're ill-formed |
| 1783 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 1784 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1785 | } else if (TUK == Sema::TUK_Reference || |
| 1786 | (TUK == Sema::TUK_Friend && |
John McCall | b7c5c27 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1787 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1788 | ProhibitAttributes(attrs); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1789 | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1790 | TemplateId->SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1791 | TemplateId->TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1792 | TemplateId->Template, |
| 1793 | TemplateId->TemplateNameLoc, |
| 1794 | TemplateId->LAngleLoc, |
| 1795 | TemplateArgsPtr, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1796 | TemplateId->RAngleLoc); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1797 | } else { |
| 1798 | // This is an explicit specialization or a class template |
| 1799 | // partial specialization. |
| 1800 | TemplateParameterLists FakedParamLists; |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1801 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1802 | // This looks like an explicit instantiation, because we have |
| 1803 | // something like |
| 1804 | // |
| 1805 | // template class Foo<X> |
| 1806 | // |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1807 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1808 | // meant to be an explicit specialization, but the user forgot |
| 1809 | // the '<>' after 'template'. |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1810 | // It this is friend declaration however, since it cannot have a |
| 1811 | // template header, it is most likely that the user meant to |
| 1812 | // remove the 'template' keyword. |
Larisse Voufo | b9bbaba | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 1813 | assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) && |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1814 | "Expected a definition here"); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1815 | |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1816 | if (TUK == Sema::TUK_Friend) { |
| 1817 | Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1818 | TemplateParams = nullptr; |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1819 | } else { |
| 1820 | SourceLocation LAngleLoc = |
| 1821 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1822 | Diag(TemplateId->TemplateNameLoc, |
| 1823 | diag::err_explicit_instantiation_with_definition) |
| 1824 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1825 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1826 | |
| 1827 | // Create a fake template parameter list that contains only |
| 1828 | // "template<>", so that we treat this construct as a class |
| 1829 | // template specialization. |
| 1830 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 1831 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 1832 | LAngleLoc, nullptr)); |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1833 | TemplateParams = &FakedParamLists; |
| 1834 | } |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | // Build the class template specialization. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1838 | TagOrTempResult = Actions.ActOnClassTemplateSpecialization( |
| 1839 | getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1840 | *TemplateId, attrs, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1841 | MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] |
| 1842 | : nullptr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 1843 | TemplateParams ? TemplateParams->size() : 0), |
| 1844 | &SkipBody); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1845 | } |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1846 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1847 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1848 | // Explicit instantiation of a member of a class template |
| 1849 | // specialization, e.g., |
| 1850 | // |
| 1851 | // template struct Outer<int>::Inner; |
| 1852 | // |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1853 | ProhibitAttributes(attrs); |
| 1854 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1855 | TagOrTempResult = Actions.ActOnExplicitInstantiation( |
| 1856 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, |
| 1857 | TagType, StartLoc, SS, Name, NameLoc, attrs); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1858 | } else if (TUK == Sema::TUK_Friend && |
| 1859 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1860 | ProhibitAttributes(attrs); |
| 1861 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1862 | TagOrTempResult = Actions.ActOnTemplatedFriendTag( |
| 1863 | getCurScope(), DS.getFriendSpecLoc(), TagType, StartLoc, SS, Name, |
| 1864 | NameLoc, attrs, |
| 1865 | MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] : nullptr, |
| 1866 | TemplateParams ? TemplateParams->size() : 0)); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1867 | } else { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1868 | if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition) |
| 1869 | ProhibitAttributes(attrs); |
Richard Smith | 003c5e1 | 2013-11-08 19:03:29 +0000 | [diff] [blame] | 1870 | |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1871 | if (TUK == Sema::TUK_Definition && |
| 1872 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1873 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1874 | // recover by ignoring the 'template' keyword. |
| 1875 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1876 | << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1877 | TemplateParams = nullptr; |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1878 | } |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1879 | |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1880 | bool IsDependent = false; |
| 1881 | |
John McCall | 32723e9 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 1882 | // Don't pass down template parameter lists if this is just a tag |
| 1883 | // reference. For example, we don't need the template parameters here: |
| 1884 | // template <class T> class A *makeA(T t); |
| 1885 | MultiTemplateParamsArg TParams; |
| 1886 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 1887 | TParams = |
| 1888 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 1889 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1890 | stripTypeAttributesOffDeclSpec(attrs, DS, TUK); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1892 | // Declaration or definition of a class type |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1893 | TagOrTempResult = Actions.ActOnTag( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1894 | getCurScope(), TagType, TUK, StartLoc, SS, Name, NameLoc, attrs, AS, |
| 1895 | DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent, |
| 1896 | SourceLocation(), false, clang::TypeResult(), |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1897 | DSC == DeclSpecContext::DSC_type_specifier, |
| 1898 | DSC == DeclSpecContext::DSC_template_param || |
| 1899 | DSC == DeclSpecContext::DSC_template_type_arg, |
| 1900 | &SkipBody); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1901 | |
| 1902 | // If ActOnTag said the type was dependent, try again with the |
| 1903 | // less common call. |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1904 | if (IsDependent) { |
| 1905 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1906 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1907 | SS, Name, StartLoc, NameLoc); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1908 | } |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1909 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1911 | // If there is a body, parse it and inform the actions module. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1912 | if (TUK == Sema::TUK_Definition) { |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1913 | assert(Tok.is(tok::l_brace) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1914 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1915 | isCXX11FinalKeyword()); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1916 | if (SkipBody.ShouldSkip) |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 1917 | SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType, |
| 1918 | TagOrTempResult.get()); |
| 1919 | else if (getLangOpts().CPlusPlus) |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1920 | ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType, |
| 1921 | TagOrTempResult.get()); |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 1922 | else { |
| 1923 | Decl *D = |
| 1924 | SkipBody.CheckSameAsPrevious ? SkipBody.New : TagOrTempResult.get(); |
| 1925 | // Parse the definition body. |
| 1926 | ParseStructUnionBody(StartLoc, TagType, D); |
| 1927 | if (SkipBody.CheckSameAsPrevious && |
| 1928 | !Actions.ActOnDuplicateDefinition(DS, TagOrTempResult.get(), |
| 1929 | SkipBody)) { |
| 1930 | DS.SetTypeSpecError(); |
| 1931 | return; |
| 1932 | } |
| 1933 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 1936 | if (!TagOrTempResult.isInvalid()) |
Hiroshi Inoue | 939d932 | 2017-06-30 05:40:31 +0000 | [diff] [blame] | 1937 | // Delayed processing of attributes. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1938 | Actions.ProcessDeclAttributeDelayed(TagOrTempResult.get(), attrs); |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 1939 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1940 | const char *PrevSpec = nullptr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1941 | unsigned DiagID; |
| 1942 | bool Result; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1943 | if (!TypeResult.isInvalid()) { |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1944 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 1945 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1946 | PrevSpec, DiagID, TypeResult.get(), Policy); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1947 | } else if (!TagOrTempResult.isInvalid()) { |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1948 | Result = DS.SetTypeSpecType(TagType, StartLoc, |
| 1949 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1950 | PrevSpec, DiagID, TagOrTempResult.get(), Owned, |
| 1951 | Policy); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1952 | } else { |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1953 | DS.SetTypeSpecError(); |
Anders Carlsson | f83c9fa | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1954 | return; |
| 1955 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1957 | if (Result) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1958 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | cf25141 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1960 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 1961 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 1962 | // going to look at what comes after it to improve error recovery. If an |
| 1963 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 1964 | // the end of the declaration and recover that way. |
| 1965 | // |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1966 | // Also enforce C++ [temp]p3: |
| 1967 | // In a template-declaration which defines a class, no declarator |
| 1968 | // is permitted. |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 1969 | // |
| 1970 | // After a type-specifier, we don't expect a semicolon. This only happens in |
| 1971 | // C, since definitions are not permitted in this context in C++. |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1972 | if (TUK == Sema::TUK_Definition && |
Richard Smith | 843f18f | 2014-08-13 02:13:15 +0000 | [diff] [blame] | 1973 | (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) && |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1974 | (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) { |
Argyrios Kyrtzidis | e6f6913 | 2012-12-17 20:10:43 +0000 | [diff] [blame] | 1975 | if (Tok.isNot(tok::semi)) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1976 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 1977 | ExpectAndConsume(tok::semi, diag::err_expected_after, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1978 | DeclSpec::getSpecifierName(TagType, PPol)); |
Argyrios Kyrtzidis | e6f6913 | 2012-12-17 20:10:43 +0000 | [diff] [blame] | 1979 | // Push this token back into the preprocessor and change our current token |
| 1980 | // to ';' so that the rest of the code recovers as though there were an |
| 1981 | // ';' after the definition. |
| 1982 | PP.EnterToken(Tok); |
| 1983 | Tok.setKind(tok::semi); |
| 1984 | } |
Chris Lattner | cf25141 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1985 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1989 | /// |
| 1990 | /// base-clause : [C++ class.derived] |
| 1991 | /// ':' base-specifier-list |
| 1992 | /// base-specifier-list: |
| 1993 | /// base-specifier '...'[opt] |
| 1994 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1995 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1996 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 1997 | ConsumeToken(); |
| 1998 | |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1999 | // Build up an array of parsed base specifiers. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2000 | SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2002 | while (true) { |
| 2003 | // Parse a base-specifier. |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2004 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | f829825 | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 2005 | if (Result.isInvalid()) { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2006 | // Skip the rest of this base specifier, up until the comma or |
| 2007 | // opening brace. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2008 | SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2009 | } else { |
| 2010 | // Add this to our array of base specifiers. |
Douglas Gregor | f829825 | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 2011 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | // If the next token is a comma, consume it and keep reading |
| 2015 | // base-specifiers. |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2016 | if (!TryConsumeToken(tok::comma)) |
| 2017 | break; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2018 | } |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2019 | |
| 2020 | // Attach the base specifiers |
Craig Topper | aa700cb | 2015-12-27 21:55:19 +0000 | [diff] [blame] | 2021 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 2025 | /// one entry in the base class list of a class specifier, for example: |
| 2026 | /// class foo : public bar, virtual private baz { |
| 2027 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 2028 | /// |
| 2029 | /// base-specifier: [C++ class.derived] |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2030 | /// attribute-specifier-seq[opt] base-type-specifier |
| 2031 | /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt] |
| 2032 | /// base-type-specifier |
| 2033 | /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt] |
| 2034 | /// base-type-specifier |
Craig Topper | 9ad7e26 | 2014-10-31 06:57:07 +0000 | [diff] [blame] | 2035 | BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2036 | bool IsVirtual = false; |
| 2037 | SourceLocation StartLoc = Tok.getLocation(); |
| 2038 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2039 | ParsedAttributesWithRange Attributes(AttrFactory); |
| 2040 | MaybeParseCXX11Attributes(Attributes); |
| 2041 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2042 | // Parse the 'virtual' keyword. |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2043 | if (TryConsumeToken(tok::kw_virtual)) |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2044 | IsVirtual = true; |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2045 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2046 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 2047 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2048 | // Parse an (optional) access specifier. |
| 2049 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 2050 | if (Access != AS_none) |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2051 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2053 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 2054 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2055 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 2056 | // access specifier. |
| 2057 | if (Tok.is(tok::kw_virtual)) { |
| 2058 | SourceLocation VirtualLoc = ConsumeToken(); |
| 2059 | if (IsVirtual) { |
| 2060 | // Complain about duplicate 'virtual' |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2061 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2062 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | IsVirtual = true; |
| 2066 | } |
| 2067 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2068 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 2069 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2070 | // Parse the class-name. |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 2071 | |
| 2072 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 2073 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 2074 | // classes in <atomic>. Treat '_Atomic' to be an identifier when we are |
| 2075 | // parsing the class-name for a base specifier. |
| 2076 | if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && |
| 2077 | NextToken().is(tok::less)) |
| 2078 | Tok.setKind(tok::identifier); |
| 2079 | |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 2080 | SourceLocation EndLocation; |
David Blaikie | 1cd5002 | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 2081 | SourceLocation BaseLoc; |
| 2082 | TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2083 | if (BaseType.isInvalid()) |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2084 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2086 | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 2087 | // actually part of the base-specifier-list grammar productions, but we |
| 2088 | // parse it here for convenience. |
| 2089 | SourceLocation EllipsisLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 2090 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
| 2091 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | // Find the complete source range for the base-specifier. |
Douglas Gregor | d54dfb8 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 2093 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2095 | // Notify semantic analysis that we have parsed a complete |
| 2096 | // base-specifier. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2097 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual, |
| 2098 | Access, BaseType.get(), BaseLoc, |
| 2099 | EllipsisLoc); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 2103 | /// a C++ access-specifier. |
| 2104 | /// |
| 2105 | /// access-specifier: [C++ class.derived] |
| 2106 | /// 'private' |
| 2107 | /// 'protected' |
| 2108 | /// 'public' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2110 | switch (Tok.getKind()) { |
| 2111 | default: return AS_none; |
| 2112 | case tok::kw_private: return AS_private; |
| 2113 | case tok::kw_protected: return AS_protected; |
| 2114 | case tok::kw_public: return AS_public; |
| 2115 | } |
| 2116 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2117 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2118 | /// 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] | 2119 | /// delayed, e.g., default arguments or an exception-specification, create a |
| 2120 | /// late-parsed method declaration record to handle the parsing at the end of |
| 2121 | /// the class definition. |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2122 | void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo, |
| 2123 | Decl *ThisDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | DeclaratorChunk::FunctionTypeInfo &FTI |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2125 | = DeclaratorInfo.getFunctionTypeInfo(); |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2126 | // If there was a late-parsed exception-specification, we'll need a |
| 2127 | // late parse |
| 2128 | bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed; |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2129 | |
Nathan Sidwell | 5bb231c | 2015-02-19 14:03:22 +0000 | [diff] [blame] | 2130 | if (!NeedLateParse) { |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2131 | // Look ahead to see if there are any default args |
Nathan Sidwell | 5bb231c | 2015-02-19 14:03:22 +0000 | [diff] [blame] | 2132 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) { |
| 2133 | auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param); |
| 2134 | if (Param->hasUnparsedDefaultArg()) { |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2135 | NeedLateParse = true; |
| 2136 | break; |
| 2137 | } |
Nathan Sidwell | 5bb231c | 2015-02-19 14:03:22 +0000 | [diff] [blame] | 2138 | } |
| 2139 | } |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2140 | |
| 2141 | if (NeedLateParse) { |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2142 | // Push this method onto the stack of late-parsed method |
| 2143 | // declarations. |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2144 | auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2145 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
| 2146 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
| 2147 | |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2148 | // Stash the exception-specification tokens in the late-pased method. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2149 | LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2150 | FTI.ExceptionSpecTokens = nullptr; |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2151 | |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2152 | // Push tokens for each parameter. Those that do not have |
| 2153 | // defaults will be NULL. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2154 | LateMethod->DefaultArgs.reserve(FTI.NumParams); |
Nathan Sidwell | d5b9a1d | 2015-01-25 00:25:44 +0000 | [diff] [blame] | 2155 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) |
Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 2156 | LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument( |
Malcolm Parsons | ca9d834 | 2016-11-17 21:00:09 +0000 | [diff] [blame] | 2157 | FTI.Params[ParamIdx].Param, |
| 2158 | std::move(FTI.Params[ParamIdx].DefaultArgTokens))); |
Eli Friedman | 3af2a77 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2162 | /// isCXX11VirtSpecifier - Determine whether the given token is a C++11 |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2163 | /// virt-specifier. |
| 2164 | /// |
| 2165 | /// virt-specifier: |
| 2166 | /// override |
| 2167 | /// final |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2168 | /// __final |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2169 | VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const { |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2170 | if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier)) |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 2171 | return VirtSpecifiers::VS_None; |
| 2172 | |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2173 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2174 | |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2175 | // Initialize the contextual keywords. |
| 2176 | if (!Ident_final) { |
| 2177 | Ident_final = &PP.getIdentifierTable().get("final"); |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2178 | if (getLangOpts().GNUKeywords) |
| 2179 | Ident_GNU_final = &PP.getIdentifierTable().get("__final"); |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2180 | if (getLangOpts().MicrosoftExt) |
| 2181 | Ident_sealed = &PP.getIdentifierTable().get("sealed"); |
| 2182 | Ident_override = &PP.getIdentifierTable().get("override"); |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2185 | if (II == Ident_override) |
| 2186 | return VirtSpecifiers::VS_Override; |
| 2187 | |
| 2188 | if (II == Ident_sealed) |
| 2189 | return VirtSpecifiers::VS_Sealed; |
| 2190 | |
| 2191 | if (II == Ident_final) |
| 2192 | return VirtSpecifiers::VS_Final; |
| 2193 | |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2194 | if (II == Ident_GNU_final) |
| 2195 | return VirtSpecifiers::VS_GNU_Final; |
| 2196 | |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2197 | return VirtSpecifiers::VS_None; |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2200 | /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq. |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2201 | /// |
| 2202 | /// virt-specifier-seq: |
| 2203 | /// virt-specifier |
| 2204 | /// virt-specifier-seq virt-specifier |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2205 | void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS, |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2206 | bool IsInterface, |
| 2207 | SourceLocation FriendLoc) { |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2208 | while (true) { |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2209 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2210 | if (Specifier == VirtSpecifiers::VS_None) |
| 2211 | return; |
| 2212 | |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2213 | if (FriendLoc.isValid()) { |
| 2214 | Diag(Tok.getLocation(), diag::err_friend_decl_spec) |
| 2215 | << VirtSpecifiers::getSpecifierName(Specifier) |
| 2216 | << FixItHint::CreateRemoval(Tok.getLocation()) |
| 2217 | << SourceRange(FriendLoc, FriendLoc); |
| 2218 | ConsumeToken(); |
| 2219 | continue; |
| 2220 | } |
| 2221 | |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2222 | // C++ [class.mem]p8: |
| 2223 | // 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] | 2224 | const char *PrevSpec = nullptr; |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 2225 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2226 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
| 2227 | << PrevSpec |
| 2228 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2229 | |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2230 | if (IsInterface && (Specifier == VirtSpecifiers::VS_Final || |
| 2231 | Specifier == VirtSpecifiers::VS_Sealed)) { |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2232 | Diag(Tok.getLocation(), diag::err_override_control_interface) |
| 2233 | << VirtSpecifiers::getSpecifierName(Specifier); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2234 | } else if (Specifier == VirtSpecifiers::VS_Sealed) { |
| 2235 | Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword); |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 2236 | } else if (Specifier == VirtSpecifiers::VS_GNU_Final) { |
| 2237 | Diag(Tok.getLocation(), diag::ext_warn_gnu_final); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2238 | } else { |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2239 | Diag(Tok.getLocation(), |
| 2240 | getLangOpts().CPlusPlus11 |
| 2241 | ? diag::warn_cxx98_compat_override_control_keyword |
| 2242 | : diag::ext_override_control_keyword) |
| 2243 | << VirtSpecifiers::getSpecifierName(Specifier); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2244 | } |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 2245 | ConsumeToken(); |
| 2246 | } |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2249 | /// isCXX11FinalKeyword - Determine whether the next token is a C++11 |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2250 | /// 'final' or Microsoft 'sealed' contextual keyword. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2251 | bool Parser::isCXX11FinalKeyword() const { |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2252 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
| 2253 | return Specifier == VirtSpecifiers::VS_Final || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2254 | Specifier == VirtSpecifiers::VS_GNU_Final || |
Alp Toker | bb4b86a | 2014-01-09 00:13:52 +0000 | [diff] [blame] | 2255 | Specifier == VirtSpecifiers::VS_Sealed; |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2258 | /// Parse a C++ member-declarator up to, but not including, the optional |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2259 | /// brace-or-equal-initializer or pure-specifier. |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2260 | bool Parser::ParseCXXMemberDeclaratorBeforeInitializer( |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2261 | Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize, |
| 2262 | LateParsedAttrList &LateParsedAttrs) { |
| 2263 | // member-declarator: |
| 2264 | // declarator pure-specifier[opt] |
| 2265 | // declarator brace-or-equal-initializer[opt] |
| 2266 | // identifier[opt] ':' constant-expression |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 2267 | if (Tok.isNot(tok::colon)) |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2268 | ParseDeclarator(DeclaratorInfo); |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2269 | else |
| 2270 | DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2271 | |
| 2272 | if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) { |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2273 | assert(DeclaratorInfo.isPastIdentifier() && |
| 2274 | "don't know where identifier would go yet?"); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2275 | BitfieldSize = ParseConstantExpression(); |
| 2276 | if (BitfieldSize.isInvalid()) |
| 2277 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2278 | } else { |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2279 | ParseOptionalCXX11VirtSpecifierSeq( |
| 2280 | VS, getCurrentClass().IsInterface, |
| 2281 | DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2282 | if (!VS.isUnset()) |
| 2283 | MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS); |
| 2284 | } |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2285 | |
| 2286 | // If a simple-asm-expr is present, parse it. |
| 2287 | if (Tok.is(tok::kw_asm)) { |
| 2288 | SourceLocation Loc; |
| 2289 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 2290 | if (AsmLabel.isInvalid()) |
| 2291 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
| 2292 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2293 | DeclaratorInfo.setAsmLabel(AsmLabel.get()); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2294 | DeclaratorInfo.SetRangeEnd(Loc); |
| 2295 | } |
| 2296 | |
| 2297 | // If attributes exist after the declarator, but before an '{', parse them. |
| 2298 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
Richard Smith | 4b5a949 | 2014-01-24 22:34:35 +0000 | [diff] [blame] | 2299 | |
| 2300 | // For compatibility with code written to older Clang, also accept a |
| 2301 | // virt-specifier *after* the GNU attributes. |
Aaron Ballman | 5d153e3 | 2014-08-04 17:03:51 +0000 | [diff] [blame] | 2302 | if (BitfieldSize.isUnset() && VS.isUnset()) { |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 2303 | ParseOptionalCXX11VirtSpecifierSeq( |
| 2304 | VS, getCurrentClass().IsInterface, |
| 2305 | DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); |
Aaron Ballman | 5d153e3 | 2014-08-04 17:03:51 +0000 | [diff] [blame] | 2306 | if (!VS.isUnset()) { |
| 2307 | // If we saw any GNU-style attributes that are known to GCC followed by a |
| 2308 | // virt-specifier, issue a GCC-compat warning. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2309 | for (const ParsedAttr &AL : DeclaratorInfo.getAttributes()) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2310 | if (AL.isKnownToGCC() && !AL.isCXX11Attribute()) |
| 2311 | Diag(AL.getLoc(), diag::warn_gcc_attribute_location); |
| 2312 | |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2313 | MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS); |
Aaron Ballman | 5d153e3 | 2014-08-04 17:03:51 +0000 | [diff] [blame] | 2314 | } |
| 2315 | } |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2316 | |
| 2317 | // If this has neither a name nor a bit width, something has gone seriously |
| 2318 | // wrong. Skip until the semi-colon or }. |
| 2319 | if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) { |
| 2320 | // If so, skip until the semi-colon or a }. |
| 2321 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 2322 | return true; |
| 2323 | } |
| 2324 | return false; |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2327 | /// Look for declaration specifiers possibly occurring after C++11 |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2328 | /// virt-specifier-seq and diagnose them. |
| 2329 | void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq( |
| 2330 | Declarator &D, |
| 2331 | VirtSpecifiers &VS) { |
| 2332 | DeclSpec DS(AttrFactory); |
| 2333 | |
| 2334 | // GNU-style and C++11 attributes are not allowed here, but they will be |
| 2335 | // handled by the caller. Diagnose everything else. |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 2336 | ParseTypeQualifierListOpt( |
| 2337 | DS, AR_NoAttributesParsed, false, |
| 2338 | /*IdentifierRequired=*/false, llvm::function_ref<void()>([&]() { |
| 2339 | Actions.CodeCompleteFunctionQualifiers(DS, D, &VS); |
| 2340 | })); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2341 | D.ExtendWithDeclSpec(DS); |
| 2342 | |
| 2343 | if (D.isFunctionDeclarator()) { |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 2344 | auto &Function = D.getFunctionTypeInfo(); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2345 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 2346 | auto DeclSpecCheck = [&] (DeclSpec::TQ TypeQual, |
| 2347 | const char *FixItName, |
| 2348 | SourceLocation SpecLoc, |
| 2349 | unsigned* QualifierLoc) { |
| 2350 | FixItHint Insertion; |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2351 | if (DS.getTypeQualifiers() & TypeQual) { |
| 2352 | if (!(Function.TypeQuals & TypeQual)) { |
| 2353 | std::string Name(FixItName); |
| 2354 | Name += " "; |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 2355 | Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name); |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2356 | Function.TypeQuals |= TypeQual; |
| 2357 | *QualifierLoc = SpecLoc.getRawEncoding(); |
| 2358 | } |
| 2359 | Diag(SpecLoc, diag::err_declspec_after_virtspec) |
| 2360 | << FixItName |
| 2361 | << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) |
| 2362 | << FixItHint::CreateRemoval(SpecLoc) |
| 2363 | << Insertion; |
| 2364 | } |
| 2365 | }; |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2366 | DeclSpecCheck(DeclSpec::TQ_const, "const", DS.getConstSpecLoc(), |
| 2367 | &Function.ConstQualifierLoc); |
| 2368 | DeclSpecCheck(DeclSpec::TQ_volatile, "volatile", DS.getVolatileSpecLoc(), |
| 2369 | &Function.VolatileQualifierLoc); |
| 2370 | DeclSpecCheck(DeclSpec::TQ_restrict, "restrict", DS.getRestrictSpecLoc(), |
| 2371 | &Function.RestrictQualifierLoc); |
| 2372 | } |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 2373 | |
| 2374 | // Parse ref-qualifiers. |
| 2375 | bool RefQualifierIsLValueRef = true; |
| 2376 | SourceLocation RefQualifierLoc; |
| 2377 | if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) { |
| 2378 | const char *Name = (RefQualifierIsLValueRef ? "& " : "&& "); |
| 2379 | FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name); |
| 2380 | Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef; |
| 2381 | Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding(); |
| 2382 | |
| 2383 | Diag(RefQualifierLoc, diag::err_declspec_after_virtspec) |
| 2384 | << (RefQualifierIsLValueRef ? "&" : "&&") |
| 2385 | << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) |
| 2386 | << FixItHint::CreateRemoval(RefQualifierLoc) |
| 2387 | << Insertion; |
| 2388 | D.SetRangeEnd(RefQualifierLoc); |
| 2389 | } |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2393 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 2394 | /// |
| 2395 | /// member-declaration: |
| 2396 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 2397 | /// function-definition ';'[opt] |
| 2398 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 2399 | /// using-declaration [TODO] |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 2400 | /// [C++0x] static_assert-declaration |
Anders Carlsson | dfbbdf6 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 2401 | /// template-declaration |
Chris Lattner | d19c1c0 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2402 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2403 | /// |
| 2404 | /// member-declarator-list: |
| 2405 | /// member-declarator |
| 2406 | /// member-declarator-list ',' member-declarator |
| 2407 | /// |
| 2408 | /// member-declarator: |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2409 | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2410 | /// declarator constant-initializer[opt] |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2411 | /// [C++11] declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2412 | /// identifier[opt] ':' constant-expression |
| 2413 | /// |
Anders Carlsson | 11fdbbc | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 2414 | /// virt-specifier-seq: |
| 2415 | /// virt-specifier |
| 2416 | /// virt-specifier-seq virt-specifier |
| 2417 | /// |
| 2418 | /// virt-specifier: |
| 2419 | /// override |
| 2420 | /// final |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2421 | /// [MS] sealed |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2422 | /// |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2423 | /// pure-specifier: |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2424 | /// '= 0' |
| 2425 | /// |
| 2426 | /// constant-initializer: |
| 2427 | /// '=' constant-expression |
| 2428 | /// |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2429 | Parser::DeclGroupPtrTy |
| 2430 | Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2431 | ParsedAttributes &AccessAttrs, |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2432 | const ParsedTemplateInfo &TemplateInfo, |
| 2433 | ParsingDeclRAIIObject *TemplateDiags) { |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2434 | if (Tok.is(tok::at)) { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 2435 | if (getLangOpts().ObjC && NextToken().isObjCAtKeyword(tok::objc_defs)) |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2436 | Diag(Tok, diag::err_at_defs_cxx); |
| 2437 | else |
| 2438 | Diag(Tok, diag::err_at_in_class); |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2439 | |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2440 | ConsumeToken(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2441 | SkipUntil(tok::r_brace, StopAtSemi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2442 | return nullptr; |
Douglas Gregor | 23c8476 | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 2443 | } |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2444 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 2445 | // Turn on colon protection early, while parsing declspec, although there is |
| 2446 | // nothing to protect there. It prevents from false errors if error recovery |
| 2447 | // incorrectly determines where the declspec ends, as in the example: |
| 2448 | // struct A { enum class B { C }; }; |
| 2449 | // const int C = 4; |
| 2450 | // struct D { A::B : C; }; |
| 2451 | ColonProtectionRAIIObject X(*this); |
| 2452 | |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2453 | // Access declarations. |
Richard Smith | 45855df | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2454 | bool MalformedTypeSpec = false; |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2455 | if (!TemplateInfo.Kind && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2456 | Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) { |
Richard Smith | 45855df | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2457 | if (TryAnnotateCXXScopeToken()) |
| 2458 | MalformedTypeSpec = true; |
| 2459 | |
| 2460 | bool isAccessDecl; |
| 2461 | if (Tok.isNot(tok::annot_cxxscope)) |
| 2462 | isAccessDecl = false; |
| 2463 | else if (NextToken().is(tok::identifier)) |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2464 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 2465 | else |
| 2466 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 2467 | |
| 2468 | if (isAccessDecl) { |
| 2469 | // Collect the scope specifier token we annotated earlier. |
| 2470 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 2471 | ParseOptionalCXXScopeSpecifier(SS, nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2472 | /*EnteringContext=*/false); |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2473 | |
Nico Weber | ef03e70 | 2014-09-10 00:59:37 +0000 | [diff] [blame] | 2474 | if (SS.isInvalid()) { |
| 2475 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2476 | return nullptr; |
Nico Weber | ef03e70 | 2014-09-10 00:59:37 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2479 | // Try to parse an unqualified-id. |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2480 | SourceLocation TemplateKWLoc; |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2481 | UnqualifiedId Name; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2482 | if (ParseUnqualifiedId(SS, false, true, true, false, nullptr, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2483 | &TemplateKWLoc, Name)) { |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2484 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2485 | return nullptr; |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | // TODO: recover from mistakenly-qualified operator declarations. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2489 | if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
| 2490 | "access declaration")) { |
| 2491 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2492 | return nullptr; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2493 | } |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2494 | |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2495 | // FIXME: We should do something with the 'template' keyword here. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2496 | return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration( |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2497 | getCurScope(), AS, /*UsingLoc*/ SourceLocation(), |
| 2498 | /*TypenameLoc*/ SourceLocation(), SS, Name, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2499 | /*EllipsisLoc*/ SourceLocation(), |
| 2500 | /*AttrList*/ ParsedAttributesView()))); |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | |
Aaron Ballman | e7c544d | 2014-08-04 20:28:35 +0000 | [diff] [blame] | 2504 | // static_assert-declaration. A templated static_assert declaration is |
| 2505 | // diagnosed in Parser::ParseSingleDeclarationAfterTemplate. |
| 2506 | if (!TemplateInfo.Kind && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2507 | Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) { |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 2508 | SourceLocation DeclEnd; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2509 | return DeclGroupPtrTy::make( |
| 2510 | DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd))); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2511 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2513 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2515 | "Nested template improperly parsed?"); |
Richard Smith | 3af7009 | 2017-02-09 22:14:25 +0000 | [diff] [blame] | 2516 | ObjCDeclContextSwitch ObjCDC(*this); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 2517 | SourceLocation DeclEnd; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2518 | return DeclGroupPtrTy::make( |
Richard Smith | 3af7009 | 2017-02-09 22:14:25 +0000 | [diff] [blame] | 2519 | DeclGroupRef(ParseTemplateDeclarationOrSpecialization( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2520 | DeclaratorContext::MemberContext, DeclEnd, AccessAttrs, AS))); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2521 | } |
Anders Carlsson | dfbbdf6 | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | d19c1c0 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2523 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 2524 | if (Tok.is(tok::kw___extension__)) { |
| 2525 | // __extension__ silences extension warnings in the subexpression. |
| 2526 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 2527 | ConsumeToken(); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2528 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs, |
| 2529 | TemplateInfo, TemplateDiags); |
Chris Lattner | d19c1c0 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2530 | } |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2531 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2532 | ParsedAttributesWithRange attrs(AttrFactory); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2533 | ParsedAttributesViewWithRange FnAttrs; |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2534 | // Optional C++11 attribute-specifier |
| 2535 | MaybeParseCXX11Attributes(attrs); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2536 | // We need to keep these attributes for future diagnostic |
| 2537 | // before they are taken over by declaration specifier. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2538 | FnAttrs.addAll(attrs.begin(), attrs.end()); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2539 | FnAttrs.Range = attrs.Range; |
| 2540 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2541 | MaybeParseMicrosoftAttributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2542 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2543 | if (Tok.is(tok::kw_using)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2544 | ProhibitAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2546 | // Eat 'using'. |
| 2547 | SourceLocation UsingLoc = ConsumeToken(); |
| 2548 | |
| 2549 | if (Tok.is(tok::kw_namespace)) { |
| 2550 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2551 | SkipUntil(tok::semi, StopBeforeMatch); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2552 | return nullptr; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2553 | } |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2554 | SourceLocation DeclEnd; |
| 2555 | // Otherwise, it must be a using-declaration or an alias-declaration. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2556 | return ParseUsingDeclaration(DeclaratorContext::MemberContext, TemplateInfo, |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 2557 | UsingLoc, DeclEnd, AS); |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2560 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2561 | LateParsedAttrList CommonLateParsedAttrs; |
| 2562 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2563 | // decl-specifier-seq: |
| 2564 | // Parse the common declaration-specifiers piece. |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2565 | ParsingDeclSpec DS(*this, TemplateDiags); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2566 | DS.takeAttributesFrom(attrs); |
Richard Smith | 45855df | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2567 | if (MalformedTypeSpec) |
| 2568 | DS.SetTypeSpecError(); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2569 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2570 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DeclSpecContext::DSC_class, |
| 2571 | &CommonLateParsedAttrs); |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 2572 | |
| 2573 | // Turn off colon protection that was set for declspec. |
| 2574 | X.restore(); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2575 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2576 | // If we had a free-standing type definition with a missing semicolon, we |
| 2577 | // may get this far before the problem becomes obvious. |
| 2578 | if (DS.hasTagDefinition() && |
| 2579 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate && |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2580 | DiagnoseMissingSemiAfterTagDefinition(DS, AS, DeclSpecContext::DSC_class, |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2581 | &CommonLateParsedAttrs)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2582 | return nullptr; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2583 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2584 | MultiTemplateParamsArg TemplateParams( |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2585 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() |
| 2586 | : nullptr, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 2587 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 2588 | |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 2589 | if (TryConsumeToken(tok::semi)) { |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2590 | if (DS.isFriendSpecified()) |
| 2591 | ProhibitAttributes(FnAttrs); |
| 2592 | |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 2593 | RecordDecl *AnonRecord = nullptr; |
| 2594 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec( |
| 2595 | getCurScope(), AS, DS, TemplateParams, false, AnonRecord); |
John McCall | 796c2a5 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2596 | DS.complete(TheDecl); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 2597 | if (AnonRecord) { |
| 2598 | Decl* decls[] = {AnonRecord, TheDecl}; |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2599 | return Actions.BuildDeclaratorGroup(decls); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 2600 | } |
| 2601 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2602 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2603 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2604 | ParsingDeclarator DeclaratorInfo(*this, DS, DeclaratorContext::MemberContext); |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2605 | VirtSpecifiers VS; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2606 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2607 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2608 | LateParsedAttrList LateParsedAttrs; |
| 2609 | |
Douglas Gregor | 50cefbf | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2610 | SourceLocation EqualLoc; |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2611 | SourceLocation PureSpecLoc; |
| 2612 | |
Yaron Keren | 180c167 | 2015-06-30 07:35:19 +0000 | [diff] [blame] | 2613 | auto TryConsumePureSpecifier = [&] (bool AllowDefinition) { |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2614 | if (Tok.isNot(tok::equal)) |
| 2615 | return false; |
| 2616 | |
| 2617 | auto &Zero = NextToken(); |
| 2618 | SmallString<8> Buffer; |
| 2619 | if (Zero.isNot(tok::numeric_constant) || Zero.getLength() != 1 || |
| 2620 | PP.getSpelling(Zero, Buffer) != "0") |
| 2621 | return false; |
| 2622 | |
| 2623 | auto &After = GetLookAheadToken(2); |
| 2624 | if (!After.isOneOf(tok::semi, tok::comma) && |
| 2625 | !(AllowDefinition && |
| 2626 | After.isOneOf(tok::l_brace, tok::colon, tok::kw_try))) |
| 2627 | return false; |
| 2628 | |
| 2629 | EqualLoc = ConsumeToken(); |
| 2630 | PureSpecLoc = ConsumeToken(); |
| 2631 | return true; |
| 2632 | }; |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2633 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2634 | SmallVector<Decl *, 8> DeclsInGroup; |
| 2635 | ExprResult BitfieldSize; |
| 2636 | bool ExpectSemi = true; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2637 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2638 | // Parse the first declarator. |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2639 | if (ParseCXXMemberDeclaratorBeforeInitializer( |
| 2640 | DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) { |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2641 | TryConsumeToken(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2642 | return nullptr; |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2643 | } |
John Thompson | 5bc5cbe | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 2644 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2645 | // Check for a member function definition. |
Richard Smith | 4b5a949 | 2014-01-24 22:34:35 +0000 | [diff] [blame] | 2646 | if (BitfieldSize.isUnset()) { |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2647 | // MSVC permits pure specifier on inline functions defined at class scope. |
Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2648 | // Hence check for =0 before checking for function definition. |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2649 | if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction()) |
| 2650 | TryConsumePureSpecifier(/*AllowDefinition*/ true); |
Francois Pichet | 3abc9b8 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2652 | FunctionDefinitionKind DefinitionKind = FDK_Declaration; |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2653 | // function-definition: |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2654 | // |
| 2655 | // In C++11, a non-function declarator followed by an open brace is a |
| 2656 | // braced-init-list for an in-class member initialization, not an |
| 2657 | // erroneous function definition. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2658 | if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2659 | DefinitionKind = FDK_Definition; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2660 | } else if (DeclaratorInfo.isFunctionDeclarator()) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2661 | if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) { |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2662 | DefinitionKind = FDK_Definition; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2663 | } else if (Tok.is(tok::equal)) { |
| 2664 | const Token &KW = NextToken(); |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2665 | if (KW.is(tok::kw_default)) |
| 2666 | DefinitionKind = FDK_Defaulted; |
| 2667 | else if (KW.is(tok::kw_delete)) |
| 2668 | DefinitionKind = FDK_Deleted; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2669 | } |
| 2670 | } |
Eli Bendersky | 4184222 | 2015-03-23 23:49:41 +0000 | [diff] [blame] | 2671 | DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2672 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2673 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2674 | // to a friend declaration, that declaration shall be a definition. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2675 | if (DeclaratorInfo.isFunctionDeclarator() && |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2676 | DefinitionKind != FDK_Definition && DS.isFriendSpecified()) { |
| 2677 | // Diagnose attributes that appear before decl specifier: |
| 2678 | // [[]] friend int foo(); |
| 2679 | ProhibitAttributes(FnAttrs); |
| 2680 | } |
| 2681 | |
Nico Weber | a7f137d | 2015-01-16 19:35:01 +0000 | [diff] [blame] | 2682 | if (DefinitionKind != FDK_Declaration) { |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2683 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
Richard Trieu | 0d73054 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2684 | Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params); |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2685 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2686 | SkipUntil(tok::r_brace); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2687 | |
Douglas Gregor | 8a4db83 | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2688 | // Consume the optional ';' |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 2689 | TryConsumeToken(tok::semi); |
| 2690 | |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2691 | return nullptr; |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2692 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2693 | |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2694 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Richard Trieu | 0d73054 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2695 | Diag(DeclaratorInfo.getIdentifierLoc(), |
| 2696 | diag::err_function_declared_typedef); |
Douglas Gregor | 8a4db83 | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2697 | |
Richard Smith | 2603b09 | 2012-11-15 22:54:20 +0000 | [diff] [blame] | 2698 | // Recover by treating the 'typedef' as spurious. |
| 2699 | DS.ClearStorageClassSpecs(); |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2702 | Decl *FunDecl = |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2703 | ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo, |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2704 | VS, PureSpecLoc); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2705 | |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2706 | if (FunDecl) { |
| 2707 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) { |
| 2708 | CommonLateParsedAttrs[i]->addDecl(FunDecl); |
| 2709 | } |
| 2710 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) { |
| 2711 | LateParsedAttrs[i]->addDecl(FunDecl); |
| 2712 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2713 | } |
| 2714 | LateParsedAttrs.clear(); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Consume the ';' - it's optional unless we have a delete or default |
Richard Trieu | 2f7dc46 | 2012-05-16 19:04:59 +0000 | [diff] [blame] | 2717 | if (Tok.is(tok::semi)) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 2718 | ConsumeExtraSemi(AfterMemberFunctionDefinition); |
Douglas Gregor | 8a4db83 | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2719 | |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2720 | return DeclGroupPtrTy::make(DeclGroupRef(FunDecl)); |
Argyrios Kyrtzidis | f4ebe9e | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2721 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | // member-declarator-list: |
| 2725 | // member-declarator |
| 2726 | // member-declarator-list ',' member-declarator |
| 2727 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2728 | while (1) { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2729 | InClassInitStyle HasInClassInit = ICIS_NoInit; |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2730 | bool HasStaticInitializer = false; |
| 2731 | if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) { |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 2732 | if (DeclaratorInfo.isDeclarationOfFunction()) { |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2733 | // It's a pure-specifier. |
| 2734 | if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false)) |
| 2735 | // Parse it as an expression so that Sema can diagnose it. |
| 2736 | HasStaticInitializer = true; |
| 2737 | } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2738 | DeclSpec::SCS_static && |
| 2739 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2740 | DeclSpec::SCS_typedef && |
| 2741 | !DS.isFriendSpecified()) { |
| 2742 | // It's a default member initializer. |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 2743 | if (BitfieldSize.get()) |
| 2744 | Diag(Tok, getLangOpts().CPlusPlus2a |
| 2745 | ? diag::warn_cxx17_compat_bitfield_member_init |
| 2746 | : diag::ext_bitfield_member_init); |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2747 | HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2748 | } else { |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2749 | HasStaticInitializer = true; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2753 | // 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] | 2754 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2755 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2756 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2757 | NamedDecl *ThisDecl = nullptr; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2758 | if (DS.isFriendSpecified()) { |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2759 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2760 | // to a friend declaration, that declaration shall be a definition. |
| 2761 | // |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2762 | // Diagnose attributes that appear in a friend member function declarator: |
| 2763 | // friend int foo [[]] (); |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2764 | SmallVector<SourceRange, 4> Ranges; |
| 2765 | DeclaratorInfo.getCXX11AttributeRanges(Ranges); |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2766 | for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(), |
| 2767 | E = Ranges.end(); I != E; ++I) |
| 2768 | Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I; |
Michael Han | ddc016d | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2770 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2771 | TemplateParams); |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2772 | } else { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2773 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2774 | DeclaratorInfo, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2775 | TemplateParams, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2776 | BitfieldSize.get(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2777 | VS, HasInClassInit); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2778 | |
| 2779 | if (VarTemplateDecl *VT = |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2780 | ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2781 | // Re-direct this decl to refer to the templated decl so that we can |
| 2782 | // initialize it. |
| 2783 | ThisDecl = VT->getTemplatedDecl(); |
| 2784 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2785 | if (ThisDecl) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 2786 | Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs); |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2787 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2788 | |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2789 | // Error recovery might have converted a non-static member into a static |
| 2790 | // member. |
David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2791 | if (HasInClassInit != ICIS_NoInit && |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2792 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() == |
| 2793 | DeclSpec::SCS_static) { |
| 2794 | HasInClassInit = ICIS_NoInit; |
| 2795 | HasStaticInitializer = true; |
| 2796 | } |
| 2797 | |
| 2798 | if (ThisDecl && PureSpecLoc.isValid()) |
| 2799 | Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc); |
| 2800 | |
| 2801 | // Handle the initializer. |
| 2802 | if (HasInClassInit != ICIS_NoInit) { |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2803 | // The initializer was deferred; parse it and cache the tokens. |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2804 | Diag(Tok, getLangOpts().CPlusPlus11 |
| 2805 | ? diag::warn_cxx98_compat_nonstatic_member_init |
| 2806 | : diag::ext_nonstatic_member_init); |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2807 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2808 | if (DeclaratorInfo.isArrayOfUnknownBound()) { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2809 | // C++11 [dcl.array]p3: An array bound may also be omitted when the |
| 2810 | // declarator is followed by an initializer. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2811 | // |
| 2812 | // A brace-or-equal-initializer for a member-declarator is not an |
David Blaikie | cdd91db | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2813 | // initializer in the grammar, so this is ill-formed. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2814 | Diag(Tok, diag::err_incomplete_array_member_init); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2815 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2816 | |
| 2817 | // Avoid later warnings about a class member of incomplete type. |
David Blaikie | cdd91db | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2818 | if (ThisDecl) |
David Blaikie | cdd91db | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2819 | ThisDecl->setInvalidDecl(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2820 | } else |
| 2821 | ParseCXXNonStaticMemberInitializer(ThisDecl); |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2822 | } else if (HasStaticInitializer) { |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2823 | // Normal initializer. |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2824 | ExprResult Init = ParseCXXMemberInitializer( |
| 2825 | ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2826 | |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2827 | if (Init.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2828 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2829 | else if (ThisDecl) |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2830 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid()); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2831 | } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2832 | // No initializer. |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2833 | Actions.ActOnUninitializedDecl(ThisDecl); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2834 | |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2835 | if (ThisDecl) { |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2836 | if (!ThisDecl->isInvalidDecl()) { |
| 2837 | // Set the Decl for any late parsed attributes |
| 2838 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) |
| 2839 | CommonLateParsedAttrs[i]->addDecl(ThisDecl); |
| 2840 | |
| 2841 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) |
| 2842 | LateParsedAttrs[i]->addDecl(ThisDecl); |
| 2843 | } |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2844 | Actions.FinalizeDeclaration(ThisDecl); |
| 2845 | DeclsInGroup.push_back(ThisDecl); |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2846 | |
| 2847 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 2848 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2849 | DeclSpec::SCS_typedef) |
| 2850 | HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2851 | } |
David Majnemer | 23252a3 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2852 | LateParsedAttrs.clear(); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2853 | |
| 2854 | DeclaratorInfo.complete(ThisDecl); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2855 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2856 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2857 | // or an error, bail out. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 2858 | SourceLocation CommaLoc; |
| 2859 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2860 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2862 | if (Tok.isAtStartOfLine() && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2863 | !MightBeDeclarator(DeclaratorContext::MemberContext)) { |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2864 | // This comma was followed by a line-break and something which can't be |
| 2865 | // the start of a declarator. The comma was probably a typo for a |
| 2866 | // semicolon. |
| 2867 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2868 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2869 | ExpectSemi = false; |
| 2870 | break; |
| 2871 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2873 | // Parse the next declarator. |
| 2874 | DeclaratorInfo.clear(); |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2875 | VS.clear(); |
Nico Weber | f56c85b | 2015-01-17 02:26:40 +0000 | [diff] [blame] | 2876 | BitfieldSize = ExprResult(/*Invalid=*/false); |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2877 | EqualLoc = PureSpecLoc = SourceLocation(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2878 | DeclaratorInfo.setCommaLoc(CommaLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2879 | |
Richard Smith | 72553fc | 2014-01-23 23:53:27 +0000 | [diff] [blame] | 2880 | // GNU attributes are allowed before the second and subsequent declarator. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2881 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2882 | |
Nico Weber | d89e6f7 | 2015-01-16 19:34:13 +0000 | [diff] [blame] | 2883 | if (ParseCXXMemberDeclaratorBeforeInitializer( |
| 2884 | DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) |
| 2885 | break; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2888 | if (ExpectSemi && |
| 2889 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
Chris Lattner | 916dbf1 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2890 | // Skip to end of block or statement. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2891 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 916dbf1 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2892 | // If we stopped at a ';', eat it. |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 2893 | TryConsumeToken(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2894 | return nullptr; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 2897 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2900 | /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer. |
| 2901 | /// Also detect and reject any attempted defaulted/deleted function definition. |
| 2902 | /// The location of the '=', if any, will be placed in EqualLoc. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2903 | /// |
Richard Smith | 9ba0fec | 2015-06-30 01:28:56 +0000 | [diff] [blame] | 2904 | /// This does not check for a pure-specifier; that's handled elsewhere. |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2905 | /// |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2906 | /// brace-or-equal-initializer: |
| 2907 | /// '=' initializer-expression |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2908 | /// braced-init-list |
| 2909 | /// |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2910 | /// initializer-clause: |
| 2911 | /// assignment-expression |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2912 | /// braced-init-list |
| 2913 | /// |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 2914 | /// defaulted/deleted function-definition: |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2915 | /// '=' 'default' |
| 2916 | /// '=' 'delete' |
| 2917 | /// |
| 2918 | /// Prior to C++0x, the assignment-expression in an initializer-clause must |
| 2919 | /// be a constant-expression. |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2920 | ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2921 | SourceLocation &EqualLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2922 | assert(Tok.isOneOf(tok::equal, tok::l_brace) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2923 | && "Data member initializer not starting with '=' or '{'"); |
| 2924 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2925 | EnterExpressionEvaluationContext Context( |
| 2926 | Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, D); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 2927 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2928 | if (Tok.is(tok::kw_delete)) { |
| 2929 | // In principle, an initializer of '= delete p;' is legal, but it will |
| 2930 | // never type-check. It's better to diagnose it as an ill-formed expression |
| 2931 | // than as an ill-formed deleted non-function member. |
| 2932 | // An initializer of '= delete p, foo' will never be parsed, because |
| 2933 | // a top-level comma always ends the initializer expression. |
| 2934 | const Token &Next = NextToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2935 | if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2936 | if (IsFunction) |
| 2937 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2938 | << 1 /* delete */; |
| 2939 | else |
| 2940 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Richard Smith | edcb26e | 2014-06-11 00:49:52 +0000 | [diff] [blame] | 2941 | return ExprError(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2942 | } |
| 2943 | } else if (Tok.is(tok::kw_default)) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2944 | if (IsFunction) |
| 2945 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
| 2946 | << 0 /* default */; |
| 2947 | else |
| 2948 | Diag(ConsumeToken(), diag::err_default_special_members); |
Richard Smith | edcb26e | 2014-06-11 00:49:52 +0000 | [diff] [blame] | 2949 | return ExprError(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2950 | } |
David Majnemer | 87ff66c | 2014-12-13 11:34:16 +0000 | [diff] [blame] | 2951 | } |
| 2952 | if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) { |
| 2953 | Diag(Tok, diag::err_ms_property_initializer) << PD; |
| 2954 | return ExprError(); |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2955 | } |
| 2956 | return ParseInitializer(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 2959 | void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc, |
| 2960 | SourceLocation AttrFixitLoc, |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 2961 | unsigned TagType, Decl *TagDecl) { |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 2962 | // Skip the optional 'final' keyword. |
| 2963 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
| 2964 | assert(isCXX11FinalKeyword() && "not a class definition"); |
| 2965 | ConsumeToken(); |
| 2966 | |
| 2967 | // Diagnose any C++11 attributes after 'final' keyword. |
| 2968 | // We deliberately discard these attributes. |
| 2969 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2970 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
| 2971 | |
| 2972 | // This can only happen if we had malformed misplaced attributes; |
| 2973 | // we only get called if there is a colon or left-brace after the |
| 2974 | // attributes. |
| 2975 | if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace)) |
| 2976 | return; |
| 2977 | } |
| 2978 | |
| 2979 | // Skip the base clauses. This requires actually parsing them, because |
| 2980 | // otherwise we can't be sure where they end (a left brace may appear |
| 2981 | // within a template argument). |
| 2982 | if (Tok.is(tok::colon)) { |
| 2983 | // Enter the scope of the class so that we can correctly parse its bases. |
| 2984 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
| 2985 | ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true, |
| 2986 | TagType == DeclSpec::TST_interface); |
Richard Smith | 0f192e8 | 2015-06-11 22:48:25 +0000 | [diff] [blame] | 2987 | auto OldContext = |
| 2988 | Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 2989 | |
| 2990 | // Parse the bases but don't attach them to the class. |
| 2991 | ParseBaseClause(nullptr); |
| 2992 | |
Richard Smith | 0f192e8 | 2015-06-11 22:48:25 +0000 | [diff] [blame] | 2993 | Actions.ActOnTagFinishSkippedDefinition(OldContext); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 2994 | |
| 2995 | if (!Tok.is(tok::l_brace)) { |
| 2996 | Diag(PP.getLocForEndOfToken(PrevTokLocation), |
| 2997 | diag::err_expected_lbrace_after_base_specifiers); |
| 2998 | return; |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | // Skip the body. |
| 3003 | assert(Tok.is(tok::l_brace)); |
| 3004 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3005 | T.consumeOpen(); |
| 3006 | T.skipToEnd(); |
Richard Smith | 04c6c1f | 2015-07-01 18:56:50 +0000 | [diff] [blame] | 3007 | |
| 3008 | // Parse and discard any trailing attributes. |
| 3009 | ParsedAttributes Attrs(AttrFactory); |
| 3010 | if (Tok.is(tok::kw___attribute)) |
| 3011 | MaybeParseGNUAttributes(Attrs); |
Richard Smith | 65ebb4a | 2015-03-26 04:09:53 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3014 | Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas( |
| 3015 | AccessSpecifier &AS, ParsedAttributesWithRange &AccessAttrs, |
| 3016 | DeclSpec::TST TagType, Decl *TagDecl) { |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 3017 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 3018 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3019 | switch (Tok.getKind()) { |
| 3020 | case tok::kw___if_exists: |
| 3021 | case tok::kw___if_not_exists: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3022 | ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, AS); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3023 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3024 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3025 | case tok::semi: |
| 3026 | // Check for extraneous top-level semicolon. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3027 | ConsumeExtraSemi(InsideStruct, TagType); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3028 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3029 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3030 | // Handle pragmas that can appear as member declarations. |
| 3031 | case tok::annot_pragma_vis: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3032 | HandlePragmaVisibility(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3033 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3034 | case tok::annot_pragma_pack: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3035 | HandlePragmaPack(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3036 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3037 | case tok::annot_pragma_align: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3038 | HandlePragmaAlign(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3039 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3040 | case tok::annot_pragma_ms_pointers_to_members: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3041 | HandlePragmaMSPointersToMembers(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3042 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3043 | case tok::annot_pragma_ms_pragma: |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3044 | HandlePragmaMSPragma(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3045 | return nullptr; |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3046 | case tok::annot_pragma_ms_vtordisp: |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 3047 | HandlePragmaMSVtorDisp(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3048 | return nullptr; |
Richard Smith | b256d30 | 2017-01-28 01:20:57 +0000 | [diff] [blame] | 3049 | case tok::annot_pragma_dump: |
| 3050 | HandlePragmaDump(); |
| 3051 | return nullptr; |
Alexey Bataev | 3d42f34 | 2015-11-20 07:02:57 +0000 | [diff] [blame] | 3052 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3053 | case tok::kw_namespace: |
| 3054 | // If we see a namespace here, a close brace was missing somewhere. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3055 | DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl)); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3056 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3057 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3058 | case tok::kw_public: |
| 3059 | case tok::kw_protected: |
| 3060 | case tok::kw_private: { |
| 3061 | AccessSpecifier NewAS = getAccessSpecifierIfPresent(); |
| 3062 | assert(NewAS != AS_none); |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3063 | // Current token is a C++ access specifier. |
| 3064 | AS = NewAS; |
| 3065 | SourceLocation ASLoc = Tok.getLocation(); |
| 3066 | unsigned TokLength = Tok.getLength(); |
| 3067 | ConsumeToken(); |
| 3068 | AccessAttrs.clear(); |
| 3069 | MaybeParseGNUAttributes(AccessAttrs); |
| 3070 | |
| 3071 | SourceLocation EndLoc; |
| 3072 | if (TryConsumeToken(tok::colon, EndLoc)) { |
| 3073 | } else if (TryConsumeToken(tok::semi, EndLoc)) { |
| 3074 | Diag(EndLoc, diag::err_expected) |
| 3075 | << tok::colon << FixItHint::CreateReplacement(EndLoc, ":"); |
| 3076 | } else { |
| 3077 | EndLoc = ASLoc.getLocWithOffset(TokLength); |
| 3078 | Diag(EndLoc, diag::err_expected) |
| 3079 | << tok::colon << FixItHint::CreateInsertion(EndLoc, ":"); |
| 3080 | } |
| 3081 | |
| 3082 | // The Microsoft extension __interface does not permit non-public |
| 3083 | // access specifiers. |
| 3084 | if (TagType == DeclSpec::TST_interface && AS != AS_public) { |
| 3085 | Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected); |
| 3086 | } |
| 3087 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3088 | if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc, AccessAttrs)) { |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3089 | // found another attribute than only annotations |
| 3090 | AccessAttrs.clear(); |
| 3091 | } |
| 3092 | |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 3093 | return nullptr; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3096 | case tok::annot_pragma_openmp: |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 3097 | return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, AccessAttrs, TagType, |
| 3098 | TagDecl); |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3099 | |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3100 | default: |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3101 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs); |
Richard Smith | b55f758 | 2017-01-28 01:12:10 +0000 | [diff] [blame] | 3102 | } |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3105 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 3106 | /// |
| 3107 | /// member-specification: |
| 3108 | /// member-declaration member-specification[opt] |
| 3109 | /// access-specifier ':' member-specification[opt] |
| 3110 | /// |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 3111 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 3112 | SourceLocation AttrFixitLoc, |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 3113 | ParsedAttributesWithRange &Attrs, |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3114 | unsigned TagType, Decl *TagDecl) { |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 3115 | assert((TagType == DeclSpec::TST_struct || |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3116 | TagType == DeclSpec::TST_interface || |
| 3117 | TagType == DeclSpec::TST_union || |
| 3118 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 3119 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 3120 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3121 | "parsing struct/union/class body"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3123 | // Determine whether this is a non-nested class. Note that local |
| 3124 | // classes are *not* considered to be nested classes. |
| 3125 | bool NonNestedClass = true; |
| 3126 | if (!ClassStack.empty()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3127 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3128 | if (S->isClassScope()) { |
| 3129 | // We're inside a class scope, so this is a nested class. |
| 3130 | NonNestedClass = false; |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3131 | |
| 3132 | // The Microsoft extension __interface does not permit nested classes. |
| 3133 | if (getCurrentClass().IsInterface) { |
| 3134 | Diag(RecordLoc, diag::err_invalid_member_in_interface) |
| 3135 | << /*ErrorType=*/6 |
| 3136 | << (isa<NamedDecl>(TagDecl) |
| 3137 | ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString() |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 3138 | : "(anonymous)"); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3139 | } |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3140 | break; |
| 3141 | } |
| 3142 | |
Serge Pavlov | d9c0bcf | 2015-07-14 10:02:10 +0000 | [diff] [blame] | 3143 | if ((S->getFlags() & Scope::FnScope)) |
| 3144 | // If we're in a function or function template then this is a local |
| 3145 | // class rather than a nested class. |
| 3146 | break; |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3147 | } |
| 3148 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3149 | |
| 3150 | // Enter a scope for the class. |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3151 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3152 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3153 | // Note that we are parsing a new (potentially-nested) class definition. |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3154 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass, |
| 3155 | TagType == DeclSpec::TST_interface); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3156 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3157 | if (TagDecl) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3158 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3159 | |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3160 | SourceLocation FinalLoc; |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3161 | bool IsFinalSpelledSealed = false; |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Parse the optional 'final' keyword. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3164 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3165 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok); |
| 3166 | assert((Specifier == VirtSpecifiers::VS_Final || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3167 | Specifier == VirtSpecifiers::VS_GNU_Final || |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3168 | Specifier == VirtSpecifiers::VS_Sealed) && |
| 3169 | "not a class definition"); |
Richard Smith | da26111 | 2011-10-15 04:21:46 +0000 | [diff] [blame] | 3170 | FinalLoc = ConsumeToken(); |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3171 | IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed; |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3172 | |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3173 | if (TagType == DeclSpec::TST_interface) |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3174 | Diag(FinalLoc, diag::err_override_control_interface) |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3175 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 3176 | else if (Specifier == VirtSpecifiers::VS_Final) |
| 3177 | Diag(FinalLoc, getLangOpts().CPlusPlus11 |
| 3178 | ? diag::warn_cxx98_compat_override_control_keyword |
| 3179 | : diag::ext_override_control_keyword) |
| 3180 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 3181 | else if (Specifier == VirtSpecifiers::VS_Sealed) |
| 3182 | Diag(FinalLoc, diag::ext_ms_sealed_keyword); |
Andrey Bokhanko | 276055b | 2016-07-29 10:42:48 +0000 | [diff] [blame] | 3183 | else if (Specifier == VirtSpecifiers::VS_GNU_Final) |
| 3184 | Diag(FinalLoc, diag::ext_warn_gnu_final); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3185 | |
Michael Han | 309af29 | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 3186 | // Parse any C++11 attributes after 'final' keyword. |
| 3187 | // These attributes are not allowed to appear here, |
| 3188 | // and the only possible place for them to appertain |
| 3189 | // to the class would be between class-key and class-name. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 3190 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
Nico Weber | 4b4be84 | 2014-12-29 06:56:50 +0000 | [diff] [blame] | 3191 | |
| 3192 | // ParseClassSpecifier() does only a superficial check for attributes before |
| 3193 | // deciding to call this method. For example, for |
| 3194 | // `class C final alignas ([l) {` it will decide that this looks like a |
| 3195 | // misplaced attribute since it sees `alignas '(' ')'`. But the actual |
| 3196 | // attribute parsing code will try to parse the '[' as a constexpr lambda |
| 3197 | // and consume enough tokens that the alignas parsing code will eat the |
| 3198 | // 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] | 3199 | if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) { |
| 3200 | if (TagDecl) |
| 3201 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
Nico Weber | 4b4be84 | 2014-12-29 06:56:50 +0000 | [diff] [blame] | 3202 | return; |
Nico Weber | 36de3a2 | 2014-12-29 21:56:22 +0000 | [diff] [blame] | 3203 | } |
Anders Carlsson | f9eb63b | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 3204 | } |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 3205 | |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3206 | if (Tok.is(tok::colon)) { |
Erik Verbruggen | 6524c05 | 2017-10-24 13:46:58 +0000 | [diff] [blame] | 3207 | ParseScope InheritanceScope(this, getCurScope()->getFlags() | |
| 3208 | Scope::ClassInheritanceScope); |
| 3209 | |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3210 | ParseBaseClause(TagDecl); |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3211 | if (!Tok.is(tok::l_brace)) { |
Ismail Pazarbasi | 129c44c | 2014-09-25 21:13:02 +0000 | [diff] [blame] | 3212 | bool SuggestFixIt = false; |
| 3213 | SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3214 | if (Tok.isAtStartOfLine()) { |
| 3215 | switch (Tok.getKind()) { |
| 3216 | case tok::kw_private: |
| 3217 | case tok::kw_protected: |
| 3218 | case tok::kw_public: |
| 3219 | SuggestFixIt = NextToken().getKind() == tok::colon; |
| 3220 | break; |
| 3221 | case tok::kw_static_assert: |
| 3222 | case tok::r_brace: |
| 3223 | case tok::kw_using: |
| 3224 | // base-clause can have simple-template-id; 'template' can't be there |
| 3225 | case tok::kw_template: |
| 3226 | SuggestFixIt = true; |
| 3227 | break; |
| 3228 | case tok::identifier: |
| 3229 | SuggestFixIt = isConstructorDeclarator(true); |
| 3230 | break; |
| 3231 | default: |
| 3232 | SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); |
| 3233 | break; |
| 3234 | } |
| 3235 | } |
| 3236 | DiagnosticBuilder LBraceDiag = |
| 3237 | Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers); |
| 3238 | if (SuggestFixIt) { |
| 3239 | LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {"); |
| 3240 | // Try recovering from missing { after base-clause. |
| 3241 | PP.EnterToken(Tok); |
| 3242 | Tok.setKind(tok::l_brace); |
| 3243 | } else { |
| 3244 | if (TagDecl) |
| 3245 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
| 3246 | return; |
| 3247 | } |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | assert(Tok.is(tok::l_brace)); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3252 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3253 | T.consumeOpen(); |
John McCall | 2d814c3 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 3254 | |
John McCall | 08bede4 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 3255 | if (TagDecl) |
Anders Carlsson | 30f2944 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 3256 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 3257 | IsFinalSpelledSealed, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3258 | T.getOpenLocation()); |
John McCall | 1c7e6ec | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 3259 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3260 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 3261 | // by default. Members of a class defined with the keywords struct or union |
| 3262 | // are public by default. |
| 3263 | AccessSpecifier CurAS; |
| 3264 | if (TagType == DeclSpec::TST_class) |
| 3265 | CurAS = AS_private; |
| 3266 | else |
| 3267 | CurAS = AS_public; |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3268 | ParsedAttributesWithRange AccessAttrs(AttrFactory); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3270 | if (TagDecl) { |
| 3271 | // While we still have something to read, read the member-declarations. |
Richard Smith | 752ada8 | 2015-11-17 23:32:01 +0000 | [diff] [blame] | 3272 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 3273 | Tok.isNot(tok::eof)) { |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3274 | // Each iteration of this loop reads one member-declaration. |
Alexey Bataev | 05c25d6 | 2015-07-31 08:42:25 +0000 | [diff] [blame] | 3275 | ParseCXXClassMemberDeclarationWithPragmas( |
| 3276 | CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl); |
Serge Pavlov | c4e04a2 | 2015-09-19 05:32:57 +0000 | [diff] [blame] | 3277 | } |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3278 | T.consumeClose(); |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3279 | } else { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3280 | SkipUntil(tok::r_brace); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3281 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3283 | // If attributes exist after class contents, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3284 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3285 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3286 | |
John McCall | 08bede4 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 3287 | if (TagDecl) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3288 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3289 | T.getOpenLocation(), |
| 3290 | T.getCloseLocation(), attrs); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3291 | |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 3292 | // C++11 [class.mem]p2: |
| 3293 | // Within the class member-specification, the class is regarded as complete |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3294 | // within function bodies, default arguments, exception-specifications, and |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 3295 | // brace-or-equal-initializers for non-static data members (including such |
| 3296 | // things in nested classes). |
Douglas Gregor | 9377c82 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 3297 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3298 | // 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] | 3299 | // are complete and we can parse the delayed portions of method |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 3300 | // declarations and the lexed inline method definitions, along with any |
| 3301 | // delayed attributes. |
Douglas Gregor | 428119e | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 3302 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 3303 | ParseLexedAttributes(getCurrentClass()); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3304 | ParseLexedMethodDeclarations(getCurrentClass()); |
Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 3305 | |
| 3306 | // We've finished with all pending member declarations. |
| 3307 | Actions.ActOnFinishCXXMemberDecls(); |
| 3308 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3309 | ParseLexedMemberInitializers(getCurrentClass()); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3310 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | 428119e | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 3311 | PrevTokLocation = SavedPrevTokLocation; |
Reid Kleckner | bba3cb9 | 2015-03-17 19:00:50 +0000 | [diff] [blame] | 3312 | |
| 3313 | // We've finished parsing everything, including default argument |
| 3314 | // initializers. |
Hans Wennborg | 99000c2 | 2015-08-15 01:18:16 +0000 | [diff] [blame] | 3315 | Actions.ActOnFinishCXXNonNestedClass(TagDecl); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
John McCall | 08bede4 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 3318 | if (TagDecl) |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3319 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); |
John McCall | 2ff380a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 3320 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3321 | // Leave the class scope. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3322 | ParsingDef.Pop(); |
Douglas Gregor | 7307d6c | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 3323 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 3324 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3325 | |
Richard Smith | 2ac43ad | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 3326 | void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) { |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3327 | assert(Tok.is(tok::kw_namespace)); |
| 3328 | |
| 3329 | // FIXME: Suggest where the close brace should have gone by looking |
| 3330 | // at indentation changes within the definition body. |
Richard Smith | 2ac43ad | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 3331 | Diag(D->getLocation(), |
| 3332 | diag::err_missing_end_of_definition) << D; |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3333 | Diag(Tok.getLocation(), |
Richard Smith | 2ac43ad | 2013-11-15 23:00:02 +0000 | [diff] [blame] | 3334 | diag::note_missing_end_of_definition_before) << D; |
Richard Smith | da35e96 | 2013-11-09 04:52:51 +0000 | [diff] [blame] | 3335 | |
| 3336 | // Push '};' onto the token stream to recover. |
| 3337 | PP.EnterToken(Tok); |
| 3338 | |
| 3339 | Tok.startToken(); |
| 3340 | Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation)); |
| 3341 | Tok.setKind(tok::semi); |
| 3342 | PP.EnterToken(Tok); |
| 3343 | |
| 3344 | Tok.setKind(tok::r_brace); |
| 3345 | } |
| 3346 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3347 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 3348 | /// which explicitly initializes the members or base classes of a |
| 3349 | /// class (C++ [class.base.init]). For example, the three initializers |
| 3350 | /// after the ':' in the Derived constructor below: |
| 3351 | /// |
| 3352 | /// @code |
| 3353 | /// class Base { }; |
| 3354 | /// class Derived : Base { |
| 3355 | /// int x; |
| 3356 | /// float f; |
| 3357 | /// public: |
| 3358 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 3359 | /// }; |
| 3360 | /// @endcode |
| 3361 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | /// [C++] ctor-initializer: |
| 3363 | /// ':' mem-initializer-list |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3364 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | /// [C++] mem-initializer-list: |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3366 | /// mem-initializer ...[opt] |
| 3367 | /// mem-initializer ...[opt] , mem-initializer-list |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3368 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Nico Weber | 3b00fdc | 2015-03-07 19:52:39 +0000 | [diff] [blame] | 3369 | assert(Tok.is(tok::colon) && |
| 3370 | "Constructor initializer always starts with ':'"); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3371 | |
Nico Weber | 3b00fdc | 2015-03-07 19:52:39 +0000 | [diff] [blame] | 3372 | // Poison the SEH identifiers so they are flagged as illegal in constructor |
| 3373 | // initializers. |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3374 | PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3375 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3376 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3377 | SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3378 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3379 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3380 | do { |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3381 | if (Tok.is(tok::code_completion)) { |
Dmitri Gribenko | 27cb3dd0 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 3382 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 3383 | MemInitializers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3384 | return cutOffParsing(); |
Douglas Gregor | eaeeca9 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 3385 | } |
Alexey Bataev | 79de17d | 2016-01-20 05:25:51 +0000 | [diff] [blame] | 3386 | |
| 3387 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 3388 | if (!MemInit.isInvalid()) |
| 3389 | MemInitializers.push_back(MemInit.get()); |
| 3390 | else |
| 3391 | AnyErrors = true; |
| 3392 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3393 | if (Tok.is(tok::comma)) |
| 3394 | ConsumeToken(); |
| 3395 | else if (Tok.is(tok::l_brace)) |
| 3396 | break; |
Alexey Bataev | 79de17d | 2016-01-20 05:25:51 +0000 | [diff] [blame] | 3397 | // If the previous initializer was valid and the next token looks like a |
| 3398 | // base or member initializer, assume that we're just missing a comma. |
| 3399 | else if (!MemInit.isInvalid() && |
| 3400 | Tok.isOneOf(tok::identifier, tok::coloncolon)) { |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3401 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3402 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 3403 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3404 | } else { |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3405 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Alexey Bataev | 79de17d | 2016-01-20 05:25:51 +0000 | [diff] [blame] | 3406 | if (!MemInit.isInvalid()) |
| 3407 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace |
| 3408 | << tok::comma; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3409 | SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3410 | break; |
| 3411 | } |
| 3412 | } while (true); |
| 3413 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3414 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3415 | AnyErrors); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 3419 | /// part of a constructor initializer that explicitly initializes one |
| 3420 | /// member or base class (C++ [class.base.init]). See |
| 3421 | /// ParseConstructorInitializer for an example. |
| 3422 | /// |
| 3423 | /// [C++] mem-initializer: |
| 3424 | /// mem-initializer-id '(' expression-list[opt] ')' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3425 | /// [C++0x] mem-initializer-id braced-init-list |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | /// |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3427 | /// [C++] mem-initializer-id: |
| 3428 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 3429 | /// identifier |
Craig Topper | 9ad7e26 | 2014-10-31 06:57:07 +0000 | [diff] [blame] | 3430 | MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 3431 | // parse '::'[opt] nested-name-specifier[opt] |
| 3432 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 3433 | ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3434 | |
| 3435 | // : identifier |
| 3436 | IdentifierInfo *II = nullptr; |
| 3437 | SourceLocation IdLoc = Tok.getLocation(); |
| 3438 | // : declype(...) |
| 3439 | DeclSpec DS(AttrFactory); |
| 3440 | // : template_name<...> |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3441 | ParsedType TemplateTypeTy; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3442 | |
| 3443 | if (Tok.is(tok::identifier)) { |
| 3444 | // Get the identifier. This may be a member name or a class name, |
| 3445 | // but we'll let the semantic analysis determine which it is. |
| 3446 | II = Tok.getIdentifierInfo(); |
| 3447 | ConsumeToken(); |
| 3448 | } else if (Tok.is(tok::annot_decltype)) { |
| 3449 | // Get the decltype expression, if there is one. |
| 3450 | // Uses of decltype will already have been converted to annot_decltype by |
| 3451 | // ParseOptionalCXXScopeSpecifier at this point. |
| 3452 | // FIXME: Can we get here with a scope specifier? |
| 3453 | ParseDecltypeSpecifier(DS); |
| 3454 | } else { |
| 3455 | TemplateIdAnnotation *TemplateId = Tok.is(tok::annot_template_id) |
| 3456 | ? takeTemplateIdAnnotation(Tok) |
| 3457 | : nullptr; |
| 3458 | if (TemplateId && (TemplateId->Kind == TNK_Type_template || |
| 3459 | TemplateId->Kind == TNK_Dependent_template_name)) { |
Richard Smith | 62559bd | 2017-02-01 21:36:38 +0000 | [diff] [blame] | 3460 | AnnotateTemplateIdTokenAsType(/*IsClassName*/true); |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 3461 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3462 | TemplateTypeTy = getTypeAnnotation(Tok); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3463 | ConsumeAnnotationToken(); |
| 3464 | } else { |
| 3465 | Diag(Tok, diag::err_expected_member_or_base_name); |
| 3466 | return true; |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 3467 | } |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 3468 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3469 | |
| 3470 | // Parse the '('. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3471 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3472 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 3473 | |
Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 3474 | // FIXME: Add support for signature help inside initializer lists. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3475 | ExprResult InitList = ParseBraceInitializer(); |
| 3476 | if (InitList.isInvalid()) |
| 3477 | return true; |
| 3478 | |
| 3479 | SourceLocation EllipsisLoc; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 3480 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3481 | |
| 3482 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3483 | TemplateTypeTy, DS, IdLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3484 | InitList.get(), EllipsisLoc); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3485 | } else if(Tok.is(tok::l_paren)) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3486 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3487 | T.consumeOpen(); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3488 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3489 | // Parse the optional expression-list. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 3490 | ExprVector ArgExprs; |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3491 | CommaLocsTy CommaLocs; |
Kadir Cetinkaya | 84774c3 | 2018-09-11 15:02:18 +0000 | [diff] [blame] | 3492 | if (Tok.isNot(tok::r_paren) && |
| 3493 | ParseExpressionList(ArgExprs, CommaLocs, [&] { |
| 3494 | QualType PreferredType = Actions.ProduceCtorInitMemberSignatureHelp( |
| 3495 | getCurScope(), ConstructorDecl, SS, TemplateTypeTy, ArgExprs, II, |
| 3496 | T.getOpenLocation()); |
| 3497 | CalledSignatureHelp = true; |
| 3498 | Actions.CodeCompleteExpression(getCurScope(), PreferredType); |
| 3499 | })) { |
| 3500 | if (PP.isCodeCompletionReached() && !CalledSignatureHelp) { |
| 3501 | Actions.ProduceCtorInitMemberSignatureHelp( |
| 3502 | getCurScope(), ConstructorDecl, SS, TemplateTypeTy, ArgExprs, II, |
| 3503 | T.getOpenLocation()); |
| 3504 | CalledSignatureHelp = true; |
| 3505 | } |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3506 | SkipUntil(tok::r_paren, StopAtSemi); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3507 | return true; |
| 3508 | } |
| 3509 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3510 | T.consumeClose(); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3511 | |
| 3512 | SourceLocation EllipsisLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 3513 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 3514 | |
| 3515 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 3516 | TemplateTypeTy, DS, IdLoc, |
Dmitri Gribenko | 139474d | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 3517 | T.getOpenLocation(), ArgExprs, |
| 3518 | T.getCloseLocation(), EllipsisLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3521 | if (getLangOpts().CPlusPlus11) |
| 3522 | return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace; |
| 3523 | else |
| 3524 | return Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3525 | } |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3526 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3527 | /// Parse a C++ exception-specification if present (C++0x [except.spec]). |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3528 | /// |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3529 | /// exception-specification: |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3530 | /// dynamic-exception-specification |
| 3531 | /// noexcept-specification |
| 3532 | /// |
| 3533 | /// noexcept-specification: |
| 3534 | /// 'noexcept' |
| 3535 | /// 'noexcept' '(' constant-expression ')' |
| 3536 | ExceptionSpecificationType |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3537 | Parser::tryParseExceptionSpecification(bool Delayed, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 3538 | SourceRange &SpecificationRange, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3539 | SmallVectorImpl<ParsedType> &DynamicExceptions, |
| 3540 | SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3541 | ExprResult &NoexceptExpr, |
| 3542 | CachedTokens *&ExceptionSpecTokens) { |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3543 | ExceptionSpecificationType Result = EST_None; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 3544 | ExceptionSpecTokens = nullptr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3545 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3546 | // Handle delayed parsing of exception-specifications. |
| 3547 | if (Delayed) { |
| 3548 | if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept)) |
| 3549 | return EST_None; |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3550 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3551 | // Consume and cache the starting token. |
| 3552 | bool IsNoexcept = Tok.is(tok::kw_noexcept); |
| 3553 | Token StartTok = Tok; |
| 3554 | SpecificationRange = SourceRange(ConsumeToken()); |
| 3555 | |
| 3556 | // Check for a '('. |
| 3557 | if (!Tok.is(tok::l_paren)) { |
| 3558 | // If this is a bare 'noexcept', we're done. |
| 3559 | if (IsNoexcept) { |
| 3560 | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 3561 | NoexceptExpr = nullptr; |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3562 | return EST_BasicNoexcept; |
| 3563 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3564 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3565 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 3566 | return EST_DynamicNone; |
| 3567 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3568 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3569 | // Cache the tokens for the exception-specification. |
| 3570 | ExceptionSpecTokens = new CachedTokens; |
| 3571 | ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept' |
| 3572 | ExceptionSpecTokens->push_back(Tok); // '(' |
| 3573 | SpecificationRange.setEnd(ConsumeParen()); // '(' |
Richard Smith | b1c217e | 2015-01-13 02:24:58 +0000 | [diff] [blame] | 3574 | |
| 3575 | ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens, |
| 3576 | /*StopAtSemi=*/true, |
| 3577 | /*ConsumeFinalToken=*/true); |
Aaron Ballman | 580ccaf | 2016-01-12 21:04:22 +0000 | [diff] [blame] | 3578 | SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation()); |
| 3579 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 3580 | return EST_Unparsed; |
| 3581 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3582 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3583 | // See if there's a dynamic specification. |
| 3584 | if (Tok.is(tok::kw_throw)) { |
| 3585 | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
| 3586 | DynamicExceptions, |
| 3587 | DynamicExceptionRanges); |
| 3588 | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
| 3589 | "Produced different number of exception types and ranges."); |
| 3590 | } |
| 3591 | |
| 3592 | // If there's no noexcept specification, we're done. |
| 3593 | if (Tok.isNot(tok::kw_noexcept)) |
| 3594 | return Result; |
| 3595 | |
Richard Smith | b15c11c | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 3596 | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
| 3597 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3598 | // If we already had a dynamic specification, parse the noexcept for, |
| 3599 | // recovery, but emit a diagnostic and don't store the results. |
| 3600 | SourceRange NoexceptRange; |
| 3601 | ExceptionSpecificationType NoexceptType = EST_None; |
| 3602 | |
| 3603 | SourceLocation KeywordLoc = ConsumeToken(); |
| 3604 | if (Tok.is(tok::l_paren)) { |
| 3605 | // There is an argument. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3606 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3607 | T.consumeOpen(); |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3608 | NoexceptExpr = ParseConstantExpression(); |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3609 | T.consumeClose(); |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3610 | if (!NoexceptExpr.isInvalid()) { |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3611 | NoexceptExpr = Actions.ActOnNoexceptSpec(KeywordLoc, NoexceptExpr.get(), |
| 3612 | NoexceptType); |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3613 | NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation()); |
| 3614 | } else { |
Malcolm Parsons | a3220ce | 2017-01-12 16:11:28 +0000 | [diff] [blame] | 3615 | NoexceptType = EST_BasicNoexcept; |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3616 | } |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3617 | } else { |
| 3618 | // There is no argument. |
| 3619 | NoexceptType = EST_BasicNoexcept; |
| 3620 | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
| 3621 | } |
| 3622 | |
| 3623 | if (Result == EST_None) { |
| 3624 | SpecificationRange = NoexceptRange; |
| 3625 | Result = NoexceptType; |
| 3626 | |
| 3627 | // If there's a dynamic specification after a noexcept specification, |
| 3628 | // parse that and ignore the results. |
| 3629 | if (Tok.is(tok::kw_throw)) { |
| 3630 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 3631 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
| 3632 | DynamicExceptionRanges); |
| 3633 | } |
| 3634 | } else { |
| 3635 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 3636 | } |
| 3637 | |
| 3638 | return Result; |
| 3639 | } |
| 3640 | |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3641 | static void diagnoseDynamicExceptionSpecification( |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 3642 | Parser &P, SourceRange Range, bool IsNoexcept) { |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3643 | if (P.getLangOpts().CPlusPlus11) { |
| 3644 | const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)"; |
Richard Smith | 82da19d | 2016-12-08 02:49:07 +0000 | [diff] [blame] | 3645 | P.Diag(Range.getBegin(), |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3646 | P.getLangOpts().CPlusPlus17 && !IsNoexcept |
Richard Smith | 82da19d | 2016-12-08 02:49:07 +0000 | [diff] [blame] | 3647 | ? diag::ext_dynamic_exception_spec |
| 3648 | : diag::warn_exception_spec_deprecated) |
| 3649 | << Range; |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3650 | P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated) |
| 3651 | << Replacement << FixItHint::CreateReplacement(Range, Replacement); |
| 3652 | } |
| 3653 | } |
| 3654 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3655 | /// ParseDynamicExceptionSpecification - Parse a C++ |
| 3656 | /// dynamic-exception-specification (C++ [except.spec]). |
| 3657 | /// |
| 3658 | /// dynamic-exception-specification: |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3659 | /// 'throw' '(' type-id-list [opt] ')' |
| 3660 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3661 | /// |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3662 | /// type-id-list: |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3663 | /// type-id ... [opt] |
| 3664 | /// type-id-list ',' type-id ... [opt] |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3665 | /// |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3666 | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
| 3667 | SourceRange &SpecificationRange, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3668 | SmallVectorImpl<ParsedType> &Exceptions, |
| 3669 | SmallVectorImpl<SourceRange> &Ranges) { |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3670 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3672 | SpecificationRange.setBegin(ConsumeToken()); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3673 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3674 | if (T.consumeOpen()) { |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3675 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 3676 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3677 | return EST_DynamicNone; |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3678 | } |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3679 | |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3680 | // Parse throw(...), a Microsoft extension that means "this function |
| 3681 | // can throw anything". |
| 3682 | if (Tok.is(tok::ellipsis)) { |
| 3683 | SourceLocation EllipsisLoc = ConsumeToken(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3684 | if (!getLangOpts().MicrosoftExt) |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3685 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3686 | T.consumeClose(); |
| 3687 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3688 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3689 | return EST_MSAny; |
Douglas Gregor | 356513d | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3692 | // Parse the sequence of type-ids. |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3693 | SourceRange Range; |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3694 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3695 | TypeResult Res(ParseTypeName(&Range)); |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3696 | |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3697 | if (Tok.is(tok::ellipsis)) { |
| 3698 | // C++0x [temp.variadic]p5: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3699 | // - In a dynamic-exception-specification (15.4); the pattern is a |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3700 | // type-id. |
| 3701 | SourceLocation Ellipsis = ConsumeToken(); |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3702 | Range.setEnd(Ellipsis); |
Douglas Gregor | 830837d | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 3703 | if (!Res.isInvalid()) |
| 3704 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
| 3705 | } |
Sebastian Redl | 965b0e3 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3706 | |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3707 | if (!Res.isInvalid()) { |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3708 | Exceptions.push_back(Res.get()); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3709 | Ranges.push_back(Range); |
| 3710 | } |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 3711 | |
| 3712 | if (!TryConsumeToken(tok::comma)) |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3713 | break; |
| 3714 | } |
| 3715 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3716 | T.consumeClose(); |
| 3717 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 8ca78a1 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3718 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, |
| 3719 | Exceptions.empty()); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3720 | return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic; |
Douglas Gregor | 2afd0be | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3721 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3723 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 3724 | /// function declaration. |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 3725 | TypeResult Parser::ParseTrailingReturnType(SourceRange &Range, |
| 3726 | bool MayBeFollowedByDirectInit) { |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3727 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 3728 | |
| 3729 | ConsumeToken(); |
| 3730 | |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 3731 | return ParseTypeName(&Range, MayBeFollowedByDirectInit |
| 3732 | ? DeclaratorContext::TrailingReturnVarContext |
| 3733 | : DeclaratorContext::TrailingReturnContext); |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3736 | /// We have just started parsing the definition of a new class, |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3737 | /// so push that class onto our stack of classes that is currently |
| 3738 | /// being parsed. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3739 | Sema::ParsingClassState |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3740 | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass, |
| 3741 | bool IsInterface) { |
Douglas Gregor | edf8f39 | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3742 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3743 | "Nested class without outer class"); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3744 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface)); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3745 | return Actions.PushParsingClass(); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3748 | /// Deallocate the given parsed class and all of its nested |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3749 | /// classes. |
| 3750 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3751 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 3752 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3753 | delete Class; |
| 3754 | } |
| 3755 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3756 | /// Pop the top class of the stack of classes that are |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3757 | /// currently being parsed. |
| 3758 | /// |
| 3759 | /// This routine should be called when we have finished parsing the |
| 3760 | /// definition of a class, but have not yet popped the Scope |
| 3761 | /// associated with the class's definition. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3762 | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3763 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3764 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3765 | Actions.PopParsingClass(state); |
| 3766 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3767 | ParsingClass *Victim = ClassStack.top(); |
| 3768 | ClassStack.pop(); |
| 3769 | if (Victim->TopLevelClass) { |
| 3770 | // Deallocate all of the nested classes of this class, |
| 3771 | // recursively: we don't need to keep any of this information. |
| 3772 | DeallocateParsedClasses(Victim); |
| 3773 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3775 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 3776 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3777 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3778 | // The victim is a nested class, but we will not need to perform |
| 3779 | // any processing after the definition of this class since it has |
| 3780 | // no members whose handling was delayed. Therefore, we can just |
| 3781 | // remove this nested class. |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3782 | DeallocateParsedClasses(Victim); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3783 | return; |
| 3784 | } |
| 3785 | |
| 3786 | // This nested class has some members that will need to be processed |
| 3787 | // after the top-level class is completely defined. Therefore, add |
| 3788 | // it to the list of nested classes within its parent. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3789 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3790 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3791 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3792 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3793 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3794 | /// Try to parse an 'identifier' which appears within an attribute-token. |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3795 | /// |
| 3796 | /// \return the parsed identifier on success, and 0 if the next token is not an |
| 3797 | /// attribute-token. |
| 3798 | /// |
| 3799 | /// C++11 [dcl.attr.grammar]p3: |
| 3800 | /// If a keyword or an alternative token that satisfies the syntactic |
| 3801 | /// requirements of an identifier is contained in an attribute-token, |
| 3802 | /// it is considered an identifier. |
| 3803 | IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) { |
| 3804 | switch (Tok.getKind()) { |
| 3805 | default: |
| 3806 | // Identifiers and keywords have identifier info attached. |
David Majnemer | d527199 | 2015-01-09 18:09:39 +0000 | [diff] [blame] | 3807 | if (!Tok.isAnnotation()) { |
| 3808 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 3809 | Loc = ConsumeToken(); |
| 3810 | return II; |
| 3811 | } |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3812 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3813 | return nullptr; |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3814 | |
Aaron Ballman | c44c1742 | 2018-11-09 17:19:45 +0000 | [diff] [blame] | 3815 | case tok::numeric_constant: { |
| 3816 | // If we got a numeric constant, check to see if it comes from a macro that |
| 3817 | // corresponds to the predefined __clang__ macro. If it does, warn the user |
| 3818 | // and recover by pretending they said _Clang instead. |
| 3819 | if (Tok.getLocation().isMacroID()) { |
| 3820 | SmallString<8> ExpansionBuf; |
| 3821 | SourceLocation ExpansionLoc = |
| 3822 | PP.getSourceManager().getExpansionLoc(Tok.getLocation()); |
| 3823 | StringRef Spelling = PP.getSpelling(ExpansionLoc, ExpansionBuf); |
| 3824 | if (Spelling == "__clang__") { |
| 3825 | SourceRange TokRange( |
| 3826 | ExpansionLoc, |
| 3827 | PP.getSourceManager().getExpansionLoc(Tok.getEndLoc())); |
| 3828 | Diag(Tok, diag::warn_wrong_clang_attr_namespace) |
| 3829 | << FixItHint::CreateReplacement(TokRange, "_Clang"); |
| 3830 | Loc = ConsumeToken(); |
| 3831 | return &PP.getIdentifierTable().get("_Clang"); |
| 3832 | } |
| 3833 | } |
| 3834 | return nullptr; |
| 3835 | } |
| 3836 | |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3837 | case tok::ampamp: // 'and' |
| 3838 | case tok::pipe: // 'bitor' |
| 3839 | case tok::pipepipe: // 'or' |
| 3840 | case tok::caret: // 'xor' |
| 3841 | case tok::tilde: // 'compl' |
| 3842 | case tok::amp: // 'bitand' |
| 3843 | case tok::ampequal: // 'and_eq' |
| 3844 | case tok::pipeequal: // 'or_eq' |
| 3845 | case tok::caretequal: // 'xor_eq' |
| 3846 | case tok::exclaim: // 'not' |
| 3847 | case tok::exclaimequal: // 'not_eq' |
| 3848 | // Alternative tokens do not have identifier info, but their spelling |
| 3849 | // starts with an alphabetical character. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3850 | SmallString<8> SpellingBuf; |
Benjamin Kramer | 60be563 | 2015-03-29 19:25:07 +0000 | [diff] [blame] | 3851 | SourceLocation SpellingLoc = |
| 3852 | PP.getSourceManager().getSpellingLoc(Tok.getLocation()); |
| 3853 | StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf); |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 3854 | if (isLetter(Spelling[0])) { |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3855 | Loc = ConsumeToken(); |
Benjamin Kramer | 5c17f9c | 2012-04-22 20:43:30 +0000 | [diff] [blame] | 3856 | return &PP.getIdentifierTable().get(Spelling); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3857 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3858 | return nullptr; |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3862 | static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3863 | IdentifierInfo *ScopeName) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3864 | switch (ParsedAttr::getKind(AttrName, ScopeName, ParsedAttr::AS_CXX11)) { |
| 3865 | case ParsedAttr::AT_CarriesDependency: |
| 3866 | case ParsedAttr::AT_Deprecated: |
| 3867 | case ParsedAttr::AT_FallThrough: |
| 3868 | case ParsedAttr::AT_CXX11NoReturn: |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3869 | return true; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3870 | case ParsedAttr::AT_WarnUnusedResult: |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 3871 | return !ScopeName && AttrName->getName().equals("nodiscard"); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3872 | case ParsedAttr::AT_Unused: |
Nico Weber | ac03bce | 2016-08-23 19:59:55 +0000 | [diff] [blame] | 3873 | return !ScopeName && AttrName->getName().equals("maybe_unused"); |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3874 | default: |
| 3875 | return false; |
| 3876 | } |
| 3877 | } |
| 3878 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 3879 | /// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause. |
| 3880 | /// |
| 3881 | /// [C++11] attribute-argument-clause: |
| 3882 | /// '(' balanced-token-seq ')' |
| 3883 | /// |
| 3884 | /// [C++11] balanced-token-seq: |
| 3885 | /// balanced-token |
| 3886 | /// balanced-token-seq balanced-token |
| 3887 | /// |
| 3888 | /// [C++11] balanced-token: |
| 3889 | /// '(' balanced-token-seq ')' |
| 3890 | /// '[' balanced-token-seq ']' |
| 3891 | /// '{' balanced-token-seq '}' |
| 3892 | /// any token but '(', ')', '[', ']', '{', or '}' |
| 3893 | bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName, |
| 3894 | SourceLocation AttrNameLoc, |
| 3895 | ParsedAttributes &Attrs, |
| 3896 | SourceLocation *EndLoc, |
| 3897 | IdentifierInfo *ScopeName, |
| 3898 | SourceLocation ScopeLoc) { |
| 3899 | 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] | 3900 | SourceLocation LParenLoc = Tok.getLocation(); |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3901 | const LangOptions &LO = getLangOpts(); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3902 | ParsedAttr::Syntax Syntax = |
| 3903 | LO.CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x; |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 3904 | |
| 3905 | // If the attribute isn't known, we will not attempt to parse any |
| 3906 | // arguments. |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3907 | if (!hasAttribute(LO.CPlusPlus ? AttrSyntax::CXX : AttrSyntax::C, ScopeName, |
| 3908 | AttrName, getTargetInfo(), getLangOpts())) { |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 3909 | // Eat the left paren, then skip to the ending right paren. |
| 3910 | ConsumeParen(); |
| 3911 | SkipUntil(tok::r_paren); |
Aaron Ballman | c44c1742 | 2018-11-09 17:19:45 +0000 | [diff] [blame] | 3912 | return false; |
| 3913 | } |
| 3914 | |
| 3915 | if (ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"))) { |
| 3916 | // GNU-scoped attributes have some special cases to handle GNU-specific |
| 3917 | // behaviors. |
| 3918 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3919 | ScopeLoc, Syntax, nullptr); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 3920 | return true; |
| 3921 | } |
| 3922 | |
| 3923 | unsigned NumArgs; |
| 3924 | // Some Clang-scoped attributes have some special parsing behavior. |
Aaron Ballman | c44c1742 | 2018-11-09 17:19:45 +0000 | [diff] [blame] | 3925 | if (ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"))) |
| 3926 | NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, |
| 3927 | ScopeName, ScopeLoc, Syntax); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 3928 | else |
| 3929 | NumArgs = |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 3930 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3931 | ScopeName, ScopeLoc, Syntax); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 3932 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3933 | if (!Attrs.empty() && |
| 3934 | IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) { |
Michael Kruse | dc5ce72 | 2018-08-03 01:21:16 +0000 | [diff] [blame] | 3935 | ParsedAttr &Attr = Attrs.back(); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 3936 | // If the attribute is a standard or built-in attribute and we are |
| 3937 | // parsing an argument list, we need to determine whether this attribute |
| 3938 | // was allowed to have an argument list (such as [[deprecated]]), and how |
| 3939 | // many arguments were parsed (so we can diagnose on [[deprecated()]]). |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3940 | if (Attr.getMaxArgs() && !NumArgs) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 3941 | // The attribute was allowed to have arguments, but none were provided |
| 3942 | // even though the attribute parsed successfully. This is an error. |
| 3943 | Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3944 | Attr.setInvalid(true); |
| 3945 | } else if (!Attr.getMaxArgs()) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 3946 | // The attribute parsed successfully, but was not allowed to have any |
| 3947 | // arguments. It doesn't matter whether any were provided -- the |
| 3948 | // presence of the argument list (even if empty) is diagnosed. |
| 3949 | Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments) |
| 3950 | << AttrName |
| 3951 | << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc)); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 3952 | Attr.setInvalid(true); |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 3953 | } |
| 3954 | } |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 3955 | return true; |
| 3956 | } |
| 3957 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3958 | /// ParseCXX11AttributeSpecifier - Parse a C++11 or C2x attribute-specifier. |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3959 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3960 | /// [C++11] attribute-specifier: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3961 | /// '[' '[' attribute-list ']' ']' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3962 | /// alignment-specifier |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3963 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3964 | /// [C++11] attribute-list: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3965 | /// attribute[opt] |
| 3966 | /// attribute-list ',' attribute[opt] |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3967 | /// attribute '...' |
| 3968 | /// attribute-list ',' attribute '...' |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3969 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3970 | /// [C++11] attribute: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3971 | /// attribute-token attribute-argument-clause[opt] |
| 3972 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3973 | /// [C++11] attribute-token: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3974 | /// identifier |
| 3975 | /// attribute-scoped-token |
| 3976 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3977 | /// [C++11] attribute-scoped-token: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3978 | /// attribute-namespace '::' identifier |
| 3979 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3980 | /// [C++11] attribute-namespace: |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3981 | /// identifier |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3982 | void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs, |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3983 | SourceLocation *endLoc) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3984 | if (Tok.is(tok::kw_alignas)) { |
Richard Smith | f679b5b | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 3985 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3986 | ParseAlignmentSpecifier(attrs, endLoc); |
| 3987 | return; |
| 3988 | } |
| 3989 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3990 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) && |
| 3991 | "Not a double square bracket attribute list"); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3992 | |
Richard Smith | f679b5b | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 3993 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute); |
| 3994 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3995 | ConsumeBracket(); |
| 3996 | ConsumeBracket(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3997 | |
Richard Smith | b7d7a04 | 2016-06-24 12:15:12 +0000 | [diff] [blame] | 3998 | SourceLocation CommonScopeLoc; |
| 3999 | IdentifierInfo *CommonScopeName = nullptr; |
| 4000 | if (Tok.is(tok::kw_using)) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4001 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
Richard Smith | b7d7a04 | 2016-06-24 12:15:12 +0000 | [diff] [blame] | 4002 | ? diag::warn_cxx14_compat_using_attribute_ns |
| 4003 | : diag::ext_using_attribute_ns); |
| 4004 | ConsumeToken(); |
| 4005 | |
| 4006 | CommonScopeName = TryParseCXX11AttributeIdentifier(CommonScopeLoc); |
| 4007 | if (!CommonScopeName) { |
| 4008 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 4009 | SkipUntil(tok::r_square, tok::colon, StopBeforeMatch); |
| 4010 | } |
| 4011 | if (!TryConsumeToken(tok::colon) && CommonScopeName) |
| 4012 | Diag(Tok.getLocation(), diag::err_expected) << tok::colon; |
| 4013 | } |
| 4014 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4015 | llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs; |
| 4016 | |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4017 | while (Tok.isNot(tok::r_square)) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4018 | // attribute not present |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 4019 | if (TryConsumeToken(tok::comma)) |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4020 | continue; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4021 | |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4022 | SourceLocation ScopeLoc, AttrLoc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4023 | IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr; |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4024 | |
| 4025 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 4026 | if (!AttrName) |
| 4027 | // Break out to the "expected ']'" diagnostic. |
| 4028 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4029 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4030 | // scoped attribute |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 4031 | if (TryConsumeToken(tok::coloncolon)) { |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4032 | ScopeName = AttrName; |
| 4033 | ScopeLoc = AttrLoc; |
| 4034 | |
| 4035 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 4036 | if (!AttrName) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4037 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4038 | SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4039 | continue; |
| 4040 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
Richard Smith | b7d7a04 | 2016-06-24 12:15:12 +0000 | [diff] [blame] | 4043 | if (CommonScopeName) { |
| 4044 | if (ScopeName) { |
| 4045 | Diag(ScopeLoc, diag::err_using_attribute_ns_conflict) |
| 4046 | << SourceRange(CommonScopeLoc); |
| 4047 | } else { |
| 4048 | ScopeName = CommonScopeName; |
| 4049 | ScopeLoc = CommonScopeLoc; |
| 4050 | } |
| 4051 | } |
| 4052 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4053 | bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4054 | bool AttrParsed = false; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4055 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4056 | if (StandardAttr && |
| 4057 | !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second) |
| 4058 | Diag(AttrLoc, diag::err_cxx11_attribute_repeated) |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4059 | << AttrName << SourceRange(SeenAttrs[AttrName]); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4060 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4061 | // Parse attribute arguments |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 4062 | if (Tok.is(tok::l_paren)) |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 4063 | AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc, |
| 4064 | ScopeName, ScopeLoc); |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4065 | |
| 4066 | if (!AttrParsed) |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4067 | attrs.addNew( |
| 4068 | AttrName, |
| 4069 | SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, AttrLoc), |
| 4070 | ScopeName, ScopeLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4071 | getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4072 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 4073 | if (TryConsumeToken(tok::ellipsis)) |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 4074 | Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) |
Richard Trieu | b402580 | 2018-03-28 04:16:13 +0000 | [diff] [blame] | 4075 | << AttrName; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4078 | if (ExpectAndConsume(tok::r_square)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4079 | SkipUntil(tok::r_square); |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4080 | if (endLoc) |
| 4081 | *endLoc = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4082 | if (ExpectAndConsume(tok::r_square)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4083 | SkipUntil(tok::r_square); |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4084 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4085 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4086 | /// ParseCXX11Attributes - Parse a C++11 or C2x attribute-specifier-seq. |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4087 | /// |
| 4088 | /// attribute-specifier-seq: |
| 4089 | /// attribute-specifier-seq[opt] attribute-specifier |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4090 | void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs, |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4091 | SourceLocation *endLoc) { |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4092 | assert(standardAttributesAllowed()); |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 4093 | |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4094 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
| 4095 | if (!endLoc) |
| 4096 | endLoc = &Loc; |
| 4097 | |
Douglas Gregor | 6f98100 | 2011-10-07 20:35:25 +0000 | [diff] [blame] | 4098 | do { |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4099 | ParseCXX11AttributeSpecifier(attrs, endLoc); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4100 | } while (isCXX11AttributeSpecifier()); |
Peter Collingbourne | 49eedec | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 4101 | |
| 4102 | attrs.Range = SourceRange(StartLoc, *endLoc); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4105 | void Parser::DiagnoseAndSkipCXX11Attributes() { |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4106 | // Start and end location of an attribute or an attribute list. |
| 4107 | SourceLocation StartLoc = Tok.getLocation(); |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 4108 | SourceLocation EndLoc = SkipCXX11Attributes(); |
| 4109 | |
| 4110 | if (EndLoc.isValid()) { |
| 4111 | SourceRange Range(StartLoc, EndLoc); |
| 4112 | Diag(StartLoc, diag::err_attributes_not_allowed) |
| 4113 | << Range; |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | SourceLocation Parser::SkipCXX11Attributes() { |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4118 | SourceLocation EndLoc; |
| 4119 | |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 4120 | if (!isCXX11AttributeSpecifier()) |
| 4121 | return EndLoc; |
| 4122 | |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4123 | do { |
| 4124 | if (Tok.is(tok::l_square)) { |
| 4125 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4126 | T.consumeOpen(); |
| 4127 | T.skipToEnd(); |
| 4128 | EndLoc = T.getCloseLocation(); |
| 4129 | } else { |
| 4130 | assert(Tok.is(tok::kw_alignas) && "not an attribute specifier"); |
| 4131 | ConsumeToken(); |
| 4132 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4133 | if (!T.consumeOpen()) |
| 4134 | T.skipToEnd(); |
| 4135 | EndLoc = T.getCloseLocation(); |
| 4136 | } |
| 4137 | } while (isCXX11AttributeSpecifier()); |
| 4138 | |
Richard Smith | 955bf01 | 2014-06-19 11:42:00 +0000 | [diff] [blame] | 4139 | return EndLoc; |
Richard Smith | c2c8bb8 | 2013-10-15 01:34:54 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4142 | /// Parse uuid() attribute when it appears in a [] Microsoft attribute. |
| 4143 | void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) { |
| 4144 | assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list"); |
| 4145 | IdentifierInfo *UuidIdent = Tok.getIdentifierInfo(); |
| 4146 | assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list"); |
| 4147 | |
| 4148 | SourceLocation UuidLoc = Tok.getLocation(); |
| 4149 | ConsumeToken(); |
| 4150 | |
| 4151 | // Ignore the left paren location for now. |
| 4152 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4153 | if (T.consumeOpen()) { |
| 4154 | Diag(Tok, diag::err_expected) << tok::l_paren; |
| 4155 | return; |
| 4156 | } |
| 4157 | |
| 4158 | ArgsVector ArgExprs; |
| 4159 | if (Tok.is(tok::string_literal)) { |
| 4160 | // Easy case: uuid("...") -- quoted string. |
| 4161 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 4162 | if (StringResult.isInvalid()) |
| 4163 | return; |
| 4164 | ArgExprs.push_back(StringResult.get()); |
| 4165 | } else { |
| 4166 | // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no |
| 4167 | // quotes in the parens. Just append the spelling of all tokens encountered |
| 4168 | // until the closing paren. |
| 4169 | |
| 4170 | SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul |
| 4171 | StrBuffer += "\""; |
| 4172 | |
| 4173 | // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace, |
| 4174 | // tok::r_brace, tok::minus, tok::identifier (think C000) and |
| 4175 | // tok::numeric_constant (0000) should be enough. But the spelling of the |
| 4176 | // uuid argument is checked later anyways, so there's no harm in accepting |
| 4177 | // almost anything here. |
| 4178 | // cl is very strict about whitespace in this form and errors out if any |
| 4179 | // is present, so check the space flags on the tokens. |
| 4180 | SourceLocation StartLoc = Tok.getLocation(); |
| 4181 | while (Tok.isNot(tok::r_paren)) { |
| 4182 | if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { |
| 4183 | Diag(Tok, diag::err_attribute_uuid_malformed_guid); |
| 4184 | SkipUntil(tok::r_paren, StopAtSemi); |
| 4185 | return; |
| 4186 | } |
| 4187 | SmallString<16> SpellingBuffer; |
| 4188 | SpellingBuffer.resize(Tok.getLength() + 1); |
| 4189 | bool Invalid = false; |
| 4190 | StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); |
| 4191 | if (Invalid) { |
| 4192 | SkipUntil(tok::r_paren, StopAtSemi); |
| 4193 | return; |
| 4194 | } |
| 4195 | StrBuffer += TokSpelling; |
| 4196 | ConsumeAnyToken(); |
| 4197 | } |
| 4198 | StrBuffer += "\""; |
| 4199 | |
| 4200 | if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { |
| 4201 | Diag(Tok, diag::err_attribute_uuid_malformed_guid); |
| 4202 | ConsumeParen(); |
| 4203 | return; |
| 4204 | } |
| 4205 | |
| 4206 | // Pretend the user wrote the appropriate string literal here. |
| 4207 | // ActOnStringLiteral() copies the string data into the literal, so it's |
| 4208 | // ok that the Token points to StrBuffer. |
| 4209 | Token Toks[1]; |
| 4210 | Toks[0].startToken(); |
| 4211 | Toks[0].setKind(tok::string_literal); |
| 4212 | Toks[0].setLocation(StartLoc); |
| 4213 | Toks[0].setLiteralData(StrBuffer.data()); |
| 4214 | Toks[0].setLength(StrBuffer.size()); |
| 4215 | StringLiteral *UuidString = |
| 4216 | cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get()); |
| 4217 | ArgExprs.push_back(UuidString); |
| 4218 | } |
| 4219 | |
| 4220 | if (!T.consumeClose()) { |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4221 | Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr, |
| 4222 | SourceLocation(), ArgExprs.data(), ArgExprs.size(), |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 4223 | ParsedAttr::AS_Microsoft); |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | |
David Majnemer | e4752e75 | 2015-07-08 05:55:00 +0000 | [diff] [blame] | 4227 | /// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr] |
Francois Pichet | c2bc5ac | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 4228 | /// |
| 4229 | /// [MS] ms-attribute: |
| 4230 | /// '[' token-seq ']' |
| 4231 | /// |
| 4232 | /// [MS] ms-attribute-seq: |
| 4233 | /// ms-attribute[opt] |
| 4234 | /// ms-attribute ms-attribute-seq |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4235 | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
| 4236 | SourceLocation *endLoc) { |
Francois Pichet | c2bc5ac | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 4237 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 4238 | |
Saleem Abdulrasool | 425efcf | 2015-06-15 20:57:04 +0000 | [diff] [blame] | 4239 | do { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4240 | // 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] | 4241 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4242 | T.consumeOpen(); |
Nico Weber | 05e1dad | 2016-09-03 03:25:22 +0000 | [diff] [blame] | 4243 | |
| 4244 | // Skip most ms attributes except for a whitelist. |
| 4245 | while (true) { |
| 4246 | SkipUntil(tok::r_square, tok::identifier, StopAtSemi | StopBeforeMatch); |
| 4247 | if (Tok.isNot(tok::identifier)) // ']', but also eof |
| 4248 | break; |
| 4249 | if (Tok.getIdentifierInfo()->getName() == "uuid") |
| 4250 | ParseMicrosoftUuidAttributeArgs(attrs); |
| 4251 | else |
| 4252 | ConsumeToken(); |
| 4253 | } |
| 4254 | |
Saleem Abdulrasool | 425efcf | 2015-06-15 20:57:04 +0000 | [diff] [blame] | 4255 | T.consumeClose(); |
| 4256 | if (endLoc) |
| 4257 | *endLoc = T.getCloseLocation(); |
| 4258 | } while (Tok.is(tok::l_square)); |
Francois Pichet | c2bc5ac | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 4259 | } |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4260 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4261 | void Parser::ParseMicrosoftIfExistsClassDeclaration( |
| 4262 | DeclSpec::TST TagType, ParsedAttributes &AccessAttrs, |
| 4263 | AccessSpecifier &CurAS) { |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4264 | IfExistsCondition Result; |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4265 | if (ParseMicrosoftIfExistsCondition(Result)) |
| 4266 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4268 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 4269 | if (Braces.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4270 | Diag(Tok, diag::err_expected) << tok::l_brace; |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4271 | return; |
| 4272 | } |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4274 | switch (Result.Behavior) { |
| 4275 | case IEB_Parse: |
| 4276 | // Parse the declarations below. |
| 4277 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4278 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4279 | case IEB_Dependent: |
| 4280 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
| 4281 | << Result.IsIfExists; |
| 4282 | // Fall through to skip. |
Galina Kistanova | d819d5b | 2017-06-01 21:19:06 +0000 | [diff] [blame] | 4283 | LLVM_FALLTHROUGH; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4285 | case IEB_Skip: |
| 4286 | Braces.skipToEnd(); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4287 | return; |
| 4288 | } |
| 4289 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 4290 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4291 | // __if_exists, __if_not_exists can nest. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 4292 | if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4293 | ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, |
| 4294 | AccessAttrs, CurAS); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4295 | continue; |
| 4296 | } |
| 4297 | |
| 4298 | // Check for extraneous top-level semicolon. |
| 4299 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 4300 | ConsumeExtraSemi(InsideStruct, TagType); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4301 | continue; |
| 4302 | } |
| 4303 | |
| 4304 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 4305 | if (AS != AS_none) { |
| 4306 | // Current token is a C++ access specifier. |
| 4307 | CurAS = AS; |
| 4308 | SourceLocation ASLoc = Tok.getLocation(); |
| 4309 | ConsumeToken(); |
| 4310 | if (Tok.is(tok::colon)) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4311 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation(), |
| 4312 | ParsedAttributesView{}); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4313 | else |
Alp Toker | 35d8703 | 2013-12-30 23:29:50 +0000 | [diff] [blame] | 4314 | Diag(Tok, diag::err_expected) << tok::colon; |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4315 | ConsumeToken(); |
| 4316 | continue; |
| 4317 | } |
| 4318 | |
| 4319 | // Parse all the comma separated declarators. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4320 | ParseCXXClassMemberDeclaration(CurAS, AccessAttrs); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4321 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4322 | |
Douglas Gregor | 43edb32 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 4323 | Braces.consumeClose(); |
Francois Pichet | 8f981d5 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 4324 | } |