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