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