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