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