Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1 | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | 1b7f898 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 14 | #include "clang/Parse/Parser.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 16 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/OperatorKinds.h" |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 19 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 20 | #include "clang/Sema/DeclSpec.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 21 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 22 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Scope.h" |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 24 | #include "clang/Sema/SemaDiagnostic.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 8f08cb7 | 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 | d078e64 | 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 | 8f08cb7 | 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 | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 37 | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 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 | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 44 | /// 'inline'[opt] 'namespace' identifier attributes[opt] |
| 45 | /// '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 46 | /// |
| 47 | /// extension-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 48 | /// 'inline'[opt] 'namespace' original-namespace-name |
| 49 | /// '{' namespace-body '}' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | /// |
Chris Lattner | 8f08cb7 | 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 | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 54 | Decl *Parser::ParseNamespace(unsigned Context, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 55 | SourceLocation &DeclEnd, |
| 56 | SourceLocation InlineLoc) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 57 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 58 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 59 | ObjCDeclContextSwitch ObjCDC(*this); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 61 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 62 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 63 | cutOffParsing(); |
| 64 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 65 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 67 | SourceLocation IdentLoc; |
| 68 | IdentifierInfo *Ident = 0; |
Richard Trieu | f858bd8 | 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 | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 72 | |
| 73 | Token attrTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 75 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 76 | Ident = Tok.getIdentifierInfo(); |
| 77 | IdentLoc = ConsumeToken(); // eat the identifier. |
Richard Trieu | f858bd8 | 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 | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 83 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 85 | // Read label attributes, if present. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 86 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 87 | if (Tok.is(tok::kw___attribute)) { |
| 88 | attrTok = Tok; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 89 | ParseGNUAttributes(attrs); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 90 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 92 | if (Tok.is(tok::equal)) { |
Nico Weber | e1bb329 | 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 | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 99 | if (!attrs.empty()) |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 100 | Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | d078e64 | 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 | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 104 | return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 105 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 108 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 109 | if (T.consumeOpen()) { |
Richard Trieu | f858bd8 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 115 | diag::err_expected_ident_lbrace); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 116 | return 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | 23c94db | 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 | f858bd8 | 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 | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 126 | Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope); |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 127 | SkipUntil(tok::r_brace, false); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 128 | return 0; |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Trieu | f858bd8 | 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 | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 141 | std::string NamespaceFix; |
Richard Trieu | f858bd8 | 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 | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 147 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 148 | std::string RBraces; |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 149 | for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i) |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 150 | RBraces += "} "; |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 151 | |
Richard Trieu | f858bd8 | 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 | 88e64ca | 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 | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 161 | if (InlineLoc.isValid()) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 162 | Diag(InlineLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 163 | diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace); |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 5144832 | 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 | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 168 | Decl *NamespcDecl = |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 169 | Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 170 | IdentLoc, Ident, T.getOpenLocation(), |
| 171 | attrs.getList()); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 172 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 173 | PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc, |
| 174 | "parsing namespace"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Richard Trieu | f858bd8 | 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 | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 179 | InlineLoc, attrs, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 181 | // Leave the namespace scope. |
| 182 | NamespaceScope.Exit(); |
| 183 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 184 | DeclEnd = T.getCloseLocation(); |
| 185 | Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 186 | |
| 187 | return NamespcDecl; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 189 | |
Richard Trieu | f858bd8 | 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 | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 195 | ParsedAttributes& attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 196 | BalancedDelimiterTracker &Tracker) { |
Richard Trieu | f858bd8 | 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 | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 200 | MaybeParseCXX11Attributes(attrs); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 201 | MaybeParseMicrosoftAttributes(attrs); |
| 202 | ParseExternalDeclaration(attrs); |
| 203 | } |
Douglas Gregor | 4a8dfb5 | 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 | f858bd8 | 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 | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 217 | Ident[index], Tracker.getOpenLocation(), |
| 218 | attrs.getList()); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 219 | |
| 220 | ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 221 | attrs, Tracker); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 222 | |
| 223 | NamespaceScope.Exit(); |
| 224 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 225 | Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation()); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Anders Carlsson | f67606a | 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 | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 231 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 232 | SourceLocation AliasLoc, |
| 233 | IdentifierInfo *Alias, |
| 234 | SourceLocation &DeclEnd) { |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 235 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 237 | ConsumeToken(); // eat the '='. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 239 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 240 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 241 | cutOffParsing(); |
| 242 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 243 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 244 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 245 | CXXScopeSpec SS; |
| 246 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 247 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Anders Carlsson | f67606a | 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 | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 253 | return 0; |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Parse identifier. |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 257 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 258 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 260 | // Eat the ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 261 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 265 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 266 | SS, IdentLoc, Ident); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | c6fdc34 | 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 | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 276 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) { |
Douglas Gregor | c19923d | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 277 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 278 | SmallString<8> LangBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 279 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 280 | StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 281 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 282 | return 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 283 | |
Richard Smith | 99831e4 | 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 | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 289 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 291 | ParseScope LinkageScope(this, Scope::DeclScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 292 | Decl *LinkageSpec |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 293 | = Actions.ActOnStartLinkageSpecification(getCurScope(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 294 | DS.getSourceRange().getBegin(), |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 295 | Loc, Lang, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 296 | Tok.is(tok::l_brace) ? Tok.getLocation() |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 297 | : SourceLocation()); |
| 298 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 299 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 300 | MaybeParseCXX11Attributes(attrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 301 | MaybeParseMicrosoftAttributes(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 302 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 303 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | f41e33c | 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 | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 309 | DS.setExternInLinkageSpec(true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 310 | ParseExternalDeclaration(attrs, &DS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 311 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 312 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 314 | |
Douglas Gregor | 63a0113 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 315 | DS.abort(); |
| 316 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 317 | ProhibitAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 319 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 320 | T.consumeOpen(); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 321 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 322 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 323 | MaybeParseCXX11Attributes(attrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 324 | MaybeParseMicrosoftAttributes(attrs); |
| 325 | ParseExternalDeclaration(attrs); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 328 | T.consumeClose(); |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 329 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 330 | T.getCloseLocation()); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 331 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 332 | |
Douglas Gregor | f780abc | 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 | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 335 | Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 336 | const ParsedTemplateInfo &TemplateInfo, |
| 337 | SourceLocation &DeclEnd, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 338 | ParsedAttributesWithRange &attrs, |
| 339 | Decl **OwnedType) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 340 | assert(Tok.is(tok::kw_using) && "Not using token"); |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 341 | ObjCDeclContextSwitch ObjCDC(*this); |
| 342 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 343 | // Eat 'using'. |
| 344 | SourceLocation UsingLoc = ConsumeToken(); |
| 345 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 346 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 347 | Actions.CodeCompleteUsing(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 348 | cutOffParsing(); |
| 349 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 350 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 351 | |
John McCall | 78b8105 | 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 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 360 | |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 361 | return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 364 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 365 | |
| 366 | // Using declarations can't have attributes. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 367 | ProhibitAttributes(attrs); |
Chris Lattner | 2f27477 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 368 | |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 369 | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 370 | AS_none, OwnedType); |
Douglas Gregor | f780abc | 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 | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 383 | Decl *Parser::ParseUsingDirective(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 384 | SourceLocation UsingLoc, |
| 385 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 386 | ParsedAttributes &attrs) { |
Douglas Gregor | f780abc | 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 | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 392 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 393 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 394 | cutOffParsing(); |
| 395 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 396 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 398 | CXXScopeSpec SS; |
| 399 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 400 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 402 | IdentifierInfo *NamespcName = 0; |
| 403 | SourceLocation IdentLoc = SourceLocation(); |
| 404 | |
| 405 | // Parse namespace-name. |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 406 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | f780abc | 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 | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 411 | return 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 414 | // Parse identifier. |
| 415 | NamespcName = Tok.getIdentifierInfo(); |
| 416 | IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 418 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Sean Hunt | bbd37c6 | 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 | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 422 | ParseGNUAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 425 | // Eat ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 426 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 427 | ExpectAndConsume(tok::semi, |
Douglas Gregor | 9ba23b4 | 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 | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 432 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 433 | IdentLoc, NamespcName, attrs.getList()); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Richard Smith | 162e1c1 | 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 | f780abc | 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 | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 441 | /// unqualified-id |
| 442 | /// 'using' :: unqualified-id |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 443 | /// |
Richard Smith | d03de6a | 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 | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 446 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 447 | Decl *Parser::ParseUsingDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 448 | const ParsedTemplateInfo &TemplateInfo, |
| 449 | SourceLocation UsingLoc, |
| 450 | SourceLocation &DeclEnd, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 451 | AccessSpecifier AS, |
| 452 | Decl **OwnedType) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 453 | CXXScopeSpec SS; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 454 | SourceLocation TypenameLoc; |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 455 | bool HasTypenameKeyword = false; |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 456 | ParsedAttributesWithRange Attrs(AttrFactory); |
Sean Hunt | 2edf0a2 | 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 | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 459 | MaybeParseCXX11Attributes(Attrs); |
| 460 | ProhibitAttributes(Attrs); |
| 461 | Attrs.clear(); |
| 462 | Attrs.Range = SourceRange(); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 463 | |
| 464 | // Ignore optional 'typename'. |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 465 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 466 | if (Tok.is(tok::kw_typename)) { |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 467 | TypenameLoc = ConsumeToken(); |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 468 | HasTypenameKeyword = true; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 469 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 470 | |
| 471 | // Parse nested-name-specifier. |
Richard Smith | 2db075b | 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 | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 477 | // Check nested-name specifier. |
| 478 | if (SS.isInvalid()) { |
| 479 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 480 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 481 | } |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 482 | |
Richard Smith | 2db075b | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 483 | SourceLocation TemplateKWLoc; |
| 484 | UnqualifiedId Name; |
| 485 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 486 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 12c118a | 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 | 2db075b | 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 | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 508 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 509 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 510 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 511 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 512 | MaybeParseCXX11Attributes(Attrs); |
Richard Smith | 162e1c1 | 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 | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 518 | // TODO: Can GNU attributes appear here? |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 519 | ConsumeToken(); |
| 520 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 521 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 522 | diag::warn_cxx98_compat_alias_declaration : |
| 523 | diag::ext_alias_declaration); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 524 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 525 | // Type alias templates cannot be specialized. |
| 526 | int SpecKind = -1; |
Richard Smith | 536e9c1 | 2011-05-05 22:36:10 +0000 | [diff] [blame] | 527 | if (TemplateInfo.Kind == ParsedTemplateInfo::Template && |
| 528 | Name.getKind() == UnqualifiedId::IK_TemplateId) |
Richard Smith | 3e4c6c4 | 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 | 162e1c1 | 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 | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 553 | } else if (HasTypenameKeyword) |
Richard Smith | 162e1c1 | 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 | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 561 | TypeAlias = ParseTypeName(0, TemplateInfo.Kind ? |
| 562 | Declarator::AliasTemplateContext : |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 563 | Declarator::AliasDeclContext, AS, OwnedType, |
| 564 | &Attrs); |
Sean Hunt | 2edf0a2 | 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 | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 568 | ProhibitAttributes(Attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 569 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 570 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 571 | MaybeParseGNUAttributes(Attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | 9cfbe48 | 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 | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 577 | !Attrs.empty() ? "attributes list" : |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 578 | IsAliasDecl ? "alias declaration" : "using declaration", |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 579 | tok::semi); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 580 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 581 | // Diagnose an attempt to declare a templated using-declaration. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 582 | // In C++11, alias-declarations can be templates: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 583 | // template <...> using id = type; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 584 | if (TemplateInfo.Kind && !IsAliasDecl) { |
John McCall | 78b8105 | 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 | 480b53c | 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 | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 597 | if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) { |
Douglas Gregor | 480b53c | 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 | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 600 | // Proceed parsing, but reset the HasTypenameKeyword flag. |
| 601 | HasTypenameKeyword = false; |
Douglas Gregor | 480b53c | 2011-09-26 14:30:28 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 604 | if (IsAliasDecl) { |
| 605 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 606 | MultiTemplateParamsArg TemplateParamsArg( |
Richard Smith | 3e4c6c4 | 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 | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 610 | UsingLoc, Name, Attrs.getList(), |
| 611 | TypeAlias); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 612 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 613 | |
Enea Zaffanella | 8d030c7 | 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 | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 620 | /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 621 | /// |
Peter Collingbourne | c6eb44b | 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 | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 625 | /// [C11] static_assert-declaration: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 626 | /// _Static_assert ( constant-expression , string-literal ) ; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 627 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 628 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Peter Collingbourne | c6eb44b | 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 | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 632 | if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 633 | Diag(Tok, diag::ext_c11_static_assert); |
Richard Smith | 841804b | 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 | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 636 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 637 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 639 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 640 | if (T.consumeOpen()) { |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 641 | Diag(Tok, diag::err_expected_lparen); |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 642 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 643 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 644 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 646 | ExprResult AssertExpr(ParseConstantExpression()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 647 | if (AssertExpr.isInvalid()) { |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 648 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 649 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 652 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 653 | return 0; |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 654 | |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 655 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 656 | Diag(Tok, diag::err_expected_string_literal) |
| 657 | << /*Source='static_assert'*/1; |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 658 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 659 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 662 | ExprResult AssertMessage(ParseStringLiteralExpression()); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 663 | if (AssertMessage.isInvalid()) { |
Richard Smith | 3686c71 | 2012-09-13 19:12:50 +0000 | [diff] [blame] | 664 | SkipMalformedDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 665 | return 0; |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 666 | } |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 667 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 668 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 670 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 671 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 672 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 673 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
| 674 | AssertExpr.take(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 675 | AssertMessage.take(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 676 | T.getCloseLocation()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 679 | /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier. |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 680 | /// |
| 681 | /// 'decltype' ( expression ) |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 682 | /// 'decltype' ( 'auto' ) [C++1y] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 683 | /// |
David Blaikie | 42d6d0c | 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 | 42d6d0c | 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 | c7b5543 | 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 | 39304fa | 2012-02-24 18:10:23 +0000 | [diff] [blame] | 703 | |
David Blaikie | 42d6d0c | 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 | a2c3646 | 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 | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 726 | |
Richard Smith | a2c3646 | 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 | 1e58469 | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 737 | } else { |
Richard Smith | a2c3646 | 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 | 1e58469 | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 747 | } |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 748 | return EndLoc; |
Argyrios Kyrtzidis | 1e58469 | 2012-10-26 22:53:44 +0000 | [diff] [blame] | 749 | } |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 750 | |
| 751 | Result = Actions.ActOnDecltypeExpression(Result.take()); |
David Blaikie | 42d6d0c | 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 | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 763 | if (Result.isInvalid()) { |
| 764 | DS.SetTypeSpecError(); |
| 765 | return T.getCloseLocation(); |
| 766 | } |
| 767 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 768 | EndLoc = T.getCloseLocation(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 769 | } |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 770 | assert(!Result.isInvalid()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 772 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 773 | unsigned DiagID; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 774 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Richard Smith | a2c3646 | 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 | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 780 | Diag(StartLoc, DiagID) << PrevSpec; |
David Blaikie | 42d6d0c | 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 | a2c3646 | 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 | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 800 | Tok.setAnnotationEndLoc(EndLoc); |
| 801 | Tok.setLocation(StartLoc); |
| 802 | PP.AnnotateCachedTokens(Tok); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Sean Hunt | db5d44b | 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 | 4a8dfb5 | 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)) { |
Sean Hunt | db5d44b | 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 | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 823 | T.consumeClose(); |
| 824 | if (T.getCloseLocation().isInvalid()) |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 825 | return; |
| 826 | |
| 827 | const char *PrevSpec = 0; |
| 828 | unsigned DiagID; |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 829 | if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec, |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 830 | DiagID, Result.release())) |
| 831 | Diag(StartLoc, DiagID) << PrevSpec; |
Enea Zaffanella | 2d77634 | 2013-07-06 18:54:58 +0000 | [diff] [blame] | 832 | DS.setTypeofParensRange(T.getRange()); |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 833 | } |
| 834 | |
David Blaikie | 09048df | 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 | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 840 | /// |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 841 | /// base-type-specifier: [C++11 class.derived] |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 842 | /// class-or-decltype |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 843 | /// class-or-decltype: [C++11 class.derived] |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 844 | /// nested-name-specifier[opt] class-name |
| 845 | /// decltype-specifier |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 846 | /// class-name: [C++ class.name] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 847 | /// identifier |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 848 | /// simple-template-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | /// |
Richard Smith | 0532140 | 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 | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 853 | Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc, |
| 854 | SourceLocation &EndLocation) { |
David Blaikie | 7fe3878 | 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 | 152aa4b | 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 | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 868 | // Parse decltype-specifier |
David Blaikie | 42d6d0c | 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 | 152aa4b | 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 | 22216eb | 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 | b577757 | 2011-12-08 04:53:15 +0000 | [diff] [blame] | 878 | EndLocation = ParseDecltypeSpecifier(DS); |
David Blaikie | 22216eb | 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 | 7f43d67 | 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 | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 886 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | d9b600c | 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 | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 889 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 890 | |
| 891 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 892 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 893 | EndLocation = Tok.getAnnotationEndLoc(); |
| 894 | ConsumeToken(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 895 | |
| 896 | if (Type) |
| 897 | return Type; |
| 898 | return true; |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | // Fall through to produce an error below. |
| 902 | } |
| 903 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 904 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 905 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 906 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Douglas Gregor | 84d0a19 | 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 | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 917 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 918 | &SS, Template, TNK)) { |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 919 | Diag(IdLoc, diag::err_unknown_template_name) |
| 920 | << Id; |
| 921 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 922 | |
Serge Pavlov | 62f675c | 2013-08-10 05:54:47 +0000 | [diff] [blame] | 923 | if (!Template) { |
| 924 | TemplateArgList TemplateArgs; |
| 925 | SourceLocation LAngleLoc, RAngleLoc; |
| 926 | ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS, |
| 927 | true, LAngleLoc, TemplateArgs, RAngleLoc); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 928 | return true; |
Serge Pavlov | 62f675c | 2013-08-10 05:54:47 +0000 | [diff] [blame] | 929 | } |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 930 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 931 | // Form the template name |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 932 | UnqualifiedId TemplateName; |
| 933 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 935 | // Parse the full template-id, then turn it into a type. |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 936 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
| 937 | TemplateName, true)) |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 938 | return true; |
| 939 | if (TNK == TNK_Dependent_template_name) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 940 | AnnotateTemplateIdTokenAsType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 941 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 942 | // If we didn't end up with a typename token, there's nothing more we |
| 943 | // can do. |
| 944 | if (Tok.isNot(tok::annot_typename)) |
| 945 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 946 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 947 | // Retrieve the type from the annotation token, consume that token, and |
| 948 | // return. |
| 949 | EndLocation = Tok.getAnnotationEndLoc(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 950 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 951 | ConsumeToken(); |
| 952 | return Type; |
| 953 | } |
| 954 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 955 | // We have an identifier; check whether it is actually a type. |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 956 | IdentifierInfo *CorrectedII = 0; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 957 | ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 958 | false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 959 | /*IsCtorOrDtorName=*/false, |
Kaelyn Uhrain | c1fb542 | 2012-06-22 23:37:05 +0000 | [diff] [blame] | 960 | /*NonTrivialTypeSourceInfo=*/true, |
| 961 | &CorrectedII); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 962 | if (!Type) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 963 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 964 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | // Consume the identifier. |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 968 | EndLocation = IdLoc; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 969 | |
| 970 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 971 | DeclSpec DS(AttrFactory); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 972 | DS.SetRangeStart(IdLoc); |
| 973 | DS.SetRangeEnd(EndLocation); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 974 | DS.getTypeSpecScope() = SS; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 975 | |
| 976 | const char *PrevSpec = 0; |
| 977 | unsigned DiagID; |
| 978 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type); |
| 979 | |
| 980 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 981 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 982 | } |
| 983 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 984 | void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) { |
| 985 | while (Tok.is(tok::kw___single_inheritance) || |
| 986 | Tok.is(tok::kw___multiple_inheritance) || |
| 987 | Tok.is(tok::kw___virtual_inheritance)) { |
| 988 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 989 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 990 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 991 | AttributeList::AS_GNU); |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 995 | /// Determine whether the following tokens are valid after a type-specifier |
| 996 | /// which could be a standalone declaration. This will conservatively return |
| 997 | /// true if there's any doubt, and is appropriate for insert-';' fixits. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 998 | bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 999 | // This switch enumerates the valid "follow" set for type-specifiers. |
| 1000 | switch (Tok.getKind()) { |
| 1001 | default: break; |
| 1002 | case tok::semi: // struct foo {...} ; |
| 1003 | case tok::star: // struct foo {...} * P; |
| 1004 | case tok::amp: // struct foo {...} & R = ... |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1005 | case tok::ampamp: // struct foo {...} && R = ... |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1006 | case tok::identifier: // struct foo {...} V ; |
| 1007 | case tok::r_paren: //(struct foo {...} ) {4} |
| 1008 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1009 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1010 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
| 1011 | case tok::l_paren: // struct foo {...} ( x); |
| 1012 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1013 | case tok::kw_operator: // struct foo operator ++() {...} |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1014 | return true; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1015 | case tok::colon: |
| 1016 | return CouldBeBitfield; // enum E { ... } : 2; |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1017 | // Type qualifiers |
| 1018 | case tok::kw_const: // struct foo {...} const x; |
| 1019 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1020 | case tok::kw_restrict: // struct foo {...} restrict x; |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1021 | // Function specifiers |
| 1022 | // Note, no 'explicit'. An explicit function must be either a conversion |
| 1023 | // operator or a constructor. Either way, it can't have a return type. |
| 1024 | case tok::kw_inline: // struct foo inline f(); |
| 1025 | case tok::kw_virtual: // struct foo virtual f(); |
| 1026 | case tok::kw_friend: // struct foo friend f(); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1027 | // Storage-class specifiers |
| 1028 | case tok::kw_static: // struct foo {...} static x; |
| 1029 | case tok::kw_extern: // struct foo {...} extern x; |
| 1030 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1031 | case tok::kw_register: // struct foo {...} register x; |
| 1032 | case tok::kw_auto: // struct foo {...} auto x; |
| 1033 | case tok::kw_mutable: // struct foo {...} mutable x; |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1034 | case tok::kw_thread_local: // struct foo {...} thread_local x; |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1035 | case tok::kw_constexpr: // struct foo {...} constexpr x; |
| 1036 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1037 | // can occur after class specifiers according to the grammar. However, |
| 1038 | // almost no one actually writes code like this. If we see one of these, |
| 1039 | // it is much more likely that someone missed a semi colon and the |
| 1040 | // type/storage class specifier we're seeing is part of the *next* |
| 1041 | // intended declaration, as in: |
| 1042 | // |
| 1043 | // struct foo { ... } |
| 1044 | // typedef int X; |
| 1045 | // |
| 1046 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1047 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1048 | // the same declaration of X. Because of this, we look ahead past this |
| 1049 | // token to see if it's a type specifier. If so, we know the code is |
| 1050 | // otherwise invalid, so we can produce the expected semi error. |
| 1051 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1052 | return true; |
| 1053 | break; |
| 1054 | case tok::r_brace: // struct bar { struct foo {...} } |
| 1055 | // Missing ';' at end of struct is accepted as an extension in C mode. |
| 1056 | if (!getLangOpts().CPlusPlus) |
| 1057 | return true; |
| 1058 | break; |
Richard Smith | ba65f50 | 2013-01-19 03:48:05 +0000 | [diff] [blame] | 1059 | // C++11 attributes |
| 1060 | case tok::l_square: // enum E [[]] x |
| 1061 | // Note, no tok::kw_alignas here; alignas cannot appertain to a type. |
| 1062 | return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square); |
Richard Smith | 8338a9d | 2013-01-29 04:13:32 +0000 | [diff] [blame] | 1063 | case tok::greater: |
| 1064 | // template<class T = class X> |
| 1065 | return getLangOpts().CPlusPlus; |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1066 | } |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1070 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 1071 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 1072 | /// until we reach the start of a definition or see a token that |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1073 | /// cannot start a definition. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1074 | /// |
| 1075 | /// class-specifier: [C++ class] |
| 1076 | /// class-head '{' member-specification[opt] '}' |
| 1077 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 1078 | /// class-head: |
| 1079 | /// class-key identifier[opt] base-clause[opt] |
| 1080 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 1081 | /// class-key nested-name-specifier[opt] simple-template-id |
| 1082 | /// base-clause[opt] |
| 1083 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1085 | /// identifier base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1087 | /// simple-template-id base-clause[opt] |
| 1088 | /// class-key: |
| 1089 | /// 'class' |
| 1090 | /// 'struct' |
| 1091 | /// 'union' |
| 1092 | /// |
| 1093 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 1095 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 1096 | /// simple-template-id |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1097 | /// |
| 1098 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 1099 | /// together, subsume the C99 struct-or-union-specifier: |
| 1100 | /// |
| 1101 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 1102 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 1103 | /// struct-or-union identifier |
| 1104 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 1105 | /// '}' attributes[opt] |
| 1106 | /// [GNU] struct-or-union attributes[opt] identifier |
| 1107 | /// struct-or-union: |
| 1108 | /// 'struct' |
| 1109 | /// 'union' |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1110 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 1111 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1112 | const ParsedTemplateInfo &TemplateInfo, |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1113 | AccessSpecifier AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1114 | bool EnteringContext, DeclSpecContext DSC, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1115 | ParsedAttributesWithRange &Attributes) { |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1116 | DeclSpec::TST TagType; |
| 1117 | if (TagTokKind == tok::kw_struct) |
| 1118 | TagType = DeclSpec::TST_struct; |
| 1119 | else if (TagTokKind == tok::kw___interface) |
| 1120 | TagType = DeclSpec::TST_interface; |
| 1121 | else if (TagTokKind == tok::kw_class) |
| 1122 | TagType = DeclSpec::TST_class; |
| 1123 | else { |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1124 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 1125 | TagType = DeclSpec::TST_union; |
| 1126 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1127 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1128 | if (Tok.is(tok::code_completion)) { |
| 1129 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1130 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1131 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1132 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1133 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1134 | // C++03 [temp.explicit] 14.7.2/8: |
| 1135 | // The usual access checking rules do not apply to names used to specify |
| 1136 | // explicit instantiations. |
| 1137 | // |
| 1138 | // As an extension we do not perform access checking on the names used to |
| 1139 | // specify explicit specializations either. This is important to allow |
| 1140 | // specializing traits classes for private types. |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1141 | // |
| 1142 | // Note that we don't suppress if this turns out to be an elaborated |
| 1143 | // type specifier. |
| 1144 | bool shouldDelayDiagsInTag = |
| 1145 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 1146 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 1147 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1148 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1149 | ParsedAttributesWithRange attrs(AttrFactory); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1150 | // If attributes exist after tag, parse them. |
| 1151 | if (Tok.is(tok::kw___attribute)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1152 | ParseGNUAttributes(attrs); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1153 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1154 | // If declspecs exist after tag, parse them. |
John McCall | b1d397c | 2010-08-05 17:13:11 +0000 | [diff] [blame] | 1155 | while (Tok.is(tok::kw___declspec)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1156 | ParseMicrosoftDeclSpec(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1157 | |
John McCall | c052dbb | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 1158 | // Parse inheritance specifiers. |
| 1159 | if (Tok.is(tok::kw___single_inheritance) || |
| 1160 | Tok.is(tok::kw___multiple_inheritance) || |
| 1161 | Tok.is(tok::kw___virtual_inheritance)) |
| 1162 | ParseMicrosoftInheritanceClassAttributes(attrs); |
| 1163 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1164 | // If C++0x attributes exist here, parse them. |
| 1165 | // FIXME: Are we consistent with the ordering of parsing of different |
| 1166 | // styles of attributes? |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1167 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1169 | // Source location used by FIXIT to insert misplaced |
| 1170 | // C++11 attributes |
| 1171 | SourceLocation AttrFixitLoc = Tok.getLocation(); |
| 1172 | |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1173 | if (TagType == DeclSpec::TST_struct && |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1174 | !Tok.is(tok::identifier) && |
| 1175 | Tok.getIdentifierInfo() && |
| 1176 | (Tok.is(tok::kw___is_arithmetic) || |
| 1177 | Tok.is(tok::kw___is_convertible) || |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1178 | Tok.is(tok::kw___is_empty) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1179 | Tok.is(tok::kw___is_floating_point) || |
| 1180 | Tok.is(tok::kw___is_function) || |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1181 | Tok.is(tok::kw___is_fundamental) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1182 | Tok.is(tok::kw___is_integral) || |
| 1183 | Tok.is(tok::kw___is_member_function_pointer) || |
| 1184 | Tok.is(tok::kw___is_member_pointer) || |
| 1185 | Tok.is(tok::kw___is_pod) || |
| 1186 | Tok.is(tok::kw___is_pointer) || |
| 1187 | Tok.is(tok::kw___is_same) || |
Douglas Gregor | 877222e | 2011-04-29 01:38:03 +0000 | [diff] [blame] | 1188 | Tok.is(tok::kw___is_scalar) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1189 | Tok.is(tok::kw___is_signed) || |
| 1190 | Tok.is(tok::kw___is_unsigned) || |
| 1191 | Tok.is(tok::kw___is_void))) { |
Douglas Gregor | 6887614 | 2011-07-30 07:01:49 +0000 | [diff] [blame] | 1192 | // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1193 | // name of struct templates, but some are keywords in GCC >= 4.3 |
| 1194 | // and Clang. Therefore, when we see the token sequence "struct |
| 1195 | // X", make X into a normal identifier rather than a keyword, to |
| 1196 | // allow libstdc++ 4.2 and libc++ to work properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 1197 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 1198 | Tok.setKind(tok::identifier); |
| 1199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1201 | // Parse the (optional) nested-name-specifier. |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1202 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1203 | if (getLangOpts().CPlusPlus) { |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1204 | // "FOO : BAR" is not a potential typo for "FOO::BAR". |
| 1205 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1207 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1208 | DS.SetTypeSpecError(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1209 | if (SS.isSet()) |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1210 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
| 1211 | Diag(Tok, diag::err_expected_ident); |
| 1212 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1214 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 1215 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1216 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1217 | IdentifierInfo *Name = 0; |
| 1218 | SourceLocation NameLoc; |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1219 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1220 | if (Tok.is(tok::identifier)) { |
| 1221 | Name = Tok.getIdentifierInfo(); |
| 1222 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1223 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1224 | if (Tok.is(tok::less) && getLangOpts().CPlusPlus) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1225 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1226 | // Eat the template argument list and try to continue parsing this as |
| 1227 | // a class (or template thereof). |
| 1228 | TemplateArgList TemplateArgs; |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1229 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1230 | if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS, |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1231 | true, LAngleLoc, |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1232 | TemplateArgs, RAngleLoc)) { |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1233 | // We couldn't parse the template argument list at all, so don't |
| 1234 | // try to give any location information for the list. |
| 1235 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 1236 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1238 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1239 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 1240 | << (TagType == DeclSpec::TST_class? 0 |
| 1241 | : TagType == DeclSpec::TST_struct? 1 |
| 1242 | : TagType == DeclSpec::TST_interface? 2 |
| 1243 | : 3) |
| 1244 | << Name |
| 1245 | << SourceRange(LAngleLoc, RAngleLoc); |
| 1246 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1247 | // Strip off the last template parameter list if it was empty, since |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1248 | // we've removed its template argument list. |
| 1249 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
| 1250 | if (TemplateParams && TemplateParams->size() > 1) { |
| 1251 | TemplateParams->pop_back(); |
| 1252 | } else { |
| 1253 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1254 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1255 | = ParsedTemplateInfo::NonTemplate; |
| 1256 | } |
| 1257 | } else if (TemplateInfo.Kind |
| 1258 | == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1259 | // Pretend this is just a forward declaration. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1260 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1261 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1262 | = ParsedTemplateInfo::NonTemplate; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1263 | const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1264 | = SourceLocation(); |
| 1265 | const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc |
| 1266 | = SourceLocation(); |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1267 | } |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1268 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1269 | } else if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1270 | TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1271 | NameLoc = ConsumeToken(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1272 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1273 | if (TemplateId->Kind != TNK_Type_template && |
| 1274 | TemplateId->Kind != TNK_Dependent_template_name) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1275 | // The template-name in the simple-template-id refers to |
| 1276 | // something other than a class template. Give an appropriate |
| 1277 | // error message and skip to the ';'. |
| 1278 | SourceRange Range(NameLoc); |
| 1279 | if (SS.isNotEmpty()) |
| 1280 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1282 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
Richard Trieu | 6e91f4b | 2013-06-19 22:25:01 +0000 | [diff] [blame] | 1283 | << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1285 | DS.SetTypeSpecError(); |
| 1286 | SkipUntil(tok::semi, false, true); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1287 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1288 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1291 | // There are four options here. |
| 1292 | // - If we are in a trailing return type, this is always just a reference, |
| 1293 | // and we must not try to parse a definition. For instance, |
| 1294 | // [] () -> struct S { }; |
| 1295 | // does not define a type. |
| 1296 | // - If we have 'struct foo {...', 'struct foo :...', |
| 1297 | // 'struct foo final :' or 'struct foo final {', then this is a definition. |
| 1298 | // - If we have 'struct foo;', then this is either a forward declaration |
| 1299 | // or a friend declaration, which have to be treated differently. |
| 1300 | // - Otherwise we have something like 'struct foo xyz', a reference. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1301 | // |
| 1302 | // We also detect these erroneous cases to provide better diagnostic for |
| 1303 | // C++11 attributes parsing. |
| 1304 | // - attributes follow class name: |
| 1305 | // struct foo [[]] {}; |
| 1306 | // - attributes appear before or after 'final': |
| 1307 | // struct foo [[]] final [[]] {}; |
| 1308 | // |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1309 | // However, in type-specifier-seq's, things look like declarations but are |
| 1310 | // just references, e.g. |
| 1311 | // new struct s; |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1312 | // or |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1313 | // &T::operator struct s; |
| 1314 | // For these, DSC is DSC_type_specifier. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1315 | |
| 1316 | // If there are attributes after class name, parse them. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1317 | MaybeParseCXX11Attributes(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1318 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1319 | Sema::TagUseKind TUK; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1320 | if (DSC == DSC_trailing) |
| 1321 | TUK = Sema::TUK_Reference; |
| 1322 | else if (Tok.is(tok::l_brace) || |
| 1323 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1324 | (isCXX11FinalKeyword() && |
David Blaikie | 6f42669 | 2012-03-12 15:39:49 +0000 | [diff] [blame] | 1325 | (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1326 | if (DS.isFriendSpecified()) { |
| 1327 | // C++ [class.friend]p2: |
| 1328 | // A class shall not be defined in a friend declaration. |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1329 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1330 | << SourceRange(DS.getFriendSpecLoc()); |
| 1331 | |
| 1332 | // Skip everything up to the semicolon, so that this looks like a proper |
| 1333 | // friend class (or template thereof) declaration. |
| 1334 | SkipUntil(tok::semi, true, true); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1335 | TUK = Sema::TUK_Friend; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1336 | } else { |
| 1337 | // Okay, this is a class definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1338 | TUK = Sema::TUK_Definition; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1339 | } |
Richard Smith | 150d853 | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1340 | } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) || |
| 1341 | NextToken().is(tok::kw_alignas))) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1342 | // We can't tell if this is a definition or reference |
| 1343 | // until we skipped the 'final' and C++11 attribute specifiers. |
| 1344 | TentativeParsingAction PA(*this); |
| 1345 | |
| 1346 | // Skip the 'final' keyword. |
| 1347 | ConsumeToken(); |
| 1348 | |
| 1349 | // Skip C++11 attribute specifiers. |
| 1350 | while (true) { |
| 1351 | if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) { |
| 1352 | ConsumeBracket(); |
| 1353 | if (!SkipUntil(tok::r_square)) |
| 1354 | break; |
Richard Smith | 150d853 | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 1355 | } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1356 | ConsumeToken(); |
| 1357 | ConsumeParen(); |
| 1358 | if (!SkipUntil(tok::r_paren)) |
| 1359 | break; |
| 1360 | } else { |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | if (Tok.is(tok::l_brace) || Tok.is(tok::colon)) |
| 1366 | TUK = Sema::TUK_Definition; |
| 1367 | else |
| 1368 | TUK = Sema::TUK_Reference; |
| 1369 | |
| 1370 | PA.Revert(); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1371 | } else if (DSC != DSC_type_specifier && |
| 1372 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 1373 | (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1374 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1375 | if (Tok.isNot(tok::semi)) { |
| 1376 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 1377 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1378 | DeclSpec::getSpecifierName(TagType)); |
| 1379 | PP.EnterToken(Tok); |
| 1380 | Tok.setKind(tok::semi); |
| 1381 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1382 | } else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1383 | TUK = Sema::TUK_Reference; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1384 | |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1385 | // Forbid misplaced attributes. In cases of a reference, we pass attributes |
| 1386 | // to caller to handle. |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1387 | if (TUK != Sema::TUK_Reference) { |
| 1388 | // If this is not a reference, then the only possible |
| 1389 | // valid place for C++11 attributes to appear here |
| 1390 | // is between class-key and class-name. If there are |
| 1391 | // any attributes after class-name, we try a fixit to move |
| 1392 | // them to the right place. |
| 1393 | SourceRange AttrRange = Attributes.Range; |
| 1394 | if (AttrRange.isValid()) { |
| 1395 | Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed) |
| 1396 | << AttrRange |
| 1397 | << FixItHint::CreateInsertionFromRange(AttrFixitLoc, |
| 1398 | CharSourceRange(AttrRange, true)) |
| 1399 | << FixItHint::CreateRemoval(AttrRange); |
| 1400 | |
| 1401 | // Recover by adding misplaced attributes to the attribute list |
| 1402 | // of the class so they can be applied on the class later. |
| 1403 | attrs.takeAllFrom(Attributes); |
| 1404 | } |
| 1405 | } |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1406 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 1407 | // If this is an elaborated type specifier, and we delayed |
| 1408 | // diagnostics before, just merge them into the current pool. |
| 1409 | if (shouldDelayDiagsInTag) { |
| 1410 | diagsFromTag.done(); |
| 1411 | if (TUK == Sema::TUK_Reference) |
| 1412 | diagsFromTag.redelay(); |
| 1413 | } |
| 1414 | |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1415 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1416 | TUK != Sema::TUK_Definition)) { |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1417 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 1418 | // We have a declaration or reference to an anonymous class. |
| 1419 | Diag(StartLoc, diag::err_anon_type_definition) |
| 1420 | << DeclSpec::getSpecifierName(TagType); |
| 1421 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1423 | SkipUntil(tok::comma, true); |
| 1424 | return; |
| 1425 | } |
| 1426 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1427 | // Create the tag portion of the class or class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1428 | DeclResult TagOrTempResult = true; // invalid |
| 1429 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1430 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1431 | bool Owned = false; |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1432 | if (TemplateId) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1433 | // Explicit specialization, class template partial specialization, |
| 1434 | // or explicit instantiation. |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1435 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1436 | TemplateId->NumArgs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1437 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1438 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1439 | // This is an explicit instantiation of a class template. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1440 | ProhibitAttributes(attrs); |
| 1441 | |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1442 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1443 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1444 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | TemplateInfo.TemplateLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1446 | TagType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | StartLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1448 | SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1449 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | TemplateId->TemplateNameLoc, |
| 1451 | TemplateId->LAngleLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1452 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1454 | attrs.getList()); |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Friend template-ids are treated as references unless |
| 1457 | // they have template headers, in which case they're ill-formed |
| 1458 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 1459 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1460 | } else if (TUK == Sema::TUK_Reference || |
| 1461 | (TUK == Sema::TUK_Friend && |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1462 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1463 | ProhibitAttributes(attrs); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1464 | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1465 | TemplateId->SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1466 | TemplateId->TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1467 | TemplateId->Template, |
| 1468 | TemplateId->TemplateNameLoc, |
| 1469 | TemplateId->LAngleLoc, |
| 1470 | TemplateArgsPtr, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1471 | TemplateId->RAngleLoc); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1472 | } else { |
| 1473 | // This is an explicit specialization or a class template |
| 1474 | // partial specialization. |
| 1475 | TemplateParameterLists FakedParamLists; |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1476 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1477 | // This looks like an explicit instantiation, because we have |
| 1478 | // something like |
| 1479 | // |
| 1480 | // template class Foo<X> |
| 1481 | // |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1482 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1483 | // meant to be an explicit specialization, but the user forgot |
| 1484 | // the '<>' after 'template'. |
Larisse Voufo | 4985429 | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 1485 | // It this is friend declaration however, since it cannot have a |
| 1486 | // template header, it is most likely that the user meant to |
| 1487 | // remove the 'template' keyword. |
| 1488 | assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) && |
| 1489 | "Expected a definition here"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1490 | |
Larisse Voufo | 4985429 | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 1491 | if (TUK == Sema::TUK_Friend) { |
| 1492 | Diag(DS.getFriendSpecLoc(), |
| 1493 | diag::err_friend_explicit_instantiation); |
| 1494 | TemplateParams = 0; |
| 1495 | } else { |
| 1496 | SourceLocation LAngleLoc |
| 1497 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1498 | Diag(TemplateId->TemplateNameLoc, |
| 1499 | diag::err_explicit_instantiation_with_definition) |
| 1500 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1501 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1502 | |
| 1503 | // Create a fake template parameter list that contains only |
| 1504 | // "template<>", so that we treat this construct as a class |
| 1505 | // template specialization. |
| 1506 | FakedParamLists.push_back( |
| 1507 | Actions.ActOnTemplateParameterList(0, SourceLocation(), |
| 1508 | TemplateInfo.TemplateLoc, |
| 1509 | LAngleLoc, |
| 1510 | 0, 0, |
| 1511 | LAngleLoc)); |
| 1512 | TemplateParams = &FakedParamLists; |
| 1513 | } |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | // Build the class template specialization. |
| 1517 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1518 | = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 1519 | StartLoc, DS.getModulePrivateSpecLoc(), SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1520 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | TemplateId->TemplateNameLoc, |
| 1522 | TemplateId->LAngleLoc, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1523 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1525 | attrs.getList(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1526 | MultiTemplateParamsArg( |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1527 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 1528 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1529 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1530 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1531 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1532 | // Explicit instantiation of a member of a class template |
| 1533 | // specialization, e.g., |
| 1534 | // |
| 1535 | // template struct Outer<int>::Inner; |
| 1536 | // |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1537 | ProhibitAttributes(attrs); |
| 1538 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1539 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1540 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1541 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | TemplateInfo.TemplateLoc, |
| 1543 | TagType, StartLoc, SS, Name, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1544 | NameLoc, attrs.getList()); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1545 | } else if (TUK == Sema::TUK_Friend && |
| 1546 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1547 | ProhibitAttributes(attrs); |
| 1548 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1549 | TagOrTempResult = |
| 1550 | Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(), |
| 1551 | TagType, StartLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1552 | Name, NameLoc, attrs.getList(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1553 | MultiTemplateParamsArg( |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1554 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 1555 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1556 | } else { |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1557 | if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition) |
| 1558 | ProhibitAttributes(attrs); |
Larisse Voufo | 7c64ef0 | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1559 | |
| 1560 | if (TUK == Sema::TUK_Definition && |
| 1561 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1562 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1563 | // recover by ignoring the 'template' keyword. |
| 1564 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1565 | << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
Larisse Voufo | 4985429 | 2013-06-22 13:56:11 +0000 | [diff] [blame] | 1566 | TemplateParams = 0; |
Larisse Voufo | 7c64ef0 | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 1567 | } |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1568 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1569 | bool IsDependent = false; |
| 1570 | |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 1571 | // Don't pass down template parameter lists if this is just a tag |
| 1572 | // reference. For example, we don't need the template parameters here: |
| 1573 | // template <class T> class A *makeA(T t); |
| 1574 | MultiTemplateParamsArg TParams; |
| 1575 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 1576 | TParams = |
| 1577 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 1578 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1579 | // Declaration or definition of a class type |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1580 | TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1581 | SS, Name, NameLoc, attrs.getList(), AS, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 1582 | DS.getModulePrivateSpecLoc(), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1583 | TParams, Owned, IsDependent, |
| 1584 | SourceLocation(), false, |
| 1585 | clang::TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1586 | |
| 1587 | // If ActOnTag said the type was dependent, try again with the |
| 1588 | // less common call. |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1589 | if (IsDependent) { |
| 1590 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1591 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1592 | SS, Name, StartLoc, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1593 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1594 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1595 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1596 | // If there is a body, parse it and inform the actions module. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1597 | if (TUK == Sema::TUK_Definition) { |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1598 | assert(Tok.is(tok::l_brace) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1599 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1600 | isCXX11FinalKeyword()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1601 | if (getLangOpts().CPlusPlus) |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 1602 | ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType, |
| 1603 | TagOrTempResult.get()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1604 | else |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1605 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1608 | const char *PrevSpec = 0; |
| 1609 | unsigned DiagID; |
| 1610 | bool Result; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1611 | if (!TypeResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1612 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 1613 | NameLoc.isValid() ? NameLoc : StartLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1614 | PrevSpec, DiagID, TypeResult.get()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1615 | } else if (!TagOrTempResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1616 | Result = DS.SetTypeSpecType(TagType, StartLoc, |
| 1617 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 1618 | PrevSpec, DiagID, TagOrTempResult.get(), Owned); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1619 | } else { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1620 | DS.SetTypeSpecError(); |
Anders Carlsson | 66e9977 | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1621 | return; |
| 1622 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1624 | if (Result) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1625 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1626 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1627 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 1628 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 1629 | // going to look at what comes after it to improve error recovery. If an |
| 1630 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 1631 | // the end of the declaration and recover that way. |
| 1632 | // |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 1633 | // Also enforce C++ [temp]p3: |
| 1634 | // In a template-declaration which defines a class, no declarator |
| 1635 | // is permitted. |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1636 | if (TUK == Sema::TUK_Definition && |
| 1637 | (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) { |
Argyrios Kyrtzidis | 7d033b2 | 2012-12-17 20:10:43 +0000 | [diff] [blame] | 1638 | if (Tok.isNot(tok::semi)) { |
| 1639 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1640 | DeclSpec::getSpecifierName(TagType)); |
| 1641 | // Push this token back into the preprocessor and change our current token |
| 1642 | // to ';' so that the rest of the code recovers as though there were an |
| 1643 | // ';' after the definition. |
| 1644 | PP.EnterToken(Tok); |
| 1645 | Tok.setKind(tok::semi); |
| 1646 | } |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1647 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1651 | /// |
| 1652 | /// base-clause : [C++ class.derived] |
| 1653 | /// ':' base-specifier-list |
| 1654 | /// base-specifier-list: |
| 1655 | /// base-specifier '...'[opt] |
| 1656 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1657 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1658 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 1659 | ConsumeToken(); |
| 1660 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1661 | // Build up an array of parsed base specifiers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1662 | SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1663 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1664 | while (true) { |
| 1665 | // Parse a base-specifier. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1666 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1667 | if (Result.isInvalid()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1668 | // Skip the rest of this base specifier, up until the comma or |
| 1669 | // opening brace. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1670 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 1671 | } else { |
| 1672 | // Add this to our array of base specifiers. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1673 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | // If the next token is a comma, consume it and keep reading |
| 1677 | // base-specifiers. |
| 1678 | if (Tok.isNot(tok::comma)) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1680 | // Consume the comma. |
| 1681 | ConsumeToken(); |
| 1682 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Attach the base specifiers |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1685 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 1689 | /// one entry in the base class list of a class specifier, for example: |
| 1690 | /// class foo : public bar, virtual private baz { |
| 1691 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 1692 | /// |
| 1693 | /// base-specifier: [C++ class.derived] |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1694 | /// attribute-specifier-seq[opt] base-type-specifier |
| 1695 | /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt] |
| 1696 | /// base-type-specifier |
| 1697 | /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt] |
| 1698 | /// base-type-specifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1699 | Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1700 | bool IsVirtual = false; |
| 1701 | SourceLocation StartLoc = Tok.getLocation(); |
| 1702 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1703 | ParsedAttributesWithRange Attributes(AttrFactory); |
| 1704 | MaybeParseCXX11Attributes(Attributes); |
| 1705 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1706 | // Parse the 'virtual' keyword. |
| 1707 | if (Tok.is(tok::kw_virtual)) { |
| 1708 | ConsumeToken(); |
| 1709 | IsVirtual = true; |
| 1710 | } |
| 1711 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1712 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 1713 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1714 | // Parse an (optional) access specifier. |
| 1715 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1716 | if (Access != AS_none) |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1717 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1719 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 1720 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1721 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 1722 | // access specifier. |
| 1723 | if (Tok.is(tok::kw_virtual)) { |
| 1724 | SourceLocation VirtualLoc = ConsumeToken(); |
| 1725 | if (IsVirtual) { |
| 1726 | // Complain about duplicate 'virtual' |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1727 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1728 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | IsVirtual = true; |
| 1732 | } |
| 1733 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1734 | CheckMisplacedCXX11Attribute(Attributes, StartLoc); |
| 1735 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1736 | // Parse the class-name. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1737 | SourceLocation EndLocation; |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1738 | SourceLocation BaseLoc; |
| 1739 | TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1740 | if (BaseType.isInvalid()) |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1741 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1743 | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
| 1744 | // actually part of the base-specifier-list grammar productions, but we |
| 1745 | // parse it here for convenience. |
| 1746 | SourceLocation EllipsisLoc; |
| 1747 | if (Tok.is(tok::ellipsis)) |
| 1748 | EllipsisLoc = ConsumeToken(); |
| 1749 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1751 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1753 | // Notify semantic analysis that we have parsed a complete |
| 1754 | // base-specifier. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1755 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual, |
| 1756 | Access, BaseType.get(), BaseLoc, |
| 1757 | EllipsisLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 1761 | /// a C++ access-specifier. |
| 1762 | /// |
| 1763 | /// access-specifier: [C++ class.derived] |
| 1764 | /// 'private' |
| 1765 | /// 'protected' |
| 1766 | /// 'public' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1768 | switch (Tok.getKind()) { |
| 1769 | default: return AS_none; |
| 1770 | case tok::kw_private: return AS_private; |
| 1771 | case tok::kw_protected: return AS_protected; |
| 1772 | case tok::kw_public: return AS_public; |
| 1773 | } |
| 1774 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1776 | /// \brief If the given declarator has any parts for which parsing has to be |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 1777 | /// delayed, e.g., default arguments, create a late-parsed method declaration |
| 1778 | /// record to handle the parsing at the end of the class definition. |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1779 | void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo, |
| 1780 | Decl *ThisDecl) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1781 | // We just declared a member function. If this member function |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 1782 | // has any default arguments, we'll need to parse them later. |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1783 | LateParsedMethodDeclaration *LateMethod = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | DeclaratorChunk::FunctionTypeInfo &FTI |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1785 | = DeclaratorInfo.getFunctionTypeInfo(); |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1786 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1787 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 1788 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 1789 | if (!LateMethod) { |
| 1790 | // Push this method onto the stack of late-parsed method |
| 1791 | // declarations. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1792 | LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
| 1793 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1794 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1795 | |
| 1796 | // Add all of the parameters prior to this one (they don't |
| 1797 | // have default arguments). |
| 1798 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 1799 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 1800 | LateMethod->DefaultArgs.push_back( |
Douglas Gregor | 8f8210c | 2010-03-02 01:29:43 +0000 | [diff] [blame] | 1801 | LateParsedDefaultArgument(FTI.ArgInfo[I].Param)); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1804 | // Add this parameter to the list of parameters (it may or may |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1805 | // not have a default argument). |
| 1806 | LateMethod->DefaultArgs.push_back( |
| 1807 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 1808 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1813 | /// isCXX11VirtSpecifier - Determine whether the given token is a C++11 |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1814 | /// virt-specifier. |
| 1815 | /// |
| 1816 | /// virt-specifier: |
| 1817 | /// override |
| 1818 | /// final |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1819 | VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1820 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1821 | return VirtSpecifiers::VS_None; |
| 1822 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1823 | if (Tok.is(tok::identifier)) { |
| 1824 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1825 | |
Anders Carlsson | 7eeb4ec | 2011-01-20 03:47:08 +0000 | [diff] [blame] | 1826 | // Initialize the contextual keywords. |
| 1827 | if (!Ident_final) { |
| 1828 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1829 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1830 | } |
| 1831 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1832 | if (II == Ident_override) |
| 1833 | return VirtSpecifiers::VS_Override; |
| 1834 | |
| 1835 | if (II == Ident_final) |
| 1836 | return VirtSpecifiers::VS_Final; |
| 1837 | } |
| 1838 | |
| 1839 | return VirtSpecifiers::VS_None; |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1842 | /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq. |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1843 | /// |
| 1844 | /// virt-specifier-seq: |
| 1845 | /// virt-specifier |
| 1846 | /// virt-specifier-seq virt-specifier |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1847 | void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS, |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1848 | bool IsInterface) { |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1849 | while (true) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1850 | VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1851 | if (Specifier == VirtSpecifiers::VS_None) |
| 1852 | return; |
| 1853 | |
| 1854 | // C++ [class.mem]p8: |
| 1855 | // A virt-specifier-seq shall contain at most one of each virt-specifier. |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1856 | const char *PrevSpec = 0; |
Anders Carlsson | 46127a9 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 1857 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1858 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
| 1859 | << PrevSpec |
| 1860 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1861 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1862 | if (IsInterface && Specifier == VirtSpecifiers::VS_Final) { |
| 1863 | Diag(Tok.getLocation(), diag::err_override_control_interface) |
| 1864 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 1865 | } else { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1866 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ? |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1867 | diag::warn_cxx98_compat_override_control_keyword : |
| 1868 | diag::ext_override_control_keyword) |
| 1869 | << VirtSpecifiers::getSpecifierName(Specifier); |
| 1870 | } |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1871 | ConsumeToken(); |
| 1872 | } |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1875 | /// isCXX11FinalKeyword - Determine whether the next token is a C++11 |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1876 | /// contextual 'final' keyword. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1877 | bool Parser::isCXX11FinalKeyword() const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1878 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1879 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1880 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1881 | if (!Tok.is(tok::identifier)) |
| 1882 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1883 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1884 | // Initialize the contextual keywords. |
| 1885 | if (!Ident_final) { |
| 1886 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1887 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1888 | } |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1889 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1890 | return Tok.getIdentifierInfo() == Ident_final; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1893 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 1894 | /// |
| 1895 | /// member-declaration: |
| 1896 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 1897 | /// function-definition ';'[opt] |
| 1898 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 1899 | /// using-declaration [TODO] |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1900 | /// [C++0x] static_assert-declaration |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1901 | /// template-declaration |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1902 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1903 | /// |
| 1904 | /// member-declarator-list: |
| 1905 | /// member-declarator |
| 1906 | /// member-declarator-list ',' member-declarator |
| 1907 | /// |
| 1908 | /// member-declarator: |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1909 | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1910 | /// declarator constant-initializer[opt] |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1911 | /// [C++11] declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1912 | /// identifier[opt] ':' constant-expression |
| 1913 | /// |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1914 | /// virt-specifier-seq: |
| 1915 | /// virt-specifier |
| 1916 | /// virt-specifier-seq virt-specifier |
| 1917 | /// |
| 1918 | /// virt-specifier: |
| 1919 | /// override |
| 1920 | /// final |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1921 | /// |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1922 | /// pure-specifier: |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1923 | /// '= 0' |
| 1924 | /// |
| 1925 | /// constant-initializer: |
| 1926 | /// '=' constant-expression |
| 1927 | /// |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1928 | void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1929 | AttributeList *AccessAttrs, |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1930 | const ParsedTemplateInfo &TemplateInfo, |
| 1931 | ParsingDeclRAIIObject *TemplateDiags) { |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1932 | if (Tok.is(tok::at)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1933 | if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs)) |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1934 | Diag(Tok, diag::err_at_defs_cxx); |
| 1935 | else |
| 1936 | Diag(Tok, diag::err_at_in_class); |
| 1937 | |
| 1938 | ConsumeToken(); |
| 1939 | SkipUntil(tok::r_brace); |
| 1940 | return; |
| 1941 | } |
| 1942 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1943 | // Access declarations. |
Richard Smith | 83a22ec | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 1944 | bool MalformedTypeSpec = false; |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1945 | if (!TemplateInfo.Kind && |
Richard Smith | 83a22ec | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 1946 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) { |
| 1947 | if (TryAnnotateCXXScopeToken()) |
| 1948 | MalformedTypeSpec = true; |
| 1949 | |
| 1950 | bool isAccessDecl; |
| 1951 | if (Tok.isNot(tok::annot_cxxscope)) |
| 1952 | isAccessDecl = false; |
| 1953 | else if (NextToken().is(tok::identifier)) |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1954 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 1955 | else |
| 1956 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 1957 | |
| 1958 | if (isAccessDecl) { |
| 1959 | // Collect the scope specifier token we annotated earlier. |
| 1960 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1961 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 1962 | /*EnteringContext=*/false); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1963 | |
| 1964 | // Try to parse an unqualified-id. |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1965 | SourceLocation TemplateKWLoc; |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1966 | UnqualifiedId Name; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1967 | if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), |
| 1968 | TemplateKWLoc, Name)) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1969 | SkipUntil(tok::semi); |
| 1970 | return; |
| 1971 | } |
| 1972 | |
| 1973 | // TODO: recover from mistakenly-qualified operator declarations. |
| 1974 | if (ExpectAndConsume(tok::semi, |
| 1975 | diag::err_expected_semi_after, |
| 1976 | "access declaration", |
| 1977 | tok::semi)) |
| 1978 | return; |
| 1979 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1980 | Actions.ActOnUsingDeclaration(getCurScope(), AS, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 1981 | /* HasUsingKeyword */ false, |
| 1982 | SourceLocation(), |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1983 | SS, Name, |
| 1984 | /* AttrList */ 0, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 1985 | /* HasTypenameKeyword */ false, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1986 | SourceLocation()); |
| 1987 | return; |
| 1988 | } |
| 1989 | } |
| 1990 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1991 | // static_assert-declaration |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1992 | if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1993 | // FIXME: Check for templates |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1994 | SourceLocation DeclEnd; |
| 1995 | ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1996 | return; |
| 1997 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1999 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2001 | "Nested template improperly parsed?"); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 2002 | SourceLocation DeclEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2004 | AS, AccessAttrs); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2005 | return; |
| 2006 | } |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 2007 | |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2008 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 2009 | if (Tok.is(tok::kw___extension__)) { |
| 2010 | // __extension__ silences extension warnings in the subexpression. |
| 2011 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 2012 | ConsumeToken(); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2013 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs, |
| 2014 | TemplateInfo, TemplateDiags); |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 2015 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2016 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 2017 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it |
| 2018 | // is a bitfield. |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2019 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2020 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2021 | ParsedAttributesWithRange attrs(AttrFactory); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2022 | ParsedAttributesWithRange FnAttrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2023 | // Optional C++11 attribute-specifier |
| 2024 | MaybeParseCXX11Attributes(attrs); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2025 | // We need to keep these attributes for future diagnostic |
| 2026 | // before they are taken over by declaration specifier. |
| 2027 | FnAttrs.addAll(attrs.getList()); |
| 2028 | FnAttrs.Range = attrs.Range; |
| 2029 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2030 | MaybeParseMicrosoftAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2032 | if (Tok.is(tok::kw_using)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2033 | ProhibitAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2035 | // Eat 'using'. |
| 2036 | SourceLocation UsingLoc = ConsumeToken(); |
| 2037 | |
| 2038 | if (Tok.is(tok::kw_namespace)) { |
| 2039 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
| 2040 | SkipUntil(tok::semi, true, true); |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2041 | } else { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2042 | SourceLocation DeclEnd; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2043 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 2044 | ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo, |
| 2045 | UsingLoc, DeclEnd, AS); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2046 | } |
| 2047 | return; |
| 2048 | } |
| 2049 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2050 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2051 | LateParsedAttrList CommonLateParsedAttrs; |
| 2052 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2053 | // decl-specifier-seq: |
| 2054 | // Parse the common declaration-specifiers piece. |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2055 | ParsingDeclSpec DS(*this, TemplateDiags); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2056 | DS.takeAttributesFrom(attrs); |
Richard Smith | 83a22ec | 2012-05-09 08:23:23 +0000 | [diff] [blame] | 2057 | if (MalformedTypeSpec) |
| 2058 | DS.SetTypeSpecError(); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2059 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class, |
| 2060 | &CommonLateParsedAttrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2061 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2062 | MultiTemplateParamsArg TemplateParams( |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 2063 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, |
| 2064 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 2065 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2066 | if (Tok.is(tok::semi)) { |
| 2067 | ConsumeToken(); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2068 | |
| 2069 | if (DS.isFriendSpecified()) |
| 2070 | ProhibitAttributes(FnAttrs); |
| 2071 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2072 | Decl *TheDecl = |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 2073 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams); |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 2074 | DS.complete(TheDecl); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2075 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2076 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2077 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2078 | ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2079 | VirtSpecifiers VS; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2080 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2081 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 2082 | LateParsedAttrList LateParsedAttrs; |
| 2083 | |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2084 | SourceLocation EqualLoc; |
| 2085 | bool HasInitializer = false; |
| 2086 | ExprResult Init; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2087 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2088 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2089 | ColonProtectionRAIIObject X(*this); |
| 2090 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2091 | // Parse the first declarator. |
| 2092 | ParseDeclarator(DeclaratorInfo); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2093 | // Error parsing the declarator? |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 2094 | if (!DeclaratorInfo.hasName()) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2095 | // If so, skip until the semi-colon or a }. |
Sebastian Redl | d941fa4 | 2011-04-24 16:27:48 +0000 | [diff] [blame] | 2096 | SkipUntil(tok::r_brace, true, true); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2097 | if (Tok.is(tok::semi)) |
| 2098 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2099 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2102 | ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2103 | |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 2104 | // If attributes exist after the declarator, but before an '{', parse them. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2105 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 2106 | |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2107 | // MSVC permits pure specifier on inline functions declared at class scope. |
| 2108 | // Hence check for =0 before checking for function definition. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2109 | if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) && |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2110 | DeclaratorInfo.isFunctionDeclarator() && |
| 2111 | NextToken().is(tok::numeric_constant)) { |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2112 | EqualLoc = ConsumeToken(); |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2113 | Init = ParseInitializer(); |
| 2114 | if (Init.isInvalid()) |
| 2115 | SkipUntil(tok::comma, true, true); |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2116 | else |
| 2117 | HasInitializer = true; |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2120 | FunctionDefinitionKind DefinitionKind = FDK_Declaration; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2121 | // function-definition: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2122 | // |
| 2123 | // In C++11, a non-function declarator followed by an open brace is a |
| 2124 | // braced-init-list for an in-class member initialization, not an |
| 2125 | // erroneous function definition. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2126 | if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2127 | DefinitionKind = FDK_Definition; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2128 | } else if (DeclaratorInfo.isFunctionDeclarator()) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2129 | if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2130 | DefinitionKind = FDK_Definition; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2131 | } else if (Tok.is(tok::equal)) { |
| 2132 | const Token &KW = NextToken(); |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2133 | if (KW.is(tok::kw_default)) |
| 2134 | DefinitionKind = FDK_Defaulted; |
| 2135 | else if (KW.is(tok::kw_delete)) |
| 2136 | DefinitionKind = FDK_Deleted; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2140 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
| 2141 | // to a friend declaration, that declaration shall be a definition. |
| 2142 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 2143 | DefinitionKind != FDK_Definition && DS.isFriendSpecified()) { |
| 2144 | // Diagnose attributes that appear before decl specifier: |
| 2145 | // [[]] friend int foo(); |
| 2146 | ProhibitAttributes(FnAttrs); |
| 2147 | } |
| 2148 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2149 | if (DefinitionKind) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2150 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2151 | Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2152 | ConsumeBrace(); |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2153 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2154 | |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2155 | // Consume the optional ';' |
| 2156 | if (Tok.is(tok::semi)) |
| 2157 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2158 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2159 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2160 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2161 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 2162 | Diag(DeclaratorInfo.getIdentifierLoc(), |
| 2163 | diag::err_function_declared_typedef); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2164 | |
Richard Smith | 6f9a445 | 2012-11-15 22:54:20 +0000 | [diff] [blame] | 2165 | // Recover by treating the 'typedef' as spurious. |
| 2166 | DS.ClearStorageClassSpecs(); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2169 | Decl *FunDecl = |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2170 | ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo, |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 2171 | VS, DefinitionKind, Init); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2172 | |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2173 | if (FunDecl) { |
| 2174 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) { |
| 2175 | CommonLateParsedAttrs[i]->addDecl(FunDecl); |
| 2176 | } |
| 2177 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) { |
| 2178 | LateParsedAttrs[i]->addDecl(FunDecl); |
| 2179 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2180 | } |
| 2181 | LateParsedAttrs.clear(); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2182 | |
| 2183 | // Consume the ';' - it's optional unless we have a delete or default |
Richard Trieu | 4b0e6f1 | 2012-05-16 19:04:59 +0000 | [diff] [blame] | 2184 | if (Tok.is(tok::semi)) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 2185 | ConsumeExtraSemi(AfterMemberFunctionDefinition); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 2186 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2187 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2188 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | // member-declarator-list: |
| 2192 | // member-declarator |
| 2193 | // member-declarator-list ',' member-declarator |
| 2194 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2195 | SmallVector<Decl *, 8> DeclsInGroup; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2196 | ExprResult BitfieldSize; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2197 | bool ExpectSemi = true; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2198 | |
| 2199 | while (1) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2200 | // member-declarator: |
| 2201 | // declarator pure-specifier[opt] |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2202 | // declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2203 | // identifier[opt] ':' constant-expression |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2204 | if (Tok.is(tok::colon)) { |
| 2205 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2206 | BitfieldSize = ParseConstantExpression(); |
| 2207 | if (BitfieldSize.isInvalid()) |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2208 | SkipUntil(tok::comma, true, true); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 2211 | // If a simple-asm-expr is present, parse it. |
| 2212 | if (Tok.is(tok::kw_asm)) { |
| 2213 | SourceLocation Loc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2214 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 2215 | if (AsmLabel.isInvalid()) |
| 2216 | SkipUntil(tok::comma, true, true); |
| 2217 | |
| 2218 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
| 2219 | DeclaratorInfo.SetRangeEnd(Loc); |
| 2220 | } |
| 2221 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2222 | // If attributes exist after the declarator, parse them. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2223 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2224 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2225 | // FIXME: When g++ adds support for this, we'll need to check whether it |
| 2226 | // goes before or after the GNU attributes and __asm__. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2227 | ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2228 | |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2229 | InClassInitStyle HasInClassInit = ICIS_NoInit; |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2230 | if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2231 | if (BitfieldSize.get()) { |
| 2232 | Diag(Tok, diag::err_bitfield_member_init); |
| 2233 | SkipUntil(tok::comma, true, true); |
| 2234 | } else { |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2235 | HasInitializer = true; |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2236 | if (!DeclaratorInfo.isDeclarationOfFunction() && |
| 2237 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2238 | != DeclSpec::SCS_typedef) |
| 2239 | HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2243 | // NOTE: If Sema is the Action module and declarator is an instance field, |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2244 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2245 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2246 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 2247 | NamedDecl *ThisDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2248 | if (DS.isFriendSpecified()) { |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2249 | // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains |
| 2250 | // to a friend declaration, that declaration shall be a definition. |
| 2251 | // |
| 2252 | // Diagnose attributes appear after friend member function declarator: |
| 2253 | // foo [[]] (); |
| 2254 | SmallVector<SourceRange, 4> Ranges; |
| 2255 | DeclaratorInfo.getCXX11AttributeRanges(Ranges); |
| 2256 | if (!Ranges.empty()) { |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 2257 | for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(), |
Michael Han | 52b501c | 2012-11-28 23:17:40 +0000 | [diff] [blame] | 2258 | E = Ranges.end(); I != E; ++I) { |
| 2259 | Diag((*I).getBegin(), diag::err_attributes_not_allowed) |
| 2260 | << *I; |
| 2261 | } |
| 2262 | } |
| 2263 | |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 2264 | // TODO: handle initializers, bitfields, 'delete' |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2265 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2266 | TemplateParams); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2267 | } else { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2268 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2269 | DeclaratorInfo, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2270 | TemplateParams, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2271 | BitfieldSize.release(), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2272 | VS, HasInClassInit); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2273 | |
| 2274 | if (VarTemplateDecl *VT = |
| 2275 | ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0) |
| 2276 | // Re-direct this decl to refer to the templated decl so that we can |
| 2277 | // initialize it. |
| 2278 | ThisDecl = VT->getTemplatedDecl(); |
| 2279 | |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2280 | if (ThisDecl && AccessAttrs) |
Richard Smith | 4a97b8e | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 2281 | Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2282 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2283 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2284 | // Handle the initializer. |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2285 | if (HasInClassInit != ICIS_NoInit && |
| 2286 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2287 | DeclSpec::SCS_static) { |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2288 | // The initializer was deferred; parse it and cache the tokens. |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2289 | Diag(Tok, getLangOpts().CPlusPlus11 |
| 2290 | ? diag::warn_cxx98_compat_nonstatic_member_init |
| 2291 | : diag::ext_nonstatic_member_init); |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2292 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2293 | if (DeclaratorInfo.isArrayOfUnknownBound()) { |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2294 | // C++11 [dcl.array]p3: An array bound may also be omitted when the |
| 2295 | // declarator is followed by an initializer. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2296 | // |
| 2297 | // A brace-or-equal-initializer for a member-declarator is not an |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2298 | // initializer in the grammar, so this is ill-formed. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2299 | Diag(Tok, diag::err_incomplete_array_member_init); |
| 2300 | SkipUntil(tok::comma, true, true); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2301 | |
| 2302 | // Avoid later warnings about a class member of incomplete type. |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2303 | if (ThisDecl) |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2304 | ThisDecl->setInvalidDecl(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2305 | } else |
| 2306 | ParseCXXNonStaticMemberInitializer(ThisDecl); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2307 | } else if (HasInitializer) { |
| 2308 | // Normal initializer. |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2309 | if (!Init.isUsable()) |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2310 | Init = ParseCXXMemberInitializer( |
| 2311 | ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc); |
| 2312 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2313 | if (Init.isInvalid()) |
| 2314 | SkipUntil(tok::comma, true, true); |
| 2315 | else if (ThisDecl) |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2316 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(), |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 2317 | DS.containsPlaceholderType()); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2318 | } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2319 | // No initializer. |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 2320 | Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType()); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2321 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2322 | if (ThisDecl) { |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2323 | if (!ThisDecl->isInvalidDecl()) { |
| 2324 | // Set the Decl for any late parsed attributes |
| 2325 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) |
| 2326 | CommonLateParsedAttrs[i]->addDecl(ThisDecl); |
| 2327 | |
| 2328 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) |
| 2329 | LateParsedAttrs[i]->addDecl(ThisDecl); |
| 2330 | } |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2331 | Actions.FinalizeDeclaration(ThisDecl); |
| 2332 | DeclsInGroup.push_back(ThisDecl); |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2333 | |
| 2334 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 2335 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() != |
| 2336 | DeclSpec::SCS_typedef) |
| 2337 | HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2338 | } |
David Majnemer | fcbe208 | 2013-08-01 04:22:55 +0000 | [diff] [blame] | 2339 | LateParsedAttrs.clear(); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2340 | |
| 2341 | DeclaratorInfo.complete(ThisDecl); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2342 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2343 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2344 | // or an error, bail out. |
| 2345 | if (Tok.isNot(tok::comma)) |
| 2346 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2347 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2348 | // Consume the comma. |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2349 | SourceLocation CommaLoc = ConsumeToken(); |
| 2350 | |
| 2351 | if (Tok.isAtStartOfLine() && |
| 2352 | !MightBeDeclarator(Declarator::MemberContext)) { |
| 2353 | // This comma was followed by a line-break and something which can't be |
| 2354 | // the start of a declarator. The comma was probably a typo for a |
| 2355 | // semicolon. |
| 2356 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2357 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2358 | ExpectSemi = false; |
| 2359 | break; |
| 2360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2362 | // Parse the next declarator. |
| 2363 | DeclaratorInfo.clear(); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2364 | VS.clear(); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2365 | BitfieldSize = true; |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2366 | Init = true; |
| 2367 | HasInitializer = false; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2368 | DeclaratorInfo.setCommaLoc(CommaLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2370 | // Attributes are only allowed on the second declarator. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2371 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2372 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2373 | if (Tok.isNot(tok::colon)) |
| 2374 | ParseDeclarator(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2377 | if (ExpectSemi && |
| 2378 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2379 | // Skip to end of block or statement. |
| 2380 | SkipUntil(tok::r_brace, true, true); |
| 2381 | // If we stopped at a ';', eat it. |
| 2382 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2383 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2386 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2389 | /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or |
| 2390 | /// pure-specifier. Also detect and reject any attempted defaulted/deleted |
| 2391 | /// function definition. The location of the '=', if any, will be placed in |
| 2392 | /// EqualLoc. |
| 2393 | /// |
| 2394 | /// pure-specifier: |
| 2395 | /// '= 0' |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2396 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2397 | /// brace-or-equal-initializer: |
| 2398 | /// '=' initializer-expression |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2399 | /// braced-init-list |
| 2400 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2401 | /// initializer-clause: |
| 2402 | /// assignment-expression |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2403 | /// braced-init-list |
| 2404 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2405 | /// defaulted/deleted function-definition: |
| 2406 | /// '=' 'default' |
| 2407 | /// '=' 'delete' |
| 2408 | /// |
| 2409 | /// Prior to C++0x, the assignment-expression in an initializer-clause must |
| 2410 | /// be a constant-expression. |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2411 | ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2412 | SourceLocation &EqualLoc) { |
| 2413 | assert((Tok.is(tok::equal) || Tok.is(tok::l_brace)) |
| 2414 | && "Data member initializer not starting with '=' or '{'"); |
| 2415 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2416 | EnterExpressionEvaluationContext Context(Actions, |
| 2417 | Sema::PotentiallyEvaluated, |
| 2418 | D); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2419 | if (Tok.is(tok::equal)) { |
| 2420 | EqualLoc = ConsumeToken(); |
| 2421 | if (Tok.is(tok::kw_delete)) { |
| 2422 | // In principle, an initializer of '= delete p;' is legal, but it will |
| 2423 | // never type-check. It's better to diagnose it as an ill-formed expression |
| 2424 | // than as an ill-formed deleted non-function member. |
| 2425 | // An initializer of '= delete p, foo' will never be parsed, because |
| 2426 | // a top-level comma always ends the initializer expression. |
| 2427 | const Token &Next = NextToken(); |
| 2428 | if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) || |
| 2429 | Next.is(tok::eof)) { |
| 2430 | if (IsFunction) |
| 2431 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2432 | << 1 /* delete */; |
| 2433 | else |
| 2434 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
| 2435 | return ExprResult(); |
| 2436 | } |
| 2437 | } else if (Tok.is(tok::kw_default)) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2438 | if (IsFunction) |
| 2439 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
| 2440 | << 0 /* default */; |
| 2441 | else |
| 2442 | Diag(ConsumeToken(), diag::err_default_special_members); |
| 2443 | return ExprResult(); |
| 2444 | } |
| 2445 | |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2446 | } |
| 2447 | return ParseInitializer(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2450 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 2451 | /// |
| 2452 | /// member-specification: |
| 2453 | /// member-declaration member-specification[opt] |
| 2454 | /// access-specifier ':' member-specification[opt] |
| 2455 | /// |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 2456 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 2457 | SourceLocation AttrFixitLoc, |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2458 | ParsedAttributesWithRange &Attrs, |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 2459 | unsigned TagType, Decl *TagDecl) { |
| 2460 | assert((TagType == DeclSpec::TST_struct || |
| 2461 | TagType == DeclSpec::TST_interface || |
| 2462 | TagType == DeclSpec::TST_union || |
| 2463 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
| 2464 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2465 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2466 | "parsing struct/union/class body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2467 | |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2468 | // Determine whether this is a non-nested class. Note that local |
| 2469 | // classes are *not* considered to be nested classes. |
| 2470 | bool NonNestedClass = true; |
| 2471 | if (!ClassStack.empty()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2472 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2473 | if (S->isClassScope()) { |
| 2474 | // We're inside a class scope, so this is a nested class. |
| 2475 | NonNestedClass = false; |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2476 | |
| 2477 | // The Microsoft extension __interface does not permit nested classes. |
| 2478 | if (getCurrentClass().IsInterface) { |
| 2479 | Diag(RecordLoc, diag::err_invalid_member_in_interface) |
| 2480 | << /*ErrorType=*/6 |
| 2481 | << (isa<NamedDecl>(TagDecl) |
| 2482 | ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString() |
| 2483 | : "<anonymous>"); |
| 2484 | } |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2485 | break; |
| 2486 | } |
| 2487 | |
| 2488 | if ((S->getFlags() & Scope::FnScope)) { |
| 2489 | // If we're in a function or function template declared in the |
| 2490 | // body of a class, then this is a local class rather than a |
| 2491 | // nested class. |
| 2492 | const Scope *Parent = S->getParent(); |
| 2493 | if (Parent->isTemplateParamScope()) |
| 2494 | Parent = Parent->getParent(); |
| 2495 | if (Parent->isClassScope()) |
| 2496 | break; |
| 2497 | } |
| 2498 | } |
| 2499 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2500 | |
| 2501 | // Enter a scope for the class. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2502 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2503 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2504 | // Note that we are parsing a new (potentially-nested) class definition. |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2505 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass, |
| 2506 | TagType == DeclSpec::TST_interface); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2508 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2509 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2510 | |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2511 | SourceLocation FinalLoc; |
| 2512 | |
| 2513 | // Parse the optional 'final' keyword. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2514 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2515 | assert(isCXX11FinalKeyword() && "not a class definition"); |
Richard Smith | 8b11b5e | 2011-10-15 04:21:46 +0000 | [diff] [blame] | 2516 | FinalLoc = ConsumeToken(); |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2517 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2518 | if (TagType == DeclSpec::TST_interface) { |
| 2519 | Diag(FinalLoc, diag::err_override_control_interface) |
| 2520 | << "final"; |
| 2521 | } else { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2522 | Diag(FinalLoc, getLangOpts().CPlusPlus11 ? |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2523 | diag::warn_cxx98_compat_override_control_keyword : |
| 2524 | diag::ext_override_control_keyword) << "final"; |
| 2525 | } |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2526 | |
Michael Han | 07fc1ba | 2013-01-07 16:57:11 +0000 | [diff] [blame] | 2527 | // Parse any C++11 attributes after 'final' keyword. |
| 2528 | // These attributes are not allowed to appear here, |
| 2529 | // and the only possible place for them to appertain |
| 2530 | // to the class would be between class-key and class-name. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 2531 | CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2532 | } |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 2533 | |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2534 | if (Tok.is(tok::colon)) { |
| 2535 | ParseBaseClause(TagDecl); |
| 2536 | |
| 2537 | if (!Tok.is(tok::l_brace)) { |
| 2538 | Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 2539 | |
| 2540 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2541 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | assert(Tok.is(tok::l_brace)); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2547 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2548 | T.consumeOpen(); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2549 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2550 | if (TagDecl) |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 2551 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2552 | T.getOpenLocation()); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 2553 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2554 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 2555 | // by default. Members of a class defined with the keywords struct or union |
| 2556 | // are public by default. |
| 2557 | AccessSpecifier CurAS; |
| 2558 | if (TagType == DeclSpec::TST_class) |
| 2559 | CurAS = AS_private; |
| 2560 | else |
| 2561 | CurAS = AS_public; |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2562 | ParsedAttributes AccessAttrs(AttrFactory); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2563 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2564 | if (TagDecl) { |
| 2565 | // While we still have something to read, read the member-declarations. |
| 2566 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 2567 | // Each iteration of this loop reads one member-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2569 | if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2570 | Tok.is(tok::kw___if_not_exists))) { |
| 2571 | ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS); |
| 2572 | continue; |
| 2573 | } |
| 2574 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2575 | // Check for extraneous top-level semicolon. |
| 2576 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 2577 | ConsumeExtraSemi(InsideStruct, TagType); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2578 | continue; |
| 2579 | } |
| 2580 | |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 2581 | if (Tok.is(tok::annot_pragma_vis)) { |
| 2582 | HandlePragmaVisibility(); |
| 2583 | continue; |
| 2584 | } |
| 2585 | |
| 2586 | if (Tok.is(tok::annot_pragma_pack)) { |
| 2587 | HandlePragmaPack(); |
| 2588 | continue; |
| 2589 | } |
| 2590 | |
Argyrios Kyrtzidis | f4deaef | 2012-10-12 17:39:59 +0000 | [diff] [blame] | 2591 | if (Tok.is(tok::annot_pragma_align)) { |
| 2592 | HandlePragmaAlign(); |
| 2593 | continue; |
| 2594 | } |
| 2595 | |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2596 | if (Tok.is(tok::annot_pragma_openmp)) { |
| 2597 | ParseOpenMPDeclarativeDirective(); |
| 2598 | continue; |
| 2599 | } |
| 2600 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2601 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 2602 | if (AS != AS_none) { |
| 2603 | // Current token is a C++ access specifier. |
| 2604 | CurAS = AS; |
| 2605 | SourceLocation ASLoc = Tok.getLocation(); |
David Blaikie | 13f8daf | 2011-10-13 06:08:43 +0000 | [diff] [blame] | 2606 | unsigned TokLength = Tok.getLength(); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2607 | ConsumeToken(); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2608 | AccessAttrs.clear(); |
| 2609 | MaybeParseGNUAttributes(AccessAttrs); |
| 2610 | |
David Blaikie | 13f8daf | 2011-10-13 06:08:43 +0000 | [diff] [blame] | 2611 | SourceLocation EndLoc; |
| 2612 | if (Tok.is(tok::colon)) { |
| 2613 | EndLoc = Tok.getLocation(); |
| 2614 | ConsumeToken(); |
| 2615 | } else if (Tok.is(tok::semi)) { |
| 2616 | EndLoc = Tok.getLocation(); |
| 2617 | ConsumeToken(); |
| 2618 | Diag(EndLoc, diag::err_expected_colon) |
| 2619 | << FixItHint::CreateReplacement(EndLoc, ":"); |
| 2620 | } else { |
| 2621 | EndLoc = ASLoc.getLocWithOffset(TokLength); |
| 2622 | Diag(EndLoc, diag::err_expected_colon) |
| 2623 | << FixItHint::CreateInsertion(EndLoc, ":"); |
| 2624 | } |
Erik Verbruggen | c35cba4 | 2011-10-17 09:54:52 +0000 | [diff] [blame] | 2625 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2626 | // The Microsoft extension __interface does not permit non-public |
| 2627 | // access specifiers. |
| 2628 | if (TagType == DeclSpec::TST_interface && CurAS != AS_public) { |
| 2629 | Diag(ASLoc, diag::err_access_specifier_interface) |
| 2630 | << (CurAS == AS_protected); |
| 2631 | } |
| 2632 | |
Erik Verbruggen | c35cba4 | 2011-10-17 09:54:52 +0000 | [diff] [blame] | 2633 | if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc, |
| 2634 | AccessAttrs.getList())) { |
| 2635 | // found another attribute than only annotations |
| 2636 | AccessAttrs.clear(); |
| 2637 | } |
| 2638 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2639 | continue; |
| 2640 | } |
| 2641 | |
| 2642 | // FIXME: Make sure we don't have a template here. |
| 2643 | |
| 2644 | // Parse all the comma separated declarators. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2645 | ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2648 | T.consumeClose(); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2649 | } else { |
| 2650 | SkipUntil(tok::r_brace, false, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2653 | // If attributes exist after class contents, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2654 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2655 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2656 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2657 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2658 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2659 | T.getOpenLocation(), |
| 2660 | T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2661 | attrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2663 | // C++11 [class.mem]p2: |
| 2664 | // Within the class member-specification, the class is regarded as complete |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2665 | // within function bodies, default arguments, and |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2666 | // brace-or-equal-initializers for non-static data members (including such |
| 2667 | // things in nested classes). |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2668 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2669 | // We are not inside a nested class. This class and its nested classes |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2670 | // are complete and we can parse the delayed portions of method |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2671 | // declarations and the lexed inline method definitions, along with any |
| 2672 | // delayed attributes. |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 2673 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2674 | ParseLexedAttributes(getCurrentClass()); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2675 | ParseLexedMethodDeclarations(getCurrentClass()); |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 2676 | |
| 2677 | // We've finished with all pending member declarations. |
| 2678 | Actions.ActOnFinishCXXMemberDecls(); |
| 2679 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2680 | ParseLexedMemberInitializers(getCurrentClass()); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2681 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 2682 | PrevTokLocation = SavedPrevTokLocation; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2685 | if (TagDecl) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2686 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2687 | T.getCloseLocation()); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 2688 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2689 | // Leave the class scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2690 | ParsingDef.Pop(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 2691 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2692 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2693 | |
| 2694 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 2695 | /// which explicitly initializes the members or base classes of a |
| 2696 | /// class (C++ [class.base.init]). For example, the three initializers |
| 2697 | /// after the ':' in the Derived constructor below: |
| 2698 | /// |
| 2699 | /// @code |
| 2700 | /// class Base { }; |
| 2701 | /// class Derived : Base { |
| 2702 | /// int x; |
| 2703 | /// float f; |
| 2704 | /// public: |
| 2705 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 2706 | /// }; |
| 2707 | /// @endcode |
| 2708 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | /// [C++] ctor-initializer: |
| 2710 | /// ':' mem-initializer-list |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2711 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | /// [C++] mem-initializer-list: |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2713 | /// mem-initializer ...[opt] |
| 2714 | /// mem-initializer ...[opt] , mem-initializer-list |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2715 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2716 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 2717 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2718 | // Poison the SEH identifiers so they are flagged as illegal in constructor initializers |
| 2719 | PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2720 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2722 | SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2723 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2724 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2725 | do { |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 2726 | if (Tok.is(tok::code_completion)) { |
Dmitri Gribenko | 572cf58 | 2013-06-23 22:58:02 +0000 | [diff] [blame] | 2727 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 2728 | MemInitializers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2729 | return cutOffParsing(); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 2730 | } else { |
| 2731 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 2732 | if (!MemInit.isInvalid()) |
| 2733 | MemInitializers.push_back(MemInit.get()); |
| 2734 | else |
| 2735 | AnyErrors = true; |
| 2736 | } |
| 2737 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2738 | if (Tok.is(tok::comma)) |
| 2739 | ConsumeToken(); |
| 2740 | else if (Tok.is(tok::l_brace)) |
| 2741 | break; |
Douglas Gregor | b1f6fa4 | 2010-09-07 14:35:10 +0000 | [diff] [blame] | 2742 | // If the next token looks like a base or member initializer, assume that |
| 2743 | // we're just missing a comma. |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 2744 | else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { |
| 2745 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 2746 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 2747 | << FixItHint::CreateInsertion(Loc, ", "); |
| 2748 | } else { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2749 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2750 | Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2751 | SkipUntil(tok::l_brace, true, true); |
| 2752 | break; |
| 2753 | } |
| 2754 | } while (true); |
| 2755 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 2756 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2757 | AnyErrors); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 2761 | /// part of a constructor initializer that explicitly initializes one |
| 2762 | /// member or base class (C++ [class.base.init]). See |
| 2763 | /// ParseConstructorInitializer for an example. |
| 2764 | /// |
| 2765 | /// [C++] mem-initializer: |
| 2766 | /// mem-initializer-id '(' expression-list[opt] ')' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2767 | /// [C++0x] mem-initializer-id braced-init-list |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | /// |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2769 | /// [C++] mem-initializer-id: |
| 2770 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 2771 | /// identifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2772 | Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2773 | // parse '::'[opt] nested-name-specifier[opt] |
| 2774 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2775 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2776 | ParsedType TemplateTypeTy; |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2777 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2778 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 2779 | if (TemplateId->Kind == TNK_Type_template || |
| 2780 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2781 | AnnotateTemplateIdTokenAsType(); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2782 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2783 | TemplateTypeTy = getTypeAnnotation(Tok); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2784 | } |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2785 | } |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2786 | // Uses of decltype will already have been converted to annot_decltype by |
| 2787 | // ParseOptionalCXXScopeSpecifier at this point. |
| 2788 | if (!TemplateTypeTy && Tok.isNot(tok::identifier) |
| 2789 | && Tok.isNot(tok::annot_decltype)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2790 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2791 | return true; |
| 2792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2794 | IdentifierInfo *II = 0; |
| 2795 | DeclSpec DS(AttrFactory); |
| 2796 | SourceLocation IdLoc = Tok.getLocation(); |
| 2797 | if (Tok.is(tok::annot_decltype)) { |
| 2798 | // Get the decltype expression, if there is one. |
| 2799 | ParseDecltypeSpecifier(DS); |
| 2800 | } else { |
| 2801 | if (Tok.is(tok::identifier)) |
| 2802 | // Get the identifier. This may be a member name or a class name, |
| 2803 | // but we'll let the semantic analysis determine which it is. |
| 2804 | II = Tok.getIdentifierInfo(); |
| 2805 | ConsumeToken(); |
| 2806 | } |
| 2807 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2808 | |
| 2809 | // Parse the '('. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2810 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2811 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2812 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2813 | ExprResult InitList = ParseBraceInitializer(); |
| 2814 | if (InitList.isInvalid()) |
| 2815 | return true; |
| 2816 | |
| 2817 | SourceLocation EllipsisLoc; |
| 2818 | if (Tok.is(tok::ellipsis)) |
| 2819 | EllipsisLoc = ConsumeToken(); |
| 2820 | |
| 2821 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2822 | TemplateTypeTy, DS, IdLoc, |
| 2823 | InitList.take(), EllipsisLoc); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2824 | } else if(Tok.is(tok::l_paren)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2825 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2826 | T.consumeOpen(); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2827 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2828 | // Parse the optional expression-list. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2829 | ExprVector ArgExprs; |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2830 | CommaLocsTy CommaLocs; |
| 2831 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 2832 | SkipUntil(tok::r_paren); |
| 2833 | return true; |
| 2834 | } |
| 2835 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2836 | T.consumeClose(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2837 | |
| 2838 | SourceLocation EllipsisLoc; |
| 2839 | if (Tok.is(tok::ellipsis)) |
| 2840 | EllipsisLoc = ConsumeToken(); |
| 2841 | |
| 2842 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2843 | TemplateTypeTy, DS, IdLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2844 | T.getOpenLocation(), ArgExprs, |
| 2845 | T.getCloseLocation(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2848 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2849 | : diag::err_expected_lparen); |
| 2850 | return true; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2851 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2852 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2853 | /// \brief Parse a C++ exception-specification if present (C++0x [except.spec]). |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2854 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2855 | /// exception-specification: |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2856 | /// dynamic-exception-specification |
| 2857 | /// noexcept-specification |
| 2858 | /// |
| 2859 | /// noexcept-specification: |
| 2860 | /// 'noexcept' |
| 2861 | /// 'noexcept' '(' constant-expression ')' |
| 2862 | ExceptionSpecificationType |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2863 | Parser::tryParseExceptionSpecification( |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2864 | SourceRange &SpecificationRange, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2865 | SmallVectorImpl<ParsedType> &DynamicExceptions, |
| 2866 | SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 2867 | ExprResult &NoexceptExpr) { |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2868 | ExceptionSpecificationType Result = EST_None; |
| 2869 | |
| 2870 | // See if there's a dynamic specification. |
| 2871 | if (Tok.is(tok::kw_throw)) { |
| 2872 | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
| 2873 | DynamicExceptions, |
| 2874 | DynamicExceptionRanges); |
| 2875 | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
| 2876 | "Produced different number of exception types and ranges."); |
| 2877 | } |
| 2878 | |
| 2879 | // If there's no noexcept specification, we're done. |
| 2880 | if (Tok.isNot(tok::kw_noexcept)) |
| 2881 | return Result; |
| 2882 | |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 2883 | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
| 2884 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2885 | // If we already had a dynamic specification, parse the noexcept for, |
| 2886 | // recovery, but emit a diagnostic and don't store the results. |
| 2887 | SourceRange NoexceptRange; |
| 2888 | ExceptionSpecificationType NoexceptType = EST_None; |
| 2889 | |
| 2890 | SourceLocation KeywordLoc = ConsumeToken(); |
| 2891 | if (Tok.is(tok::l_paren)) { |
| 2892 | // There is an argument. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2893 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2894 | T.consumeOpen(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2895 | NoexceptType = EST_ComputedNoexcept; |
| 2896 | NoexceptExpr = ParseConstantExpression(); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2897 | // The argument must be contextually convertible to bool. We use |
| 2898 | // ActOnBooleanCondition for this purpose. |
| 2899 | if (!NoexceptExpr.isInvalid()) |
| 2900 | NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc, |
| 2901 | NoexceptExpr.get()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2902 | T.consumeClose(); |
| 2903 | NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation()); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2904 | } else { |
| 2905 | // There is no argument. |
| 2906 | NoexceptType = EST_BasicNoexcept; |
| 2907 | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
| 2908 | } |
| 2909 | |
| 2910 | if (Result == EST_None) { |
| 2911 | SpecificationRange = NoexceptRange; |
| 2912 | Result = NoexceptType; |
| 2913 | |
| 2914 | // If there's a dynamic specification after a noexcept specification, |
| 2915 | // parse that and ignore the results. |
| 2916 | if (Tok.is(tok::kw_throw)) { |
| 2917 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2918 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
| 2919 | DynamicExceptionRanges); |
| 2920 | } |
| 2921 | } else { |
| 2922 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2923 | } |
| 2924 | |
| 2925 | return Result; |
| 2926 | } |
| 2927 | |
Richard Smith | 79f4bb7 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 2928 | static void diagnoseDynamicExceptionSpecification( |
| 2929 | Parser &P, const SourceRange &Range, bool IsNoexcept) { |
| 2930 | if (P.getLangOpts().CPlusPlus11) { |
| 2931 | const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)"; |
| 2932 | P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range; |
| 2933 | P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated) |
| 2934 | << Replacement << FixItHint::CreateReplacement(Range, Replacement); |
| 2935 | } |
| 2936 | } |
| 2937 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2938 | /// ParseDynamicExceptionSpecification - Parse a C++ |
| 2939 | /// dynamic-exception-specification (C++ [except.spec]). |
| 2940 | /// |
| 2941 | /// dynamic-exception-specification: |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2942 | /// 'throw' '(' type-id-list [opt] ')' |
| 2943 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2945 | /// type-id-list: |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2946 | /// type-id ... [opt] |
| 2947 | /// type-id-list ',' type-id ... [opt] |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2948 | /// |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2949 | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
| 2950 | SourceRange &SpecificationRange, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2951 | SmallVectorImpl<ParsedType> &Exceptions, |
| 2952 | SmallVectorImpl<SourceRange> &Ranges) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2953 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2955 | SpecificationRange.setBegin(ConsumeToken()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2956 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2957 | if (T.consumeOpen()) { |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2958 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 2959 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2960 | return EST_DynamicNone; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2961 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2962 | |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2963 | // Parse throw(...), a Microsoft extension that means "this function |
| 2964 | // can throw anything". |
| 2965 | if (Tok.is(tok::ellipsis)) { |
| 2966 | SourceLocation EllipsisLoc = ConsumeToken(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2967 | if (!getLangOpts().MicrosoftExt) |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2968 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2969 | T.consumeClose(); |
| 2970 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 79f4bb7 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 2971 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2972 | return EST_MSAny; |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2975 | // Parse the sequence of type-ids. |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2976 | SourceRange Range; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2977 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2978 | TypeResult Res(ParseTypeName(&Range)); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2979 | |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2980 | if (Tok.is(tok::ellipsis)) { |
| 2981 | // C++0x [temp.variadic]p5: |
| 2982 | // - In a dynamic-exception-specification (15.4); the pattern is a |
| 2983 | // type-id. |
| 2984 | SourceLocation Ellipsis = ConsumeToken(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2985 | Range.setEnd(Ellipsis); |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2986 | if (!Res.isInvalid()) |
| 2987 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
| 2988 | } |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2989 | |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2990 | if (!Res.isInvalid()) { |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2991 | Exceptions.push_back(Res.get()); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2992 | Ranges.push_back(Range); |
| 2993 | } |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2994 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2995 | if (Tok.is(tok::comma)) |
| 2996 | ConsumeToken(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2997 | else |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2998 | break; |
| 2999 | } |
| 3000 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3001 | T.consumeClose(); |
| 3002 | SpecificationRange.setEnd(T.getCloseLocation()); |
Richard Smith | 79f4bb7 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 3003 | diagnoseDynamicExceptionSpecification(*this, SpecificationRange, |
| 3004 | Exceptions.empty()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3005 | return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3006 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3007 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3008 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 3009 | /// function declaration. |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 3010 | TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) { |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3011 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 3012 | |
| 3013 | ConsumeToken(); |
| 3014 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3015 | return ParseTypeName(&Range, Declarator::TrailingReturnContext); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3018 | /// \brief We have just started parsing the definition of a new class, |
| 3019 | /// so push that class onto our stack of classes that is currently |
| 3020 | /// being parsed. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3021 | Sema::ParsingClassState |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3022 | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass, |
| 3023 | bool IsInterface) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 3024 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3025 | "Nested class without outer class"); |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 3026 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface)); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3027 | return Actions.PushParsingClass(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | /// \brief Deallocate the given parsed class and all of its nested |
| 3031 | /// classes. |
| 3032 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3033 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 3034 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3035 | delete Class; |
| 3036 | } |
| 3037 | |
| 3038 | /// \brief Pop the top class of the stack of classes that are |
| 3039 | /// currently being parsed. |
| 3040 | /// |
| 3041 | /// This routine should be called when we have finished parsing the |
| 3042 | /// definition of a class, but have not yet popped the Scope |
| 3043 | /// associated with the class's definition. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3044 | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3045 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3047 | Actions.PopParsingClass(state); |
| 3048 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3049 | ParsingClass *Victim = ClassStack.top(); |
| 3050 | ClassStack.pop(); |
| 3051 | if (Victim->TopLevelClass) { |
| 3052 | // Deallocate all of the nested classes of this class, |
| 3053 | // recursively: we don't need to keep any of this information. |
| 3054 | DeallocateParsedClasses(Victim); |
| 3055 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3057 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 3058 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3059 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3060 | // The victim is a nested class, but we will not need to perform |
| 3061 | // any processing after the definition of this class since it has |
| 3062 | // no members whose handling was delayed. Therefore, we can just |
| 3063 | // remove this nested class. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3064 | DeallocateParsedClasses(Victim); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3065 | return; |
| 3066 | } |
| 3067 | |
| 3068 | // This nested class has some members that will need to be processed |
| 3069 | // after the top-level class is completely defined. Therefore, add |
| 3070 | // it to the list of nested classes within its parent. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3071 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 3072 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3073 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3074 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3075 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3076 | /// \brief Try to parse an 'identifier' which appears within an attribute-token. |
| 3077 | /// |
| 3078 | /// \return the parsed identifier on success, and 0 if the next token is not an |
| 3079 | /// attribute-token. |
| 3080 | /// |
| 3081 | /// C++11 [dcl.attr.grammar]p3: |
| 3082 | /// If a keyword or an alternative token that satisfies the syntactic |
| 3083 | /// requirements of an identifier is contained in an attribute-token, |
| 3084 | /// it is considered an identifier. |
| 3085 | IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) { |
| 3086 | switch (Tok.getKind()) { |
| 3087 | default: |
| 3088 | // Identifiers and keywords have identifier info attached. |
| 3089 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 3090 | Loc = ConsumeToken(); |
| 3091 | return II; |
| 3092 | } |
| 3093 | return 0; |
| 3094 | |
| 3095 | case tok::ampamp: // 'and' |
| 3096 | case tok::pipe: // 'bitor' |
| 3097 | case tok::pipepipe: // 'or' |
| 3098 | case tok::caret: // 'xor' |
| 3099 | case tok::tilde: // 'compl' |
| 3100 | case tok::amp: // 'bitand' |
| 3101 | case tok::ampequal: // 'and_eq' |
| 3102 | case tok::pipeequal: // 'or_eq' |
| 3103 | case tok::caretequal: // 'xor_eq' |
| 3104 | case tok::exclaim: // 'not' |
| 3105 | case tok::exclaimequal: // 'not_eq' |
| 3106 | // Alternative tokens do not have identifier info, but their spelling |
| 3107 | // starts with an alphabetical character. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3108 | SmallString<8> SpellingBuf; |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3109 | StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf); |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 3110 | if (isLetter(Spelling[0])) { |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3111 | Loc = ConsumeToken(); |
Benjamin Kramer | 0eb7526 | 2012-04-22 20:43:30 +0000 | [diff] [blame] | 3112 | return &PP.getIdentifierTable().get(Spelling); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3113 | } |
| 3114 | return 0; |
| 3115 | } |
| 3116 | } |
| 3117 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3118 | static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName, |
| 3119 | IdentifierInfo *ScopeName) { |
| 3120 | switch (AttributeList::getKind(AttrName, ScopeName, |
| 3121 | AttributeList::AS_CXX11)) { |
| 3122 | case AttributeList::AT_CarriesDependency: |
| 3123 | case AttributeList::AT_FallThrough: |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3124 | case AttributeList::AT_CXX11NoReturn: { |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3125 | return true; |
| 3126 | } |
| 3127 | |
| 3128 | default: |
| 3129 | return false; |
| 3130 | } |
| 3131 | } |
| 3132 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3133 | /// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3134 | /// only parses standard attributes. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3135 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3136 | /// [C++11] attribute-specifier: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3137 | /// '[' '[' attribute-list ']' ']' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3138 | /// alignment-specifier |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3139 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3140 | /// [C++11] attribute-list: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3141 | /// attribute[opt] |
| 3142 | /// attribute-list ',' attribute[opt] |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3143 | /// attribute '...' |
| 3144 | /// attribute-list ',' attribute '...' |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3145 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3146 | /// [C++11] attribute: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3147 | /// attribute-token attribute-argument-clause[opt] |
| 3148 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3149 | /// [C++11] attribute-token: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3150 | /// identifier |
| 3151 | /// attribute-scoped-token |
| 3152 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3153 | /// [C++11] attribute-scoped-token: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3154 | /// attribute-namespace '::' identifier |
| 3155 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3156 | /// [C++11] attribute-namespace: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3157 | /// identifier |
| 3158 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3159 | /// [C++11] attribute-argument-clause: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3160 | /// '(' balanced-token-seq ')' |
| 3161 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3162 | /// [C++11] balanced-token-seq: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3163 | /// balanced-token |
| 3164 | /// balanced-token-seq balanced-token |
| 3165 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3166 | /// [C++11] balanced-token: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3167 | /// '(' balanced-token-seq ')' |
| 3168 | /// '[' balanced-token-seq ']' |
| 3169 | /// '{' balanced-token-seq '}' |
| 3170 | /// any token but '(', ')', '[', ']', '{', or '}' |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3171 | void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs, |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3172 | SourceLocation *endLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3173 | if (Tok.is(tok::kw_alignas)) { |
Richard Smith | 41be673 | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 3174 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3175 | ParseAlignmentSpecifier(attrs, endLoc); |
| 3176 | return; |
| 3177 | } |
| 3178 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3179 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3180 | && "Not a C++11 attribute list"); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3181 | |
Richard Smith | 41be673 | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 3182 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute); |
| 3183 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3184 | ConsumeBracket(); |
| 3185 | ConsumeBracket(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3186 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3187 | llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs; |
| 3188 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3189 | while (Tok.isNot(tok::r_square)) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3190 | // attribute not present |
| 3191 | if (Tok.is(tok::comma)) { |
| 3192 | ConsumeToken(); |
| 3193 | continue; |
| 3194 | } |
| 3195 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3196 | SourceLocation ScopeLoc, AttrLoc; |
| 3197 | IdentifierInfo *ScopeName = 0, *AttrName = 0; |
| 3198 | |
| 3199 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 3200 | if (!AttrName) |
| 3201 | // Break out to the "expected ']'" diagnostic. |
| 3202 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3203 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3204 | // scoped attribute |
| 3205 | if (Tok.is(tok::coloncolon)) { |
| 3206 | ConsumeToken(); |
| 3207 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3208 | ScopeName = AttrName; |
| 3209 | ScopeLoc = AttrLoc; |
| 3210 | |
| 3211 | AttrName = TryParseCXX11AttributeIdentifier(AttrLoc); |
| 3212 | if (!AttrName) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3213 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 3214 | SkipUntil(tok::r_square, tok::comma, true, true); |
| 3215 | continue; |
| 3216 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3219 | bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3220 | bool AttrParsed = false; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3221 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3222 | if (StandardAttr && |
| 3223 | !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second) |
| 3224 | Diag(AttrLoc, diag::err_cxx11_attribute_repeated) |
| 3225 | << AttrName << SourceRange(SeenAttrs[AttrName]); |
| 3226 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3227 | // Parse attribute arguments |
| 3228 | if (Tok.is(tok::l_paren)) { |
| 3229 | if (ScopeName && ScopeName->getName() == "gnu") { |
| 3230 | ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc, |
| 3231 | ScopeName, ScopeLoc, AttributeList::AS_CXX11); |
| 3232 | AttrParsed = true; |
| 3233 | } else { |
| 3234 | if (StandardAttr) |
| 3235 | Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments) |
| 3236 | << AttrName->getName(); |
| 3237 | |
| 3238 | // FIXME: handle other formats of c++11 attribute arguments |
| 3239 | ConsumeParen(); |
| 3240 | SkipUntil(tok::r_paren, false); |
| 3241 | } |
| 3242 | } |
| 3243 | |
| 3244 | if (!AttrParsed) |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 3245 | attrs.addNew(AttrName, |
| 3246 | SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, |
| 3247 | AttrLoc), |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 3248 | ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3249 | |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3250 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3251 | ConsumeToken(); |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 3252 | |
| 3253 | Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) |
| 3254 | << AttrName->getName(); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3255 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 3259 | SkipUntil(tok::r_square, false); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3260 | if (endLoc) |
| 3261 | *endLoc = Tok.getLocation(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3262 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 3263 | SkipUntil(tok::r_square, false); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3264 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3265 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3266 | /// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq. |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3267 | /// |
| 3268 | /// attribute-specifier-seq: |
| 3269 | /// attribute-specifier-seq[opt] attribute-specifier |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3270 | void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs, |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3271 | SourceLocation *endLoc) { |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 3272 | assert(getLangOpts().CPlusPlus11); |
| 3273 | |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3274 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
| 3275 | if (!endLoc) |
| 3276 | endLoc = &Loc; |
| 3277 | |
Douglas Gregor | 8828ee7 | 2011-10-07 20:35:25 +0000 | [diff] [blame] | 3278 | do { |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3279 | ParseCXX11AttributeSpecifier(attrs, endLoc); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3280 | } while (isCXX11AttributeSpecifier()); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 3281 | |
| 3282 | attrs.Range = SourceRange(StartLoc, *endLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3285 | /// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr] |
| 3286 | /// |
| 3287 | /// [MS] ms-attribute: |
| 3288 | /// '[' token-seq ']' |
| 3289 | /// |
| 3290 | /// [MS] ms-attribute-seq: |
| 3291 | /// ms-attribute[opt] |
| 3292 | /// ms-attribute ms-attribute-seq |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3293 | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
| 3294 | SourceLocation *endLoc) { |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3295 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 3296 | |
| 3297 | while (Tok.is(tok::l_square)) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3298 | // FIXME: If this is actually a C++11 attribute, parse it as one. |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3299 | ConsumeBracket(); |
| 3300 | SkipUntil(tok::r_square, true, true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3301 | if (endLoc) *endLoc = Tok.getLocation(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 3302 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); |
| 3303 | } |
| 3304 | } |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3305 | |
| 3306 | void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType, |
| 3307 | AccessSpecifier& CurAS) { |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3308 | IfExistsCondition Result; |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3309 | if (ParseMicrosoftIfExistsCondition(Result)) |
| 3310 | return; |
| 3311 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3312 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 3313 | if (Braces.consumeOpen()) { |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3314 | Diag(Tok, diag::err_expected_lbrace); |
| 3315 | return; |
| 3316 | } |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3317 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3318 | switch (Result.Behavior) { |
| 3319 | case IEB_Parse: |
| 3320 | // Parse the declarations below. |
| 3321 | break; |
| 3322 | |
| 3323 | case IEB_Dependent: |
| 3324 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
| 3325 | << Result.IsIfExists; |
| 3326 | // Fall through to skip. |
| 3327 | |
| 3328 | case IEB_Skip: |
| 3329 | Braces.skipToEnd(); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3330 | return; |
| 3331 | } |
| 3332 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3333 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3334 | // __if_exists, __if_not_exists can nest. |
| 3335 | if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) { |
| 3336 | ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS); |
| 3337 | continue; |
| 3338 | } |
| 3339 | |
| 3340 | // Check for extraneous top-level semicolon. |
| 3341 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3342 | ConsumeExtraSemi(InsideStruct, TagType); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3343 | continue; |
| 3344 | } |
| 3345 | |
| 3346 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 3347 | if (AS != AS_none) { |
| 3348 | // Current token is a C++ access specifier. |
| 3349 | CurAS = AS; |
| 3350 | SourceLocation ASLoc = Tok.getLocation(); |
| 3351 | ConsumeToken(); |
| 3352 | if (Tok.is(tok::colon)) |
| 3353 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation()); |
| 3354 | else |
| 3355 | Diag(Tok, diag::err_expected_colon); |
| 3356 | ConsumeToken(); |
| 3357 | continue; |
| 3358 | } |
| 3359 | |
| 3360 | // Parse all the comma separated declarators. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3361 | ParseCXXClassMemberDeclaration(CurAS, 0); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3362 | } |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 3363 | |
| 3364 | Braces.consumeClose(); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 3365 | } |