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