Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1 | //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anders Carlsson | 0c6139d | 2009-06-27 00:27:47 +0000 | [diff] [blame] | 14 | #include "clang/Basic/OperatorKinds.h" |
Douglas Gregor | 1b7f898 | 2008-04-14 00:13:42 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 16 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" |
| 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 20 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 21 | #include "RAIIObjectsForParser.h" |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | /// ParseNamespace - We know that the current token is a namespace keyword. This |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 25 | /// may either be a top level namespace or a block-level namespace alias. If |
| 26 | /// there was an inline keyword, it has already been parsed. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 27 | /// |
| 28 | /// namespace-definition: [C++ 7.3: basic.namespace] |
| 29 | /// named-namespace-definition |
| 30 | /// unnamed-namespace-definition |
| 31 | /// |
| 32 | /// unnamed-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 33 | /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// named-namespace-definition: |
| 36 | /// original-namespace-definition |
| 37 | /// extension-namespace-definition |
| 38 | /// |
| 39 | /// original-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 40 | /// 'inline'[opt] 'namespace' identifier attributes[opt] |
| 41 | /// '{' namespace-body '}' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 42 | /// |
| 43 | /// extension-namespace-definition: |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 44 | /// 'inline'[opt] 'namespace' original-namespace-name |
| 45 | /// '{' namespace-body '}' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | /// |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 47 | /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] |
| 48 | /// 'namespace' identifier '=' qualified-namespace-specifier ';' |
| 49 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 50 | Decl *Parser::ParseNamespace(unsigned Context, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 51 | SourceLocation &DeclEnd, |
| 52 | SourceLocation InlineLoc) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 53 | assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 54 | SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 56 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 57 | Actions.CodeCompleteNamespaceDecl(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 58 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 59 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 61 | SourceLocation IdentLoc; |
| 62 | IdentifierInfo *Ident = 0; |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 63 | |
| 64 | Token attrTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 66 | if (Tok.is(tok::identifier)) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 67 | Ident = Tok.getIdentifierInfo(); |
| 68 | IdentLoc = ConsumeToken(); // eat the identifier. |
| 69 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 71 | // Read label attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 72 | ParsedAttributes attrs; |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 73 | if (Tok.is(tok::kw___attribute)) { |
| 74 | attrTok = Tok; |
| 75 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 76 | // FIXME: save these somewhere. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 77 | ParseGNUAttributes(attrs); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 78 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 80 | if (Tok.is(tok::equal)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 81 | if (!attrs.empty()) |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 82 | Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 83 | if (InlineLoc.isValid()) |
| 84 | Diag(InlineLoc, diag::err_inline_namespace_alias) |
| 85 | << FixItHint::CreateRemoval(InlineLoc); |
Douglas Gregor | 6a588dd | 2009-06-17 19:49:00 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 87 | return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); |
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 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 90 | if (Tok.isNot(tok::l_brace)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | Diag(Tok, Ident ? diag::err_expected_lbrace : |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 92 | diag::err_expected_ident_lbrace); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 93 | return 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 94 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 96 | SourceLocation LBrace = ConsumeBrace(); |
| 97 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 98 | if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || |
| 99 | getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || |
| 100 | getCurScope()->getFnParent()) { |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 101 | Diag(LBrace, diag::err_namespace_nonnamespace_scope); |
| 102 | SkipUntil(tok::r_brace, false); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 103 | return 0; |
Douglas Gregor | 95f1b15 | 2010-05-14 05:08:22 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 106 | // If we're still good, complain about inline namespaces in non-C++0x now. |
| 107 | if (!getLang().CPlusPlus0x && InlineLoc.isValid()) |
| 108 | Diag(InlineLoc, diag::ext_inline_namespace); |
| 109 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 110 | // Enter a scope for the namespace. |
| 111 | ParseScope NamespaceScope(this, Scope::DeclScope); |
| 112 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 113 | Decl *NamespcDecl = |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 114 | Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc, |
| 115 | IdentLoc, Ident, LBrace, attrs.getList()); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 116 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 117 | PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc, |
| 118 | "parsing namespace"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 120 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 121 | ParsedAttributesWithRange attrs; |
| 122 | MaybeParseCXX0XAttributes(attrs); |
| 123 | MaybeParseMicrosoftAttributes(attrs); |
| 124 | ParseExternalDeclaration(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 127 | // Leave the namespace scope. |
| 128 | NamespaceScope.Exit(); |
| 129 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 130 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace); |
| 131 | Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc); |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 133 | DeclEnd = RBraceLoc; |
Chris Lattner | 5144832 | 2009-03-29 14:02:43 +0000 | [diff] [blame] | 134 | return NamespcDecl; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 136 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 137 | /// ParseNamespaceAlias - Parse the part after the '=' in a namespace |
| 138 | /// alias definition. |
| 139 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 140 | Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | SourceLocation AliasLoc, |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 142 | IdentifierInfo *Alias, |
| 143 | SourceLocation &DeclEnd) { |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 144 | assert(Tok.is(tok::equal) && "Not equal token"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 146 | ConsumeToken(); // eat the '='. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 148 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 149 | Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 150 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 151 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 152 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 153 | CXXScopeSpec SS; |
| 154 | // Parse (optional) nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 155 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 156 | |
| 157 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
| 158 | Diag(Tok, diag::err_expected_namespace_name); |
| 159 | // Skip to end of the definition and eat the ';'. |
| 160 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 161 | return 0; |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Parse identifier. |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 165 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 166 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 168 | // Eat the ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 169 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 170 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, |
| 171 | "", tok::semi); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 173 | return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 174 | SS, IdentLoc, Ident); |
Anders Carlsson | f67606a | 2009-03-28 04:07:16 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 177 | /// ParseLinkage - We know that the current token is a string_literal |
| 178 | /// and just before that, that extern was seen. |
| 179 | /// |
| 180 | /// linkage-specification: [C++ 7.5p2: dcl.link] |
| 181 | /// 'extern' string-literal '{' declaration-seq[opt] '}' |
| 182 | /// 'extern' string-literal declaration |
| 183 | /// |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 184 | Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) { |
Douglas Gregor | c19923d | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 185 | assert(Tok.is(tok::string_literal) && "Not a string literal!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 186 | llvm::SmallString<8> LangBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 187 | bool Invalid = false; |
| 188 | llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid); |
| 189 | if (Invalid) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 190 | return 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 191 | |
| 192 | SourceLocation Loc = ConsumeStringToken(); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 194 | ParseScope LinkageScope(this, Scope::DeclScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 195 | Decl *LinkageSpec |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 196 | = Actions.ActOnStartLinkageSpecification(getCurScope(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame^] | 197 | DS.getSourceRange().getBegin(), |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 198 | Loc, Lang, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame^] | 199 | Tok.is(tok::l_brace) ? Tok.getLocation() |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 200 | : SourceLocation()); |
| 201 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 202 | ParsedAttributesWithRange attrs; |
| 203 | MaybeParseCXX0XAttributes(attrs); |
| 204 | MaybeParseMicrosoftAttributes(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 205 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 206 | if (Tok.isNot(tok::l_brace)) { |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 207 | DS.setExternInLinkageSpec(true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 208 | ParseExternalDeclaration(attrs, &DS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 209 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 210 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | } |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | 63a0113 | 2010-02-07 08:38:28 +0000 | [diff] [blame] | 213 | DS.abort(); |
| 214 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 215 | ProhibitAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 217 | SourceLocation LBrace = ConsumeBrace(); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 218 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 219 | ParsedAttributesWithRange attrs; |
| 220 | MaybeParseCXX0XAttributes(attrs); |
| 221 | MaybeParseMicrosoftAttributes(attrs); |
| 222 | ParseExternalDeclaration(attrs); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 225 | SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 226 | return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec, |
| 227 | RBrace); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 228 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 230 | /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or |
| 231 | /// using-directive. Assumes that current token is 'using'. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 232 | Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 233 | const ParsedTemplateInfo &TemplateInfo, |
| 234 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 235 | ParsedAttributesWithRange &attrs) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 236 | assert(Tok.is(tok::kw_using) && "Not using token"); |
| 237 | |
| 238 | // Eat 'using'. |
| 239 | SourceLocation UsingLoc = ConsumeToken(); |
| 240 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 241 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 242 | Actions.CodeCompleteUsing(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 243 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 244 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 245 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 246 | // 'using namespace' means this is a using-directive. |
| 247 | if (Tok.is(tok::kw_namespace)) { |
| 248 | // Template parameters are always an error here. |
| 249 | if (TemplateInfo.Kind) { |
| 250 | SourceRange R = TemplateInfo.getSourceRange(); |
| 251 | Diag(UsingLoc, diag::err_templated_using_directive) |
| 252 | << R << FixItHint::CreateRemoval(R); |
| 253 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 254 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 255 | return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Otherwise, it must be a using-declaration. |
| 259 | |
| 260 | // Using declarations can't have attributes. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 261 | ProhibitAttributes(attrs); |
Chris Lattner | 2f27477 | 2009-01-06 06:55:51 +0000 | [diff] [blame] | 262 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 263 | return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /// ParseUsingDirective - Parse C++ using-directive, assumes |
| 267 | /// that current token is 'namespace' and 'using' was already parsed. |
| 268 | /// |
| 269 | /// using-directive: [C++ 7.3.p4: namespace.udir] |
| 270 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 271 | /// namespace-name ; |
| 272 | /// [GNU] using-directive: |
| 273 | /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] |
| 274 | /// namespace-name attributes[opt] ; |
| 275 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 276 | Decl *Parser::ParseUsingDirective(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 277 | SourceLocation UsingLoc, |
| 278 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 279 | ParsedAttributes &attrs) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 280 | assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); |
| 281 | |
| 282 | // Eat 'namespace'. |
| 283 | SourceLocation NamespcLoc = ConsumeToken(); |
| 284 | |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 285 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 286 | Actions.CodeCompleteUsingDirective(getCurScope()); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 287 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 49f40bd | 2009-09-18 19:03:04 +0000 | [diff] [blame] | 288 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 289 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 290 | CXXScopeSpec SS; |
| 291 | // Parse (optional) nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 292 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 293 | |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 294 | IdentifierInfo *NamespcName = 0; |
| 295 | SourceLocation IdentLoc = SourceLocation(); |
| 296 | |
| 297 | // Parse namespace-name. |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 298 | if (SS.isInvalid() || Tok.isNot(tok::identifier)) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 299 | Diag(Tok, diag::err_expected_namespace_name); |
| 300 | // If there was invalid namespace name, skip to end of decl, and eat ';'. |
| 301 | SkipUntil(tok::semi); |
| 302 | // FIXME: Are there cases, when we would like to call ActOnUsingDirective? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 303 | return 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 306 | // Parse identifier. |
| 307 | NamespcName = Tok.getIdentifierInfo(); |
| 308 | IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 310 | // Parse (optional) attributes (most likely GNU strong-using extension). |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 311 | bool GNUAttr = false; |
| 312 | if (Tok.is(tok::kw___attribute)) { |
| 313 | GNUAttr = true; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 314 | ParseGNUAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 823c44e | 2009-01-06 07:27:21 +0000 | [diff] [blame] | 317 | // Eat ';'. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 318 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 6869d8e | 2009-06-14 00:07:48 +0000 | [diff] [blame] | 319 | ExpectAndConsume(tok::semi, |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 320 | GNUAttr ? diag::err_expected_semi_after_attribute_list |
| 321 | : diag::err_expected_semi_after_namespace_name, |
| 322 | "", tok::semi); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 323 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 324 | return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 325 | IdentLoc, NamespcName, attrs.getList()); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that |
| 329 | /// 'using' was already seen. |
| 330 | /// |
| 331 | /// using-declaration: [C++ 7.3.p3: namespace.udecl] |
| 332 | /// 'using' 'typename'[opt] ::[opt] nested-name-specifier |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 333 | /// unqualified-id |
| 334 | /// 'using' :: unqualified-id |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 335 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 336 | Decl *Parser::ParseUsingDeclaration(unsigned Context, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 337 | const ParsedTemplateInfo &TemplateInfo, |
| 338 | SourceLocation UsingLoc, |
| 339 | SourceLocation &DeclEnd, |
| 340 | AccessSpecifier AS) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 341 | CXXScopeSpec SS; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 342 | SourceLocation TypenameLoc; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 343 | bool IsTypeName; |
| 344 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 345 | // TODO: in C++0x, if we have template parameters this must be a |
| 346 | // template alias: |
| 347 | // template <...> using id = type; |
| 348 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 349 | // Ignore optional 'typename'. |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 350 | // FIXME: This is wrong; we should parse this as a typename-specifier. |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 351 | if (Tok.is(tok::kw_typename)) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 352 | TypenameLoc = Tok.getLocation(); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 353 | ConsumeToken(); |
| 354 | IsTypeName = true; |
| 355 | } |
| 356 | else |
| 357 | IsTypeName = false; |
| 358 | |
| 359 | // Parse nested-name-specifier. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 360 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 361 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 362 | // Check nested-name specifier. |
| 363 | if (SS.isInvalid()) { |
| 364 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 365 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 366 | } |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 367 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 368 | // Parse the unqualified-id. We allow parsing of both constructor and |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 369 | // destructor names and allow the action module to diagnose any semantic |
| 370 | // errors. |
| 371 | UnqualifiedId Name; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 372 | if (ParseUnqualifiedId(SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 373 | /*EnteringContext=*/false, |
| 374 | /*AllowDestructorName=*/true, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 375 | /*AllowConstructorName=*/true, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 376 | ParsedType(), |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 377 | Name)) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 378 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 379 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 380 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 382 | // Parse (optional) attributes (most likely GNU strong-using extension). |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 383 | ParsedAttributes attrs; |
| 384 | MaybeParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 386 | // Eat ';'. |
| 387 | DeclEnd = Tok.getLocation(); |
| 388 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 389 | !attrs.empty() ? "attributes list" : "using declaration", |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 390 | tok::semi); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 391 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 392 | // Diagnose an attempt to declare a templated using-declaration. |
| 393 | if (TemplateInfo.Kind) { |
| 394 | SourceRange R = TemplateInfo.getSourceRange(); |
| 395 | Diag(UsingLoc, diag::err_templated_using_declaration) |
| 396 | << R << FixItHint::CreateRemoval(R); |
| 397 | |
| 398 | // Unfortunately, we have to bail out instead of recovering by |
| 399 | // ignoring the parameters, just in case the nested name specifier |
| 400 | // depends on the parameters. |
| 401 | return 0; |
| 402 | } |
| 403 | |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 404 | return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 405 | Name, attrs.getList(), |
| 406 | IsTypeName, TypenameLoc); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 409 | /// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion. |
| 410 | /// |
| 411 | /// static_assert-declaration: |
| 412 | /// static_assert ( constant-expression , string-literal ) ; |
| 413 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 414 | Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 415 | assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration"); |
| 416 | SourceLocation StaticAssertLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 418 | if (Tok.isNot(tok::l_paren)) { |
| 419 | Diag(Tok, diag::err_expected_lparen); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 420 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 423 | SourceLocation LParenLoc = ConsumeParen(); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 424 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 425 | ExprResult AssertExpr(ParseConstantExpression()); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 426 | if (AssertExpr.isInvalid()) { |
| 427 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 428 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 431 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 432 | return 0; |
Anders Carlsson | ad5f960 | 2009-03-13 23:29:20 +0000 | [diff] [blame] | 433 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 434 | if (Tok.isNot(tok::string_literal)) { |
| 435 | Diag(Tok, diag::err_expected_string_literal); |
| 436 | SkipUntil(tok::semi); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 437 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 440 | ExprResult AssertMessage(ParseStringLiteralExpression()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | if (AssertMessage.isInvalid()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 442 | return 0; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 443 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame^] | 444 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 446 | DeclEnd = Tok.getLocation(); |
Douglas Gregor | 9ba23b4 | 2010-09-07 15:23:11 +0000 | [diff] [blame] | 447 | ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 448 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 449 | return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, |
| 450 | AssertExpr.take(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame^] | 451 | AssertMessage.take(), |
| 452 | RParenLoc); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 455 | /// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. |
| 456 | /// |
| 457 | /// 'decltype' ( expression ) |
| 458 | /// |
| 459 | void Parser::ParseDecltypeSpecifier(DeclSpec &DS) { |
| 460 | assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier"); |
| 461 | |
| 462 | SourceLocation StartLoc = ConsumeToken(); |
| 463 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
| 465 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 466 | "decltype")) { |
| 467 | SkipUntil(tok::r_paren); |
| 468 | return; |
| 469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 471 | // Parse the expression |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 473 | // C++0x [dcl.type.simple]p4: |
| 474 | // The operand of the decltype specifier is an unevaluated operand. |
| 475 | EnterExpressionEvaluationContext Unevaluated(Actions, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 476 | Sema::Unevaluated); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 477 | ExprResult Result = ParseExpression(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 478 | if (Result.isInvalid()) { |
| 479 | SkipUntil(tok::r_paren); |
| 480 | return; |
| 481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 483 | // Match the ')' |
| 484 | SourceLocation RParenLoc; |
| 485 | if (Tok.is(tok::r_paren)) |
| 486 | RParenLoc = ConsumeParen(); |
| 487 | else |
| 488 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 490 | if (RParenLoc.isInvalid()) |
| 491 | return; |
| 492 | |
| 493 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 494 | unsigned DiagID; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 495 | // Check for duplicate type specifiers (e.g. "int decltype(a)"). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 497 | DiagID, Result.release())) |
| 498 | Diag(StartLoc, DiagID) << PrevSpec; |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 501 | /// ParseClassName - Parse a C++ class-name, which names a class. Note |
| 502 | /// that we only check that the result names a type; semantic analysis |
| 503 | /// will need to verify that the type names a class. The result is |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 504 | /// either a type or NULL, depending on whether a type name was |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 505 | /// found. |
| 506 | /// |
| 507 | /// class-name: [C++ 9.1] |
| 508 | /// identifier |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 509 | /// simple-template-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | /// |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 511 | Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 512 | CXXScopeSpec &SS) { |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 513 | // Check whether we have a template-id that names a type. |
| 514 | if (Tok.is(tok::annot_template_id)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 516 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 517 | if (TemplateId->Kind == TNK_Type_template || |
| 518 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 519 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 520 | |
| 521 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 522 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 523 | EndLocation = Tok.getAnnotationEndLoc(); |
| 524 | ConsumeToken(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 525 | |
| 526 | if (Type) |
| 527 | return Type; |
| 528 | return true; |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // Fall through to produce an error below. |
| 532 | } |
| 533 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 534 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 535 | Diag(Tok, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 536 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 539 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 540 | SourceLocation IdLoc = ConsumeToken(); |
| 541 | |
| 542 | if (Tok.is(tok::less)) { |
| 543 | // It looks the user intended to write a template-id here, but the |
| 544 | // template-name was wrong. Try to fix that. |
| 545 | TemplateNameKind TNK = TNK_Type_template; |
| 546 | TemplateTy Template; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 547 | if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 548 | &SS, Template, TNK)) { |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 549 | Diag(IdLoc, diag::err_unknown_template_name) |
| 550 | << Id; |
| 551 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 553 | if (!Template) |
| 554 | return true; |
| 555 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 556 | // Form the template name |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 557 | UnqualifiedId TemplateName; |
| 558 | TemplateName.setIdentifier(Id, IdLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 560 | // Parse the full template-id, then turn it into a type. |
| 561 | if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName, |
| 562 | SourceLocation(), true)) |
| 563 | return true; |
| 564 | if (TNK == TNK_Dependent_template_name) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 565 | AnnotateTemplateIdTokenAsType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 567 | // If we didn't end up with a typename token, there's nothing more we |
| 568 | // can do. |
| 569 | if (Tok.isNot(tok::annot_typename)) |
| 570 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 571 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 572 | // Retrieve the type from the annotation token, consume that token, and |
| 573 | // return. |
| 574 | EndLocation = Tok.getAnnotationEndLoc(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 575 | ParsedType Type = getTypeAnnotation(Tok); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 576 | ConsumeToken(); |
| 577 | return Type; |
| 578 | } |
| 579 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 580 | // We have an identifier; check whether it is actually a type. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 581 | ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 582 | false, ParsedType(), |
| 583 | /*NonTrivialTypeSourceInfo=*/true); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 584 | if (!Type) { |
Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 585 | Diag(IdLoc, diag::err_expected_class_name); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 586 | return true; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Consume the identifier. |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 590 | EndLocation = IdLoc; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 591 | |
| 592 | // Fake up a Declarator to use with ActOnTypeName. |
| 593 | DeclSpec DS; |
| 594 | DS.SetRangeStart(IdLoc); |
| 595 | DS.SetRangeEnd(EndLocation); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 596 | DS.getTypeSpecScope() = SS; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 597 | |
| 598 | const char *PrevSpec = 0; |
| 599 | unsigned DiagID; |
| 600 | DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type); |
| 601 | |
| 602 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 603 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 606 | /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or |
| 607 | /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which |
| 608 | /// until we reach the start of a definition or see a token that |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 609 | /// cannot start a definition. If SuppressDeclarations is true, we do know. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 610 | /// |
| 611 | /// class-specifier: [C++ class] |
| 612 | /// class-head '{' member-specification[opt] '}' |
| 613 | /// class-head '{' member-specification[opt] '}' attributes[opt] |
| 614 | /// class-head: |
| 615 | /// class-key identifier[opt] base-clause[opt] |
| 616 | /// class-key nested-name-specifier identifier base-clause[opt] |
| 617 | /// class-key nested-name-specifier[opt] simple-template-id |
| 618 | /// base-clause[opt] |
| 619 | /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | /// [GNU] class-key attributes[opt] nested-name-specifier |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 621 | /// identifier base-clause[opt] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | /// [GNU] class-key attributes[opt] nested-name-specifier[opt] |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 623 | /// simple-template-id base-clause[opt] |
| 624 | /// class-key: |
| 625 | /// 'class' |
| 626 | /// 'struct' |
| 627 | /// 'union' |
| 628 | /// |
| 629 | /// elaborated-type-specifier: [C++ dcl.type.elab] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 630 | /// class-key ::[opt] nested-name-specifier[opt] identifier |
| 631 | /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] |
| 632 | /// simple-template-id |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 633 | /// |
| 634 | /// Note that the C++ class-specifier and elaborated-type-specifier, |
| 635 | /// together, subsume the C99 struct-or-union-specifier: |
| 636 | /// |
| 637 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 638 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 639 | /// struct-or-union identifier |
| 640 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 641 | /// '}' attributes[opt] |
| 642 | /// [GNU] struct-or-union attributes[opt] identifier |
| 643 | /// struct-or-union: |
| 644 | /// 'struct' |
| 645 | /// 'union' |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 646 | void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, |
| 647 | SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 648 | const ParsedTemplateInfo &TemplateInfo, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 649 | AccessSpecifier AS, bool SuppressDeclarations){ |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 650 | DeclSpec::TST TagType; |
| 651 | if (TagTokKind == tok::kw_struct) |
| 652 | TagType = DeclSpec::TST_struct; |
| 653 | else if (TagTokKind == tok::kw_class) |
| 654 | TagType = DeclSpec::TST_class; |
| 655 | else { |
| 656 | assert(TagTokKind == tok::kw_union && "Not a class specifier"); |
| 657 | TagType = DeclSpec::TST_union; |
| 658 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 659 | |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 660 | if (Tok.is(tok::code_completion)) { |
| 661 | // Code completion for a struct, class, or union name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 662 | Actions.CodeCompleteTag(getCurScope(), TagType); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 663 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 664 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 665 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 666 | // C++03 [temp.explicit] 14.7.2/8: |
| 667 | // The usual access checking rules do not apply to names used to specify |
| 668 | // explicit instantiations. |
| 669 | // |
| 670 | // As an extension we do not perform access checking on the names used to |
| 671 | // specify explicit specializations either. This is important to allow |
| 672 | // specializing traits classes for private types. |
| 673 | bool SuppressingAccessChecks = false; |
| 674 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 675 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) { |
| 676 | Actions.ActOnStartSuppressingAccessChecks(); |
| 677 | SuppressingAccessChecks = true; |
| 678 | } |
| 679 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 680 | ParsedAttributes attrs; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 681 | // If attributes exist after tag, parse them. |
| 682 | if (Tok.is(tok::kw___attribute)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 683 | ParseGNUAttributes(attrs); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 684 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 685 | // If declspecs exist after tag, parse them. |
John McCall | b1d397c | 2010-08-05 17:13:11 +0000 | [diff] [blame] | 686 | while (Tok.is(tok::kw___declspec)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 687 | ParseMicrosoftDeclSpec(attrs); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 688 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 689 | // If C++0x attributes exist here, parse them. |
| 690 | // FIXME: Are we consistent with the ordering of parsing of different |
| 691 | // styles of attributes? |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 692 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 694 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) { |
| 695 | // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but |
| 696 | // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | // token sequence "struct __is_pod", make __is_pod into a normal |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 698 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 699 | // properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 700 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 701 | Tok.setKind(tok::identifier); |
| 702 | } |
| 703 | |
| 704 | if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) { |
| 705 | // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but |
| 706 | // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | // token sequence "struct __is_empty", make __is_empty into a normal |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 708 | // identifier rather than a keyword, to allow libstdc++ 4.2 to work |
| 709 | // properly. |
Argyrios Kyrtzidis | 646395b | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 710 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
Douglas Gregor | b117a60 | 2009-09-04 05:53:02 +0000 | [diff] [blame] | 711 | Tok.setKind(tok::identifier); |
| 712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 714 | // Parse the (optional) nested-name-specifier. |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 715 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 716 | if (getLang().CPlusPlus) { |
| 717 | // "FOO : BAR" is not a potential typo for "FOO::BAR". |
| 718 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 719 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 720 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 721 | DS.SetTypeSpecError(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 722 | if (SS.isSet()) |
Chris Lattner | 08d92ec | 2009-12-10 00:32:41 +0000 | [diff] [blame] | 723 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) |
| 724 | Diag(Tok, diag::err_expected_ident); |
| 725 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 727 | TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; |
| 728 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 729 | // Parse the (optional) class name or simple-template-id. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 730 | IdentifierInfo *Name = 0; |
| 731 | SourceLocation NameLoc; |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 732 | TemplateIdAnnotation *TemplateId = 0; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 733 | if (Tok.is(tok::identifier)) { |
| 734 | Name = Tok.getIdentifierInfo(); |
| 735 | NameLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | 5ee3734 | 2010-05-30 22:30:21 +0000 | [diff] [blame] | 737 | if (Tok.is(tok::less) && getLang().CPlusPlus) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 738 | // The name was supposed to refer to a template, but didn't. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 739 | // Eat the template argument list and try to continue parsing this as |
| 740 | // a class (or template thereof). |
| 741 | TemplateArgList TemplateArgs; |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 742 | SourceLocation LAngleLoc, RAngleLoc; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 743 | if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS, |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 744 | true, LAngleLoc, |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 745 | TemplateArgs, RAngleLoc)) { |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 746 | // We couldn't parse the template argument list at all, so don't |
| 747 | // try to give any location information for the list. |
| 748 | LAngleLoc = RAngleLoc = SourceLocation(); |
| 749 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 751 | Diag(NameLoc, diag::err_explicit_spec_non_template) |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 752 | << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 753 | << (TagType == DeclSpec::TST_class? 0 |
| 754 | : TagType == DeclSpec::TST_struct? 1 |
| 755 | : 2) |
| 756 | << Name |
| 757 | << SourceRange(LAngleLoc, RAngleLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 758 | |
| 759 | // Strip off the last template parameter list if it was empty, since |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 760 | // we've removed its template argument list. |
| 761 | if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { |
| 762 | if (TemplateParams && TemplateParams->size() > 1) { |
| 763 | TemplateParams->pop_back(); |
| 764 | } else { |
| 765 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 766 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 767 | = ParsedTemplateInfo::NonTemplate; |
| 768 | } |
| 769 | } else if (TemplateInfo.Kind |
| 770 | == ParsedTemplateInfo::ExplicitInstantiation) { |
| 771 | // Pretend this is just a forward declaration. |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 772 | TemplateParams = 0; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 773 | const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 774 | = ParsedTemplateInfo::NonTemplate; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 775 | const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc |
Douglas Gregor | c78c06d | 2009-10-30 22:09:44 +0000 | [diff] [blame] | 776 | = SourceLocation(); |
| 777 | const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc |
| 778 | = SourceLocation(); |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 779 | } |
Douglas Gregor | 2cc782f | 2009-10-30 21:46:58 +0000 | [diff] [blame] | 780 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 781 | } else if (Tok.is(tok::annot_template_id)) { |
| 782 | TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 783 | NameLoc = ConsumeToken(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 784 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 785 | if (TemplateId->Kind != TNK_Type_template && |
| 786 | TemplateId->Kind != TNK_Dependent_template_name) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 787 | // The template-name in the simple-template-id refers to |
| 788 | // something other than a class template. Give an appropriate |
| 789 | // error message and skip to the ';'. |
| 790 | SourceRange Range(NameLoc); |
| 791 | if (SS.isNotEmpty()) |
| 792 | Range.setBegin(SS.getBeginLoc()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 794 | Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) |
| 795 | << Name << static_cast<int>(TemplateId->Kind) << Range; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 797 | DS.SetTypeSpecError(); |
| 798 | SkipUntil(tok::semi, false, true); |
| 799 | TemplateId->Destroy(); |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 800 | if (SuppressingAccessChecks) |
| 801 | Actions.ActOnStopSuppressingAccessChecks(); |
| 802 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 803 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 804 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 807 | // As soon as we're finished parsing the class's template-id, turn access |
| 808 | // checking back on. |
| 809 | if (SuppressingAccessChecks) |
| 810 | Actions.ActOnStopSuppressingAccessChecks(); |
| 811 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 812 | // There are four options here. If we have 'struct foo;', then this |
| 813 | // is either a forward declaration or a friend declaration, which |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 814 | // have to be treated differently. If we have 'struct foo {...', |
| 815 | // 'struct foo :...' or 'struct foo <class-virt-specifier>' then this is a |
| 816 | // definition. Otherwise we have something like 'struct foo xyz', a reference. |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 817 | // However, in some contexts, things look like declarations but are just |
| 818 | // references, e.g. |
| 819 | // new struct s; |
| 820 | // or |
| 821 | // &T::operator struct s; |
| 822 | // For these, SuppressDeclarations is true. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 823 | Sema::TagUseKind TUK; |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 824 | if (SuppressDeclarations) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 825 | TUK = Sema::TUK_Reference; |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 826 | else if (Tok.is(tok::l_brace) || |
| 827 | (getLang().CPlusPlus && Tok.is(tok::colon)) || |
| 828 | isCXX0XClassVirtSpecifier() != ClassVirtSpecifiers::CVS_None) { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 829 | if (DS.isFriendSpecified()) { |
| 830 | // C++ [class.friend]p2: |
| 831 | // A class shall not be defined in a friend declaration. |
| 832 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_class) |
| 833 | << SourceRange(DS.getFriendSpecLoc()); |
| 834 | |
| 835 | // Skip everything up to the semicolon, so that this looks like a proper |
| 836 | // friend class (or template thereof) declaration. |
| 837 | SkipUntil(tok::semi, true, true); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 838 | TUK = Sema::TUK_Friend; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 839 | } else { |
| 840 | // Okay, this is a class definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 841 | TUK = Sema::TUK_Definition; |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 842 | } |
| 843 | } else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 844 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 845 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 846 | TUK = Sema::TUK_Reference; |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 847 | |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 848 | if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error || |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 849 | TUK != Sema::TUK_Definition)) { |
John McCall | 207014e | 2010-07-30 06:26:29 +0000 | [diff] [blame] | 850 | if (DS.getTypeSpecType() != DeclSpec::TST_error) { |
| 851 | // We have a declaration or reference to an anonymous class. |
| 852 | Diag(StartLoc, diag::err_anon_type_definition) |
| 853 | << DeclSpec::getSpecifierName(TagType); |
| 854 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 856 | SkipUntil(tok::comma, true); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 857 | |
| 858 | if (TemplateId) |
| 859 | TemplateId->Destroy(); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 863 | // Create the tag portion of the class or class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 864 | DeclResult TagOrTempResult = true; // invalid |
| 865 | TypeResult TypeResult = true; // invalid |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 867 | bool Owned = false; |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 868 | if (TemplateId) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 869 | // Explicit specialization, class template partial specialization, |
| 870 | // or explicit instantiation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 872 | TemplateId->getTemplateArgs(), |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 873 | TemplateId->NumArgs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 874 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 875 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 876 | // This is an explicit instantiation of a class template. |
| 877 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 878 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 879 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | TemplateInfo.TemplateLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 881 | TagType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | StartLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 883 | SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 884 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | TemplateId->TemplateNameLoc, |
| 886 | TemplateId->LAngleLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 887 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 889 | attrs.getList()); |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 890 | |
| 891 | // Friend template-ids are treated as references unless |
| 892 | // they have template headers, in which case they're ill-formed |
| 893 | // (FIXME: "template <class T> friend class A<T>::B<int>;"). |
| 894 | // We diagnose this error in ActOnClassTemplateSpecialization. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 895 | } else if (TUK == Sema::TUK_Reference || |
| 896 | (TUK == Sema::TUK_Friend && |
John McCall | 74256f5 | 2010-04-14 00:24:33 +0000 | [diff] [blame] | 897 | TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 898 | TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, |
| 899 | StartLoc, |
| 900 | TemplateId->SS, |
| 901 | TemplateId->Template, |
| 902 | TemplateId->TemplateNameLoc, |
| 903 | TemplateId->LAngleLoc, |
| 904 | TemplateArgsPtr, |
| 905 | TemplateId->RAngleLoc); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 906 | } else { |
| 907 | // This is an explicit specialization or a class template |
| 908 | // partial specialization. |
| 909 | TemplateParameterLists FakedParamLists; |
| 910 | |
| 911 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 912 | // This looks like an explicit instantiation, because we have |
| 913 | // something like |
| 914 | // |
| 915 | // template class Foo<X> |
| 916 | // |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 917 | // but it actually has a definition. Most likely, this was |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 918 | // meant to be an explicit specialization, but the user forgot |
| 919 | // the '<>' after 'template'. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 920 | assert(TUK == Sema::TUK_Definition && "Expected a definition here"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 921 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | SourceLocation LAngleLoc |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 923 | = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | Diag(TemplateId->TemplateNameLoc, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 925 | diag::err_explicit_instantiation_with_definition) |
| 926 | << SourceRange(TemplateInfo.TemplateLoc) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 927 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 928 | |
| 929 | // Create a fake template parameter list that contains only |
| 930 | // "template<>", so that we treat this construct as a class |
| 931 | // template specialization. |
| 932 | FakedParamLists.push_back( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | Actions.ActOnTemplateParameterList(0, SourceLocation(), |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 934 | TemplateInfo.TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | LAngleLoc, |
| 936 | 0, 0, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 937 | LAngleLoc)); |
| 938 | TemplateParams = &FakedParamLists; |
| 939 | } |
| 940 | |
| 941 | // Build the class template specialization. |
| 942 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 943 | = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 944 | StartLoc, SS, |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 945 | TemplateId->Template, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | TemplateId->TemplateNameLoc, |
| 947 | TemplateId->LAngleLoc, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 948 | TemplateArgsPtr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | TemplateId->RAngleLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 950 | attrs.getList(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 951 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 952 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 953 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 954 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 955 | TemplateId->Destroy(); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 956 | } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 957 | TUK == Sema::TUK_Declaration) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 958 | // Explicit instantiation of a member of a class template |
| 959 | // specialization, e.g., |
| 960 | // |
| 961 | // template struct Outer<int>::Inner; |
| 962 | // |
| 963 | TagOrTempResult |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 964 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 965 | TemplateInfo.ExternLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | TemplateInfo.TemplateLoc, |
| 967 | TagType, StartLoc, SS, Name, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 968 | NameLoc, attrs.getList()); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 969 | } else if (TUK == Sema::TUK_Friend && |
| 970 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { |
| 971 | TagOrTempResult = |
| 972 | Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(), |
| 973 | TagType, StartLoc, SS, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 974 | Name, NameLoc, attrs.getList(), |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 975 | MultiTemplateParamsArg(Actions, |
| 976 | TemplateParams? &(*TemplateParams)[0] : 0, |
| 977 | TemplateParams? TemplateParams->size() : 0)); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 978 | } else { |
| 979 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 980 | TUK == Sema::TUK_Definition) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 981 | // FIXME: Diagnose this particular error. |
| 982 | } |
| 983 | |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 984 | bool IsDependent = false; |
| 985 | |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 986 | // Don't pass down template parameter lists if this is just a tag |
| 987 | // reference. For example, we don't need the template parameters here: |
| 988 | // template <class T> class A *makeA(T t); |
| 989 | MultiTemplateParamsArg TParams; |
| 990 | if (TUK != Sema::TUK_Reference && TemplateParams) |
| 991 | TParams = |
| 992 | MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); |
| 993 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 994 | // Declaration or definition of a class type |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 995 | TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 996 | SS, Name, NameLoc, attrs.getList(), AS, |
John McCall | a25c408 | 2010-10-19 18:40:57 +0000 | [diff] [blame] | 997 | TParams, Owned, IsDependent, false, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 998 | false, clang::TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 999 | |
| 1000 | // If ActOnTag said the type was dependent, try again with the |
| 1001 | // less common call. |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1002 | if (IsDependent) { |
| 1003 | assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1004 | TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1005 | SS, Name, StartLoc, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1006 | } |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 1007 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1008 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1009 | // If there is a body, parse it and inform the actions module. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1010 | if (TUK == Sema::TUK_Definition) { |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1011 | assert(Tok.is(tok::l_brace) || |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1012 | (getLang().CPlusPlus && Tok.is(tok::colon)) || |
| 1013 | isCXX0XClassVirtSpecifier() != ClassVirtSpecifiers::CVS_None); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1014 | if (getLang().CPlusPlus) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1015 | ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1016 | else |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1017 | ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1020 | // FIXME: The DeclSpec should keep the locations of both the keyword and the |
| 1021 | // name (if there is one). |
| 1022 | SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc; |
| 1023 | |
| 1024 | const char *PrevSpec = 0; |
| 1025 | unsigned DiagID; |
| 1026 | bool Result; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1027 | if (!TypeResult.isInvalid()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1028 | Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, |
| 1029 | PrevSpec, DiagID, TypeResult.get()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1030 | } else if (!TagOrTempResult.isInvalid()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1031 | Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID, |
| 1032 | TagOrTempResult.get(), Owned); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1033 | } else { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1034 | DS.SetTypeSpecError(); |
Anders Carlsson | 66e9977 | 2009-05-11 22:27:47 +0000 | [diff] [blame] | 1035 | return; |
| 1036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1038 | if (Result) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1039 | Diag(StartLoc, DiagID) << PrevSpec; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1041 | // At this point, we've successfully parsed a class-specifier in 'definition' |
| 1042 | // form (e.g. "struct foo { int x; }". While we could just return here, we're |
| 1043 | // going to look at what comes after it to improve error recovery. If an |
| 1044 | // impossible token occurs next, we assume that the programmer forgot a ; at |
| 1045 | // the end of the declaration and recover that way. |
| 1046 | // |
| 1047 | // This switch enumerates the valid "follow" set for definition. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1048 | if (TUK == Sema::TUK_Definition) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1049 | bool ExpectedSemi = true; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1050 | switch (Tok.getKind()) { |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1051 | default: break; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1052 | case tok::semi: // struct foo {...} ; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1053 | case tok::star: // struct foo {...} * P; |
| 1054 | case tok::amp: // struct foo {...} & R = ... |
| 1055 | case tok::identifier: // struct foo {...} V ; |
| 1056 | case tok::r_paren: //(struct foo {...} ) {4} |
| 1057 | case tok::annot_cxxscope: // struct foo {...} a:: b; |
| 1058 | case tok::annot_typename: // struct foo {...} a ::b; |
| 1059 | case tok::annot_template_id: // struct foo {...} a<int> ::b; |
Chris Lattner | c2e1c1a | 2010-02-03 20:41:24 +0000 | [diff] [blame] | 1060 | case tok::l_paren: // struct foo {...} ( x); |
Chris Lattner | 16acfee | 2010-02-03 01:45:03 +0000 | [diff] [blame] | 1061 | case tok::comma: // __builtin_offsetof(struct foo{...} , |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1062 | ExpectedSemi = false; |
| 1063 | break; |
| 1064 | // Type qualifiers |
| 1065 | case tok::kw_const: // struct foo {...} const x; |
| 1066 | case tok::kw_volatile: // struct foo {...} volatile x; |
| 1067 | case tok::kw_restrict: // struct foo {...} restrict x; |
| 1068 | case tok::kw_inline: // struct foo {...} inline foo() {}; |
Chris Lattner | 99c9520 | 2010-02-02 17:32:27 +0000 | [diff] [blame] | 1069 | // Storage-class specifiers |
| 1070 | case tok::kw_static: // struct foo {...} static x; |
| 1071 | case tok::kw_extern: // struct foo {...} extern x; |
| 1072 | case tok::kw_typedef: // struct foo {...} typedef x; |
| 1073 | case tok::kw_register: // struct foo {...} register x; |
| 1074 | case tok::kw_auto: // struct foo {...} auto x; |
Douglas Gregor | 33f9924 | 2010-05-17 18:19:56 +0000 | [diff] [blame] | 1075 | case tok::kw_mutable: // struct foo {...} mutable x; |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1076 | // As shown above, type qualifiers and storage class specifiers absolutely |
| 1077 | // can occur after class specifiers according to the grammar. However, |
| 1078 | // almost noone actually writes code like this. If we see one of these, |
| 1079 | // it is much more likely that someone missed a semi colon and the |
| 1080 | // type/storage class specifier we're seeing is part of the *next* |
| 1081 | // intended declaration, as in: |
| 1082 | // |
| 1083 | // struct foo { ... } |
| 1084 | // typedef int X; |
| 1085 | // |
| 1086 | // We'd really like to emit a missing semicolon error instead of emitting |
| 1087 | // an error on the 'int' saying that you can't have two type specifiers in |
| 1088 | // the same declaration of X. Because of this, we look ahead past this |
| 1089 | // token to see if it's a type specifier. If so, we know the code is |
| 1090 | // otherwise invalid, so we can produce the expected semi error. |
| 1091 | if (!isKnownToBeTypeSpecifier(NextToken())) |
| 1092 | ExpectedSemi = false; |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1093 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1094 | |
| 1095 | case tok::r_brace: // struct bar { struct foo {...} } |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1096 | // Missing ';' at end of struct is accepted as an extension in C mode. |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1097 | if (!getLang().CPlusPlus) |
| 1098 | ExpectedSemi = false; |
| 1099 | break; |
| 1100 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 1102 | if (ExpectedSemi) { |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1103 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 1104 | TagType == DeclSpec::TST_class ? "class" |
| 1105 | : TagType == DeclSpec::TST_struct? "struct" : "union"); |
| 1106 | // Push this token back into the preprocessor and change our current token |
| 1107 | // to ';' so that the rest of the code recovers as though there were an |
| 1108 | // ';' after the definition. |
| 1109 | PP.EnterToken(Tok); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1110 | Tok.setKind(tok::semi); |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1111 | } |
| 1112 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1116 | /// |
| 1117 | /// base-clause : [C++ class.derived] |
| 1118 | /// ':' base-specifier-list |
| 1119 | /// base-specifier-list: |
| 1120 | /// base-specifier '...'[opt] |
| 1121 | /// base-specifier-list ',' base-specifier '...'[opt] |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1122 | void Parser::ParseBaseClause(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1123 | assert(Tok.is(tok::colon) && "Not a base clause"); |
| 1124 | ConsumeToken(); |
| 1125 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1126 | // Build up an array of parsed base specifiers. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1127 | llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1128 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1129 | while (true) { |
| 1130 | // Parse a base-specifier. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1131 | BaseResult Result = ParseBaseSpecifier(ClassDecl); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1132 | if (Result.isInvalid()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1133 | // Skip the rest of this base specifier, up until the comma or |
| 1134 | // opening brace. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1135 | SkipUntil(tok::comma, tok::l_brace, true, true); |
| 1136 | } else { |
| 1137 | // Add this to our array of base specifiers. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1138 | BaseInfo.push_back(Result.get()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | // If the next token is a comma, consume it and keep reading |
| 1142 | // base-specifiers. |
| 1143 | if (Tok.isNot(tok::comma)) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1145 | // Consume the comma. |
| 1146 | ConsumeToken(); |
| 1147 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1148 | |
| 1149 | // Attach the base specifiers |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1150 | Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is |
| 1154 | /// one entry in the base class list of a class specifier, for example: |
| 1155 | /// class foo : public bar, virtual private baz { |
| 1156 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 1157 | /// |
| 1158 | /// base-specifier: [C++ class.derived] |
| 1159 | /// ::[opt] nested-name-specifier[opt] class-name |
| 1160 | /// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] |
| 1161 | /// class-name |
| 1162 | /// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] |
| 1163 | /// class-name |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1164 | Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1165 | bool IsVirtual = false; |
| 1166 | SourceLocation StartLoc = Tok.getLocation(); |
| 1167 | |
| 1168 | // Parse the 'virtual' keyword. |
| 1169 | if (Tok.is(tok::kw_virtual)) { |
| 1170 | ConsumeToken(); |
| 1171 | IsVirtual = true; |
| 1172 | } |
| 1173 | |
| 1174 | // Parse an (optional) access specifier. |
| 1175 | AccessSpecifier Access = getAccessSpecifierIfPresent(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1176 | if (Access != AS_none) |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1177 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1179 | // Parse the 'virtual' keyword (again!), in case it came after the |
| 1180 | // access specifier. |
| 1181 | if (Tok.is(tok::kw_virtual)) { |
| 1182 | SourceLocation VirtualLoc = ConsumeToken(); |
| 1183 | if (IsVirtual) { |
| 1184 | // Complain about duplicate 'virtual' |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1185 | Diag(VirtualLoc, diag::err_dup_virtual) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1186 | << FixItHint::CreateRemoval(VirtualLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | IsVirtual = true; |
| 1190 | } |
| 1191 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1192 | // Parse optional '::' and optional nested-name-specifier. |
| 1193 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1194 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1196 | // The location of the base class itself. |
| 1197 | SourceLocation BaseLoc = Tok.getLocation(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Parse the class-name. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1200 | SourceLocation EndLocation; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1201 | TypeResult BaseType = ParseClassName(EndLocation, SS); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1202 | if (BaseType.isInvalid()) |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1203 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1205 | // Parse the optional ellipsis (for a pack expansion). The ellipsis is |
| 1206 | // actually part of the base-specifier-list grammar productions, but we |
| 1207 | // parse it here for convenience. |
| 1208 | SourceLocation EllipsisLoc; |
| 1209 | if (Tok.is(tok::ellipsis)) |
| 1210 | EllipsisLoc = ConsumeToken(); |
| 1211 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | // Find the complete source range for the base-specifier. |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1213 | SourceRange Range(StartLoc, EndLocation); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1215 | // Notify semantic analysis that we have parsed a complete |
| 1216 | // base-specifier. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1217 | return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1218 | BaseType.get(), BaseLoc, EllipsisLoc); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /// getAccessSpecifierIfPresent - Determine whether the next token is |
| 1222 | /// a C++ access-specifier. |
| 1223 | /// |
| 1224 | /// access-specifier: [C++ class.derived] |
| 1225 | /// 'private' |
| 1226 | /// 'protected' |
| 1227 | /// 'public' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | AccessSpecifier Parser::getAccessSpecifierIfPresent() const { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1229 | switch (Tok.getKind()) { |
| 1230 | default: return AS_none; |
| 1231 | case tok::kw_private: return AS_private; |
| 1232 | case tok::kw_protected: return AS_protected; |
| 1233 | case tok::kw_public: return AS_public; |
| 1234 | } |
| 1235 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1236 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1237 | void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1238 | Decl *ThisDecl) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1239 | // We just declared a member function. If this member function |
| 1240 | // has any default arguments, we'll need to parse them later. |
| 1241 | LateParsedMethodDeclaration *LateMethod = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | DeclaratorChunk::FunctionTypeInfo &FTI |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1243 | = DeclaratorInfo.getFunctionTypeInfo(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1244 | for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { |
| 1245 | if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { |
| 1246 | if (!LateMethod) { |
| 1247 | // Push this method onto the stack of late-parsed method |
| 1248 | // declarations. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1249 | LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); |
| 1250 | getCurrentClass().LateParsedDeclarations.push_back(LateMethod); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1251 | LateMethod->TemplateScope = getCurScope()->isTemplateParamScope(); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Add all of the parameters prior to this one (they don't |
| 1254 | // have default arguments). |
| 1255 | LateMethod->DefaultArgs.reserve(FTI.NumArgs); |
| 1256 | for (unsigned I = 0; I < ParamIdx; ++I) |
| 1257 | LateMethod->DefaultArgs.push_back( |
Douglas Gregor | 8f8210c | 2010-03-02 01:29:43 +0000 | [diff] [blame] | 1258 | LateParsedDefaultArgument(FTI.ArgInfo[I].Param)); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | // Add this parameter to the list of parameters (it or may |
| 1262 | // not have a default argument). |
| 1263 | LateMethod->DefaultArgs.push_back( |
| 1264 | LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, |
| 1265 | FTI.ArgInfo[ParamIdx].DefaultArgTokens)); |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1270 | /// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x |
| 1271 | /// virt-specifier. |
| 1272 | /// |
| 1273 | /// virt-specifier: |
| 1274 | /// override |
| 1275 | /// final |
| 1276 | /// new |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1277 | VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const { |
Anders Carlsson | ce93a7c | 2011-01-22 23:01:49 +0000 | [diff] [blame] | 1278 | if (!getLang().CPlusPlus) |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1279 | return VirtSpecifiers::VS_None; |
| 1280 | |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1281 | if (Tok.is(tok::kw_new)) |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1282 | return VirtSpecifiers::VS_New; |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1283 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1284 | if (Tok.is(tok::identifier)) { |
| 1285 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1286 | |
Anders Carlsson | 7eeb4ec | 2011-01-20 03:47:08 +0000 | [diff] [blame] | 1287 | // Initialize the contextual keywords. |
| 1288 | if (!Ident_final) { |
| 1289 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1290 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1291 | } |
| 1292 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1293 | if (II == Ident_override) |
| 1294 | return VirtSpecifiers::VS_Override; |
| 1295 | |
| 1296 | if (II == Ident_final) |
| 1297 | return VirtSpecifiers::VS_Final; |
| 1298 | } |
| 1299 | |
| 1300 | return VirtSpecifiers::VS_None; |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | /// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq. |
| 1304 | /// |
| 1305 | /// virt-specifier-seq: |
| 1306 | /// virt-specifier |
| 1307 | /// virt-specifier-seq virt-specifier |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1308 | void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) { |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1309 | while (true) { |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1310 | VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier(); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1311 | if (Specifier == VirtSpecifiers::VS_None) |
| 1312 | return; |
| 1313 | |
| 1314 | // C++ [class.mem]p8: |
| 1315 | // 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] | 1316 | const char *PrevSpec = 0; |
Anders Carlsson | 46127a9 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 1317 | if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1318 | Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) |
| 1319 | << PrevSpec |
| 1320 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1321 | |
Anders Carlsson | ce93a7c | 2011-01-22 23:01:49 +0000 | [diff] [blame] | 1322 | if (!getLang().CPlusPlus0x) |
| 1323 | Diag(Tok.getLocation(), diag::ext_override_control_keyword) |
| 1324 | << VirtSpecifiers::getSpecifierName(Specifier); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1325 | ConsumeToken(); |
| 1326 | } |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1329 | /// isCXX0XClassVirtSpecifier - Determine whether the next token is a C++0x |
| 1330 | /// class-virt-specifier. |
| 1331 | /// |
| 1332 | /// class-virt-specifier: |
| 1333 | /// final |
| 1334 | /// explicit |
| 1335 | ClassVirtSpecifiers::Specifier Parser::isCXX0XClassVirtSpecifier() const { |
Anders Carlsson | ce93a7c | 2011-01-22 23:01:49 +0000 | [diff] [blame] | 1336 | if (!getLang().CPlusPlus) |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1337 | return ClassVirtSpecifiers::CVS_None; |
| 1338 | |
| 1339 | if (Tok.is(tok::kw_explicit)) |
| 1340 | return ClassVirtSpecifiers::CVS_Explicit; |
| 1341 | |
| 1342 | if (Tok.is(tok::identifier)) { |
| 1343 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1344 | |
| 1345 | // Initialize the contextual keywords. |
| 1346 | if (!Ident_final) { |
| 1347 | Ident_final = &PP.getIdentifierTable().get("final"); |
| 1348 | Ident_override = &PP.getIdentifierTable().get("override"); |
| 1349 | } |
| 1350 | |
| 1351 | if (II == Ident_final) |
| 1352 | return ClassVirtSpecifiers::CVS_Final; |
| 1353 | } |
| 1354 | |
| 1355 | return ClassVirtSpecifiers::CVS_None; |
| 1356 | } |
| 1357 | |
| 1358 | /// ParseOptionalCXX0XClassVirtSpecifierSeq - Parse a class-virt-specifier-seq. |
| 1359 | /// |
| 1360 | /// class-virt-specifier-seq: |
| 1361 | /// class-virt-specifier |
| 1362 | /// class-virt-specifier-seq class-virt-specifier |
| 1363 | void Parser::ParseOptionalCXX0XClassVirtSpecifierSeq(ClassVirtSpecifiers &CVS) { |
| 1364 | while (true) { |
| 1365 | ClassVirtSpecifiers::Specifier Specifier = isCXX0XClassVirtSpecifier(); |
| 1366 | if (Specifier == ClassVirtSpecifiers::CVS_None) |
| 1367 | return; |
| 1368 | |
| 1369 | // C++ [class]p1: |
| 1370 | // A class-virt-specifier-seq shall contain at most one of each |
| 1371 | // class-virt-specifier. |
| 1372 | const char *PrevSpec = 0; |
| 1373 | if (CVS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) |
| 1374 | Diag(Tok.getLocation(), diag::err_duplicate_class_virt_specifier) |
| 1375 | << PrevSpec |
| 1376 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Anders Carlsson | ce93a7c | 2011-01-22 23:01:49 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (!getLang().CPlusPlus0x) |
| 1379 | Diag(Tok.getLocation(), diag::ext_override_control_keyword) |
| 1380 | << ClassVirtSpecifiers::getSpecifierName(Specifier); |
| 1381 | |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1382 | ConsumeToken(); |
| 1383 | } |
| 1384 | } |
| 1385 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1386 | /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. |
| 1387 | /// |
| 1388 | /// member-declaration: |
| 1389 | /// decl-specifier-seq[opt] member-declarator-list[opt] ';' |
| 1390 | /// function-definition ';'[opt] |
| 1391 | /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] |
| 1392 | /// using-declaration [TODO] |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1393 | /// [C++0x] static_assert-declaration |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1394 | /// template-declaration |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1395 | /// [GNU] '__extension__' member-declaration |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1396 | /// |
| 1397 | /// member-declarator-list: |
| 1398 | /// member-declarator |
| 1399 | /// member-declarator-list ',' member-declarator |
| 1400 | /// |
| 1401 | /// member-declarator: |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1402 | /// declarator virt-specifier-seq[opt] pure-specifier[opt] |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1403 | /// declarator constant-initializer[opt] |
| 1404 | /// identifier[opt] ':' constant-expression |
| 1405 | /// |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1406 | /// virt-specifier-seq: |
| 1407 | /// virt-specifier |
| 1408 | /// virt-specifier-seq virt-specifier |
| 1409 | /// |
| 1410 | /// virt-specifier: |
| 1411 | /// override |
| 1412 | /// final |
| 1413 | /// new |
| 1414 | /// |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1415 | /// pure-specifier: |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1416 | /// '= 0' |
| 1417 | /// |
| 1418 | /// constant-initializer: |
| 1419 | /// '=' constant-expression |
| 1420 | /// |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1421 | void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1422 | const ParsedTemplateInfo &TemplateInfo, |
| 1423 | ParsingDeclRAIIObject *TemplateDiags) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1424 | // Access declarations. |
| 1425 | if (!TemplateInfo.Kind && |
| 1426 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1427 | !TryAnnotateCXXScopeToken() && |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1428 | Tok.is(tok::annot_cxxscope)) { |
| 1429 | bool isAccessDecl = false; |
| 1430 | if (NextToken().is(tok::identifier)) |
| 1431 | isAccessDecl = GetLookAheadToken(2).is(tok::semi); |
| 1432 | else |
| 1433 | isAccessDecl = NextToken().is(tok::kw_operator); |
| 1434 | |
| 1435 | if (isAccessDecl) { |
| 1436 | // Collect the scope specifier token we annotated earlier. |
| 1437 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1438 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1439 | |
| 1440 | // Try to parse an unqualified-id. |
| 1441 | UnqualifiedId Name; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1442 | if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) { |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1443 | SkipUntil(tok::semi); |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | // TODO: recover from mistakenly-qualified operator declarations. |
| 1448 | if (ExpectAndConsume(tok::semi, |
| 1449 | diag::err_expected_semi_after, |
| 1450 | "access declaration", |
| 1451 | tok::semi)) |
| 1452 | return; |
| 1453 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1454 | Actions.ActOnUsingDeclaration(getCurScope(), AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 1455 | false, SourceLocation(), |
| 1456 | SS, Name, |
| 1457 | /* AttrList */ 0, |
| 1458 | /* IsTypeName */ false, |
| 1459 | SourceLocation()); |
| 1460 | return; |
| 1461 | } |
| 1462 | } |
| 1463 | |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1464 | // static_assert-declaration |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1465 | if (Tok.is(tok::kw_static_assert)) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1466 | // FIXME: Check for templates |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1467 | SourceLocation DeclEnd; |
| 1468 | ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1469 | return; |
| 1470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1472 | if (Tok.is(tok::kw_template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | assert(!TemplateInfo.TemplateParams && |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1474 | "Nested template improperly parsed?"); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1475 | SourceLocation DeclEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1477 | AS); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1478 | return; |
| 1479 | } |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 1480 | |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1481 | // Handle: member-declaration ::= '__extension__' member-declaration |
| 1482 | if (Tok.is(tok::kw___extension__)) { |
| 1483 | // __extension__ silences extension warnings in the subexpression. |
| 1484 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
| 1485 | ConsumeToken(); |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1486 | return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags); |
Chris Lattner | bc8d564 | 2008-12-18 01:12:00 +0000 | [diff] [blame] | 1487 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 4ed5d91 | 2010-02-02 01:23:29 +0000 | [diff] [blame] | 1489 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it |
| 1490 | // is a bitfield. |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1491 | ColonProtectionRAIIObject X(*this); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1492 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1493 | ParsedAttributesWithRange attrs; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1494 | // Optional C++0x attribute-specifier |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1495 | MaybeParseCXX0XAttributes(attrs); |
| 1496 | MaybeParseMicrosoftAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1497 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1498 | if (Tok.is(tok::kw_using)) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1499 | // FIXME: Check for template aliases |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1500 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1501 | ProhibitAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1503 | // Eat 'using'. |
| 1504 | SourceLocation UsingLoc = ConsumeToken(); |
| 1505 | |
| 1506 | if (Tok.is(tok::kw_namespace)) { |
| 1507 | Diag(UsingLoc, diag::err_using_namespace_in_class); |
| 1508 | SkipUntil(tok::semi, true, true); |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1509 | } else { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1510 | SourceLocation DeclEnd; |
| 1511 | // Otherwise, it must be using-declaration. |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1512 | ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo, |
| 1513 | UsingLoc, DeclEnd, AS); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1514 | } |
| 1515 | return; |
| 1516 | } |
| 1517 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1518 | // decl-specifier-seq: |
| 1519 | // Parse the common declaration-specifiers piece. |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1520 | ParsingDeclSpec DS(*this, TemplateDiags); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1521 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1522 | ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1523 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1524 | MultiTemplateParamsArg TemplateParams(Actions, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 1525 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, |
| 1526 | TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); |
| 1527 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1528 | if (Tok.is(tok::semi)) { |
| 1529 | ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1530 | Decl *TheDecl = |
John McCall | c9068d7 | 2010-07-16 08:13:16 +0000 | [diff] [blame] | 1531 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS); |
| 1532 | DS.complete(TheDecl); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1533 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1534 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1535 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1536 | ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1537 | VirtSpecifiers VS; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1538 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1539 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1540 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 1541 | ColonProtectionRAIIObject X(*this); |
| 1542 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1543 | // Parse the first declarator. |
| 1544 | ParseDeclarator(DeclaratorInfo); |
| 1545 | // Error parsing the declarator? |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1546 | if (!DeclaratorInfo.hasName()) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1547 | // If so, skip until the semi-colon or a }. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1548 | SkipUntil(tok::r_brace, true); |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1549 | if (Tok.is(tok::semi)) |
| 1550 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1551 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1554 | ParseOptionalCXX0XVirtSpecifierSeq(VS); |
| 1555 | |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 1556 | // If attributes exist after the declarator, but before an '{', parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1557 | MaybeParseGNUAttributes(DeclaratorInfo); |
John Thompson | 1b2fc0f | 2009-11-25 22:58:06 +0000 | [diff] [blame] | 1558 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1559 | // function-definition: |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1560 | if (Tok.is(tok::l_brace) |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1561 | || (DeclaratorInfo.isFunctionDeclarator() && |
| 1562 | (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) { |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1563 | if (!DeclaratorInfo.isFunctionDeclarator()) { |
| 1564 | Diag(Tok, diag::err_func_def_no_params); |
| 1565 | ConsumeBrace(); |
| 1566 | SkipUntil(tok::r_brace, true); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1567 | |
| 1568 | // Consume the optional ';' |
| 1569 | if (Tok.is(tok::semi)) |
| 1570 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1571 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1572 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1573 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1574 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1575 | Diag(Tok, diag::err_function_declared_typedef); |
| 1576 | // This recovery skips the entire function body. It would be nice |
| 1577 | // to simply call ParseCXXInlineMethodDef() below, however Sema |
| 1578 | // assumes the declarator represents a function, not a typedef. |
| 1579 | ConsumeBrace(); |
| 1580 | SkipUntil(tok::r_brace, true); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1581 | |
| 1582 | // Consume the optional ';' |
| 1583 | if (Tok.is(tok::semi)) |
| 1584 | ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1585 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1588 | ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS); |
Douglas Gregor | 9ea416e | 2011-01-19 16:41:58 +0000 | [diff] [blame] | 1589 | // Consume the optional ';' |
| 1590 | if (Tok.is(tok::semi)) |
| 1591 | ConsumeToken(); |
| 1592 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1593 | return; |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1594 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | // member-declarator-list: |
| 1598 | // member-declarator |
| 1599 | // member-declarator-list ',' member-declarator |
| 1600 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1601 | llvm::SmallVector<Decl *, 8> DeclsInGroup; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1602 | ExprResult BitfieldSize; |
| 1603 | ExprResult Init; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1604 | bool Deleted = false; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1605 | |
| 1606 | while (1) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1607 | // member-declarator: |
| 1608 | // declarator pure-specifier[opt] |
| 1609 | // declarator constant-initializer[opt] |
| 1610 | // identifier[opt] ':' constant-expression |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1611 | if (Tok.is(tok::colon)) { |
| 1612 | ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1613 | BitfieldSize = ParseConstantExpression(); |
| 1614 | if (BitfieldSize.isInvalid()) |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1615 | SkipUntil(tok::comma, true, true); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1616 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 1618 | ParseOptionalCXX0XVirtSpecifierSeq(VS); |
Anders Carlsson | 1f3b6fd | 2011-01-16 23:56:42 +0000 | [diff] [blame] | 1619 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1620 | // pure-specifier: |
| 1621 | // '= 0' |
| 1622 | // |
| 1623 | // constant-initializer: |
| 1624 | // '=' constant-expression |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1625 | // |
| 1626 | // defaulted/deleted function-definition: |
| 1627 | // '=' 'default' [TODO] |
| 1628 | // '=' 'delete' |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1629 | if (Tok.is(tok::equal)) { |
| 1630 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1631 | if (Tok.is(tok::kw_delete)) { |
| 1632 | if (!getLang().CPlusPlus0x) |
| 1633 | Diag(Tok, diag::warn_deleted_function_accepted_as_extension); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1634 | ConsumeToken(); |
| 1635 | Deleted = true; |
| 1636 | } else { |
| 1637 | Init = ParseInitializer(); |
| 1638 | if (Init.isInvalid()) |
| 1639 | SkipUntil(tok::comma, true, true); |
| 1640 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1643 | // If a simple-asm-expr is present, parse it. |
| 1644 | if (Tok.is(tok::kw_asm)) { |
| 1645 | SourceLocation Loc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1646 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Chris Lattner | e656325 | 2010-06-13 05:34:18 +0000 | [diff] [blame] | 1647 | if (AsmLabel.isInvalid()) |
| 1648 | SkipUntil(tok::comma, true, true); |
| 1649 | |
| 1650 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
| 1651 | DeclaratorInfo.SetRangeEnd(Loc); |
| 1652 | } |
| 1653 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1654 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1655 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1656 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1657 | // 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] | 1658 | // this call will *not* return the created decl; It will return null. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1659 | // See Sema::ActOnCXXMemberDeclarator for details. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1660 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1661 | Decl *ThisDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1662 | if (DS.isFriendSpecified()) { |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1663 | // TODO: handle initializers, bitfields, 'delete' |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1664 | ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 1665 | /*IsDefinition*/ false, |
| 1666 | move(TemplateParams)); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1667 | } else { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1668 | ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1669 | DeclaratorInfo, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1670 | move(TemplateParams), |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1671 | BitfieldSize.release(), |
Anders Carlsson | 69a8735 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 1672 | VS, Init.release(), |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 1673 | /*IsDefinition*/Deleted, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1674 | Deleted); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1675 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1676 | if (ThisDecl) |
| 1677 | DeclsInGroup.push_back(ThisDecl); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1678 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1679 | if (DeclaratorInfo.isFunctionDeclarator() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | DeclaratorInfo.getDeclSpec().getStorageClassSpec() |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1681 | != DeclSpec::SCS_typedef) { |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 1682 | HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1685 | DeclaratorInfo.complete(ThisDecl); |
| 1686 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1687 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1688 | // or an error, bail out. |
| 1689 | if (Tok.isNot(tok::comma)) |
| 1690 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1692 | // Consume the comma. |
| 1693 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1694 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1695 | // Parse the next declarator. |
| 1696 | DeclaratorInfo.clear(); |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 1697 | VS.clear(); |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1698 | BitfieldSize = 0; |
| 1699 | Init = 0; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1700 | Deleted = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1702 | // Attributes are only allowed on the second declarator. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1703 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1704 | |
Argyrios Kyrtzidis | 3a9fdb4 | 2008-06-28 08:10:48 +0000 | [diff] [blame] | 1705 | if (Tok.isNot(tok::colon)) |
| 1706 | ParseDeclarator(DeclaratorInfo); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1709 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { |
| 1710 | // Skip to end of block or statement. |
| 1711 | SkipUntil(tok::r_brace, true, true); |
| 1712 | // If we stopped at a ';', eat it. |
| 1713 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1714 | return; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1717 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(), |
Chris Lattner | ae50d50 | 2010-02-02 00:43:15 +0000 | [diff] [blame] | 1718 | DeclsInGroup.size()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | /// ParseCXXMemberSpecification - Parse the class definition. |
| 1722 | /// |
| 1723 | /// member-specification: |
| 1724 | /// member-declaration member-specification[opt] |
| 1725 | /// access-specifier ':' member-specification[opt] |
| 1726 | /// |
| 1727 | void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1728 | unsigned TagType, Decl *TagDecl) { |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1729 | assert((TagType == DeclSpec::TST_struct || |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1730 | TagType == DeclSpec::TST_union || |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1731 | TagType == DeclSpec::TST_class) && "Invalid TagType!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1732 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1733 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 1734 | "parsing struct/union/class body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1736 | // Determine whether this is a non-nested class. Note that local |
| 1737 | // classes are *not* considered to be nested classes. |
| 1738 | bool NonNestedClass = true; |
| 1739 | if (!ClassStack.empty()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1740 | for (const Scope *S = getCurScope(); S; S = S->getParent()) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1741 | if (S->isClassScope()) { |
| 1742 | // We're inside a class scope, so this is a nested class. |
| 1743 | NonNestedClass = false; |
| 1744 | break; |
| 1745 | } |
| 1746 | |
| 1747 | if ((S->getFlags() & Scope::FnScope)) { |
| 1748 | // If we're in a function or function template declared in the |
| 1749 | // body of a class, then this is a local class rather than a |
| 1750 | // nested class. |
| 1751 | const Scope *Parent = S->getParent(); |
| 1752 | if (Parent->isTemplateParamScope()) |
| 1753 | Parent = Parent->getParent(); |
| 1754 | if (Parent->isClassScope()) |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
| 1758 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Enter a scope for the class. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1761 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1762 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1763 | // Note that we are parsing a new (potentially-nested) class definition. |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 1764 | ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1766 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1767 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1768 | |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 1769 | ClassVirtSpecifiers CVS; |
| 1770 | ParseOptionalCXX0XClassVirtSpecifierSeq(CVS); |
| 1771 | |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1772 | if (Tok.is(tok::colon)) { |
| 1773 | ParseBaseClause(TagDecl); |
| 1774 | |
| 1775 | if (!Tok.is(tok::l_brace)) { |
| 1776 | Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 1777 | |
| 1778 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1779 | Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); |
John McCall | bd0dfa5 | 2009-12-19 21:48:58 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | assert(Tok.is(tok::l_brace)); |
| 1785 | |
| 1786 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1787 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1788 | if (TagDecl) |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1789 | Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, CVS, |
| 1790 | LBraceLoc); |
John McCall | f936815 | 2009-12-20 07:58:13 +0000 | [diff] [blame] | 1791 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1792 | // C++ 11p3: Members of a class defined with the keyword class are private |
| 1793 | // by default. Members of a class defined with the keywords struct or union |
| 1794 | // are public by default. |
| 1795 | AccessSpecifier CurAS; |
| 1796 | if (TagType == DeclSpec::TST_class) |
| 1797 | CurAS = AS_private; |
| 1798 | else |
| 1799 | CurAS = AS_public; |
| 1800 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1801 | SourceLocation RBraceLoc; |
| 1802 | if (TagDecl) { |
| 1803 | // While we still have something to read, read the member-declarations. |
| 1804 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
| 1805 | // Each iteration of this loop reads one member-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1807 | // Check for extraneous top-level semicolon. |
| 1808 | if (Tok.is(tok::semi)) { |
| 1809 | Diag(Tok, diag::ext_extra_struct_semi) |
| 1810 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
| 1811 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1812 | ConsumeToken(); |
| 1813 | continue; |
| 1814 | } |
| 1815 | |
| 1816 | AccessSpecifier AS = getAccessSpecifierIfPresent(); |
| 1817 | if (AS != AS_none) { |
| 1818 | // Current token is a C++ access specifier. |
| 1819 | CurAS = AS; |
| 1820 | SourceLocation ASLoc = Tok.getLocation(); |
| 1821 | ConsumeToken(); |
| 1822 | if (Tok.is(tok::colon)) |
| 1823 | Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation()); |
| 1824 | else |
| 1825 | Diag(Tok, diag::err_expected_colon); |
| 1826 | ConsumeToken(); |
| 1827 | continue; |
| 1828 | } |
| 1829 | |
| 1830 | // FIXME: Make sure we don't have a template here. |
| 1831 | |
| 1832 | // Parse all the comma separated declarators. |
| 1833 | ParseCXXClassMemberDeclaration(CurAS); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1836 | RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 1837 | } else { |
| 1838 | SkipUntil(tok::r_brace, false, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1841 | // If attributes exist after class contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1842 | ParsedAttributes attrs; |
| 1843 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1844 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1845 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1846 | Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1847 | LBraceLoc, RBraceLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1848 | attrs.getList()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1849 | |
| 1850 | // C++ 9.2p2: Within the class member-specification, the class is regarded as |
| 1851 | // complete within function bodies, default arguments, |
| 1852 | // exception-specifications, and constructor ctor-initializers (including |
| 1853 | // such things in nested classes). |
| 1854 | // |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 1855 | // FIXME: Only function bodies and constructor ctor-initializers are |
| 1856 | // parsed correctly, fix the rest. |
Douglas Gregor | 07976d2 | 2010-06-21 22:31:09 +0000 | [diff] [blame] | 1857 | if (TagDecl && NonNestedClass) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1858 | // 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] | 1859 | // are complete and we can parse the delayed portions of method |
| 1860 | // declarations and the lexed inline method definitions. |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 1861 | SourceLocation SavedPrevTokLocation = PrevTokLocation; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1862 | ParseLexedMethodDeclarations(getCurrentClass()); |
| 1863 | ParseLexedMethodDefs(getCurrentClass()); |
Douglas Gregor | e0cc047 | 2010-06-16 23:45:56 +0000 | [diff] [blame] | 1864 | PrevTokLocation = SavedPrevTokLocation; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
John McCall | 42a4f66 | 2010-05-28 08:11:17 +0000 | [diff] [blame] | 1867 | if (TagDecl) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1868 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc); |
John McCall | db7bb4a | 2010-03-17 00:38:33 +0000 | [diff] [blame] | 1869 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1870 | // Leave the class scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 1871 | ParsingDef.Pop(); |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 1872 | ClassScope.Exit(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1873 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1874 | |
| 1875 | /// ParseConstructorInitializer - Parse a C++ constructor initializer, |
| 1876 | /// which explicitly initializes the members or base classes of a |
| 1877 | /// class (C++ [class.base.init]). For example, the three initializers |
| 1878 | /// after the ':' in the Derived constructor below: |
| 1879 | /// |
| 1880 | /// @code |
| 1881 | /// class Base { }; |
| 1882 | /// class Derived : Base { |
| 1883 | /// int x; |
| 1884 | /// float f; |
| 1885 | /// public: |
| 1886 | /// Derived(float f) : Base(), x(17), f(f) { } |
| 1887 | /// }; |
| 1888 | /// @endcode |
| 1889 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | /// [C++] ctor-initializer: |
| 1891 | /// ':' mem-initializer-list |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1892 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | /// [C++] mem-initializer-list: |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1894 | /// mem-initializer ...[opt] |
| 1895 | /// mem-initializer ...[opt] , mem-initializer-list |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1896 | void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1897 | assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); |
| 1898 | |
| 1899 | SourceLocation ColonLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1901 | llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1902 | bool AnyErrors = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1904 | do { |
Douglas Gregor | 0133f52 | 2010-08-28 00:00:50 +0000 | [diff] [blame] | 1905 | if (Tok.is(tok::code_completion)) { |
| 1906 | Actions.CodeCompleteConstructorInitializer(ConstructorDecl, |
| 1907 | MemInitializers.data(), |
| 1908 | MemInitializers.size()); |
| 1909 | ConsumeCodeCompletionToken(); |
| 1910 | } else { |
| 1911 | MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); |
| 1912 | if (!MemInit.isInvalid()) |
| 1913 | MemInitializers.push_back(MemInit.get()); |
| 1914 | else |
| 1915 | AnyErrors = true; |
| 1916 | } |
| 1917 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1918 | if (Tok.is(tok::comma)) |
| 1919 | ConsumeToken(); |
| 1920 | else if (Tok.is(tok::l_brace)) |
| 1921 | break; |
Douglas Gregor | b1f6fa4 | 2010-09-07 14:35:10 +0000 | [diff] [blame] | 1922 | // If the next token looks like a base or member initializer, assume that |
| 1923 | // we're just missing a comma. |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 1924 | else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { |
| 1925 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 1926 | Diag(Loc, diag::err_ctor_init_missing_comma) |
| 1927 | << FixItHint::CreateInsertion(Loc, ", "); |
| 1928 | } else { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1929 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1930 | Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1931 | SkipUntil(tok::l_brace, true, true); |
| 1932 | break; |
| 1933 | } |
| 1934 | } while (true); |
| 1935 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1937 | MemInitializers.data(), MemInitializers.size(), |
| 1938 | AnyErrors); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | /// ParseMemInitializer - Parse a C++ member initializer, which is |
| 1942 | /// part of a constructor initializer that explicitly initializes one |
| 1943 | /// member or base class (C++ [class.base.init]). See |
| 1944 | /// ParseConstructorInitializer for an example. |
| 1945 | /// |
| 1946 | /// [C++] mem-initializer: |
| 1947 | /// mem-initializer-id '(' expression-list[opt] ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | /// |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1949 | /// [C++] mem-initializer-id: |
| 1950 | /// '::'[opt] nested-name-specifier[opt] class-name |
| 1951 | /// identifier |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1952 | Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1953 | // parse '::'[opt] nested-name-specifier[opt] |
| 1954 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1955 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false); |
| 1956 | ParsedType TemplateTypeTy; |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1957 | if (Tok.is(tok::annot_template_id)) { |
| 1958 | TemplateIdAnnotation *TemplateId |
| 1959 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | d9b600c | 2010-01-12 17:52:59 +0000 | [diff] [blame] | 1960 | if (TemplateId->Kind == TNK_Type_template || |
| 1961 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1962 | AnnotateTemplateIdTokenAsType(); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1963 | assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1964 | TemplateTypeTy = getTypeAnnotation(Tok); |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1965 | } |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1966 | } |
| 1967 | if (!TemplateTypeTy && Tok.isNot(tok::identifier)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1968 | Diag(Tok, diag::err_expected_member_or_base_name); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1969 | return true; |
| 1970 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1972 | // Get the identifier. This may be a member name or a class name, |
| 1973 | // but we'll let the semantic analysis determine which it is. |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1974 | IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1975 | SourceLocation IdLoc = ConsumeToken(); |
| 1976 | |
| 1977 | // Parse the '('. |
| 1978 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1979 | Diag(Tok, diag::err_expected_lparen); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1980 | return true; |
| 1981 | } |
| 1982 | SourceLocation LParenLoc = ConsumeParen(); |
| 1983 | |
| 1984 | // Parse the optional expression-list. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1985 | ExprVector ArgExprs(Actions); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1986 | CommaLocsTy CommaLocs; |
| 1987 | if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { |
| 1988 | SkipUntil(tok::r_paren); |
| 1989 | return true; |
| 1990 | } |
| 1991 | |
| 1992 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1993 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1994 | SourceLocation EllipsisLoc; |
| 1995 | if (Tok.is(tok::ellipsis)) |
| 1996 | EllipsisLoc = ConsumeToken(); |
| 1997 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1998 | return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1999 | TemplateTypeTy, IdLoc, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 2000 | LParenLoc, ArgExprs.take(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2001 | ArgExprs.size(), RParenLoc, |
| 2002 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2003 | } |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2004 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2005 | /// \brief Parse a C++ exception-specification if present (C++0x [except.spec]). |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2006 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2007 | /// exception-specification: |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2008 | /// dynamic-exception-specification |
| 2009 | /// noexcept-specification |
| 2010 | /// |
| 2011 | /// noexcept-specification: |
| 2012 | /// 'noexcept' |
| 2013 | /// 'noexcept' '(' constant-expression ')' |
| 2014 | ExceptionSpecificationType |
| 2015 | Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange, |
| 2016 | llvm::SmallVectorImpl<ParsedType> &DynamicExceptions, |
| 2017 | llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges, |
| 2018 | ExprResult &NoexceptExpr) { |
| 2019 | ExceptionSpecificationType Result = EST_None; |
| 2020 | |
| 2021 | // See if there's a dynamic specification. |
| 2022 | if (Tok.is(tok::kw_throw)) { |
| 2023 | Result = ParseDynamicExceptionSpecification(SpecificationRange, |
| 2024 | DynamicExceptions, |
| 2025 | DynamicExceptionRanges); |
| 2026 | assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && |
| 2027 | "Produced different number of exception types and ranges."); |
| 2028 | } |
| 2029 | |
| 2030 | // If there's no noexcept specification, we're done. |
| 2031 | if (Tok.isNot(tok::kw_noexcept)) |
| 2032 | return Result; |
| 2033 | |
| 2034 | // If we already had a dynamic specification, parse the noexcept for, |
| 2035 | // recovery, but emit a diagnostic and don't store the results. |
| 2036 | SourceRange NoexceptRange; |
| 2037 | ExceptionSpecificationType NoexceptType = EST_None; |
| 2038 | |
| 2039 | SourceLocation KeywordLoc = ConsumeToken(); |
| 2040 | if (Tok.is(tok::l_paren)) { |
| 2041 | // There is an argument. |
| 2042 | SourceLocation LParenLoc = ConsumeParen(); |
| 2043 | NoexceptType = EST_ComputedNoexcept; |
| 2044 | NoexceptExpr = ParseConstantExpression(); |
| 2045 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2046 | NoexceptRange = SourceRange(KeywordLoc, RParenLoc); |
| 2047 | } else { |
| 2048 | // There is no argument. |
| 2049 | NoexceptType = EST_BasicNoexcept; |
| 2050 | NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); |
| 2051 | } |
| 2052 | |
| 2053 | if (Result == EST_None) { |
| 2054 | SpecificationRange = NoexceptRange; |
| 2055 | Result = NoexceptType; |
| 2056 | |
| 2057 | // If there's a dynamic specification after a noexcept specification, |
| 2058 | // parse that and ignore the results. |
| 2059 | if (Tok.is(tok::kw_throw)) { |
| 2060 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2061 | ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, |
| 2062 | DynamicExceptionRanges); |
| 2063 | } |
| 2064 | } else { |
| 2065 | Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); |
| 2066 | } |
| 2067 | |
| 2068 | return Result; |
| 2069 | } |
| 2070 | |
| 2071 | /// ParseDynamicExceptionSpecification - Parse a C++ |
| 2072 | /// dynamic-exception-specification (C++ [except.spec]). |
| 2073 | /// |
| 2074 | /// dynamic-exception-specification: |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2075 | /// 'throw' '(' type-id-list [opt] ')' |
| 2076 | /// [MS] 'throw' '(' '...' ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | /// |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2078 | /// type-id-list: |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2079 | /// type-id ... [opt] |
| 2080 | /// type-id-list ',' type-id ... [opt] |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2081 | /// |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2082 | ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( |
| 2083 | SourceRange &SpecificationRange, |
| 2084 | llvm::SmallVectorImpl<ParsedType> &Exceptions, |
| 2085 | llvm::SmallVectorImpl<SourceRange> &Ranges) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2086 | assert(Tok.is(tok::kw_throw) && "expected throw"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2088 | SpecificationRange.setBegin(ConsumeToken()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2090 | if (!Tok.is(tok::l_paren)) { |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2091 | Diag(Tok, diag::err_expected_lparen_after) << "throw"; |
| 2092 | SpecificationRange.setEnd(SpecificationRange.getBegin()); |
| 2093 | return EST_Dynamic; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2094 | } |
| 2095 | SourceLocation LParenLoc = ConsumeParen(); |
| 2096 | |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2097 | // Parse throw(...), a Microsoft extension that means "this function |
| 2098 | // can throw anything". |
| 2099 | if (Tok.is(tok::ellipsis)) { |
| 2100 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 2101 | if (!getLang().Microsoft) |
| 2102 | Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2103 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2104 | SpecificationRange.setEnd(RParenLoc); |
| 2105 | return EST_DynamicAny; |
Douglas Gregor | a474561 | 2008-12-01 18:00:20 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2108 | // Parse the sequence of type-ids. |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2109 | SourceRange Range; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2110 | while (Tok.isNot(tok::r_paren)) { |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2111 | TypeResult Res(ParseTypeName(&Range)); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2113 | if (Tok.is(tok::ellipsis)) { |
| 2114 | // C++0x [temp.variadic]p5: |
| 2115 | // - In a dynamic-exception-specification (15.4); the pattern is a |
| 2116 | // type-id. |
| 2117 | SourceLocation Ellipsis = ConsumeToken(); |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2118 | Range.setEnd(Ellipsis); |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2119 | if (!Res.isInvalid()) |
| 2120 | Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); |
| 2121 | } |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2122 | |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2123 | if (!Res.isInvalid()) { |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2124 | Exceptions.push_back(Res.get()); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2125 | Ranges.push_back(Range); |
| 2126 | } |
Douglas Gregor | a04426c | 2010-12-20 23:57:46 +0000 | [diff] [blame] | 2127 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2128 | if (Tok.is(tok::comma)) |
| 2129 | ConsumeToken(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2130 | else |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | } |
| 2133 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 2134 | SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc)); |
| 2135 | return EST_Dynamic; |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2136 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 2138 | /// ParseTrailingReturnType - Parse a trailing return type on a new-style |
| 2139 | /// function declaration. |
| 2140 | TypeResult Parser::ParseTrailingReturnType() { |
| 2141 | assert(Tok.is(tok::arrow) && "expected arrow"); |
| 2142 | |
| 2143 | ConsumeToken(); |
| 2144 | |
| 2145 | // FIXME: Need to suppress declarations when parsing this typename. |
| 2146 | // Otherwise in this function definition: |
| 2147 | // |
| 2148 | // auto f() -> struct X {} |
| 2149 | // |
| 2150 | // struct X is parsed as class definition because of the trailing |
| 2151 | // brace. |
| 2152 | |
| 2153 | SourceRange Range; |
| 2154 | return ParseTypeName(&Range); |
| 2155 | } |
| 2156 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2157 | /// \brief We have just started parsing the definition of a new class, |
| 2158 | /// so push that class onto our stack of classes that is currently |
| 2159 | /// being parsed. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2160 | Sema::ParsingClassState |
| 2161 | Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) { |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2162 | assert((NonNestedClass || !ClassStack.empty()) && |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2163 | "Nested class without outer class"); |
Douglas Gregor | 26997fd | 2010-01-16 20:52:59 +0000 | [diff] [blame] | 2164 | ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass)); |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2165 | return Actions.PushParsingClass(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | /// \brief Deallocate the given parsed class and all of its nested |
| 2169 | /// classes. |
| 2170 | void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2171 | for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) |
| 2172 | delete Class->LateParsedDeclarations[I]; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2173 | delete Class; |
| 2174 | } |
| 2175 | |
| 2176 | /// \brief Pop the top class of the stack of classes that are |
| 2177 | /// currently being parsed. |
| 2178 | /// |
| 2179 | /// This routine should be called when we have finished parsing the |
| 2180 | /// definition of a class, but have not yet popped the Scope |
| 2181 | /// associated with the class's definition. |
| 2182 | /// |
| 2183 | /// \returns true if the class we've popped is a top-level class, |
| 2184 | /// false otherwise. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2185 | void Parser::PopParsingClass(Sema::ParsingClassState state) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2186 | assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2188 | Actions.PopParsingClass(state); |
| 2189 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2190 | ParsingClass *Victim = ClassStack.top(); |
| 2191 | ClassStack.pop(); |
| 2192 | if (Victim->TopLevelClass) { |
| 2193 | // Deallocate all of the nested classes of this class, |
| 2194 | // recursively: we don't need to keep any of this information. |
| 2195 | DeallocateParsedClasses(Victim); |
| 2196 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2198 | assert(!ClassStack.empty() && "Missing top-level class?"); |
| 2199 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2200 | if (Victim->LateParsedDeclarations.empty()) { |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2201 | // The victim is a nested class, but we will not need to perform |
| 2202 | // any processing after the definition of this class since it has |
| 2203 | // no members whose handling was delayed. Therefore, we can just |
| 2204 | // remove this nested class. |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2205 | DeallocateParsedClasses(Victim); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2206 | return; |
| 2207 | } |
| 2208 | |
| 2209 | // This nested class has some members that will need to be processed |
| 2210 | // after the top-level class is completely defined. Therefore, add |
| 2211 | // it to the list of nested classes within its parent. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2212 | assert(getCurScope()->isClassScope() && "Nested class outside of class scope?"); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 2213 | ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim)); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2214 | Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2215 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2216 | |
| 2217 | /// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only |
| 2218 | /// parses standard attributes. |
| 2219 | /// |
| 2220 | /// [C++0x] attribute-specifier: |
| 2221 | /// '[' '[' attribute-list ']' ']' |
| 2222 | /// |
| 2223 | /// [C++0x] attribute-list: |
| 2224 | /// attribute[opt] |
| 2225 | /// attribute-list ',' attribute[opt] |
| 2226 | /// |
| 2227 | /// [C++0x] attribute: |
| 2228 | /// attribute-token attribute-argument-clause[opt] |
| 2229 | /// |
| 2230 | /// [C++0x] attribute-token: |
| 2231 | /// identifier |
| 2232 | /// attribute-scoped-token |
| 2233 | /// |
| 2234 | /// [C++0x] attribute-scoped-token: |
| 2235 | /// attribute-namespace '::' identifier |
| 2236 | /// |
| 2237 | /// [C++0x] attribute-namespace: |
| 2238 | /// identifier |
| 2239 | /// |
| 2240 | /// [C++0x] attribute-argument-clause: |
| 2241 | /// '(' balanced-token-seq ')' |
| 2242 | /// |
| 2243 | /// [C++0x] balanced-token-seq: |
| 2244 | /// balanced-token |
| 2245 | /// balanced-token-seq balanced-token |
| 2246 | /// |
| 2247 | /// [C++0x] balanced-token: |
| 2248 | /// '(' balanced-token-seq ')' |
| 2249 | /// '[' balanced-token-seq ']' |
| 2250 | /// '{' balanced-token-seq '}' |
| 2251 | /// any token but '(', ')', '[', ']', '{', or '}' |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2252 | void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs, |
| 2253 | SourceLocation *endLoc) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2254 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) |
| 2255 | && "Not a C++0x attribute list"); |
| 2256 | |
| 2257 | SourceLocation StartLoc = Tok.getLocation(), Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2258 | |
| 2259 | ConsumeBracket(); |
| 2260 | ConsumeBracket(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2261 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2262 | if (Tok.is(tok::comma)) { |
| 2263 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2264 | ConsumeToken(); |
| 2265 | } |
| 2266 | |
| 2267 | while (Tok.is(tok::identifier) || Tok.is(tok::comma)) { |
| 2268 | // attribute not present |
| 2269 | if (Tok.is(tok::comma)) { |
| 2270 | ConsumeToken(); |
| 2271 | continue; |
| 2272 | } |
| 2273 | |
| 2274 | IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo(); |
| 2275 | SourceLocation ScopeLoc, AttrLoc = ConsumeToken(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2276 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2277 | // scoped attribute |
| 2278 | if (Tok.is(tok::coloncolon)) { |
| 2279 | ConsumeToken(); |
| 2280 | |
| 2281 | if (!Tok.is(tok::identifier)) { |
| 2282 | Diag(Tok.getLocation(), diag::err_expected_ident); |
| 2283 | SkipUntil(tok::r_square, tok::comma, true, true); |
| 2284 | continue; |
| 2285 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2286 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2287 | ScopeName = AttrName; |
| 2288 | ScopeLoc = AttrLoc; |
| 2289 | |
| 2290 | AttrName = Tok.getIdentifierInfo(); |
| 2291 | AttrLoc = ConsumeToken(); |
| 2292 | } |
| 2293 | |
| 2294 | bool AttrParsed = false; |
| 2295 | // No scoped names are supported; ideally we could put all non-standard |
| 2296 | // attributes into namespaces. |
| 2297 | if (!ScopeName) { |
| 2298 | switch(AttributeList::getKind(AttrName)) |
| 2299 | { |
| 2300 | // No arguments |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2301 | case AttributeList::AT_carries_dependency: |
Anders Carlsson | 15e14a2 | 2011-01-23 21:33:18 +0000 | [diff] [blame] | 2302 | case AttributeList::AT_noreturn: { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2303 | if (Tok.is(tok::l_paren)) { |
| 2304 | Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments) |
| 2305 | << AttrName->getName(); |
| 2306 | break; |
| 2307 | } |
| 2308 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2309 | attrs.add(AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc, 0, |
| 2310 | SourceLocation(), 0, 0, false, true)); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2311 | AttrParsed = true; |
| 2312 | break; |
| 2313 | } |
| 2314 | |
| 2315 | // One argument; must be a type-id or assignment-expression |
| 2316 | case AttributeList::AT_aligned: { |
| 2317 | if (Tok.isNot(tok::l_paren)) { |
| 2318 | Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments) |
| 2319 | << AttrName->getName(); |
| 2320 | break; |
| 2321 | } |
| 2322 | SourceLocation ParamLoc = ConsumeParen(); |
| 2323 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2324 | ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2325 | |
| 2326 | MatchRHSPunctuation(tok::r_paren, ParamLoc); |
| 2327 | |
| 2328 | ExprVector ArgExprs(Actions); |
| 2329 | ArgExprs.push_back(ArgExpr.release()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2330 | attrs.add(AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc, |
| 2331 | 0, ParamLoc, ArgExprs.take(), 1, |
| 2332 | false, true)); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2333 | |
| 2334 | AttrParsed = true; |
| 2335 | break; |
| 2336 | } |
| 2337 | |
| 2338 | // Silence warnings |
| 2339 | default: break; |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | // Skip the entire parameter clause, if any |
| 2344 | if (!AttrParsed && Tok.is(tok::l_paren)) { |
| 2345 | ConsumeParen(); |
| 2346 | // SkipUntil maintains the balancedness of tokens. |
| 2347 | SkipUntil(tok::r_paren, false); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2352 | SkipUntil(tok::r_square, false); |
| 2353 | Loc = Tok.getLocation(); |
| 2354 | if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) |
| 2355 | SkipUntil(tok::r_square, false); |
| 2356 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2357 | attrs.Range = SourceRange(StartLoc, Loc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | /// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]] |
| 2361 | /// attribute. |
| 2362 | /// |
| 2363 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2364 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2365 | /// |
| 2366 | /// [C++0x] 'align' '(' type-id ')' |
| 2367 | /// [C++0x] 'align' '(' assignment-expression ')' |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2368 | ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2369 | if (isTypeIdInParens()) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2370 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2371 | SourceLocation TypeLoc = Tok.getLocation(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2372 | ParsedType Ty = ParseTypeName().get(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2373 | SourceRange TypeRange(Start, Tok.getLocation()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2374 | return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, |
| 2375 | Ty.getAsOpaquePtr(), TypeRange); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2376 | } else |
| 2377 | return ParseConstantExpression(); |
| 2378 | } |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2379 | |
| 2380 | /// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr] |
| 2381 | /// |
| 2382 | /// [MS] ms-attribute: |
| 2383 | /// '[' token-seq ']' |
| 2384 | /// |
| 2385 | /// [MS] ms-attribute-seq: |
| 2386 | /// ms-attribute[opt] |
| 2387 | /// ms-attribute ms-attribute-seq |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2388 | void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs, |
| 2389 | SourceLocation *endLoc) { |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2390 | assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); |
| 2391 | |
| 2392 | while (Tok.is(tok::l_square)) { |
| 2393 | ConsumeBracket(); |
| 2394 | SkipUntil(tok::r_square, true, true); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2395 | if (endLoc) *endLoc = Tok.getLocation(); |
Francois Pichet | 334d47e | 2010-10-11 12:59:39 +0000 | [diff] [blame] | 2396 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); |
| 2397 | } |
| 2398 | } |