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