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 | |
Anders Carlsson | 0c6139d | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 14 | #include "clang/Basic/OperatorKinds.h" |
Douglas Gregor | 1b7f898 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
| 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 20 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 22 | #include "RAIIObjectsForParser.h" |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 26 | /// may either be a top level namespace or a block-level namespace alias. If |
| 27 | /// there was an inline keyword, it has already been parsed. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 28 | /// |
| 29 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 30 | /// named-namespace-definition |
| 31 | /// unnamed-namespace-definition |
| 32 | /// |
| 33 | /// unnamed-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 34 | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 35 | /// |
| 36 | /// named-namespace-definition: |
| 37 | /// original-namespace-definition |
| 38 | /// extension-namespace-definition |
| 39 | /// |
| 40 | /// original-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 41 | /// 'inline'[opt] 'namespace' identifier attributes[opt] |
| 42 | /// '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 43 | /// |
| 44 | /// extension-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 45 | /// 'inline'[opt] 'namespace' original-namespace-name |
| 46 | /// '{' namespace-body '}' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | /// |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 48 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 49 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 50 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 51 | Decl *Parser::ParseNamespace(unsigned Context, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 52 | SourceLocation &DeclEnd, |
| 53 | SourceLocation InlineLoc) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 54 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 55 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 56 | ObjCDeclContextSwitch ObjCDC(*this); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 58 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 59 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 60 | cutOffParsing(); |
| 61 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 62 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 64 | SourceLocation IdentLoc; |
| 65 | IdentifierInfo *Ident = 0; |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 66 | std::vector<SourceLocation> ExtraIdentLoc; |
| 67 | std::vector<IdentifierInfo*> ExtraIdent; |
| 68 | std::vector<SourceLocation> ExtraNamespaceLoc; |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 69 | |
| 70 | Token attrTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 72 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 73 | Ident = Tok.getIdentifierInfo(); |
| 74 | IdentLoc = ConsumeToken(); // eat the identifier. |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 75 | while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) { |
| 76 | ExtraNamespaceLoc.push_back(ConsumeToken()); |
| 77 | ExtraIdent.push_back(Tok.getIdentifierInfo()); |
| 78 | ExtraIdentLoc.push_back(ConsumeToken()); |
| 79 | } |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 80 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 82 | // Read label attributes, if present. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 83 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 84 | if (Tok.is(tok::kw___attribute)) { |
| 85 | attrTok = Tok; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 86 | ParseGNUAttributes(attrs); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 87 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 89 | if (Tok.is(tok::equal)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 90 | if (!attrs.empty()) |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 91 | Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 92 | if (InlineLoc.isValid()) |
| 93 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
| 94 | << FixItHint::CreateRemoval(InlineLoc); |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 95 | return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 96 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 99 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 100 | if (T.consumeOpen()) { |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 101 | if (!ExtraIdent.empty()) { |
| 102 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 103 | << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back()); |
| 104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 106 | diag::err_expected_ident_lbrace); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 107 | return 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 108 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 110 | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
| 111 | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
| 112 | getCurScope()->getFnParent()) { |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 113 | if (!ExtraIdent.empty()) { |
| 114 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 115 | << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back()); |
| 116 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 117 | Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope); |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 118 | SkipUntil(tok::r_brace, false); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 119 | return 0; |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 122 | if (!ExtraIdent.empty()) { |
| 123 | TentativeParsingAction TPA(*this); |
| 124 | SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true); |
| 125 | Token rBraceToken = Tok; |
| 126 | TPA.Revert(); |
| 127 | |
| 128 | if (!rBraceToken.is(tok::r_brace)) { |
| 129 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 130 | << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back()); |
| 131 | } else { |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 132 | std::string NamespaceFix; |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 133 | for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(), |
| 134 | E = ExtraIdent.end(); I != E; ++I) { |
| 135 | NamespaceFix += " { namespace "; |
| 136 | NamespaceFix += (*I)->getName(); |
| 137 | } |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 138 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 139 | std::string RBraces; |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 140 | for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i) |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 141 | RBraces += "} "; |
Benjamin Kramer | 9910df0 | 2011-05-26 21:32:30 +0000 | [diff] [blame] | 142 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 143 | Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon) |
| 144 | << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(), |
| 145 | ExtraIdentLoc.back()), |
| 146 | NamespaceFix) |
| 147 | << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces); |
| 148 | } |
| 149 | } |
| 150 | |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 151 | // 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] | 152 | if (InlineLoc.isValid()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 153 | Diag(InlineLoc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 154 | diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace); |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 156 | // Enter a scope for the namespace. |
| 157 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 158 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 159 | Decl *NamespcDecl = |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 160 | Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 161 | IdentLoc, Ident, T.getOpenLocation(), |
| 162 | attrs.getList()); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 163 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 164 | PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc, |
| 165 | "parsing namespace"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 167 | // Parse the contents of the namespace. This includes parsing recovery on |
| 168 | // any improperly nested namespaces. |
| 169 | ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 170 | InlineLoc, attrs, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 172 | // Leave the namespace scope. |
| 173 | NamespaceScope.Exit(); |
| 174 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 175 | DeclEnd = T.getCloseLocation(); |
| 176 | Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 177 | |
| 178 | return NamespcDecl; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 179 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 180 | |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 181 | /// ParseInnerNamespace - Parse the contents of a namespace. |
| 182 | void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc, |
| 183 | std::vector<IdentifierInfo*>& Ident, |
| 184 | std::vector<SourceLocation>& NamespaceLoc, |
| 185 | unsigned int index, SourceLocation& InlineLoc, |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 186 | ParsedAttributes& attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 187 | BalancedDelimiterTracker &Tracker) { |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 188 | if (index == Ident.size()) { |
| 189 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 190 | ParsedAttributesWithRange attrs(AttrFactory); |
| 191 | MaybeParseCXX0XAttributes(attrs); |
| 192 | MaybeParseMicrosoftAttributes(attrs); |
| 193 | ParseExternalDeclaration(attrs); |
| 194 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 195 | |
| 196 | // The caller is what called check -- we are simply calling |
| 197 | // the close for it. |
| 198 | Tracker.consumeClose(); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 199 | |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | // Parse improperly nested namespaces. |
| 204 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 205 | Decl *NamespcDecl = |
| 206 | Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(), |
| 207 | NamespaceLoc[index], IdentLoc[index], |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 208 | Ident[index], Tracker.getOpenLocation(), |
| 209 | attrs.getList()); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 210 | |
| 211 | ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 212 | attrs, Tracker); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 213 | |
| 214 | NamespaceScope.Exit(); |
| 215 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 216 | Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation()); |
Richard Trieu | f858bd8 | 2011-05-26 20:11:09 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 219 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 220 | /// alias definition. |
| 221 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 222 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 223 | SourceLocation AliasLoc, |
| 224 | IdentifierInfo *Alias, |
| 225 | SourceLocation &DeclEnd) { |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 226 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 228 | ConsumeToken(); // eat the '='. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 230 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 231 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 232 | cutOffParsing(); |
| 233 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 234 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 235 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 236 | CXXScopeSpec SS; |
| 237 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 238 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 239 | |
| 240 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
| 241 | Diag(Tok, diag::err_expected_namespace_name); |
| 242 | // Skip to end of the definition and eat the ';'. |
| 243 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 244 | return 0; |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Parse identifier. |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 248 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 249 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 251 | // Eat the ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 252 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 253 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, |
| 254 | "", tok::semi); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 256 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 257 | SS, IdentLoc, Ident); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 260 | /// ParseLinkage - We know that the current token is a string_literal |
| 261 | /// and just before that, that extern was seen. |
| 262 | /// |
| 263 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 264 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 265 | /// 'extern' string-literal declaration |
| 266 | /// |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 267 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) { |
Douglas Gregor | c19923d | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 268 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 269 | SmallString<8> LangBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 270 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 271 | StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 272 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 273 | return 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 274 | |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 275 | // FIXME: This is incorrect: linkage-specifiers are parsed in translation |
| 276 | // phase 7, so string-literal concatenation is supposed to occur. |
| 277 | // extern "" "C" "" "+" "+" { } is legal. |
| 278 | if (Tok.hasUDSuffix()) |
| 279 | Diag(Tok, diag::err_invalid_string_udl); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 280 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 282 | ParseScope LinkageScope(this, Scope::DeclScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 283 | Decl *LinkageSpec |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 284 | = Actions.ActOnStartLinkageSpecification(getCurScope(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 285 | DS.getSourceRange().getBegin(), |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 286 | Loc, Lang, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 287 | Tok.is(tok::l_brace) ? Tok.getLocation() |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 288 | : SourceLocation()); |
| 289 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 290 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 291 | MaybeParseCXX0XAttributes(attrs); |
| 292 | MaybeParseMicrosoftAttributes(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 293 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 294 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | f41e33c | 2011-05-01 16:25:54 +0000 | [diff] [blame] | 295 | // Reset the source range in DS, as the leading "extern" |
| 296 | // does not really belong to the inner declaration ... |
| 297 | DS.SetRangeStart(SourceLocation()); |
| 298 | DS.SetRangeEnd(SourceLocation()); |
| 299 | // ... but anyway remember that such an "extern" was seen. |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 300 | DS.setExternInLinkageSpec(true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 301 | ParseExternalDeclaration(attrs, &DS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 302 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 303 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 305 | |
Douglas Gregor | 63a0113 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 306 | DS.abort(); |
| 307 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 308 | ProhibitAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 309 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 310 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 311 | T.consumeOpen(); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 312 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 313 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 314 | MaybeParseCXX0XAttributes(attrs); |
| 315 | MaybeParseMicrosoftAttributes(attrs); |
| 316 | ParseExternalDeclaration(attrs); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 319 | T.consumeClose(); |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 320 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 321 | T.getCloseLocation()); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 322 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 323 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 324 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 325 | /// using-directive. Assumes that current token is 'using'. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 326 | Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 327 | const ParsedTemplateInfo &TemplateInfo, |
| 328 | SourceLocation &DeclEnd, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 329 | ParsedAttributesWithRange &attrs, |
| 330 | Decl **OwnedType) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 331 | assert(Tok.is(tok::kw_using) && "Not using token"); |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 332 | ObjCDeclContextSwitch ObjCDC(*this); |
| 333 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 334 | // Eat 'using'. |
| 335 | SourceLocation UsingLoc = ConsumeToken(); |
| 336 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 337 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 338 | Actions.CodeCompleteUsing(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 339 | cutOffParsing(); |
| 340 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 341 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 342 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 343 | // 'using namespace' means this is a using-directive. |
| 344 | if (Tok.is(tok::kw_namespace)) { |
| 345 | // Template parameters are always an error here. |
| 346 | if (TemplateInfo.Kind) { |
| 347 | SourceRange R = TemplateInfo.getSourceRange(); |
| 348 | Diag(UsingLoc, diag::err_templated_using_directive) |
| 349 | << R << FixItHint::CreateRemoval(R); |
| 350 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 351 | |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 352 | return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 355 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 356 | |
| 357 | // Using declarations can't have attributes. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 358 | ProhibitAttributes(attrs); |
Chris Lattner | 2f27477 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 359 | |
Fariborz Jahanian | 9735c5e | 2011-08-22 17:59:19 +0000 | [diff] [blame] | 360 | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 361 | AS_none, OwnedType); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 365 | /// that current token is 'namespace' and 'using' was already parsed. |
| 366 | /// |
| 367 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 368 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 369 | /// namespace-name ; |
| 370 | /// [GNU] using-directive: |
| 371 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 372 | /// namespace-name attributes[opt] ; |
| 373 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 374 | Decl *Parser::ParseUsingDirective(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 375 | SourceLocation UsingLoc, |
| 376 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 377 | ParsedAttributes &attrs) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 378 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 379 | |
| 380 | // Eat 'namespace'. |
| 381 | SourceLocation NamespcLoc = ConsumeToken(); |
| 382 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 383 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 384 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 385 | cutOffParsing(); |
| 386 | return 0; |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 387 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 389 | CXXScopeSpec SS; |
| 390 | // Parse (optional) nested-name-specifier. |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 391 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 393 | IdentifierInfo *NamespcName = 0; |
| 394 | SourceLocation IdentLoc = SourceLocation(); |
| 395 | |
| 396 | // Parse namespace-name. |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 397 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 398 | Diag(Tok, diag::err_expected_namespace_name); |
| 399 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 400 | SkipUntil(tok::semi); |
| 401 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 402 | return 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 405 | // Parse identifier. |
| 406 | NamespcName = Tok.getIdentifierInfo(); |
| 407 | IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 409 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 410 | bool GNUAttr = false; |
| 411 | if (Tok.is(tok::kw___attribute)) { |
| 412 | GNUAttr = true; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 413 | ParseGNUAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 416 | // Eat ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 417 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 418 | ExpectAndConsume(tok::semi, |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 419 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
| 420 | : diag::err_expected_semi_after_namespace_name, |
| 421 | "", tok::semi); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 423 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 424 | IdentLoc, NamespcName, attrs.getList()); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 427 | /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration. |
| 428 | /// Assumes that 'using' was already seen. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 429 | /// |
| 430 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 431 | /// 'using' 'typename'[opt] ::[opt] nested-name-specifier |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 432 | /// unqualified-id |
| 433 | /// 'using' :: unqualified-id |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 434 | /// |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 435 | /// alias-declaration: C++0x [decl.typedef]p2 |
| 436 | /// 'using' identifier = type-id ; |
| 437 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 438 | Decl *Parser::ParseUsingDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 439 | const ParsedTemplateInfo &TemplateInfo, |
| 440 | SourceLocation UsingLoc, |
| 441 | SourceLocation &DeclEnd, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 442 | AccessSpecifier AS, |
| 443 | Decl **OwnedType) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 444 | CXXScopeSpec SS; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 445 | SourceLocation TypenameLoc; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 446 | bool IsTypeName; |
| 447 | |
| 448 | // Ignore optional 'typename'. |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 449 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 450 | if (Tok.is(tok::kw_typename)) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 451 | TypenameLoc = Tok.getLocation(); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 452 | ConsumeToken(); |
| 453 | IsTypeName = true; |
| 454 | } |
| 455 | else |
| 456 | IsTypeName = false; |
| 457 | |
| 458 | // Parse nested-name-specifier. |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 459 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 461 | // Check nested-name specifier. |
| 462 | if (SS.isInvalid()) { |
| 463 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 464 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 465 | } |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 466 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 467 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 468 | // destructor names and allow the action module to diagnose any semantic |
| 469 | // errors. |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 470 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 471 | UnqualifiedId Name; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 472 | if (ParseUnqualifiedId(SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 473 | /*EnteringContext=*/false, |
| 474 | /*AllowDestructorName=*/true, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 475 | /*AllowConstructorName=*/true, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 476 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 477 | TemplateKWLoc, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 478 | Name)) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 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 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 482 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 483 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 484 | |
| 485 | // Maybe this is an alias-declaration. |
| 486 | bool IsAliasDecl = Tok.is(tok::equal); |
| 487 | TypeResult TypeAlias; |
| 488 | if (IsAliasDecl) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 489 | // TODO: Attribute support. C++0x attributes may appear before the equals. |
| 490 | // Where can GNU attributes appear? |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 491 | ConsumeToken(); |
| 492 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 493 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 494 | diag::warn_cxx98_compat_alias_declaration : |
| 495 | diag::ext_alias_declaration); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 496 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 497 | // Type alias templates cannot be specialized. |
| 498 | int SpecKind = -1; |
Richard Smith | 536e9c1 | 2011-05-05 22:36:10 +0000 | [diff] [blame] | 499 | if (TemplateInfo.Kind == ParsedTemplateInfo::Template && |
| 500 | Name.getKind() == UnqualifiedId::IK_TemplateId) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 501 | SpecKind = 0; |
| 502 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) |
| 503 | SpecKind = 1; |
| 504 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
| 505 | SpecKind = 2; |
| 506 | if (SpecKind != -1) { |
| 507 | SourceRange Range; |
| 508 | if (SpecKind == 0) |
| 509 | Range = SourceRange(Name.TemplateId->LAngleLoc, |
| 510 | Name.TemplateId->RAngleLoc); |
| 511 | else |
| 512 | Range = TemplateInfo.getSourceRange(); |
| 513 | Diag(Range.getBegin(), diag::err_alias_declaration_specialization) |
| 514 | << SpecKind << Range; |
| 515 | SkipUntil(tok::semi); |
| 516 | return 0; |
| 517 | } |
| 518 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 519 | // Name must be an identifier. |
| 520 | if (Name.getKind() != UnqualifiedId::IK_Identifier) { |
| 521 | Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier); |
| 522 | // No removal fixit: can't recover from this. |
| 523 | SkipUntil(tok::semi); |
| 524 | return 0; |
| 525 | } else if (IsTypeName) |
| 526 | Diag(TypenameLoc, diag::err_alias_declaration_not_identifier) |
| 527 | << FixItHint::CreateRemoval(SourceRange(TypenameLoc, |
| 528 | SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc)); |
| 529 | else if (SS.isNotEmpty()) |
| 530 | Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier) |
| 531 | << FixItHint::CreateRemoval(SS.getRange()); |
| 532 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 533 | TypeAlias = ParseTypeName(0, TemplateInfo.Kind ? |
| 534 | Declarator::AliasTemplateContext : |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 535 | Declarator::AliasDeclContext, AS, OwnedType); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 536 | } else |
| 537 | // Parse (optional) attributes (most likely GNU strong-using extension). |
| 538 | MaybeParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 540 | // Eat ';'. |
| 541 | DeclEnd = Tok.getLocation(); |
| 542 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 543 | !attrs.empty() ? "attributes list" : |
| 544 | IsAliasDecl ? "alias declaration" : "using declaration", |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 545 | tok::semi); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 546 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 547 | // Diagnose an attempt to declare a templated using-declaration. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 548 | // In C++0x, alias-declarations can be templates: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 549 | // template <...> using id = type; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 550 | if (TemplateInfo.Kind && !IsAliasDecl) { |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 551 | SourceRange R = TemplateInfo.getSourceRange(); |
| 552 | Diag(UsingLoc, diag::err_templated_using_declaration) |
| 553 | << R << FixItHint::CreateRemoval(R); |
| 554 | |
| 555 | // Unfortunately, we have to bail out instead of recovering by |
| 556 | // ignoring the parameters, just in case the nested name specifier |
| 557 | // depends on the parameters. |
| 558 | return 0; |
| 559 | } |
| 560 | |
Douglas Gregor | 480b53c | 2011-09-26 14:30:28 +0000 | [diff] [blame] | 561 | // "typename" keyword is allowed for identifiers only, |
| 562 | // because it may be a type definition. |
| 563 | if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) { |
| 564 | Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only) |
| 565 | << FixItHint::CreateRemoval(SourceRange(TypenameLoc)); |
| 566 | // Proceed parsing, but reset the IsTypeName flag. |
| 567 | IsTypeName = false; |
| 568 | } |
| 569 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 570 | if (IsAliasDecl) { |
| 571 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 572 | MultiTemplateParamsArg TemplateParamsArg(Actions, |
| 573 | TemplateParams ? TemplateParams->data() : 0, |
| 574 | TemplateParams ? TemplateParams->size() : 0); |
| 575 | return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg, |
| 576 | UsingLoc, Name, TypeAlias); |
| 577 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 578 | |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 579 | return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 580 | Name, attrs.getList(), |
| 581 | IsTypeName, TypenameLoc); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 584 | /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 585 | /// |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 586 | /// [C++0x] static_assert-declaration: |
| 587 | /// static_assert ( constant-expression , string-literal ) ; |
| 588 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 589 | /// [C11] static_assert-declaration: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 590 | /// _Static_assert ( constant-expression , string-literal ) ; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 591 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 592 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 593 | assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) && |
| 594 | "Not a static_assert declaration"); |
| 595 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 596 | if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 597 | Diag(Tok, diag::ext_c11_static_assert); |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 598 | if (Tok.is(tok::kw_static_assert)) |
| 599 | Diag(Tok, diag::warn_cxx98_compat_static_assert); |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 600 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 601 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 603 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 604 | if (T.consumeOpen()) { |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 605 | Diag(Tok, diag::err_expected_lparen); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 606 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 609 | ExprResult AssertExpr(ParseConstantExpression()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 610 | if (AssertExpr.isInvalid()) { |
| 611 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 612 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 613 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 615 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 616 | return 0; |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 617 | |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 618 | if (!isTokenStringLiteral()) { |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 619 | Diag(Tok, diag::err_expected_string_literal); |
| 620 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 621 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 622 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 624 | ExprResult AssertMessage(ParseStringLiteralExpression()); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 625 | if (AssertMessage.isInvalid()) { |
| 626 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 627 | return 0; |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 628 | } |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 630 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 632 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 633 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 634 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 635 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
| 636 | AssertExpr.take(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 637 | AssertMessage.take(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 638 | T.getCloseLocation()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 641 | /// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. |
| 642 | /// |
| 643 | /// 'decltype' ( expression ) |
| 644 | /// |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 645 | SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
| 646 | assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) |
| 647 | && "Not a decltype specifier"); |
| 648 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 649 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 650 | ExprResult Result; |
| 651 | SourceLocation StartLoc = Tok.getLocation(); |
| 652 | SourceLocation EndLoc; |
| 653 | |
| 654 | if (Tok.is(tok::annot_decltype)) { |
| 655 | Result = getExprAnnotation(Tok); |
| 656 | EndLoc = Tok.getAnnotationEndLoc(); |
| 657 | ConsumeToken(); |
| 658 | if (Result.isInvalid()) { |
| 659 | DS.SetTypeSpecError(); |
| 660 | return EndLoc; |
| 661 | } |
| 662 | } else { |
Richard Smith | c7b5543 | 2012-02-24 22:30:04 +0000 | [diff] [blame] | 663 | if (Tok.getIdentifierInfo()->isStr("decltype")) |
| 664 | Diag(Tok, diag::warn_cxx98_compat_decltype); |
Richard Smith | 39304fa | 2012-02-24 18:10:23 +0000 | [diff] [blame] | 665 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 666 | ConsumeToken(); |
| 667 | |
| 668 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 669 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 670 | "decltype", tok::r_paren)) { |
| 671 | DS.SetTypeSpecError(); |
| 672 | return T.getOpenLocation() == Tok.getLocation() ? |
| 673 | StartLoc : T.getOpenLocation(); |
| 674 | } |
| 675 | |
| 676 | // Parse the expression |
| 677 | |
| 678 | // C++0x [dcl.type.simple]p4: |
| 679 | // The operand of the decltype specifier is an unevaluated operand. |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 680 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 681 | 0, /*IsDecltype=*/true); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 682 | Result = ParseExpression(); |
| 683 | if (Result.isInvalid()) { |
Richard Smith | d8e4dac | 2012-02-27 05:24:00 +0000 | [diff] [blame] | 684 | SkipUntil(tok::r_paren); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 685 | DS.SetTypeSpecError(); |
Richard Smith | d8e4dac | 2012-02-27 05:24:00 +0000 | [diff] [blame] | 686 | return StartLoc; |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | // Match the ')' |
| 690 | T.consumeClose(); |
| 691 | if (T.getCloseLocation().isInvalid()) { |
| 692 | DS.SetTypeSpecError(); |
| 693 | // FIXME: this should return the location of the last token |
| 694 | // that was consumed (by "consumeClose()") |
| 695 | return T.getCloseLocation(); |
| 696 | } |
| 697 | |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 698 | Result = Actions.ActOnDecltypeExpression(Result.take()); |
| 699 | if (Result.isInvalid()) { |
| 700 | DS.SetTypeSpecError(); |
| 701 | return T.getCloseLocation(); |
| 702 | } |
| 703 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 704 | EndLoc = T.getCloseLocation(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 707 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 708 | unsigned DiagID; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 709 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 711 | DiagID, Result.release())) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 712 | Diag(StartLoc, DiagID) << PrevSpec; |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 713 | DS.SetTypeSpecError(); |
| 714 | } |
| 715 | return EndLoc; |
| 716 | } |
| 717 | |
| 718 | void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS, |
| 719 | SourceLocation StartLoc, |
| 720 | SourceLocation EndLoc) { |
| 721 | // make sure we have a token we can turn into an annotation token |
| 722 | if (PP.isBacktrackEnabled()) |
| 723 | PP.RevertCachedTokens(1); |
| 724 | else |
| 725 | PP.EnterToken(Tok); |
| 726 | |
| 727 | Tok.setKind(tok::annot_decltype); |
| 728 | setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ? |
| 729 | DS.getRepAsExpr() : ExprResult()); |
| 730 | Tok.setAnnotationEndLoc(EndLoc); |
| 731 | Tok.setLocation(StartLoc); |
| 732 | PP.AnnotateCachedTokens(Tok); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 735 | void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) { |
| 736 | assert(Tok.is(tok::kw___underlying_type) && |
| 737 | "Not an underlying type specifier"); |
| 738 | |
| 739 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 740 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 741 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
| 742 | "__underlying_type", tok::r_paren)) { |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | |
| 746 | TypeResult Result = ParseTypeName(); |
| 747 | if (Result.isInvalid()) { |
| 748 | SkipUntil(tok::r_paren); |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 753 | T.consumeClose(); |
| 754 | if (T.getCloseLocation().isInvalid()) |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 755 | return; |
| 756 | |
| 757 | const char *PrevSpec = 0; |
| 758 | unsigned DiagID; |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 759 | if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec, |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 760 | DiagID, Result.release())) |
| 761 | Diag(StartLoc, DiagID) << PrevSpec; |
| 762 | } |
| 763 | |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 764 | /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a |
| 765 | /// class name or decltype-specifier. Note that we only check that the result |
| 766 | /// names a type; semantic analysis will need to verify that the type names a |
| 767 | /// class. The result is either a type or null, depending on whether a type |
| 768 | /// name was found. |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 769 | /// |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 770 | /// base-type-specifier: [C++ 10.1] |
| 771 | /// class-or-decltype |
| 772 | /// class-or-decltype: [C++ 10.1] |
| 773 | /// nested-name-specifier[opt] class-name |
| 774 | /// decltype-specifier |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 775 | /// class-name: [C++ 9.1] |
| 776 | /// identifier |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 777 | /// simple-template-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | /// |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 779 | Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc, |
| 780 | SourceLocation &EndLocation) { |
David Blaikie | 7fe3878 | 2011-10-25 18:46:41 +0000 | [diff] [blame] | 781 | // Ignore attempts to use typename |
| 782 | if (Tok.is(tok::kw_typename)) { |
| 783 | Diag(Tok, diag::err_expected_class_name_not_template) |
| 784 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 785 | ConsumeToken(); |
| 786 | } |
| 787 | |
David Blaikie | 152aa4b | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 788 | // Parse optional nested-name-specifier |
| 789 | CXXScopeSpec SS; |
| 790 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
| 791 | |
| 792 | BaseLoc = Tok.getLocation(); |
| 793 | |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 794 | // Parse decltype-specifier |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 795 | // tok == kw_decltype is just error recovery, it can only happen when SS |
| 796 | // isn't empty |
| 797 | if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) { |
David Blaikie | 152aa4b | 2011-10-25 18:17:58 +0000 | [diff] [blame] | 798 | if (SS.isNotEmpty()) |
| 799 | Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype) |
| 800 | << FixItHint::CreateRemoval(SS.getRange()); |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 801 | // Fake up a Declarator to use with ActOnTypeName. |
| 802 | DeclSpec DS(AttrFactory); |
| 803 | |
David Blaikie | b577757 | 2011-12-08 04:53:15 +0000 | [diff] [blame] | 804 | EndLocation = ParseDecltypeSpecifier(DS); |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 805 | |
| 806 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 807 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 808 | } |
| 809 | |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 810 | // Check whether we have a template-id that names a type. |
| 811 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 812 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 813 | if (TemplateId->Kind == TNK_Type_template || |
| 814 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 815 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 816 | |
| 817 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 818 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 819 | EndLocation = Tok.getAnnotationEndLoc(); |
| 820 | ConsumeToken(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 821 | |
| 822 | if (Type) |
| 823 | return Type; |
| 824 | return true; |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | // Fall through to produce an error below. |
| 828 | } |
| 829 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 830 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 831 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 832 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 835 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 836 | SourceLocation IdLoc = ConsumeToken(); |
| 837 | |
| 838 | if (Tok.is(tok::less)) { |
| 839 | // It looks the user intended to write a template-id here, but the |
| 840 | // template-name was wrong. Try to fix that. |
| 841 | TemplateNameKind TNK = TNK_Type_template; |
| 842 | TemplateTy Template; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 843 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 844 | &SS, Template, TNK)) { |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 845 | Diag(IdLoc, diag::err_unknown_template_name) |
| 846 | << Id; |
| 847 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 849 | if (!Template) |
| 850 | return true; |
| 851 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 852 | // Form the template name |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 853 | UnqualifiedId TemplateName; |
| 854 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 856 | // Parse the full template-id, then turn it into a type. |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 857 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
| 858 | TemplateName, true)) |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 859 | return true; |
| 860 | if (TNK == TNK_Dependent_template_name) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 861 | AnnotateTemplateIdTokenAsType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 862 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 863 | // If we didn't end up with a typename token, there's nothing more we |
| 864 | // can do. |
| 865 | if (Tok.isNot(tok::annot_typename)) |
| 866 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 868 | // Retrieve the type from the annotation token, consume that token, and |
| 869 | // return. |
| 870 | EndLocation = Tok.getAnnotationEndLoc(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 871 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 872 | ConsumeToken(); |
| 873 | return Type; |
| 874 | } |
| 875 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 876 | // We have an identifier; check whether it is actually a type. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 877 | ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 878 | false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 879 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 880 | /*NonTrivialTypeSourceInfo=*/true); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 881 | if (!Type) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 882 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 883 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | // Consume the identifier. |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 887 | EndLocation = IdLoc; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 888 | |
| 889 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 890 | DeclSpec DS(AttrFactory); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 891 | DS.SetRangeStart(IdLoc); |
| 892 | DS.SetRangeEnd(EndLocation); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 893 | DS.getTypeSpecScope() = SS; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 894 | |
| 895 | const char *PrevSpec = 0; |
| 896 | unsigned DiagID; |
| 897 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type); |
| 898 | |
| 899 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 900 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 903 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 904 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 905 | /// 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] | 906 | /// cannot start a definition. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 907 | /// |
| 908 | /// class-specifier: [C++ class] |
| 909 | /// class-head '{' member-specification[opt] '}' |
| 910 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 911 | /// class-head: |
| 912 | /// class-key identifier[opt] base-clause[opt] |
| 913 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 914 | /// class-key nested-name-specifier[opt] simple-template-id |
| 915 | /// base-clause[opt] |
| 916 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 918 | /// identifier base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 920 | /// simple-template-id base-clause[opt] |
| 921 | /// class-key: |
| 922 | /// 'class' |
| 923 | /// 'struct' |
| 924 | /// 'union' |
| 925 | /// |
| 926 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 928 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 929 | /// simple-template-id |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 930 | /// |
| 931 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 932 | /// together, subsume the C99 struct-or-union-specifier: |
| 933 | /// |
| 934 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 935 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 936 | /// struct-or-union identifier |
| 937 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 938 | /// '}' attributes[opt] |
| 939 | /// [GNU] struct-or-union attributes[opt] identifier |
| 940 | /// struct-or-union: |
| 941 | /// 'struct' |
| 942 | /// 'union' |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 943 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 944 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 945 | const ParsedTemplateInfo &TemplateInfo, |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 946 | AccessSpecifier AS, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 947 | bool EnteringContext, DeclSpecContext DSC) { |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 948 | DeclSpec::TST TagType; |
| 949 | if (TagTokKind == tok::kw_struct) |
| 950 | TagType = DeclSpec::TST_struct; |
| 951 | else if (TagTokKind == tok::kw_class) |
| 952 | TagType = DeclSpec::TST_class; |
| 953 | else { |
| 954 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 955 | TagType = DeclSpec::TST_union; |
| 956 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 958 | if (Tok.is(tok::code_completion)) { |
| 959 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 960 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 961 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 962 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 963 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 964 | // C++03 [temp.explicit] 14.7.2/8: |
| 965 | // The usual access checking rules do not apply to names used to specify |
| 966 | // explicit instantiations. |
| 967 | // |
| 968 | // As an extension we do not perform access checking on the names used to |
| 969 | // specify explicit specializations either. This is important to allow |
| 970 | // specializing traits classes for private types. |
| 971 | bool SuppressingAccessChecks = false; |
| 972 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 973 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) { |
| 974 | Actions.ActOnStartSuppressingAccessChecks(); |
| 975 | SuppressingAccessChecks = true; |
| 976 | } |
| 977 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 978 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 979 | // If attributes exist after tag, parse them. |
| 980 | if (Tok.is(tok::kw___attribute)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 981 | ParseGNUAttributes(attrs); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 982 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 983 | // If declspecs exist after tag, parse them. |
John McCall | b1d397c | 2010-08-05 17:13:11 +0000 | [diff] [blame] | 984 | while (Tok.is(tok::kw___declspec)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 985 | ParseMicrosoftDeclSpec(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 986 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 987 | // If C++0x attributes exist here, parse them. |
| 988 | // FIXME: Are we consistent with the ordering of parsing of different |
| 989 | // styles of attributes? |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 990 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 992 | if (TagType == DeclSpec::TST_struct && |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 993 | !Tok.is(tok::identifier) && |
| 994 | Tok.getIdentifierInfo() && |
| 995 | (Tok.is(tok::kw___is_arithmetic) || |
| 996 | Tok.is(tok::kw___is_convertible) || |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 997 | Tok.is(tok::kw___is_empty) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 998 | Tok.is(tok::kw___is_floating_point) || |
| 999 | Tok.is(tok::kw___is_function) || |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1000 | Tok.is(tok::kw___is_fundamental) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1001 | Tok.is(tok::kw___is_integral) || |
| 1002 | Tok.is(tok::kw___is_member_function_pointer) || |
| 1003 | Tok.is(tok::kw___is_member_pointer) || |
| 1004 | Tok.is(tok::kw___is_pod) || |
| 1005 | Tok.is(tok::kw___is_pointer) || |
| 1006 | Tok.is(tok::kw___is_same) || |
Douglas Gregor | 877222e | 2011-04-29 01:38:03 +0000 | [diff] [blame] | 1007 | Tok.is(tok::kw___is_scalar) || |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1008 | Tok.is(tok::kw___is_signed) || |
| 1009 | Tok.is(tok::kw___is_unsigned) || |
| 1010 | Tok.is(tok::kw___is_void))) { |
Douglas Gregor | 6887614 | 2011-07-30 07:01:49 +0000 | [diff] [blame] | 1011 | // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the |
Douglas Gregor | b467cda | 2011-04-29 15:31:39 +0000 | [diff] [blame] | 1012 | // name of struct templates, but some are keywords in GCC >= 4.3 |
| 1013 | // and Clang. Therefore, when we see the token sequence "struct |
| 1014 | // X", make X into a normal identifier rather than a keyword, to |
| 1015 | // allow libstdc++ 4.2 and libc++ to work properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 1016 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 1017 | Tok.setKind(tok::identifier); |
| 1018 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1020 | // Parse the (optional) nested-name-specifier. |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1021 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1022 | if (getLangOpts().CPlusPlus) { |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1023 | // "FOO : BAR" is not a potential typo for "FOO::BAR". |
| 1024 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1026 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext)) |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1027 | DS.SetTypeSpecError(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1028 | if (SS.isSet()) |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 1029 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
| 1030 | Diag(Tok, diag::err_expected_ident); |
| 1031 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1033 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 1034 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1035 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1036 | IdentifierInfo *Name = 0; |
| 1037 | SourceLocation NameLoc; |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1038 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1039 | if (Tok.is(tok::identifier)) { |
| 1040 | Name = Tok.getIdentifierInfo(); |
| 1041 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1042 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1043 | if (Tok.is(tok::less) && getLangOpts().CPlusPlus) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1044 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1045 | // Eat the template argument list and try to continue parsing this as |
| 1046 | // a class (or template thereof). |
| 1047 | TemplateArgList TemplateArgs; |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1048 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1049 | if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS, |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1050 | true, LAngleLoc, |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1051 | TemplateArgs, RAngleLoc)) { |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1052 | // We couldn't parse the template argument list at all, so don't |
| 1053 | // try to give any location information for the list. |
| 1054 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 1055 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1056 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1057 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1058 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1059 | << (TagType == DeclSpec::TST_class? 0 |
| 1060 | : TagType == DeclSpec::TST_struct? 1 |
| 1061 | : 2) |
| 1062 | << Name |
| 1063 | << SourceRange(LAngleLoc, RAngleLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1064 | |
| 1065 | // Strip off the last template parameter list if it was empty, since |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1066 | // we've removed its template argument list. |
| 1067 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
| 1068 | if (TemplateParams && TemplateParams->size() > 1) { |
| 1069 | TemplateParams->pop_back(); |
| 1070 | } else { |
| 1071 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1072 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1073 | = ParsedTemplateInfo::NonTemplate; |
| 1074 | } |
| 1075 | } else if (TemplateInfo.Kind |
| 1076 | == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1077 | // Pretend this is just a forward declaration. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1078 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1079 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1080 | = ParsedTemplateInfo::NonTemplate; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1081 | const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 1082 | = SourceLocation(); |
| 1083 | const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc |
| 1084 | = SourceLocation(); |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1085 | } |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 1086 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1087 | } else if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1088 | TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1089 | NameLoc = ConsumeToken(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1090 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1091 | if (TemplateId->Kind != TNK_Type_template && |
| 1092 | TemplateId->Kind != TNK_Dependent_template_name) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1093 | // The template-name in the simple-template-id refers to |
| 1094 | // something other than a class template. Give an appropriate |
| 1095 | // error message and skip to the ';'. |
| 1096 | SourceRange Range(NameLoc); |
| 1097 | if (SS.isNotEmpty()) |
| 1098 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1099 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1100 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
| 1101 | << Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1103 | DS.SetTypeSpecError(); |
| 1104 | SkipUntil(tok::semi, false, true); |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1105 | if (SuppressingAccessChecks) |
| 1106 | Actions.ActOnStopSuppressingAccessChecks(); |
| 1107 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1108 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1109 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1112 | // As soon as we're finished parsing the class's template-id, turn access |
| 1113 | // checking back on. |
| 1114 | if (SuppressingAccessChecks) |
| 1115 | Actions.ActOnStopSuppressingAccessChecks(); |
| 1116 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1117 | // There are four options here. |
| 1118 | // - If we are in a trailing return type, this is always just a reference, |
| 1119 | // and we must not try to parse a definition. For instance, |
| 1120 | // [] () -> struct S { }; |
| 1121 | // does not define a type. |
| 1122 | // - If we have 'struct foo {...', 'struct foo :...', |
| 1123 | // 'struct foo final :' or 'struct foo final {', then this is a definition. |
| 1124 | // - If we have 'struct foo;', then this is either a forward declaration |
| 1125 | // or a friend declaration, which have to be treated differently. |
| 1126 | // - Otherwise we have something like 'struct foo xyz', a reference. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1127 | // However, in type-specifier-seq's, things look like declarations but are |
| 1128 | // just references, e.g. |
| 1129 | // new struct s; |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1130 | // or |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1131 | // &T::operator struct s; |
| 1132 | // For these, DSC is DSC_type_specifier. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1133 | Sema::TagUseKind TUK; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 1134 | if (DSC == DSC_trailing) |
| 1135 | TUK = Sema::TUK_Reference; |
| 1136 | else if (Tok.is(tok::l_brace) || |
| 1137 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
| 1138 | (isCXX0XFinalKeyword() && |
David Blaikie | 6f42669 | 2012-03-12 15:39:49 +0000 | [diff] [blame] | 1139 | (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1140 | if (DS.isFriendSpecified()) { |
| 1141 | // C++ [class.friend]p2: |
| 1142 | // A class shall not be defined in a friend declaration. |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1143 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1144 | << SourceRange(DS.getFriendSpecLoc()); |
| 1145 | |
| 1146 | // Skip everything up to the semicolon, so that this looks like a proper |
| 1147 | // friend class (or template thereof) declaration. |
| 1148 | SkipUntil(tok::semi, true, true); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1149 | TUK = Sema::TUK_Friend; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1150 | } else { |
| 1151 | // Okay, this is a class definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1152 | TUK = Sema::TUK_Definition; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1153 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1154 | } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1155 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1156 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1157 | TUK = Sema::TUK_Reference; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1158 | |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1159 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1160 | TUK != Sema::TUK_Definition)) { |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 1161 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 1162 | // We have a declaration or reference to an anonymous class. |
| 1163 | Diag(StartLoc, diag::err_anon_type_definition) |
| 1164 | << DeclSpec::getSpecifierName(TagType); |
| 1165 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1166 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1167 | SkipUntil(tok::comma, true); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1171 | // Create the tag portion of the class or class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1172 | DeclResult TagOrTempResult = true; // invalid |
| 1173 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1175 | bool Owned = false; |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1176 | if (TemplateId) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1177 | // Explicit specialization, class template partial specialization, |
| 1178 | // or explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1180 | TemplateId->getTemplateArgs(), |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1181 | TemplateId->NumArgs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1182 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1183 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1184 | // This is an explicit instantiation of a class template. |
| 1185 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1186 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1187 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | TemplateInfo.TemplateLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1189 | TagType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | StartLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1191 | SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1192 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | TemplateId->TemplateNameLoc, |
| 1194 | TemplateId->LAngleLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1195 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1197 | attrs.getList()); |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Friend template-ids are treated as references unless |
| 1200 | // they have template headers, in which case they're ill-formed |
| 1201 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 1202 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1203 | } else if (TUK == Sema::TUK_Reference || |
| 1204 | (TUK == Sema::TUK_Friend && |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 1205 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1206 | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1207 | TemplateId->SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1208 | TemplateId->TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1209 | TemplateId->Template, |
| 1210 | TemplateId->TemplateNameLoc, |
| 1211 | TemplateId->LAngleLoc, |
| 1212 | TemplateArgsPtr, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 1213 | TemplateId->RAngleLoc); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1214 | } else { |
| 1215 | // This is an explicit specialization or a class template |
| 1216 | // partial specialization. |
| 1217 | TemplateParameterLists FakedParamLists; |
| 1218 | |
| 1219 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 1220 | // This looks like an explicit instantiation, because we have |
| 1221 | // something like |
| 1222 | // |
| 1223 | // template class Foo<X> |
| 1224 | // |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1225 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1226 | // meant to be an explicit specialization, but the user forgot |
| 1227 | // the '<>' after 'template'. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1228 | assert(TUK == Sema::TUK_Definition && "Expected a definition here"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1229 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | SourceLocation LAngleLoc |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1231 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | Diag(TemplateId->TemplateNameLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1233 | diag::err_explicit_instantiation_with_definition) |
| 1234 | << SourceRange(TemplateInfo.TemplateLoc) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1235 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1236 | |
| 1237 | // Create a fake template parameter list that contains only |
| 1238 | // "template<>", so that we treat this construct as a class |
| 1239 | // template specialization. |
| 1240 | FakedParamLists.push_back( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | Actions.ActOnTemplateParameterList(0, SourceLocation(), |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1242 | TemplateInfo.TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | LAngleLoc, |
| 1244 | 0, 0, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1245 | LAngleLoc)); |
| 1246 | TemplateParams = &FakedParamLists; |
| 1247 | } |
| 1248 | |
| 1249 | // Build the class template specialization. |
| 1250 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1251 | = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 1252 | StartLoc, DS.getModulePrivateSpecLoc(), SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1253 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | TemplateId->TemplateNameLoc, |
| 1255 | TemplateId->LAngleLoc, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1256 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1258 | attrs.getList(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1259 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1260 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 1261 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1262 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1263 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1264 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1265 | // Explicit instantiation of a member of a class template |
| 1266 | // specialization, e.g., |
| 1267 | // |
| 1268 | // template struct Outer<int>::Inner; |
| 1269 | // |
| 1270 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1271 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 1272 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | TemplateInfo.TemplateLoc, |
| 1274 | TagType, StartLoc, SS, Name, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1275 | NameLoc, attrs.getList()); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1276 | } else if (TUK == Sema::TUK_Friend && |
| 1277 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
| 1278 | TagOrTempResult = |
| 1279 | Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(), |
| 1280 | TagType, StartLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1281 | Name, NameLoc, attrs.getList(), |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1282 | MultiTemplateParamsArg(Actions, |
| 1283 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 1284 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1285 | } else { |
| 1286 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1287 | TUK == Sema::TUK_Definition) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1288 | // FIXME: Diagnose this particular error. |
| 1289 | } |
| 1290 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1291 | bool IsDependent = false; |
| 1292 | |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 1293 | // Don't pass down template parameter lists if this is just a tag |
| 1294 | // reference. For example, we don't need the template parameters here: |
| 1295 | // template <class T> class A *makeA(T t); |
| 1296 | MultiTemplateParamsArg TParams; |
| 1297 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 1298 | TParams = |
| 1299 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 1300 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1301 | // Declaration or definition of a class type |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1302 | TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1303 | SS, Name, NameLoc, attrs.getList(), AS, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 1304 | DS.getModulePrivateSpecLoc(), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 1305 | TParams, Owned, IsDependent, |
| 1306 | SourceLocation(), false, |
| 1307 | clang::TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1308 | |
| 1309 | // If ActOnTag said the type was dependent, try again with the |
| 1310 | // less common call. |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1311 | if (IsDependent) { |
| 1312 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1313 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1314 | SS, Name, StartLoc, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1315 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1316 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1318 | // If there is a body, parse it and inform the actions module. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1319 | if (TUK == Sema::TUK_Definition) { |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1320 | assert(Tok.is(tok::l_brace) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1321 | (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1322 | isCXX0XFinalKeyword()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1323 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1324 | ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1325 | else |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1326 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1329 | const char *PrevSpec = 0; |
| 1330 | unsigned DiagID; |
| 1331 | bool Result; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1332 | if (!TypeResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1333 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 1334 | NameLoc.isValid() ? NameLoc : StartLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1335 | PrevSpec, DiagID, TypeResult.get()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1336 | } else if (!TagOrTempResult.isInvalid()) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 1337 | Result = DS.SetTypeSpecType(TagType, StartLoc, |
| 1338 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 1339 | PrevSpec, DiagID, TagOrTempResult.get(), Owned); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1340 | } else { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1341 | DS.SetTypeSpecError(); |
Anders Carlsson | 66e9977 | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1342 | return; |
| 1343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1345 | if (Result) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1346 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1348 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 1349 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 1350 | // going to look at what comes after it to improve error recovery. If an |
| 1351 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 1352 | // the end of the declaration and recover that way. |
| 1353 | // |
| 1354 | // This switch enumerates the valid "follow" set for definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1355 | if (TUK == Sema::TUK_Definition) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1356 | bool ExpectedSemi = true; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1357 | switch (Tok.getKind()) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1358 | default: break; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1359 | case tok::semi: // struct foo {...} ; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1360 | case tok::star: // struct foo {...} * P; |
| 1361 | case tok::amp: // struct foo {...} & R = ... |
| 1362 | case tok::identifier: // struct foo {...} V ; |
| 1363 | case tok::r_paren: //(struct foo {...} ) {4} |
| 1364 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1365 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1366 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
Chris Lattner | c2e1c1a | 2010-02-03 20:41:24 +0000 | [diff] [blame] | 1367 | case tok::l_paren: // struct foo {...} ( x); |
Chris Lattner | 16acfee | 2010-02-03 01:45:03 +0000 | [diff] [blame] | 1368 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1369 | ExpectedSemi = false; |
| 1370 | break; |
| 1371 | // Type qualifiers |
| 1372 | case tok::kw_const: // struct foo {...} const x; |
| 1373 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1374 | case tok::kw_restrict: // struct foo {...} restrict x; |
| 1375 | case tok::kw_inline: // struct foo {...} inline foo() {}; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1376 | // Storage-class specifiers |
| 1377 | case tok::kw_static: // struct foo {...} static x; |
| 1378 | case tok::kw_extern: // struct foo {...} extern x; |
| 1379 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1380 | case tok::kw_register: // struct foo {...} register x; |
| 1381 | case tok::kw_auto: // struct foo {...} auto x; |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1382 | case tok::kw_mutable: // struct foo {...} mutable x; |
| 1383 | case tok::kw_constexpr: // struct foo {...} constexpr x; |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1384 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1385 | // can occur after class specifiers according to the grammar. However, |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1386 | // almost no one actually writes code like this. If we see one of these, |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1387 | // it is much more likely that someone missed a semi colon and the |
| 1388 | // type/storage class specifier we're seeing is part of the *next* |
| 1389 | // intended declaration, as in: |
| 1390 | // |
| 1391 | // struct foo { ... } |
| 1392 | // typedef int X; |
| 1393 | // |
| 1394 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1395 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1396 | // the same declaration of X. Because of this, we look ahead past this |
| 1397 | // token to see if it's a type specifier. If so, we know the code is |
| 1398 | // otherwise invalid, so we can produce the expected semi error. |
| 1399 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1400 | ExpectedSemi = false; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1401 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1402 | |
| 1403 | case tok::r_brace: // struct bar { struct foo {...} } |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1404 | // Missing ';' at end of struct is accepted as an extension in C mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1405 | if (!getLangOpts().CPlusPlus) |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1406 | ExpectedSemi = false; |
| 1407 | break; |
| 1408 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1409 | |
Richard Smith | cf6b0a2 | 2011-07-14 21:35:26 +0000 | [diff] [blame] | 1410 | // C++ [temp]p3 In a template-declaration which defines a class, no |
| 1411 | // declarator is permitted. |
| 1412 | if (TemplateInfo.Kind) |
| 1413 | ExpectedSemi = true; |
| 1414 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1415 | if (ExpectedSemi) { |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1416 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1417 | TagType == DeclSpec::TST_class ? "class" |
| 1418 | : TagType == DeclSpec::TST_struct? "struct" : "union"); |
| 1419 | // Push this token back into the preprocessor and change our current token |
| 1420 | // to ';' so that the rest of the code recovers as though there were an |
| 1421 | // ';' after the definition. |
| 1422 | PP.EnterToken(Tok); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1423 | Tok.setKind(tok::semi); |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1424 | } |
| 1425 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1429 | /// |
| 1430 | /// base-clause : [C++ class.derived] |
| 1431 | /// ':' base-specifier-list |
| 1432 | /// base-specifier-list: |
| 1433 | /// base-specifier '...'[opt] |
| 1434 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1435 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1436 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 1437 | ConsumeToken(); |
| 1438 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1439 | // Build up an array of parsed base specifiers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1440 | SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1441 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1442 | while (true) { |
| 1443 | // Parse a base-specifier. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1444 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1445 | if (Result.isInvalid()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1446 | // Skip the rest of this base specifier, up until the comma or |
| 1447 | // opening brace. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1448 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 1449 | } else { |
| 1450 | // Add this to our array of base specifiers. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1451 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // If the next token is a comma, consume it and keep reading |
| 1455 | // base-specifiers. |
| 1456 | if (Tok.isNot(tok::comma)) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1458 | // Consume the comma. |
| 1459 | ConsumeToken(); |
| 1460 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Attach the base specifiers |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1463 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 1467 | /// one entry in the base class list of a class specifier, for example: |
| 1468 | /// class foo : public bar, virtual private baz { |
| 1469 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 1470 | /// |
| 1471 | /// base-specifier: [C++ class.derived] |
| 1472 | /// ::[opt] nested-name-specifier[opt] class-name |
| 1473 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1474 | /// base-type-specifier |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1475 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
David Blaikie | 09048df | 2011-10-25 15:01:20 +0000 | [diff] [blame] | 1476 | /// base-type-specifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1477 | Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1478 | bool IsVirtual = false; |
| 1479 | SourceLocation StartLoc = Tok.getLocation(); |
| 1480 | |
| 1481 | // Parse the 'virtual' keyword. |
| 1482 | if (Tok.is(tok::kw_virtual)) { |
| 1483 | ConsumeToken(); |
| 1484 | IsVirtual = true; |
| 1485 | } |
| 1486 | |
| 1487 | // Parse an (optional) access specifier. |
| 1488 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1489 | if (Access != AS_none) |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1490 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1492 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 1493 | // access specifier. |
| 1494 | if (Tok.is(tok::kw_virtual)) { |
| 1495 | SourceLocation VirtualLoc = ConsumeToken(); |
| 1496 | if (IsVirtual) { |
| 1497 | // Complain about duplicate 'virtual' |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1498 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1499 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | IsVirtual = true; |
| 1503 | } |
| 1504 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1505 | // Parse the class-name. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1506 | SourceLocation EndLocation; |
David Blaikie | 22216eb | 2011-10-25 17:10:12 +0000 | [diff] [blame] | 1507 | SourceLocation BaseLoc; |
| 1508 | TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1509 | if (BaseType.isInvalid()) |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1510 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1512 | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
| 1513 | // actually part of the base-specifier-list grammar productions, but we |
| 1514 | // parse it here for convenience. |
| 1515 | SourceLocation EllipsisLoc; |
| 1516 | if (Tok.is(tok::ellipsis)) |
| 1517 | EllipsisLoc = ConsumeToken(); |
| 1518 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1520 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1522 | // Notify semantic analysis that we have parsed a complete |
| 1523 | // base-specifier. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1524 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1525 | BaseType.get(), BaseLoc, EllipsisLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 1529 | /// a C++ access-specifier. |
| 1530 | /// |
| 1531 | /// access-specifier: [C++ class.derived] |
| 1532 | /// 'private' |
| 1533 | /// 'protected' |
| 1534 | /// 'public' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1536 | switch (Tok.getKind()) { |
| 1537 | default: return AS_none; |
| 1538 | case tok::kw_private: return AS_private; |
| 1539 | case tok::kw_protected: return AS_protected; |
| 1540 | case tok::kw_public: return AS_public; |
| 1541 | } |
| 1542 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1543 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1544 | void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1545 | Decl *ThisDecl) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1546 | // We just declared a member function. If this member function |
| 1547 | // has any default arguments, we'll need to parse them later. |
| 1548 | LateParsedMethodDeclaration *LateMethod = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | DeclaratorChunk::FunctionTypeInfo &FTI |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1550 | = DeclaratorInfo.getFunctionTypeInfo(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1551 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 1552 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 1553 | if (!LateMethod) { |
| 1554 | // Push this method onto the stack of late-parsed method |
| 1555 | // declarations. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1556 | LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
| 1557 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1558 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Add all of the parameters prior to this one (they don't |
| 1561 | // have default arguments). |
| 1562 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 1563 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 1564 | LateMethod->DefaultArgs.push_back( |
Douglas Gregor | 8f8210c | 2010-03-02 01:29:43 +0000 | [diff] [blame] | 1565 | LateParsedDefaultArgument(FTI.ArgInfo[I].Param)); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | // Add this parameter to the list of parameters (it or may |
| 1569 | // not have a default argument). |
| 1570 | LateMethod->DefaultArgs.push_back( |
| 1571 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 1572 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1577 | /// isCXX0XVirtSpecifier - Determine whether the given token is a C++0x |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1578 | /// virt-specifier. |
| 1579 | /// |
| 1580 | /// virt-specifier: |
| 1581 | /// override |
| 1582 | /// final |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1583 | VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier(const Token &Tok) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1584 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1585 | return VirtSpecifiers::VS_None; |
| 1586 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1587 | if (Tok.is(tok::identifier)) { |
| 1588 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1589 | |
Anders Carlsson | 7eeb4ec | 2011-01-20 03:47:08 +0000 | [diff] [blame] | 1590 | // Initialize the contextual keywords. |
| 1591 | if (!Ident_final) { |
| 1592 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1593 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1594 | } |
| 1595 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1596 | if (II == Ident_override) |
| 1597 | return VirtSpecifiers::VS_Override; |
| 1598 | |
| 1599 | if (II == Ident_final) |
| 1600 | return VirtSpecifiers::VS_Final; |
| 1601 | } |
| 1602 | |
| 1603 | return VirtSpecifiers::VS_None; |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | /// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq. |
| 1607 | /// |
| 1608 | /// virt-specifier-seq: |
| 1609 | /// virt-specifier |
| 1610 | /// virt-specifier-seq virt-specifier |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1611 | void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) { |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1612 | while (true) { |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1613 | VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier(); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1614 | if (Specifier == VirtSpecifiers::VS_None) |
| 1615 | return; |
| 1616 | |
| 1617 | // C++ [class.mem]p8: |
| 1618 | // 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] | 1619 | const char *PrevSpec = 0; |
Anders Carlsson | 46127a9 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 1620 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1621 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
| 1622 | << PrevSpec |
| 1623 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1624 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1625 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1626 | diag::warn_cxx98_compat_override_control_keyword : |
| 1627 | diag::ext_override_control_keyword) |
| 1628 | << VirtSpecifiers::getSpecifierName(Specifier); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1629 | ConsumeToken(); |
| 1630 | } |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1633 | /// isCXX0XFinalKeyword - Determine whether the next token is a C++0x |
| 1634 | /// contextual 'final' keyword. |
| 1635 | bool Parser::isCXX0XFinalKeyword() const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1636 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1637 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1638 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1639 | if (!Tok.is(tok::identifier)) |
| 1640 | return false; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1641 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1642 | // Initialize the contextual keywords. |
| 1643 | if (!Ident_final) { |
| 1644 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1645 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1646 | } |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1647 | |
Anders Carlsson | 8a29ba0 | 2011-03-25 14:53:29 +0000 | [diff] [blame] | 1648 | return Tok.getIdentifierInfo() == Ident_final; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1651 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 1652 | /// |
| 1653 | /// member-declaration: |
| 1654 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 1655 | /// function-definition ';'[opt] |
| 1656 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 1657 | /// using-declaration [TODO] |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1658 | /// [C++0x] static_assert-declaration |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1659 | /// template-declaration |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1660 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1661 | /// |
| 1662 | /// member-declarator-list: |
| 1663 | /// member-declarator |
| 1664 | /// member-declarator-list ',' member-declarator |
| 1665 | /// |
| 1666 | /// member-declarator: |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1667 | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1668 | /// declarator constant-initializer[opt] |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1669 | /// [C++11] declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1670 | /// identifier[opt] ':' constant-expression |
| 1671 | /// |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1672 | /// virt-specifier-seq: |
| 1673 | /// virt-specifier |
| 1674 | /// virt-specifier-seq virt-specifier |
| 1675 | /// |
| 1676 | /// virt-specifier: |
| 1677 | /// override |
| 1678 | /// final |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1679 | /// |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1680 | /// pure-specifier: |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1681 | /// '= 0' |
| 1682 | /// |
| 1683 | /// constant-initializer: |
| 1684 | /// '=' constant-expression |
| 1685 | /// |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1686 | void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1687 | AttributeList *AccessAttrs, |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1688 | const ParsedTemplateInfo &TemplateInfo, |
| 1689 | ParsingDeclRAIIObject *TemplateDiags) { |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1690 | if (Tok.is(tok::at)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1691 | if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs)) |
Douglas Gregor | 8a9013d | 2011-04-14 17:21:19 +0000 | [diff] [blame] | 1692 | Diag(Tok, diag::err_at_defs_cxx); |
| 1693 | else |
| 1694 | Diag(Tok, diag::err_at_in_class); |
| 1695 | |
| 1696 | ConsumeToken(); |
| 1697 | SkipUntil(tok::r_brace); |
| 1698 | return; |
| 1699 | } |
| 1700 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1701 | // Access declarations. |
| 1702 | if (!TemplateInfo.Kind && |
| 1703 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1704 | !TryAnnotateCXXScopeToken() && |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1705 | Tok.is(tok::annot_cxxscope)) { |
| 1706 | bool isAccessDecl = false; |
| 1707 | if (NextToken().is(tok::identifier)) |
| 1708 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 1709 | else |
| 1710 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 1711 | |
| 1712 | if (isAccessDecl) { |
| 1713 | // Collect the scope specifier token we annotated earlier. |
| 1714 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1715 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 1716 | /*EnteringContext=*/false); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1717 | |
| 1718 | // Try to parse an unqualified-id. |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1719 | SourceLocation TemplateKWLoc; |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1720 | UnqualifiedId Name; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1721 | if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), |
| 1722 | TemplateKWLoc, Name)) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1723 | SkipUntil(tok::semi); |
| 1724 | return; |
| 1725 | } |
| 1726 | |
| 1727 | // TODO: recover from mistakenly-qualified operator declarations. |
| 1728 | if (ExpectAndConsume(tok::semi, |
| 1729 | diag::err_expected_semi_after, |
| 1730 | "access declaration", |
| 1731 | tok::semi)) |
| 1732 | return; |
| 1733 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1734 | Actions.ActOnUsingDeclaration(getCurScope(), AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1735 | false, SourceLocation(), |
| 1736 | SS, Name, |
| 1737 | /* AttrList */ 0, |
| 1738 | /* IsTypeName */ false, |
| 1739 | SourceLocation()); |
| 1740 | return; |
| 1741 | } |
| 1742 | } |
| 1743 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1744 | // static_assert-declaration |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1745 | 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] | 1746 | // FIXME: Check for templates |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1747 | SourceLocation DeclEnd; |
| 1748 | ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1749 | return; |
| 1750 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1752 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1754 | "Nested template improperly parsed?"); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1755 | SourceLocation DeclEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1757 | AS, AccessAttrs); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1758 | return; |
| 1759 | } |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1760 | |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1761 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 1762 | if (Tok.is(tok::kw___extension__)) { |
| 1763 | // __extension__ silences extension warnings in the subexpression. |
| 1764 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 1765 | ConsumeToken(); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1766 | return ParseCXXClassMemberDeclaration(AS, AccessAttrs, |
| 1767 | TemplateInfo, TemplateDiags); |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1768 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1769 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1770 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it |
| 1771 | // is a bitfield. |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1772 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1773 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1774 | ParsedAttributesWithRange attrs(AttrFactory); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1775 | // Optional C++0x attribute-specifier |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1776 | MaybeParseCXX0XAttributes(attrs); |
| 1777 | MaybeParseMicrosoftAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1779 | if (Tok.is(tok::kw_using)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1780 | ProhibitAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1782 | // Eat 'using'. |
| 1783 | SourceLocation UsingLoc = ConsumeToken(); |
| 1784 | |
| 1785 | if (Tok.is(tok::kw_namespace)) { |
| 1786 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
| 1787 | SkipUntil(tok::semi, true, true); |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1788 | } else { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1789 | SourceLocation DeclEnd; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1790 | // Otherwise, it must be a using-declaration or an alias-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1791 | ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo, |
| 1792 | UsingLoc, DeclEnd, AS); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1793 | } |
| 1794 | return; |
| 1795 | } |
| 1796 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1797 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 1798 | LateParsedAttrList CommonLateParsedAttrs; |
| 1799 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1800 | // decl-specifier-seq: |
| 1801 | // Parse the common declaration-specifiers piece. |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1802 | ParsingDeclSpec DS(*this, TemplateDiags); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1803 | DS.takeAttributesFrom(attrs); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1804 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class, |
| 1805 | &CommonLateParsedAttrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1806 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1807 | MultiTemplateParamsArg TemplateParams(Actions, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 1808 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, |
| 1809 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 1810 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1811 | if (Tok.is(tok::semi)) { |
| 1812 | ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1813 | Decl *TheDecl = |
Chandler Carruth | 0f4be74 | 2011-05-03 18:35:10 +0000 | [diff] [blame] | 1814 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams); |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1815 | DS.complete(TheDecl); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1816 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1817 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1818 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1819 | ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1820 | VirtSpecifiers VS; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1821 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1822 | // Hold late-parsed attributes so we can attach a Decl to them later. |
| 1823 | LateParsedAttrList LateParsedAttrs; |
| 1824 | |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 1825 | SourceLocation EqualLoc; |
| 1826 | bool HasInitializer = false; |
| 1827 | ExprResult Init; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1828 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1829 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 1830 | ColonProtectionRAIIObject X(*this); |
| 1831 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1832 | // Parse the first declarator. |
| 1833 | ParseDeclarator(DeclaratorInfo); |
| 1834 | // Error parsing the declarator? |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1835 | if (!DeclaratorInfo.hasName()) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1836 | // If so, skip until the semi-colon or a }. |
Sebastian Redl | d941fa4 | 2011-04-24 16:27:48 +0000 | [diff] [blame] | 1837 | SkipUntil(tok::r_brace, true, true); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1838 | if (Tok.is(tok::semi)) |
| 1839 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1840 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1843 | ParseOptionalCXX0XVirtSpecifierSeq(VS); |
| 1844 | |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 1845 | // If attributes exist after the declarator, but before an '{', parse them. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1846 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 1847 | |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 1848 | // MSVC permits pure specifier on inline functions declared at class scope. |
| 1849 | // Hence check for =0 before checking for function definition. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1850 | if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) && |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 1851 | DeclaratorInfo.isFunctionDeclarator() && |
| 1852 | NextToken().is(tok::numeric_constant)) { |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 1853 | EqualLoc = ConsumeToken(); |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 1854 | Init = ParseInitializer(); |
| 1855 | if (Init.isInvalid()) |
| 1856 | SkipUntil(tok::comma, true, true); |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 1857 | else |
| 1858 | HasInitializer = true; |
Francois Pichet | 6a24747 | 2011-05-11 02:14:46 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 1861 | FunctionDefinitionKind DefinitionKind = FDK_Declaration; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1862 | // function-definition: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1863 | // |
| 1864 | // In C++11, a non-function declarator followed by an open brace is a |
| 1865 | // braced-init-list for an in-class member initialization, not an |
| 1866 | // erroneous function definition. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1867 | if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus0x) { |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 1868 | DefinitionKind = FDK_Definition; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1869 | } else if (DeclaratorInfo.isFunctionDeclarator()) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1870 | 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] | 1871 | DefinitionKind = FDK_Definition; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1872 | } else if (Tok.is(tok::equal)) { |
| 1873 | const Token &KW = NextToken(); |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 1874 | if (KW.is(tok::kw_default)) |
| 1875 | DefinitionKind = FDK_Defaulted; |
| 1876 | else if (KW.is(tok::kw_delete)) |
| 1877 | DefinitionKind = FDK_Deleted; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 1881 | if (DefinitionKind) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1882 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 1883 | Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1884 | ConsumeBrace(); |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 1885 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1886 | |
| 1887 | // Consume the optional ';' |
| 1888 | if (Tok.is(tok::semi)) |
| 1889 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1890 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1891 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1892 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1893 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 1894 | Diag(DeclaratorInfo.getIdentifierLoc(), |
| 1895 | diag::err_function_declared_typedef); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1896 | // This recovery skips the entire function body. It would be nice |
| 1897 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 1898 | // assumes the declarator represents a function, not a typedef. |
| 1899 | ConsumeBrace(); |
Richard Trieu | 65ba948 | 2012-01-21 02:59:18 +0000 | [diff] [blame] | 1900 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1901 | |
| 1902 | // Consume the optional ';' |
| 1903 | if (Tok.is(tok::semi)) |
| 1904 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1905 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1908 | Decl *FunDecl = |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1909 | ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo, |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 1910 | VS, DefinitionKind, Init); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1911 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1912 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) { |
| 1913 | CommonLateParsedAttrs[i]->addDecl(FunDecl); |
| 1914 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1915 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1916 | LateParsedAttrs[i]->addDecl(FunDecl); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1917 | } |
| 1918 | LateParsedAttrs.clear(); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1919 | |
| 1920 | // Consume the ';' - it's optional unless we have a delete or default |
| 1921 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1922 | ConsumeToken(); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1923 | } |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1924 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1925 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1926 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | // member-declarator-list: |
| 1930 | // member-declarator |
| 1931 | // member-declarator-list ',' member-declarator |
| 1932 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1933 | SmallVector<Decl *, 8> DeclsInGroup; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1934 | ExprResult BitfieldSize; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1935 | bool ExpectSemi = true; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1936 | |
| 1937 | while (1) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1938 | // member-declarator: |
| 1939 | // declarator pure-specifier[opt] |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1940 | // declarator brace-or-equal-initializer[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1941 | // identifier[opt] ':' constant-expression |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1942 | if (Tok.is(tok::colon)) { |
| 1943 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1944 | BitfieldSize = ParseConstantExpression(); |
| 1945 | if (BitfieldSize.isInvalid()) |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1946 | SkipUntil(tok::comma, true, true); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1949 | // If a simple-asm-expr is present, parse it. |
| 1950 | if (Tok.is(tok::kw_asm)) { |
| 1951 | SourceLocation Loc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1952 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1953 | if (AsmLabel.isInvalid()) |
| 1954 | SkipUntil(tok::comma, true, true); |
| 1955 | |
| 1956 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
| 1957 | DeclaratorInfo.SetRangeEnd(Loc); |
| 1958 | } |
| 1959 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1960 | // If attributes exist after the declarator, parse them. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1961 | MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1962 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1963 | // FIXME: When g++ adds support for this, we'll need to check whether it |
| 1964 | // goes before or after the GNU attributes and __asm__. |
| 1965 | ParseOptionalCXX0XVirtSpecifierSeq(VS); |
| 1966 | |
| 1967 | bool HasDeferredInitializer = false; |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 1968 | if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1969 | if (BitfieldSize.get()) { |
| 1970 | Diag(Tok, diag::err_bitfield_member_init); |
| 1971 | SkipUntil(tok::comma, true, true); |
| 1972 | } else { |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 1973 | HasInitializer = true; |
Douglas Gregor | 555f57e | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 1974 | HasDeferredInitializer = !DeclaratorInfo.isDeclarationOfFunction() && |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1975 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
Richard Smith | c2cdd53 | 2011-06-12 11:43:46 +0000 | [diff] [blame] | 1976 | != DeclSpec::SCS_static && |
| 1977 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
| 1978 | != DeclSpec::SCS_typedef; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1979 | } |
| 1980 | } |
| 1981 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1982 | // 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] | 1983 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1984 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1985 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1986 | Decl *ThisDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1987 | if (DS.isFriendSpecified()) { |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1988 | // TODO: handle initializers, bitfields, 'delete' |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1989 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1990 | move(TemplateParams)); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1991 | } else { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1992 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1993 | DeclaratorInfo, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1994 | move(TemplateParams), |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1995 | BitfieldSize.release(), |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1996 | VS, HasDeferredInitializer); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1997 | if (AccessAttrs) |
| 1998 | Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs, |
| 1999 | false, true); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2000 | } |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2001 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2002 | // Set the Decl for any late parsed attributes |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2003 | for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) { |
| 2004 | CommonLateParsedAttrs[i]->addDecl(ThisDecl); |
| 2005 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2006 | for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2007 | LateParsedAttrs[i]->addDecl(ThisDecl); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2008 | } |
| 2009 | LateParsedAttrs.clear(); |
| 2010 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2011 | // Handle the initializer. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2012 | if (HasDeferredInitializer) { |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2013 | // The initializer was deferred; parse it and cache the tokens. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2014 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2015 | diag::warn_cxx98_compat_nonstatic_member_init : |
| 2016 | diag::ext_nonstatic_member_init); |
| 2017 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2018 | if (DeclaratorInfo.isArrayOfUnknownBound()) { |
| 2019 | // C++0x [dcl.array]p3: An array bound may also be omitted when the |
| 2020 | // declarator is followed by an initializer. |
| 2021 | // |
| 2022 | // A brace-or-equal-initializer for a member-declarator is not an |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2023 | // initializer in the grammar, so this is ill-formed. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2024 | Diag(Tok, diag::err_incomplete_array_member_init); |
| 2025 | SkipUntil(tok::comma, true, true); |
David Blaikie | 3164c14 | 2012-02-14 09:00:46 +0000 | [diff] [blame] | 2026 | if (ThisDecl) |
| 2027 | // Avoid later warnings about a class member of incomplete type. |
| 2028 | ThisDecl->setInvalidDecl(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2029 | } else |
| 2030 | ParseCXXNonStaticMemberInitializer(ThisDecl); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2031 | } else if (HasInitializer) { |
| 2032 | // Normal initializer. |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2033 | if (!Init.isUsable()) |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2034 | Init = ParseCXXMemberInitializer(ThisDecl, |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2035 | DeclaratorInfo.isDeclarationOfFunction(), EqualLoc); |
| 2036 | |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2037 | if (Init.isInvalid()) |
| 2038 | SkipUntil(tok::comma, true, true); |
| 2039 | else if (ThisDecl) |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2040 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(), |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2041 | DS.getTypeSpecType() == DeclSpec::TST_auto); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2042 | } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2043 | // No initializer. |
| 2044 | Actions.ActOnUninitializedDecl(ThisDecl, |
| 2045 | DS.getTypeSpecType() == DeclSpec::TST_auto); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2046 | } |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2047 | |
| 2048 | if (ThisDecl) { |
| 2049 | Actions.FinalizeDeclaration(ThisDecl); |
| 2050 | DeclsInGroup.push_back(ThisDecl); |
| 2051 | } |
| 2052 | |
| 2053 | if (DeclaratorInfo.isFunctionDeclarator() && |
| 2054 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
| 2055 | != DeclSpec::SCS_typedef) { |
| 2056 | HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl); |
| 2057 | } |
| 2058 | |
| 2059 | DeclaratorInfo.complete(ThisDecl); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2060 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2061 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2062 | // or an error, bail out. |
| 2063 | if (Tok.isNot(tok::comma)) |
| 2064 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2066 | // Consume the comma. |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2067 | SourceLocation CommaLoc = ConsumeToken(); |
| 2068 | |
| 2069 | if (Tok.isAtStartOfLine() && |
| 2070 | !MightBeDeclarator(Declarator::MemberContext)) { |
| 2071 | // This comma was followed by a line-break and something which can't be |
| 2072 | // the start of a declarator. The comma was probably a typo for a |
| 2073 | // semicolon. |
| 2074 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2075 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2076 | ExpectSemi = false; |
| 2077 | break; |
| 2078 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2080 | // Parse the next declarator. |
| 2081 | DeclaratorInfo.clear(); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 2082 | VS.clear(); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 2083 | BitfieldSize = true; |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 2084 | Init = true; |
| 2085 | HasInitializer = false; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2086 | DeclaratorInfo.setCommaLoc(CommaLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2088 | // Attributes are only allowed on the second declarator. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2089 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2090 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 2091 | if (Tok.isNot(tok::colon)) |
| 2092 | ParseDeclarator(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 2095 | if (ExpectSemi && |
| 2096 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2097 | // Skip to end of block or statement. |
| 2098 | SkipUntil(tok::r_brace, true, true); |
| 2099 | // If we stopped at a ';', eat it. |
| 2100 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2101 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2104 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(), |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 2105 | DeclsInGroup.size()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2108 | /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or |
| 2109 | /// pure-specifier. Also detect and reject any attempted defaulted/deleted |
| 2110 | /// function definition. The location of the '=', if any, will be placed in |
| 2111 | /// EqualLoc. |
| 2112 | /// |
| 2113 | /// pure-specifier: |
| 2114 | /// '= 0' |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2115 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2116 | /// brace-or-equal-initializer: |
| 2117 | /// '=' initializer-expression |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2118 | /// braced-init-list |
| 2119 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2120 | /// initializer-clause: |
| 2121 | /// assignment-expression |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2122 | /// braced-init-list |
| 2123 | /// |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2124 | /// defaulted/deleted function-definition: |
| 2125 | /// '=' 'default' |
| 2126 | /// '=' 'delete' |
| 2127 | /// |
| 2128 | /// Prior to C++0x, the assignment-expression in an initializer-clause must |
| 2129 | /// be a constant-expression. |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2130 | ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2131 | SourceLocation &EqualLoc) { |
| 2132 | assert((Tok.is(tok::equal) || Tok.is(tok::l_brace)) |
| 2133 | && "Data member initializer not starting with '=' or '{'"); |
| 2134 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 2135 | EnterExpressionEvaluationContext Context(Actions, |
| 2136 | Sema::PotentiallyEvaluated, |
| 2137 | D); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2138 | if (Tok.is(tok::equal)) { |
| 2139 | EqualLoc = ConsumeToken(); |
| 2140 | if (Tok.is(tok::kw_delete)) { |
| 2141 | // In principle, an initializer of '= delete p;' is legal, but it will |
| 2142 | // never type-check. It's better to diagnose it as an ill-formed expression |
| 2143 | // than as an ill-formed deleted non-function member. |
| 2144 | // An initializer of '= delete p, foo' will never be parsed, because |
| 2145 | // a top-level comma always ends the initializer expression. |
| 2146 | const Token &Next = NextToken(); |
| 2147 | if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) || |
| 2148 | Next.is(tok::eof)) { |
| 2149 | if (IsFunction) |
| 2150 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2151 | << 1 /* delete */; |
| 2152 | else |
| 2153 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
| 2154 | return ExprResult(); |
| 2155 | } |
| 2156 | } else if (Tok.is(tok::kw_default)) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2157 | if (IsFunction) |
| 2158 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
| 2159 | << 0 /* default */; |
| 2160 | else |
| 2161 | Diag(ConsumeToken(), diag::err_default_special_members); |
| 2162 | return ExprResult(); |
| 2163 | } |
| 2164 | |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2165 | } |
| 2166 | return ParseInitializer(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2169 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 2170 | /// |
| 2171 | /// member-specification: |
| 2172 | /// member-declaration member-specification[opt] |
| 2173 | /// access-specifier ':' member-specification[opt] |
| 2174 | /// |
| 2175 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2176 | unsigned TagType, Decl *TagDecl) { |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 2177 | assert((TagType == DeclSpec::TST_struct || |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2178 | TagType == DeclSpec::TST_union || |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 2179 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2180 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2181 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2182 | "parsing struct/union/class body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2183 | |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2184 | // Determine whether this is a non-nested class. Note that local |
| 2185 | // classes are *not* considered to be nested classes. |
| 2186 | bool NonNestedClass = true; |
| 2187 | if (!ClassStack.empty()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2188 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2189 | if (S->isClassScope()) { |
| 2190 | // We're inside a class scope, so this is a nested class. |
| 2191 | NonNestedClass = false; |
| 2192 | break; |
| 2193 | } |
| 2194 | |
| 2195 | if ((S->getFlags() & Scope::FnScope)) { |
| 2196 | // If we're in a function or function template declared in the |
| 2197 | // body of a class, then this is a local class rather than a |
| 2198 | // nested class. |
| 2199 | const Scope *Parent = S->getParent(); |
| 2200 | if (Parent->isTemplateParamScope()) |
| 2201 | Parent = Parent->getParent(); |
| 2202 | if (Parent->isClassScope()) |
| 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2207 | |
| 2208 | // Enter a scope for the class. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2209 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2211 | // Note that we are parsing a new (potentially-nested) class definition. |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2212 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2213 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2214 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2215 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2216 | |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2217 | SourceLocation FinalLoc; |
| 2218 | |
| 2219 | // Parse the optional 'final' keyword. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2220 | if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { |
Richard Smith | 8b11b5e | 2011-10-15 04:21:46 +0000 | [diff] [blame] | 2221 | assert(isCXX0XFinalKeyword() && "not a class definition"); |
| 2222 | FinalLoc = ConsumeToken(); |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2223 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2224 | Diag(FinalLoc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2225 | diag::warn_cxx98_compat_override_control_keyword : |
| 2226 | diag::ext_override_control_keyword) << "final"; |
Anders Carlsson | b184a18 | 2011-03-25 14:46:08 +0000 | [diff] [blame] | 2227 | } |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 2228 | |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2229 | if (Tok.is(tok::colon)) { |
| 2230 | ParseBaseClause(TagDecl); |
| 2231 | |
| 2232 | if (!Tok.is(tok::l_brace)) { |
| 2233 | Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 2234 | |
| 2235 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2236 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2237 | return; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | assert(Tok.is(tok::l_brace)); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2242 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2243 | T.consumeOpen(); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 2244 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2245 | if (TagDecl) |
Anders Carlsson | 2c3ee54 | 2011-03-25 14:31:08 +0000 | [diff] [blame] | 2246 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2247 | T.getOpenLocation()); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 2248 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2249 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 2250 | // by default. Members of a class defined with the keywords struct or union |
| 2251 | // are public by default. |
| 2252 | AccessSpecifier CurAS; |
| 2253 | if (TagType == DeclSpec::TST_class) |
| 2254 | CurAS = AS_private; |
| 2255 | else |
| 2256 | CurAS = AS_public; |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2257 | ParsedAttributes AccessAttrs(AttrFactory); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2258 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2259 | if (TagDecl) { |
| 2260 | // While we still have something to read, read the member-declarations. |
| 2261 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 2262 | // Each iteration of this loop reads one member-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2264 | if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2265 | Tok.is(tok::kw___if_not_exists))) { |
| 2266 | ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS); |
| 2267 | continue; |
| 2268 | } |
| 2269 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2270 | // Check for extraneous top-level semicolon. |
| 2271 | if (Tok.is(tok::semi)) { |
| 2272 | Diag(Tok, diag::ext_extra_struct_semi) |
| 2273 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
| 2274 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2275 | ConsumeToken(); |
| 2276 | continue; |
| 2277 | } |
| 2278 | |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 2279 | if (Tok.is(tok::annot_pragma_vis)) { |
| 2280 | HandlePragmaVisibility(); |
| 2281 | continue; |
| 2282 | } |
| 2283 | |
| 2284 | if (Tok.is(tok::annot_pragma_pack)) { |
| 2285 | HandlePragmaPack(); |
| 2286 | continue; |
| 2287 | } |
| 2288 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2289 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 2290 | if (AS != AS_none) { |
| 2291 | // Current token is a C++ access specifier. |
| 2292 | CurAS = AS; |
| 2293 | SourceLocation ASLoc = Tok.getLocation(); |
David Blaikie | 13f8daf | 2011-10-13 06:08:43 +0000 | [diff] [blame] | 2294 | unsigned TokLength = Tok.getLength(); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2295 | ConsumeToken(); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2296 | AccessAttrs.clear(); |
| 2297 | MaybeParseGNUAttributes(AccessAttrs); |
| 2298 | |
David Blaikie | 13f8daf | 2011-10-13 06:08:43 +0000 | [diff] [blame] | 2299 | SourceLocation EndLoc; |
| 2300 | if (Tok.is(tok::colon)) { |
| 2301 | EndLoc = Tok.getLocation(); |
| 2302 | ConsumeToken(); |
| 2303 | } else if (Tok.is(tok::semi)) { |
| 2304 | EndLoc = Tok.getLocation(); |
| 2305 | ConsumeToken(); |
| 2306 | Diag(EndLoc, diag::err_expected_colon) |
| 2307 | << FixItHint::CreateReplacement(EndLoc, ":"); |
| 2308 | } else { |
| 2309 | EndLoc = ASLoc.getLocWithOffset(TokLength); |
| 2310 | Diag(EndLoc, diag::err_expected_colon) |
| 2311 | << FixItHint::CreateInsertion(EndLoc, ":"); |
| 2312 | } |
Erik Verbruggen | c35cba4 | 2011-10-17 09:54:52 +0000 | [diff] [blame] | 2313 | |
| 2314 | if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc, |
| 2315 | AccessAttrs.getList())) { |
| 2316 | // found another attribute than only annotations |
| 2317 | AccessAttrs.clear(); |
| 2318 | } |
| 2319 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2320 | continue; |
| 2321 | } |
| 2322 | |
| 2323 | // FIXME: Make sure we don't have a template here. |
| 2324 | |
| 2325 | // Parse all the comma separated declarators. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2326 | ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2329 | T.consumeClose(); |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2330 | } else { |
| 2331 | SkipUntil(tok::r_brace, false, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2333 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2334 | // If attributes exist after class contents, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2335 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2336 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2337 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2338 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2339 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2340 | T.getOpenLocation(), |
| 2341 | T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2342 | attrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2343 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2344 | // C++0x [class.mem]p2: Within the class member-specification, the class is |
| 2345 | // regarded as complete within function bodies, default arguments, exception- |
| 2346 | // specifications, and brace-or-equal-initializers for non-static data |
| 2347 | // members (including such things in nested classes). |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2348 | // |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2349 | // FIXME: Only function bodies and brace-or-equal-initializers are currently |
| 2350 | // handled. Fix the others! |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 2351 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2352 | // 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] | 2353 | // are complete and we can parse the delayed portions of method |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2354 | // declarations and the lexed inline method definitions, along with any |
| 2355 | // delayed attributes. |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 2356 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 2357 | ParseLexedAttributes(getCurrentClass()); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2358 | ParseLexedMethodDeclarations(getCurrentClass()); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2359 | ParseLexedMemberInitializers(getCurrentClass()); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2360 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 2361 | PrevTokLocation = SavedPrevTokLocation; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 2364 | if (TagDecl) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2365 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2366 | T.getCloseLocation()); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 2367 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2368 | // Leave the class scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2369 | ParsingDef.Pop(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 2370 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 2371 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2372 | |
| 2373 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 2374 | /// which explicitly initializes the members or base classes of a |
| 2375 | /// class (C++ [class.base.init]). For example, the three initializers |
| 2376 | /// after the ':' in the Derived constructor below: |
| 2377 | /// |
| 2378 | /// @code |
| 2379 | /// class Base { }; |
| 2380 | /// class Derived : Base { |
| 2381 | /// int x; |
| 2382 | /// float f; |
| 2383 | /// public: |
| 2384 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 2385 | /// }; |
| 2386 | /// @endcode |
| 2387 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2388 | /// [C++] ctor-initializer: |
| 2389 | /// ':' mem-initializer-list |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2390 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | /// [C++] mem-initializer-list: |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2392 | /// mem-initializer ...[opt] |
| 2393 | /// mem-initializer ...[opt] , mem-initializer-list |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2394 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2395 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 2396 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 2397 | // Poison the SEH identifiers so they are flagged as illegal in constructor initializers |
| 2398 | PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2399 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2401 | SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2402 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2403 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2404 | do { |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 2405 | if (Tok.is(tok::code_completion)) { |
| 2406 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 2407 | MemInitializers.data(), |
| 2408 | MemInitializers.size()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2409 | return cutOffParsing(); |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 2410 | } else { |
| 2411 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 2412 | if (!MemInit.isInvalid()) |
| 2413 | MemInitializers.push_back(MemInit.get()); |
| 2414 | else |
| 2415 | AnyErrors = true; |
| 2416 | } |
| 2417 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2418 | if (Tok.is(tok::comma)) |
| 2419 | ConsumeToken(); |
| 2420 | else if (Tok.is(tok::l_brace)) |
| 2421 | break; |
Douglas Gregor | b1f6fa4 | 2010-09-07 14:35:10 +0000 | [diff] [blame] | 2422 | // If the next token looks like a base or member initializer, assume that |
| 2423 | // we're just missing a comma. |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 2424 | else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { |
| 2425 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 2426 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 2427 | << FixItHint::CreateInsertion(Loc, ", "); |
| 2428 | } else { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2429 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 2430 | Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2431 | SkipUntil(tok::l_brace, true, true); |
| 2432 | break; |
| 2433 | } |
| 2434 | } while (true); |
| 2435 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2437 | MemInitializers.data(), MemInitializers.size(), |
| 2438 | AnyErrors); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 2442 | /// part of a constructor initializer that explicitly initializes one |
| 2443 | /// member or base class (C++ [class.base.init]). See |
| 2444 | /// ParseConstructorInitializer for an example. |
| 2445 | /// |
| 2446 | /// [C++] mem-initializer: |
| 2447 | /// mem-initializer-id '(' expression-list[opt] ')' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2448 | /// [C++0x] mem-initializer-id braced-init-list |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | /// |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2450 | /// [C++] mem-initializer-id: |
| 2451 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 2452 | /// identifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2453 | Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2454 | // parse '::'[opt] nested-name-specifier[opt] |
| 2455 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2456 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2457 | ParsedType TemplateTypeTy; |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2458 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2459 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 2460 | if (TemplateId->Kind == TNK_Type_template || |
| 2461 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2462 | AnnotateTemplateIdTokenAsType(); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2463 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2464 | TemplateTypeTy = getTypeAnnotation(Tok); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2465 | } |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2466 | } |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2467 | // Uses of decltype will already have been converted to annot_decltype by |
| 2468 | // ParseOptionalCXXScopeSpecifier at this point. |
| 2469 | if (!TemplateTypeTy && Tok.isNot(tok::identifier) |
| 2470 | && Tok.isNot(tok::annot_decltype)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2471 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2472 | return true; |
| 2473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2475 | IdentifierInfo *II = 0; |
| 2476 | DeclSpec DS(AttrFactory); |
| 2477 | SourceLocation IdLoc = Tok.getLocation(); |
| 2478 | if (Tok.is(tok::annot_decltype)) { |
| 2479 | // Get the decltype expression, if there is one. |
| 2480 | ParseDecltypeSpecifier(DS); |
| 2481 | } else { |
| 2482 | if (Tok.is(tok::identifier)) |
| 2483 | // Get the identifier. This may be a member name or a class name, |
| 2484 | // but we'll let the semantic analysis determine which it is. |
| 2485 | II = Tok.getIdentifierInfo(); |
| 2486 | ConsumeToken(); |
| 2487 | } |
| 2488 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2489 | |
| 2490 | // Parse the '('. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2491 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2492 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2493 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2494 | ExprResult InitList = ParseBraceInitializer(); |
| 2495 | if (InitList.isInvalid()) |
| 2496 | return true; |
| 2497 | |
| 2498 | SourceLocation EllipsisLoc; |
| 2499 | if (Tok.is(tok::ellipsis)) |
| 2500 | EllipsisLoc = ConsumeToken(); |
| 2501 | |
| 2502 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2503 | TemplateTypeTy, DS, IdLoc, |
| 2504 | InitList.take(), EllipsisLoc); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2505 | } else if(Tok.is(tok::l_paren)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2506 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2507 | T.consumeOpen(); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2508 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2509 | // Parse the optional expression-list. |
| 2510 | ExprVector ArgExprs(Actions); |
| 2511 | CommaLocsTy CommaLocs; |
| 2512 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 2513 | SkipUntil(tok::r_paren); |
| 2514 | return true; |
| 2515 | } |
| 2516 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2517 | T.consumeClose(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2518 | |
| 2519 | SourceLocation EllipsisLoc; |
| 2520 | if (Tok.is(tok::ellipsis)) |
| 2521 | EllipsisLoc = ConsumeToken(); |
| 2522 | |
| 2523 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2524 | TemplateTypeTy, DS, IdLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2525 | T.getOpenLocation(), ArgExprs.take(), |
| 2526 | ArgExprs.size(), T.getCloseLocation(), |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2527 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2530 | Diag(Tok, getLangOpts().CPlusPlus0x ? diag::err_expected_lparen_or_lbrace |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2531 | : diag::err_expected_lparen); |
| 2532 | return true; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2533 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2534 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2535 | /// \brief Parse a C++ exception-specification if present (C++0x [except.spec]). |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2536 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2537 | /// exception-specification: |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2538 | /// dynamic-exception-specification |
| 2539 | /// noexcept-specification |
| 2540 | /// |
| 2541 | /// noexcept-specification: |
| 2542 | /// 'noexcept' |
| 2543 | /// 'noexcept' '(' constant-expression ')' |
| 2544 | ExceptionSpecificationType |
| 2545 | Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2546 | SmallVectorImpl<ParsedType> &DynamicExceptions, |
| 2547 | SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2548 | ExprResult &NoexceptExpr) { |
| 2549 | ExceptionSpecificationType Result = EST_None; |
| 2550 | |
| 2551 | // See if there's a dynamic specification. |
| 2552 | if (Tok.is(tok::kw_throw)) { |
| 2553 | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
| 2554 | DynamicExceptions, |
| 2555 | DynamicExceptionRanges); |
| 2556 | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
| 2557 | "Produced different number of exception types and ranges."); |
| 2558 | } |
| 2559 | |
| 2560 | // If there's no noexcept specification, we're done. |
| 2561 | if (Tok.isNot(tok::kw_noexcept)) |
| 2562 | return Result; |
| 2563 | |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 2564 | Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); |
| 2565 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2566 | // If we already had a dynamic specification, parse the noexcept for, |
| 2567 | // recovery, but emit a diagnostic and don't store the results. |
| 2568 | SourceRange NoexceptRange; |
| 2569 | ExceptionSpecificationType NoexceptType = EST_None; |
| 2570 | |
| 2571 | SourceLocation KeywordLoc = ConsumeToken(); |
| 2572 | if (Tok.is(tok::l_paren)) { |
| 2573 | // There is an argument. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2574 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2575 | T.consumeOpen(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2576 | NoexceptType = EST_ComputedNoexcept; |
| 2577 | NoexceptExpr = ParseConstantExpression(); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2578 | // The argument must be contextually convertible to bool. We use |
| 2579 | // ActOnBooleanCondition for this purpose. |
| 2580 | if (!NoexceptExpr.isInvalid()) |
| 2581 | NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc, |
| 2582 | NoexceptExpr.get()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2583 | T.consumeClose(); |
| 2584 | NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation()); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2585 | } else { |
| 2586 | // There is no argument. |
| 2587 | NoexceptType = EST_BasicNoexcept; |
| 2588 | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
| 2589 | } |
| 2590 | |
| 2591 | if (Result == EST_None) { |
| 2592 | SpecificationRange = NoexceptRange; |
| 2593 | Result = NoexceptType; |
| 2594 | |
| 2595 | // If there's a dynamic specification after a noexcept specification, |
| 2596 | // parse that and ignore the results. |
| 2597 | if (Tok.is(tok::kw_throw)) { |
| 2598 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2599 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
| 2600 | DynamicExceptionRanges); |
| 2601 | } |
| 2602 | } else { |
| 2603 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2604 | } |
| 2605 | |
| 2606 | return Result; |
| 2607 | } |
| 2608 | |
| 2609 | /// ParseDynamicExceptionSpecification - Parse a C++ |
| 2610 | /// dynamic-exception-specification (C++ [except.spec]). |
| 2611 | /// |
| 2612 | /// dynamic-exception-specification: |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2613 | /// 'throw' '(' type-id-list [opt] ')' |
| 2614 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2616 | /// type-id-list: |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2617 | /// type-id ... [opt] |
| 2618 | /// type-id-list ',' type-id ... [opt] |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2619 | /// |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2620 | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
| 2621 | SourceRange &SpecificationRange, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2622 | SmallVectorImpl<ParsedType> &Exceptions, |
| 2623 | SmallVectorImpl<SourceRange> &Ranges) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2624 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2625 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2626 | SpecificationRange.setBegin(ConsumeToken()); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2627 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2628 | if (T.consumeOpen()) { |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2629 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 2630 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2631 | return EST_DynamicNone; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2632 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2634 | // Parse throw(...), a Microsoft extension that means "this function |
| 2635 | // can throw anything". |
| 2636 | if (Tok.is(tok::ellipsis)) { |
| 2637 | SourceLocation EllipsisLoc = ConsumeToken(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2638 | if (!getLangOpts().MicrosoftExt) |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2639 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2640 | T.consumeClose(); |
| 2641 | SpecificationRange.setEnd(T.getCloseLocation()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2642 | return EST_MSAny; |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2645 | // Parse the sequence of type-ids. |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2646 | SourceRange Range; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2647 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2648 | TypeResult Res(ParseTypeName(&Range)); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2649 | |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2650 | if (Tok.is(tok::ellipsis)) { |
| 2651 | // C++0x [temp.variadic]p5: |
| 2652 | // - In a dynamic-exception-specification (15.4); the pattern is a |
| 2653 | // type-id. |
| 2654 | SourceLocation Ellipsis = ConsumeToken(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2655 | Range.setEnd(Ellipsis); |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2656 | if (!Res.isInvalid()) |
| 2657 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
| 2658 | } |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2659 | |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2660 | if (!Res.isInvalid()) { |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2661 | Exceptions.push_back(Res.get()); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2662 | Ranges.push_back(Range); |
| 2663 | } |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2664 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2665 | if (Tok.is(tok::comma)) |
| 2666 | ConsumeToken(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2667 | else |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | } |
| 2670 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2671 | T.consumeClose(); |
| 2672 | SpecificationRange.setEnd(T.getCloseLocation()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2673 | return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2674 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2675 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 2676 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 2677 | /// function declaration. |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 2678 | TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) { |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 2679 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 2680 | |
| 2681 | ConsumeToken(); |
| 2682 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2683 | return ParseTypeName(&Range, Declarator::TrailingReturnContext); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2686 | /// \brief We have just started parsing the definition of a new class, |
| 2687 | /// so push that class onto our stack of classes that is currently |
| 2688 | /// being parsed. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2689 | Sema::ParsingClassState |
| 2690 | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2691 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2692 | "Nested class without outer class"); |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2693 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass)); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2694 | return Actions.PushParsingClass(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | /// \brief Deallocate the given parsed class and all of its nested |
| 2698 | /// classes. |
| 2699 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2700 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 2701 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2702 | delete Class; |
| 2703 | } |
| 2704 | |
| 2705 | /// \brief Pop the top class of the stack of classes that are |
| 2706 | /// currently being parsed. |
| 2707 | /// |
| 2708 | /// This routine should be called when we have finished parsing the |
| 2709 | /// definition of a class, but have not yet popped the Scope |
| 2710 | /// associated with the class's definition. |
| 2711 | /// |
| 2712 | /// \returns true if the class we've popped is a top-level class, |
| 2713 | /// false otherwise. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2714 | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2715 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2717 | Actions.PopParsingClass(state); |
| 2718 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2719 | ParsingClass *Victim = ClassStack.top(); |
| 2720 | ClassStack.pop(); |
| 2721 | if (Victim->TopLevelClass) { |
| 2722 | // Deallocate all of the nested classes of this class, |
| 2723 | // recursively: we don't need to keep any of this information. |
| 2724 | DeallocateParsedClasses(Victim); |
| 2725 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2727 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 2728 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2729 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2730 | // The victim is a nested class, but we will not need to perform |
| 2731 | // any processing after the definition of this class since it has |
| 2732 | // no members whose handling was delayed. Therefore, we can just |
| 2733 | // remove this nested class. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2734 | DeallocateParsedClasses(Victim); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2735 | return; |
| 2736 | } |
| 2737 | |
| 2738 | // This nested class has some members that will need to be processed |
| 2739 | // after the top-level class is completely defined. Therefore, add |
| 2740 | // it to the list of nested classes within its parent. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2741 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2742 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2743 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2744 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2745 | |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 2746 | /// ParseCXX0XAttributeSpecifier - Parse a C++0x attribute-specifier. Currently |
| 2747 | /// only parses standard attributes. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2748 | /// |
| 2749 | /// [C++0x] attribute-specifier: |
| 2750 | /// '[' '[' attribute-list ']' ']' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2751 | /// alignment-specifier |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2752 | /// |
| 2753 | /// [C++0x] attribute-list: |
| 2754 | /// attribute[opt] |
| 2755 | /// attribute-list ',' attribute[opt] |
| 2756 | /// |
| 2757 | /// [C++0x] attribute: |
| 2758 | /// attribute-token attribute-argument-clause[opt] |
| 2759 | /// |
| 2760 | /// [C++0x] attribute-token: |
| 2761 | /// identifier |
| 2762 | /// attribute-scoped-token |
| 2763 | /// |
| 2764 | /// [C++0x] attribute-scoped-token: |
| 2765 | /// attribute-namespace '::' identifier |
| 2766 | /// |
| 2767 | /// [C++0x] attribute-namespace: |
| 2768 | /// identifier |
| 2769 | /// |
| 2770 | /// [C++0x] attribute-argument-clause: |
| 2771 | /// '(' balanced-token-seq ')' |
| 2772 | /// |
| 2773 | /// [C++0x] balanced-token-seq: |
| 2774 | /// balanced-token |
| 2775 | /// balanced-token-seq balanced-token |
| 2776 | /// |
| 2777 | /// [C++0x] balanced-token: |
| 2778 | /// '(' balanced-token-seq ')' |
| 2779 | /// '[' balanced-token-seq ']' |
| 2780 | /// '{' balanced-token-seq '}' |
| 2781 | /// any token but '(', ')', '[', ']', '{', or '}' |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 2782 | void Parser::ParseCXX0XAttributeSpecifier(ParsedAttributes &attrs, |
| 2783 | SourceLocation *endLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2784 | if (Tok.is(tok::kw_alignas)) { |
Richard Smith | 41be673 | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 2785 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2786 | ParseAlignmentSpecifier(attrs, endLoc); |
| 2787 | return; |
| 2788 | } |
| 2789 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2790 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) |
| 2791 | && "Not a C++0x attribute list"); |
| 2792 | |
Richard Smith | 41be673 | 2011-10-14 20:48:27 +0000 | [diff] [blame] | 2793 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute); |
| 2794 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2795 | ConsumeBracket(); |
| 2796 | ConsumeBracket(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2797 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2798 | if (Tok.is(tok::comma)) { |
| 2799 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2800 | ConsumeToken(); |
| 2801 | } |
| 2802 | |
| 2803 | while (Tok.is(tok::identifier) || Tok.is(tok::comma)) { |
| 2804 | // attribute not present |
| 2805 | if (Tok.is(tok::comma)) { |
| 2806 | ConsumeToken(); |
| 2807 | continue; |
| 2808 | } |
| 2809 | |
| 2810 | IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo(); |
| 2811 | SourceLocation ScopeLoc, AttrLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2812 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2813 | // scoped attribute |
| 2814 | if (Tok.is(tok::coloncolon)) { |
| 2815 | ConsumeToken(); |
| 2816 | |
| 2817 | if (!Tok.is(tok::identifier)) { |
| 2818 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2819 | SkipUntil(tok::r_square, tok::comma, true, true); |
| 2820 | continue; |
| 2821 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2822 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2823 | ScopeName = AttrName; |
| 2824 | ScopeLoc = AttrLoc; |
| 2825 | |
| 2826 | AttrName = Tok.getIdentifierInfo(); |
| 2827 | AttrLoc = ConsumeToken(); |
| 2828 | } |
| 2829 | |
| 2830 | bool AttrParsed = false; |
| 2831 | // No scoped names are supported; ideally we could put all non-standard |
| 2832 | // attributes into namespaces. |
| 2833 | if (!ScopeName) { |
| 2834 | switch(AttributeList::getKind(AttrName)) |
| 2835 | { |
| 2836 | // No arguments |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2837 | case AttributeList::AT_carries_dependency: |
Anders Carlsson | 15e14a2 | 2011-01-23 21:33:18 +0000 | [diff] [blame] | 2838 | case AttributeList::AT_noreturn: { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2839 | if (Tok.is(tok::l_paren)) { |
| 2840 | Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments) |
| 2841 | << AttrName->getName(); |
| 2842 | break; |
| 2843 | } |
| 2844 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2845 | attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0, |
| 2846 | SourceLocation(), 0, 0, false, true); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2847 | AttrParsed = true; |
| 2848 | break; |
| 2849 | } |
| 2850 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2851 | // Silence warnings |
| 2852 | default: break; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | // Skip the entire parameter clause, if any |
| 2857 | if (!AttrParsed && Tok.is(tok::l_paren)) { |
| 2858 | ConsumeParen(); |
| 2859 | // SkipUntil maintains the balancedness of tokens. |
| 2860 | SkipUntil(tok::r_paren, false); |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2865 | SkipUntil(tok::r_square, false); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 2866 | if (endLoc) |
| 2867 | *endLoc = Tok.getLocation(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2868 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2869 | SkipUntil(tok::r_square, false); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 2870 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2871 | |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 2872 | /// ParseCXX0XAttributes - Parse a C++0x attribute-specifier-seq. |
| 2873 | /// |
| 2874 | /// attribute-specifier-seq: |
| 2875 | /// attribute-specifier-seq[opt] attribute-specifier |
| 2876 | void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs, |
| 2877 | SourceLocation *endLoc) { |
| 2878 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
| 2879 | if (!endLoc) |
| 2880 | endLoc = &Loc; |
| 2881 | |
Douglas Gregor | 8828ee7 | 2011-10-07 20:35:25 +0000 | [diff] [blame] | 2882 | do { |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 2883 | ParseCXX0XAttributeSpecifier(attrs, endLoc); |
Douglas Gregor | 8828ee7 | 2011-10-07 20:35:25 +0000 | [diff] [blame] | 2884 | } while (isCXX0XAttributeSpecifier()); |
Peter Collingbourne | 3497fdf | 2011-09-29 18:04:05 +0000 | [diff] [blame] | 2885 | |
| 2886 | attrs.Range = SourceRange(StartLoc, *endLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2889 | /// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr] |
| 2890 | /// |
| 2891 | /// [MS] ms-attribute: |
| 2892 | /// '[' token-seq ']' |
| 2893 | /// |
| 2894 | /// [MS] ms-attribute-seq: |
| 2895 | /// ms-attribute[opt] |
| 2896 | /// ms-attribute ms-attribute-seq |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2897 | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
| 2898 | SourceLocation *endLoc) { |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2899 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 2900 | |
| 2901 | while (Tok.is(tok::l_square)) { |
| 2902 | ConsumeBracket(); |
| 2903 | SkipUntil(tok::r_square, true, true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2904 | if (endLoc) *endLoc = Tok.getLocation(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2905 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); |
| 2906 | } |
| 2907 | } |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2908 | |
| 2909 | void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType, |
| 2910 | AccessSpecifier& CurAS) { |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2911 | IfExistsCondition Result; |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2912 | if (ParseMicrosoftIfExistsCondition(Result)) |
| 2913 | return; |
| 2914 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2915 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 2916 | if (Braces.consumeOpen()) { |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2917 | Diag(Tok, diag::err_expected_lbrace); |
| 2918 | return; |
| 2919 | } |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2920 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2921 | switch (Result.Behavior) { |
| 2922 | case IEB_Parse: |
| 2923 | // Parse the declarations below. |
| 2924 | break; |
| 2925 | |
| 2926 | case IEB_Dependent: |
| 2927 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
| 2928 | << Result.IsIfExists; |
| 2929 | // Fall through to skip. |
| 2930 | |
| 2931 | case IEB_Skip: |
| 2932 | Braces.skipToEnd(); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2933 | return; |
| 2934 | } |
| 2935 | |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2936 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2937 | // __if_exists, __if_not_exists can nest. |
| 2938 | if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) { |
| 2939 | ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS); |
| 2940 | continue; |
| 2941 | } |
| 2942 | |
| 2943 | // Check for extraneous top-level semicolon. |
| 2944 | if (Tok.is(tok::semi)) { |
| 2945 | Diag(Tok, diag::ext_extra_struct_semi) |
| 2946 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
| 2947 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2948 | ConsumeToken(); |
| 2949 | continue; |
| 2950 | } |
| 2951 | |
| 2952 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 2953 | if (AS != AS_none) { |
| 2954 | // Current token is a C++ access specifier. |
| 2955 | CurAS = AS; |
| 2956 | SourceLocation ASLoc = Tok.getLocation(); |
| 2957 | ConsumeToken(); |
| 2958 | if (Tok.is(tok::colon)) |
| 2959 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation()); |
| 2960 | else |
| 2961 | Diag(Tok, diag::err_expected_colon); |
| 2962 | ConsumeToken(); |
| 2963 | continue; |
| 2964 | } |
| 2965 | |
| 2966 | // Parse all the comma separated declarators. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2967 | ParseCXXClassMemberDeclaration(CurAS, 0); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2968 | } |
Douglas Gregor | 3896fc5 | 2011-10-24 22:31:10 +0000 | [diff] [blame] | 2969 | |
| 2970 | Braces.consumeClose(); |
Francois Pichet | 563a645 | 2011-05-25 10:19:49 +0000 | [diff] [blame] | 2971 | } |