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