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