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