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