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