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