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