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