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