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