Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 15 | #include "clang/Sema/CXXFieldCollector.h" |
| 16 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
| 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DeclSpec.h" |
| 29 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 30 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 34 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 35 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
| 38 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // CheckDefaultArgumentVisitor |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 45 | /// the default argument of a parameter to determine whether it |
| 46 | /// contains any ill-formed subexpressions. For example, this will |
| 47 | /// diagnose the use of local variables or parameters within the |
| 48 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 49 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 50 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | Expr *DefaultArg; |
| 52 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | bool VisitExpr(Expr *Node); |
| 59 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 60 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | /// VisitExpr - Visit all of the children of this expression. |
| 64 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 65 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 66 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 67 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 68 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 71 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 72 | /// determine whether this declaration can be used in the default |
| 73 | /// argument expression. |
| 74 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 75 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 76 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 77 | // C++ [dcl.fct.default]p9 |
| 78 | // Default arguments are evaluated each time the function is |
| 79 | // called. The order of evaluation of function arguments is |
| 80 | // unspecified. Consequently, parameters of a function shall not |
| 81 | // be used in default argument expressions, even if they are not |
| 82 | // evaluated. Parameters of a function declared before a default |
| 83 | // argument expression are in scope and can hide namespace and |
| 84 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 86 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 87 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 88 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 89 | // C++ [dcl.fct.default]p7 |
| 90 | // Local variables shall not be used in default argument |
| 91 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 92 | if (VDecl->isLocalVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 94 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 95 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 96 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 97 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 100 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 101 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 102 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 103 | // C++ [dcl.fct.default]p8: |
| 104 | // The keyword this shall not be used in a default argument of a |
| 105 | // member function. |
| 106 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 107 | diag::err_param_default_argument_references_this) |
| 108 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 112 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 113 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 115 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 116 | diag::err_typecheck_decl_incomplete_type)) { |
| 117 | Param->setInvalidDecl(); |
| 118 | return true; |
| 119 | } |
| 120 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 121 | // C++ [dcl.fct.default]p5 |
| 122 | // A default argument expression is implicitly converted (clause |
| 123 | // 4) to the parameter type. The default argument expression has |
| 124 | // the same semantic constraints as the initializer expression in |
| 125 | // a declaration of a variable of the parameter type, using the |
| 126 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 127 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 128 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 129 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 130 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 131 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 132 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 133 | MultiExprArg(*this, &Arg, 1)); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 134 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 135 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 136 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 137 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 138 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 139 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 141 | // Okay: add the default argument to the parameter |
| 142 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 144 | // We have already instantiated this parameter; provide each of the |
| 145 | // instantiations with the uninstantiated default argument. |
| 146 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 147 | = UnparsedDefaultArgInstantiations.find(Param); |
| 148 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 149 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 150 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 151 | |
| 152 | // We're done tracking this parameter's instantiations. |
| 153 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 154 | } |
| 155 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 156 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 159 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 160 | /// provided for a function parameter is well-formed. If so, attach it |
| 161 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 162 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 163 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 164 | Expr *DefaultArg) { |
| 165 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 166 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 168 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 169 | UnparsedDefaultArgLocs.erase(Param); |
| 170 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 171 | // Default arguments are only permitted in C++ |
| 172 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 173 | Diag(EqualLoc, diag::err_param_default_argument) |
| 174 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 175 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 179 | // Check for unexpanded parameter packs. |
| 180 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 181 | Param->setInvalidDecl(); |
| 182 | return; |
| 183 | } |
| 184 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 185 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 186 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 187 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 188 | Param->setInvalidDecl(); |
| 189 | return; |
| 190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 192 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 195 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 196 | /// argument for a function parameter, but we can't parse it yet |
| 197 | /// because we're inside a class definition. Note that this default |
| 198 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 199 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 200 | SourceLocation EqualLoc, |
| 201 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 202 | if (!param) |
| 203 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 205 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 206 | if (Param) |
| 207 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 209 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 212 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 213 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 214 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 215 | if (!param) |
| 216 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 218 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 220 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 222 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 225 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 226 | /// arguments in the declarator, which is not a function declaration |
| 227 | /// or definition and therefore is not permitted to have default |
| 228 | /// arguments. This routine should be invoked for every declarator |
| 229 | /// that is not a function declaration or definition. |
| 230 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 231 | // C++ [dcl.fct.default]p3 |
| 232 | // A default argument expression shall be specified only in the |
| 233 | // parameter-declaration-clause of a function declaration or in a |
| 234 | // template-parameter (14.1). It shall not be specified for a |
| 235 | // parameter pack. If it is specified in a |
| 236 | // parameter-declaration-clause, it shall not occur within a |
| 237 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 238 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 239 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 240 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 241 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 242 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 243 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 244 | if (Param->hasUnparsedDefaultArg()) { |
| 245 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 246 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 247 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 248 | delete Toks; |
| 249 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 250 | } else if (Param->getDefaultArg()) { |
| 251 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 252 | << Param->getDefaultArg()->getSourceRange(); |
| 253 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 260 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 261 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 262 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 263 | // error, false otherwise. |
| 264 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 265 | bool Invalid = false; |
| 266 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 267 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 268 | // For non-template functions, default arguments can be added in |
| 269 | // later declarations of a function in the same |
| 270 | // scope. Declarations in different scopes have completely |
| 271 | // distinct sets of default arguments. That is, declarations in |
| 272 | // inner scopes do not acquire default arguments from |
| 273 | // declarations in outer scopes, and vice versa. In a given |
| 274 | // function declaration, all parameters subsequent to a |
| 275 | // parameter with a default argument shall have default |
| 276 | // arguments supplied in this or previous declarations. A |
| 277 | // default argument shall not be redefined by a later |
| 278 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 279 | // |
| 280 | // C++ [dcl.fct.default]p6: |
| 281 | // Except for member functions of class templates, the default arguments |
| 282 | // in a member function definition that appears outside of the class |
| 283 | // definition are added to the set of default arguments provided by the |
| 284 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 285 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 286 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 287 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 288 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 289 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 290 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 291 | // hint here. Alternatively, we could walk the type-source information |
| 292 | // for NewParam to find the last source location in the type... but it |
| 293 | // isn't worth the effort right now. This is the kind of test case that |
| 294 | // is hard to get right: |
| 295 | |
| 296 | // int f(int); |
| 297 | // void g(int (*fp)(int) = f); |
| 298 | // void g(int (*fp)(int) = &f); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 300 | diag::err_param_default_argument_redefinition) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 301 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 302 | |
| 303 | // Look for the function declaration where the default argument was |
| 304 | // actually written, which may be a declaration prior to Old. |
| 305 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 306 | Older; Older = Older->getPreviousDeclaration()) { |
| 307 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 308 | break; |
| 309 | |
| 310 | OldParam = Older->getParamDecl(p); |
| 311 | } |
| 312 | |
| 313 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 314 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 315 | Invalid = true; |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 316 | } else if (OldParam->hasDefaultArg()) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 317 | // Merge the old default argument into the new parameter. |
| 318 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 319 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 320 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 321 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 322 | NewParam->setUninstantiatedDefaultArg( |
| 323 | OldParam->getUninstantiatedDefaultArg()); |
| 324 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 325 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 326 | } else if (NewParam->hasDefaultArg()) { |
| 327 | if (New->getDescribedFunctionTemplate()) { |
| 328 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 329 | Diag(NewParam->getLocation(), |
| 330 | diag::err_param_default_argument_template_redecl) |
| 331 | << NewParam->getDefaultArgRange(); |
| 332 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 333 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 334 | } else if (New->getTemplateSpecializationKind() |
| 335 | != TSK_ImplicitInstantiation && |
| 336 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 337 | // C++ [temp.expr.spec]p21: |
| 338 | // Default function arguments shall not be specified in a declaration |
| 339 | // or a definition for one of the following explicit specializations: |
| 340 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 341 | // - the explicit specialization of a member function template; |
| 342 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 343 | // template where the class template specialization to which the |
| 344 | // member function specialization belongs is implicitly |
| 345 | // instantiated. |
| 346 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 347 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 348 | << New->getDeclName() |
| 349 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 350 | } else if (New->getDeclContext()->isDependentContext()) { |
| 351 | // C++ [dcl.fct.default]p6 (DR217): |
| 352 | // Default arguments for a member function of a class template shall |
| 353 | // be specified on the initial declaration of the member function |
| 354 | // within the class template. |
| 355 | // |
| 356 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 357 | // leads me to the conclusion that one cannot add default function |
| 358 | // arguments for an out-of-line definition of a member function of a |
| 359 | // dependent type. |
| 360 | int WhichKind = 2; |
| 361 | if (CXXRecordDecl *Record |
| 362 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 363 | if (Record->getDescribedClassTemplate()) |
| 364 | WhichKind = 0; |
| 365 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 366 | WhichKind = 1; |
| 367 | else |
| 368 | WhichKind = 2; |
| 369 | } |
| 370 | |
| 371 | Diag(NewParam->getLocation(), |
| 372 | diag::err_param_default_argument_member_template_redecl) |
| 373 | << WhichKind |
| 374 | << NewParam->getDefaultArgRange(); |
| 375 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 379 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 380 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 382 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 386 | /// function declaration are well-formed according to C++ |
| 387 | /// [dcl.fct.default]. |
| 388 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 389 | unsigned NumParams = FD->getNumParams(); |
| 390 | unsigned p; |
| 391 | |
| 392 | // Find first parameter with a default argument |
| 393 | for (p = 0; p < NumParams; ++p) { |
| 394 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 395 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | |
| 399 | // C++ [dcl.fct.default]p4: |
| 400 | // In a given function declaration, all parameters |
| 401 | // subsequent to a parameter with a default argument shall |
| 402 | // have default arguments supplied in this or previous |
| 403 | // declarations. A default argument shall not be redefined |
| 404 | // by a later declaration (not even to the same value). |
| 405 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 407 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 408 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 409 | if (Param->isInvalidDecl()) |
| 410 | /* We already complained about this parameter. */; |
| 411 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 413 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 414 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 415 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 417 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 419 | LastMissingDefaultArg = p; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (LastMissingDefaultArg > 0) { |
| 424 | // Some default arguments were missing. Clear out all of the |
| 425 | // default arguments up to (and including) the last missing |
| 426 | // default argument, so that we leave the function parameters |
| 427 | // in a semantically valid state. |
| 428 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 429 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 430 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 431 | Param->setDefaultArg(0); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 437 | /// isCurrentClassName - Determine whether the identifier II is the |
| 438 | /// name of the class type currently being defined. In the case of |
| 439 | /// nested classes, this will only return true if II is the name of |
| 440 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 441 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 442 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 443 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 444 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 445 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 446 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 447 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 448 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 449 | } else |
| 450 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 451 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 452 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 453 | return &II == CurDecl->getIdentifier(); |
| 454 | else |
| 455 | return false; |
| 456 | } |
| 457 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 459 | /// |
| 460 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 461 | /// and returns NULL otherwise. |
| 462 | CXXBaseSpecifier * |
| 463 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 464 | SourceRange SpecifierRange, |
| 465 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 466 | TypeSourceInfo *TInfo, |
| 467 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 468 | QualType BaseType = TInfo->getType(); |
| 469 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 470 | // C++ [class.union]p1: |
| 471 | // A union shall not have base classes. |
| 472 | if (Class->isUnion()) { |
| 473 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 474 | << SpecifierRange; |
| 475 | return 0; |
| 476 | } |
| 477 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 478 | if (EllipsisLoc.isValid() && |
| 479 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 480 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 481 | << TInfo->getTypeLoc().getSourceRange(); |
| 482 | EllipsisLoc = SourceLocation(); |
| 483 | } |
| 484 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 485 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 487 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 488 | Access, TInfo, EllipsisLoc); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 489 | |
| 490 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 491 | |
| 492 | // Base specifiers must be record types. |
| 493 | if (!BaseType->isRecordType()) { |
| 494 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | // C++ [class.union]p1: |
| 499 | // A union shall not be used as a base class. |
| 500 | if (BaseType->isUnionType()) { |
| 501 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | // C++ [class.derived]p2: |
| 506 | // The class-name in a base-specifier shall not be an incompletely |
| 507 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 509 | PDiag(diag::err_incomplete_base_class) |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 510 | << SpecifierRange)) { |
| 511 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 512 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 513 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 514 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 515 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 516 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 517 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 518 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 519 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 520 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 521 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 522 | |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 523 | // C++ [class.derived]p2: |
| 524 | // If a class is marked with the class-virt-specifier final and it appears |
| 525 | // as a base-type-specifier in a base-clause (10 class.derived), the program |
| 526 | // is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 527 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 528 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 529 | << CXXBaseDecl->getDeclName(); |
| 530 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 531 | << CXXBaseDecl->getDeclName(); |
| 532 | return 0; |
| 533 | } |
| 534 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 535 | if (BaseDecl->isInvalidDecl()) |
| 536 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 537 | |
| 538 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 539 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 540 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 541 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 544 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 545 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | /// example: |
| 547 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 548 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 549 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 550 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 551 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 552 | ParsedType basetype, SourceLocation BaseLoc, |
| 553 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 554 | if (!classdecl) |
| 555 | return true; |
| 556 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 557 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 558 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 559 | if (!Class) |
| 560 | return true; |
| 561 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 562 | TypeSourceInfo *TInfo = 0; |
| 563 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 565 | if (EllipsisLoc.isInvalid() && |
| 566 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 567 | UPPC_BaseType)) |
| 568 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 570 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 571 | Virtual, Access, TInfo, |
| 572 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 573 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 575 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 576 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 577 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 578 | /// \brief Performs the actual work of attaching the given base class |
| 579 | /// specifiers to a C++ class. |
| 580 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 581 | unsigned NumBases) { |
| 582 | if (NumBases == 0) |
| 583 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 584 | |
| 585 | // Used to keep track of which base types we have already seen, so |
| 586 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 587 | // that the key is always the unqualified canonical type of the base |
| 588 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 589 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 590 | |
| 591 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 592 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 593 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 594 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 597 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Fariborz Jahanian | 0ed5c5d | 2010-05-20 23:34:56 +0000 | [diff] [blame] | 598 | if (!Class->hasObjectMember()) { |
| 599 | if (const RecordType *FDTTy = |
| 600 | NewBaseType.getTypePtr()->getAs<RecordType>()) |
| 601 | if (FDTTy->getDecl()->hasObjectMember()) |
| 602 | Class->setHasObjectMember(true); |
| 603 | } |
| 604 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 605 | if (KnownBaseTypes[NewBaseType]) { |
| 606 | // C++ [class.mi]p3: |
| 607 | // A class shall not be specified as a direct base class of a |
| 608 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 609 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 610 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 611 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 612 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 613 | |
| 614 | // Delete the duplicate base class specifier; we're going to |
| 615 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 616 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 617 | |
| 618 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 619 | } else { |
| 620 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 621 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 622 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 627 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 628 | |
| 629 | // Delete the remaining (good) base class specifiers, since their |
| 630 | // data has been copied into the CXXRecordDecl. |
| 631 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 632 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 633 | |
| 634 | return Invalid; |
| 635 | } |
| 636 | |
| 637 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 638 | /// class, after checking whether there are any duplicate base |
| 639 | /// classes. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 640 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | unsigned NumBases) { |
| 642 | if (!ClassDecl || !Bases || !NumBases) |
| 643 | return; |
| 644 | |
| 645 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 646 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 647 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 648 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 649 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 650 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 651 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 652 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 653 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 654 | return ICT->getDecl(); |
| 655 | else |
| 656 | return 0; |
| 657 | } |
| 658 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 659 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 660 | /// derived from the type \p Base. |
| 661 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 662 | if (!getLangOptions().CPlusPlus) |
| 663 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 664 | |
| 665 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 666 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 667 | return false; |
| 668 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 669 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 670 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 671 | return false; |
| 672 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 673 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 674 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 678 | /// derived from the type \p Base. |
| 679 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 680 | if (!getLangOptions().CPlusPlus) |
| 681 | return false; |
| 682 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 683 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 684 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 685 | return false; |
| 686 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 687 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 688 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 689 | return false; |
| 690 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 691 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 692 | } |
| 693 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 694 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 695 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 696 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 697 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 698 | |
| 699 | const CXXBasePath &Path = Paths.front(); |
| 700 | |
| 701 | // We first go backward and check if we have a virtual base. |
| 702 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 703 | // the nearest virtual base. |
| 704 | unsigned Start = 0; |
| 705 | for (unsigned I = Path.size(); I != 0; --I) { |
| 706 | if (Path[I - 1].Base->isVirtual()) { |
| 707 | Start = I - 1; |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // Now add all bases. |
| 713 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 714 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 717 | /// \brief Determine whether the given base path includes a virtual |
| 718 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 719 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 720 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 721 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 722 | B != BEnd; ++B) |
| 723 | if ((*B)->isVirtual()) |
| 724 | return true; |
| 725 | |
| 726 | return false; |
| 727 | } |
| 728 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 729 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 730 | /// conversion (where Derived and Base are class types) is |
| 731 | /// well-formed, meaning that the conversion is unambiguous (and |
| 732 | /// that all of the base classes are accessible). Returns true |
| 733 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 734 | /// otherwise. Loc is the location where this routine should point to |
| 735 | /// if there is an error, and Range is the source range to highlight |
| 736 | /// if there is an error. |
| 737 | bool |
| 738 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 739 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 740 | unsigned AmbigiousBaseConvID, |
| 741 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 742 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 743 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 744 | // First, determine whether the path from Derived to Base is |
| 745 | // ambiguous. This is slightly more expensive than checking whether |
| 746 | // the Derived to Base conversion exists, because here we need to |
| 747 | // explore multiple paths to determine if there is an ambiguity. |
| 748 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 749 | /*DetectVirtual=*/false); |
| 750 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 751 | assert(DerivationOkay && |
| 752 | "Can only be used with a derived-to-base conversion"); |
| 753 | (void)DerivationOkay; |
| 754 | |
| 755 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 756 | if (InaccessibleBaseID) { |
| 757 | // Check that the base class can be accessed. |
| 758 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 759 | InaccessibleBaseID)) { |
| 760 | case AR_inaccessible: |
| 761 | return true; |
| 762 | case AR_accessible: |
| 763 | case AR_dependent: |
| 764 | case AR_delayed: |
| 765 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 766 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 767 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 768 | |
| 769 | // Build a base path if necessary. |
| 770 | if (BasePath) |
| 771 | BuildBasePathArray(Paths, *BasePath); |
| 772 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // We know that the derived-to-base conversion is ambiguous, and |
| 776 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 777 | // search just one more time to compute all of the possible paths so |
| 778 | // that we can print them out. This is more expensive than any of |
| 779 | // the previous derived-to-base checks we've done, but at this point |
| 780 | // performance isn't as much of an issue. |
| 781 | Paths.clear(); |
| 782 | Paths.setRecordingPaths(true); |
| 783 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 784 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 785 | (void)StillOkay; |
| 786 | |
| 787 | // Build up a textual representation of the ambiguous paths, e.g., |
| 788 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 789 | // conversions in the diagnostic. We only print one of the paths |
| 790 | // to each base class subobject. |
| 791 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 792 | |
| 793 | Diag(Loc, AmbigiousBaseConvID) |
| 794 | << Derived << Base << PathDisplayStr << Range << Name; |
| 795 | return true; |
| 796 | } |
| 797 | |
| 798 | bool |
| 799 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 800 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 801 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 802 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 803 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 804 | IgnoreAccess ? 0 |
| 805 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 806 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 807 | Loc, Range, DeclarationName(), |
| 808 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | |
| 812 | /// @brief Builds a string representing ambiguous paths from a |
| 813 | /// specific derived class to different subobjects of the same base |
| 814 | /// class. |
| 815 | /// |
| 816 | /// This function builds a string that can be used in error messages |
| 817 | /// to show the different paths that one can take through the |
| 818 | /// inheritance hierarchy to go from the derived class to different |
| 819 | /// subobjects of a base class. The result looks something like this: |
| 820 | /// @code |
| 821 | /// struct D -> struct B -> struct A |
| 822 | /// struct D -> struct C -> struct A |
| 823 | /// @endcode |
| 824 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 825 | std::string PathDisplayStr; |
| 826 | std::set<unsigned> DisplayedPaths; |
| 827 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 828 | Path != Paths.end(); ++Path) { |
| 829 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 830 | // We haven't displayed a path to this particular base |
| 831 | // class subobject yet. |
| 832 | PathDisplayStr += "\n "; |
| 833 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 834 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 835 | Element != Path->end(); ++Element) |
| 836 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | return PathDisplayStr; |
| 841 | } |
| 842 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 843 | //===----------------------------------------------------------------------===// |
| 844 | // C++ class member Handling |
| 845 | //===----------------------------------------------------------------------===// |
| 846 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 847 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 848 | Decl *Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 849 | SourceLocation ASLoc, |
| 850 | SourceLocation ColonLoc) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 851 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 852 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 853 | ASLoc, ColonLoc); |
| 854 | CurContext->addHiddenDecl(ASDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 855 | return ASDecl; |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 858 | /// CheckOverrideControl - Check C++0x override control semantics. |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 859 | void Sema::CheckOverrideControl(const Decl *D) { |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 860 | const CXXMethodDecl *MD = llvm::dyn_cast<CXXMethodDecl>(D); |
| 861 | if (!MD || !MD->isVirtual()) |
| 862 | return; |
| 863 | |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 864 | if (MD->isDependentContext()) |
| 865 | return; |
| 866 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 867 | // C++0x [class.virtual]p3: |
| 868 | // If a virtual function is marked with the virt-specifier override and does |
| 869 | // not override a member function of a base class, |
| 870 | // the program is ill-formed. |
| 871 | bool HasOverriddenMethods = |
| 872 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 873 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) { |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 874 | Diag(MD->getLocation(), |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 875 | diag::err_function_marked_override_not_overriding) |
| 876 | << MD->getDeclName(); |
| 877 | return; |
| 878 | } |
Anders Carlsson | aa23d28 | 2011-01-22 22:23:37 +0000 | [diff] [blame] | 879 | |
| 880 | // C++0x [class.derived]p8: |
| 881 | // In a class definition marked with the class-virt-specifier explicit, |
| 882 | // if a virtual member function that is neither implicitly-declared nor a |
| 883 | // destructor overrides a member function of a base class and it is not |
| 884 | // marked with the virt-specifier override, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 885 | if (MD->getParent()->hasAttr<ExplicitAttr>() && !isa<CXXDestructorDecl>(MD) && |
| 886 | HasOverriddenMethods && !MD->hasAttr<OverrideAttr>()) { |
Anders Carlsson | aa23d28 | 2011-01-22 22:23:37 +0000 | [diff] [blame] | 887 | llvm::SmallVector<const CXXMethodDecl*, 4> |
| 888 | OverriddenMethods(MD->begin_overridden_methods(), |
| 889 | MD->end_overridden_methods()); |
| 890 | |
| 891 | Diag(MD->getLocation(), diag::err_function_overriding_without_override) |
| 892 | << MD->getDeclName() |
| 893 | << (unsigned)OverriddenMethods.size(); |
| 894 | |
| 895 | for (unsigned I = 0; I != OverriddenMethods.size(); ++I) |
| 896 | Diag(OverriddenMethods[I]->getLocation(), |
| 897 | diag::note_overridden_virtual_function); |
| 898 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 901 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
| 902 | /// function overrides a virtual member function marked 'final', according to |
| 903 | /// C++0x [class.virtual]p3. |
| 904 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 905 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 906 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 907 | return false; |
| 908 | |
| 909 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 910 | << New->getDeclName(); |
| 911 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 912 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 915 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 916 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 917 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 918 | /// any. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 919 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 920 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 921 | MultiTemplateParamsArg TemplateParameterLists, |
Anders Carlsson | 69a8735 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 922 | ExprTy *BW, const VirtSpecifiers &VS, |
| 923 | ExprTy *InitExpr, bool IsDefinition, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 924 | bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 925 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 926 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 927 | DeclarationName Name = NameInfo.getName(); |
| 928 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 929 | |
| 930 | // For anonymous bitfields, the location should point to the type. |
| 931 | if (Loc.isInvalid()) |
| 932 | Loc = D.getSourceRange().getBegin(); |
| 933 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 934 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 935 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 936 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 937 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 938 | assert(!DS.isFriendSpecified()); |
| 939 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 940 | bool isFunc = false; |
| 941 | if (D.isFunctionDeclarator()) |
| 942 | isFunc = true; |
| 943 | else if (D.getNumTypeObjects() == 0 && |
| 944 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 945 | QualType TDType = GetTypeFromParser(DS.getRepAsType()); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 946 | isFunc = TDType->isFunctionType(); |
| 947 | } |
| 948 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 949 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 950 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 951 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 952 | // data members and cannot be applied to names declared const or static, |
| 953 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 954 | switch (DS.getStorageClassSpec()) { |
| 955 | case DeclSpec::SCS_unspecified: |
| 956 | case DeclSpec::SCS_typedef: |
| 957 | case DeclSpec::SCS_static: |
| 958 | // FALL THROUGH. |
| 959 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 960 | case DeclSpec::SCS_mutable: |
| 961 | if (isFunc) { |
| 962 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 963 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 964 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 965 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 967 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 968 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 969 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 970 | } |
| 971 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 972 | default: |
| 973 | if (DS.getStorageClassSpecLoc().isValid()) |
| 974 | Diag(DS.getStorageClassSpecLoc(), |
| 975 | diag::err_storageclass_invalid_for_member); |
| 976 | else |
| 977 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 978 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 979 | } |
| 980 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 981 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 982 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 983 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 984 | |
| 985 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 986 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 987 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 988 | |
| 989 | |
| 990 | if (SS.isSet() && !SS.isInvalid()) { |
| 991 | // The user provided a superfluous scope specifier inside a class |
| 992 | // definition: |
| 993 | // |
| 994 | // class X { |
| 995 | // int X::member; |
| 996 | // }; |
| 997 | DeclContext *DC = 0; |
| 998 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 999 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 1000 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 1001 | else |
| 1002 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1003 | << Name << SS.getRange(); |
| 1004 | |
| 1005 | SS.clear(); |
| 1006 | } |
| 1007 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1008 | // FIXME: Check for template parameters! |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 1009 | // FIXME: Check that the name is an identifier! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1010 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 1011 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1012 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1013 | } else { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1014 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1015 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1016 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1017 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1018 | |
| 1019 | // Non-instance-fields can't have a bitfield. |
| 1020 | if (BitWidth) { |
| 1021 | if (Member->isInvalidDecl()) { |
| 1022 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1023 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1024 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1025 | // "static member 'A' cannot be a bit-field" |
| 1026 | Diag(Loc, diag::err_static_not_bitfield) |
| 1027 | << Name << BitWidth->getSourceRange(); |
| 1028 | } else if (isa<TypedefDecl>(Member)) { |
| 1029 | // "typedef member 'x' cannot be a bit-field" |
| 1030 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1031 | << Name << BitWidth->getSourceRange(); |
| 1032 | } else { |
| 1033 | // A function typedef ("typedef int f(); f a;"). |
| 1034 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1035 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1037 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1038 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1040 | BitWidth = 0; |
| 1041 | Member->setInvalidDecl(); |
| 1042 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1043 | |
| 1044 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1046 | // If we have declared a member function template, set the access of the |
| 1047 | // templated declaration as well. |
| 1048 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1049 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1050 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1051 | |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1052 | if (VS.isOverrideSpecified()) { |
| 1053 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1054 | if (!MD || !MD->isVirtual()) { |
| 1055 | Diag(Member->getLocStart(), |
| 1056 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1057 | << "override" << FixItHint::CreateRemoval(VS.getOverrideLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1058 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1059 | MD->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1060 | } |
| 1061 | if (VS.isFinalSpecified()) { |
| 1062 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1063 | if (!MD || !MD->isVirtual()) { |
| 1064 | Diag(Member->getLocStart(), |
| 1065 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1066 | << "final" << FixItHint::CreateRemoval(VS.getFinalLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1067 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1068 | MD->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1069 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1070 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1071 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1073 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1074 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 1075 | if (Init) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1076 | AddInitializerToDecl(Member, Init, false, |
| 1077 | DS.getTypeSpecType() == DeclSpec::TST_auto); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1078 | if (Deleted) // FIXME: Source location is not very good. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1079 | SetDeclDeleted(Member, D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1080 | |
John McCall | b25b295 | 2011-02-15 07:12:36 +0000 | [diff] [blame] | 1081 | if (isInstField) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1082 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1083 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1086 | /// \brief Find the direct and/or virtual base specifiers that |
| 1087 | /// correspond to the given base type, for use in base initialization |
| 1088 | /// within a constructor. |
| 1089 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1090 | CXXRecordDecl *ClassDecl, |
| 1091 | QualType BaseType, |
| 1092 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1093 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1094 | // First, check for a direct base class. |
| 1095 | DirectBaseSpec = 0; |
| 1096 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1097 | = ClassDecl->bases_begin(); |
| 1098 | Base != ClassDecl->bases_end(); ++Base) { |
| 1099 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1100 | // We found a direct base of this type. That's what we're |
| 1101 | // initializing. |
| 1102 | DirectBaseSpec = &*Base; |
| 1103 | break; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | // Check for a virtual base class. |
| 1108 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1109 | // there are no virtual bases. |
| 1110 | VirtualBaseSpec = 0; |
| 1111 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1112 | // We haven't found a base yet; search the class hierarchy for a |
| 1113 | // virtual base class. |
| 1114 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1115 | /*DetectVirtual=*/false); |
| 1116 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1117 | BaseType, Paths)) { |
| 1118 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1119 | Path != Paths.end(); ++Path) { |
| 1120 | if (Path->back().Base->isVirtual()) { |
| 1121 | VirtualBaseSpec = Path->back().Base; |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | return DirectBaseSpec || VirtualBaseSpec; |
| 1129 | } |
| 1130 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1131 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1132 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1133 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1134 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1135 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1136 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1137 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1138 | SourceLocation IdLoc, |
| 1139 | SourceLocation LParenLoc, |
| 1140 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1141 | SourceLocation RParenLoc, |
| 1142 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1143 | if (!ConstructorD) |
| 1144 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1146 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
| 1148 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1149 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1150 | if (!Constructor) { |
| 1151 | // The user wrote a constructor initializer on a function that is |
| 1152 | // not a C++ constructor. Ignore the error for now, because we may |
| 1153 | // have more member initializers coming; we'll diagnose it just |
| 1154 | // once in ActOnMemInitializers. |
| 1155 | return true; |
| 1156 | } |
| 1157 | |
| 1158 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1159 | |
| 1160 | // C++ [class.base.init]p2: |
| 1161 | // Names in a mem-initializer-id are looked up in the scope of the |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1162 | // constructor's class and, if not found in that scope, are looked |
| 1163 | // up in the scope containing the constructor's definition. |
| 1164 | // [Note: if the constructor's class contains a member with the |
| 1165 | // same name as a direct or virtual base class of the class, a |
| 1166 | // mem-initializer-id naming the member or base class and composed |
| 1167 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1168 | // mem-initializer-id for the hidden base class may be specified |
| 1169 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1170 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1171 | // Look for a member, first. |
| 1172 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1174 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1175 | if (Result.first != Result.second) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1176 | Member = dyn_cast<FieldDecl>(*Result.first); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1177 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1178 | if (Member) { |
| 1179 | if (EllipsisLoc.isValid()) |
| 1180 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1181 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1182 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1183 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1184 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1187 | // Handle anonymous union case. |
| 1188 | if (IndirectFieldDecl* IndirectField |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1189 | = dyn_cast<IndirectFieldDecl>(*Result.first)) { |
| 1190 | if (EllipsisLoc.isValid()) |
| 1191 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1192 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1193 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1194 | return BuildMemberInitializer(IndirectField, (Expr**)Args, |
| 1195 | NumArgs, IdLoc, |
| 1196 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1197 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1198 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1199 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1200 | // It didn't name a member, so see if it names a class. |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1201 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1202 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1205 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1206 | } else { |
| 1207 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1208 | LookupParsedName(R, S, &SS); |
| 1209 | |
| 1210 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1211 | if (!TyD) { |
| 1212 | if (R.isAmbiguous()) return true; |
| 1213 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1214 | // We don't want access-control diagnostics here. |
| 1215 | R.suppressDiagnostics(); |
| 1216 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1217 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1218 | bool NotUnknownSpecialization = false; |
| 1219 | DeclContext *DC = computeDeclContext(SS, false); |
| 1220 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1221 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1222 | |
| 1223 | if (!NotUnknownSpecialization) { |
| 1224 | // When the scope specifier can refer to a member of an unknown |
| 1225 | // specialization, we take it as a type name. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1226 | BaseType = CheckTypenameType(ETK_None, |
| 1227 | (NestedNameSpecifier *)SS.getScopeRep(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1228 | *MemberOrBase, SourceLocation(), |
| 1229 | SS.getRange(), IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1230 | if (BaseType.isNull()) |
| 1231 | return true; |
| 1232 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1233 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1234 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1238 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1239 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1240 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1241 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1242 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1243 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1244 | // We have found a non-static data member with a similar |
| 1245 | // name to what was typed; complain and initialize that |
| 1246 | // member. |
| 1247 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1248 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1249 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1250 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1251 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1252 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1253 | |
| 1254 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1255 | LParenLoc, RParenLoc); |
| 1256 | } |
| 1257 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1258 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1259 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1260 | if (FindBaseInitializer(*this, ClassDecl, |
| 1261 | Context.getTypeDeclType(Type), |
| 1262 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1263 | // We have found a direct or virtual base class with a |
| 1264 | // similar name to what was typed; complain and initialize |
| 1265 | // that base class. |
| 1266 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1267 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1268 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1269 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1270 | |
| 1271 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1272 | : VirtualBaseSpec; |
| 1273 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1274 | diag::note_base_class_specified_here) |
| 1275 | << BaseSpec->getType() |
| 1276 | << BaseSpec->getSourceRange(); |
| 1277 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1278 | TyD = Type; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1283 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1284 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1285 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1286 | return true; |
| 1287 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1290 | if (BaseType.isNull()) { |
| 1291 | BaseType = Context.getTypeDeclType(TyD); |
| 1292 | if (SS.isSet()) { |
| 1293 | NestedNameSpecifier *Qualifier = |
| 1294 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1295 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1296 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1297 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1298 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1302 | if (!TInfo) |
| 1303 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1304 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1305 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1306 | LParenLoc, RParenLoc, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1309 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1310 | /// containing the field that is being initialized. Returns true if there is an |
| 1311 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1312 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1313 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1314 | const ValueDecl *LhsField, |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1315 | SourceLocation *L) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1316 | assert(isa<FieldDecl>(LhsField) || isa<IndirectFieldDecl>(LhsField)); |
| 1317 | |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1318 | if (isa<CallExpr>(S)) { |
| 1319 | // Do not descend into function calls or constructors, as the use |
| 1320 | // of an uninitialized field may be valid. One would have to inspect |
| 1321 | // the contents of the function/ctor to determine if it is safe or not. |
| 1322 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1323 | // may be safe, depending on what the function/ctor does. |
| 1324 | return false; |
| 1325 | } |
| 1326 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1327 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1330 | // The member expression points to a static data member. |
| 1331 | assert(VD->isStaticDataMember() && |
| 1332 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1333 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1334 | return false; |
| 1335 | } |
| 1336 | |
| 1337 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1338 | // The member expression points to an enum. |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1342 | if (RhsField == LhsField) { |
| 1343 | // Initializing a field with itself. Throw a warning. |
| 1344 | // But wait; there are exceptions! |
| 1345 | // Exception #1: The field may not belong to this record. |
| 1346 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1347 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1348 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1349 | // Even though the field matches, it does not belong to this record. |
| 1350 | return false; |
| 1351 | } |
| 1352 | // None of the exceptions triggered; return true to indicate an |
| 1353 | // uninitialized field was used. |
| 1354 | *L = ME->getMemberLoc(); |
| 1355 | return true; |
| 1356 | } |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1357 | } else if (isa<SizeOfAlignOfExpr>(S)) { |
| 1358 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1359 | return false; |
| 1360 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1361 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1362 | // in the same expression but it would be rare; and weird). |
| 1363 | if (UOE->getOpcode() == UO_AddrOf) |
| 1364 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1365 | } |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1366 | for (Stmt::const_child_range it = S->children(); it; ++it) { |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1367 | if (!*it) { |
| 1368 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1369 | continue; |
| 1370 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1371 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1372 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1373 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1374 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1377 | MemInitResult |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1378 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr **Args, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1379 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1380 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1381 | SourceLocation RParenLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1382 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 1383 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 1384 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1385 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 1386 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 1387 | if (Member->isInvalidDecl()) |
| 1388 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1389 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1390 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1391 | // foo(foo) |
| 1392 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1393 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1394 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1395 | SourceLocation L; |
| 1396 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1397 | // FIXME: Return true in the case when other fields are used before being |
| 1398 | // uninitialized. For example, let this field be the i'th field. When |
| 1399 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1400 | // fields are used, as they are not yet initialized. |
| 1401 | // Right now we are only handling the case where the i'th field uses |
| 1402 | // itself in its initializer. |
| 1403 | Diag(L, diag::warn_field_is_uninit); |
| 1404 | } |
| 1405 | } |
| 1406 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1407 | bool HasDependentArg = false; |
| 1408 | for (unsigned i = 0; i < NumArgs; i++) |
| 1409 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1410 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1411 | Expr *Init; |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1412 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1413 | // Can't check initialization for a member of dependent type or when |
| 1414 | // any of the arguments are type-dependent expressions. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1415 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1416 | RParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1417 | |
| 1418 | // Erase any temporaries within this evaluation context; we're not |
| 1419 | // going to track them in the AST, since we'll be rebuilding the |
| 1420 | // ASTs during template instantiation. |
| 1421 | ExprTemporaries.erase( |
| 1422 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1423 | ExprTemporaries.end()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1424 | } else { |
| 1425 | // Initialize the member. |
| 1426 | InitializedEntity MemberEntity = |
| 1427 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 1428 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 1429 | InitializationKind Kind = |
| 1430 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1431 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1432 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1433 | |
| 1434 | ExprResult MemberInit = |
| 1435 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1436 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1437 | if (MemberInit.isInvalid()) |
| 1438 | return true; |
| 1439 | |
| 1440 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
| 1441 | |
| 1442 | // C++0x [class.base.init]p7: |
| 1443 | // The initialization of each base and member constitutes a |
| 1444 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1445 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1446 | if (MemberInit.isInvalid()) |
| 1447 | return true; |
| 1448 | |
| 1449 | // If we are in a dependent context, template instantiation will |
| 1450 | // perform this type-checking again. Just save the arguments that we |
| 1451 | // received in a ParenListExpr. |
| 1452 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1453 | // of the information that we have about the member |
| 1454 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1455 | // and this approach is far more likely to get the corner cases right. |
| 1456 | if (CurContext->isDependentContext()) |
| 1457 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1458 | RParenLoc); |
| 1459 | else |
| 1460 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1463 | if (DirectMember) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1464 | return new (Context) CXXCtorInitializer(Context, DirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1465 | IdLoc, LParenLoc, Init, |
| 1466 | RParenLoc); |
| 1467 | } else { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1468 | return new (Context) CXXCtorInitializer(Context, IndirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1469 | IdLoc, LParenLoc, Init, |
| 1470 | RParenLoc); |
| 1471 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1474 | MemInitResult |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1475 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, |
| 1476 | Expr **Args, unsigned NumArgs, |
| 1477 | SourceLocation LParenLoc, |
| 1478 | SourceLocation RParenLoc, |
| 1479 | CXXRecordDecl *ClassDecl, |
| 1480 | SourceLocation EllipsisLoc) { |
| 1481 | SourceLocation Loc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1482 | if (!LangOpts.CPlusPlus0x) |
| 1483 | return Diag(Loc, diag::err_delegation_0x_only) |
| 1484 | << TInfo->getTypeLoc().getLocalSourceRange(); |
| 1485 | |
| 1486 | return Diag(Loc, diag::err_delegation_unimplemented) |
| 1487 | << TInfo->getTypeLoc().getLocalSourceRange(); |
| 1488 | } |
| 1489 | |
| 1490 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1491 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1492 | Expr **Args, unsigned NumArgs, |
| 1493 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1494 | CXXRecordDecl *ClassDecl, |
| 1495 | SourceLocation EllipsisLoc) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1496 | bool HasDependentArg = false; |
| 1497 | for (unsigned i = 0; i < NumArgs; i++) |
| 1498 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1499 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1500 | SourceLocation BaseLoc |
| 1501 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1502 | |
| 1503 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1504 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1505 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1506 | |
| 1507 | // C++ [class.base.init]p2: |
| 1508 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1509 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1510 | // of that class, the mem-initializer is ill-formed. A |
| 1511 | // mem-initializer-list can initialize a base class using any |
| 1512 | // name that denotes that base class type. |
| 1513 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1514 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1515 | if (EllipsisLoc.isValid()) { |
| 1516 | // This is a pack expansion. |
| 1517 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 1518 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1519 | << SourceRange(BaseLoc, RParenLoc); |
| 1520 | |
| 1521 | EllipsisLoc = SourceLocation(); |
| 1522 | } |
| 1523 | } else { |
| 1524 | // Check for any unexpanded parameter packs. |
| 1525 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 1526 | return true; |
| 1527 | |
| 1528 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1529 | if (DiagnoseUnexpandedParameterPack(Args[I])) |
| 1530 | return true; |
| 1531 | } |
| 1532 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1533 | // Check for direct and virtual base classes. |
| 1534 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1535 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1536 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1537 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 1538 | BaseType)) |
| 1539 | return BuildDelegatingInitializer(BaseTInfo, Args, NumArgs, |
| 1540 | LParenLoc, RParenLoc, ClassDecl, |
| 1541 | EllipsisLoc); |
| 1542 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1543 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1544 | VirtualBaseSpec); |
| 1545 | |
| 1546 | // C++ [base.class.init]p2: |
| 1547 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1548 | // constructor's class or a direct or virtual base of that class, the |
| 1549 | // mem-initializer is ill-formed. |
| 1550 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1551 | // If the class has any dependent bases, then it's possible that |
| 1552 | // one of those types will resolve to the same type as |
| 1553 | // BaseType. Therefore, just treat this as a dependent base |
| 1554 | // class initialization. FIXME: Should we try to check the |
| 1555 | // initialization anyway? It seems odd. |
| 1556 | if (ClassDecl->hasAnyDependentBases()) |
| 1557 | Dependent = true; |
| 1558 | else |
| 1559 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1560 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1561 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1566 | // Can't check initialization for a base of dependent type or when |
| 1567 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1568 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1569 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1570 | RParenLoc)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1571 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1572 | // Erase any temporaries within this evaluation context; we're not |
| 1573 | // going to track them in the AST, since we'll be rebuilding the |
| 1574 | // ASTs during template instantiation. |
| 1575 | ExprTemporaries.erase( |
| 1576 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1577 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1579 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1580 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1581 | LParenLoc, |
| 1582 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1583 | RParenLoc, |
| 1584 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1585 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1586 | |
| 1587 | // C++ [base.class.init]p2: |
| 1588 | // If a mem-initializer-id is ambiguous because it designates both |
| 1589 | // a direct non-virtual base class and an inherited virtual base |
| 1590 | // class, the mem-initializer is ill-formed. |
| 1591 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1592 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1593 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1594 | |
| 1595 | CXXBaseSpecifier *BaseSpec |
| 1596 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1597 | if (!BaseSpec) |
| 1598 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1599 | |
| 1600 | // Initialize the base. |
| 1601 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1602 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1603 | InitializationKind Kind = |
| 1604 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1605 | |
| 1606 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1607 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1608 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1609 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1610 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1611 | if (BaseInit.isInvalid()) |
| 1612 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1613 | |
| 1614 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1615 | |
| 1616 | // C++0x [class.base.init]p7: |
| 1617 | // The initialization of each base and member constitutes a |
| 1618 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1619 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1620 | if (BaseInit.isInvalid()) |
| 1621 | return true; |
| 1622 | |
| 1623 | // If we are in a dependent context, template instantiation will |
| 1624 | // perform this type-checking again. Just save the arguments that we |
| 1625 | // received in a ParenListExpr. |
| 1626 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1627 | // of the information that we have about the base |
| 1628 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1629 | // and this approach is far more likely to get the corner cases right. |
| 1630 | if (CurContext->isDependentContext()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1631 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1632 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1633 | RParenLoc)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1634 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1635 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1636 | LParenLoc, |
| 1637 | Init.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1638 | RParenLoc, |
| 1639 | EllipsisLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1642 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1643 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1644 | LParenLoc, |
| 1645 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1646 | RParenLoc, |
| 1647 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1650 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1651 | /// initialize its base or member. |
| 1652 | enum ImplicitInitializerKind { |
| 1653 | IIK_Default, |
| 1654 | IIK_Copy, |
| 1655 | IIK_Move |
| 1656 | }; |
| 1657 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1658 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1659 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1660 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1661 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1662 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1663 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1664 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1665 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1666 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1667 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1668 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1669 | |
| 1670 | switch (ImplicitInitKind) { |
| 1671 | case IIK_Default: { |
| 1672 | InitializationKind InitKind |
| 1673 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1674 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1675 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1676 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1677 | break; |
| 1678 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1679 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1680 | case IIK_Copy: { |
| 1681 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1682 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1683 | |
| 1684 | Expr *CopyCtorArg = |
| 1685 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1686 | Constructor->getLocation(), ParamType, |
| 1687 | VK_LValue, 0); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1688 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1689 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1690 | QualType ArgTy = |
| 1691 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1692 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1693 | |
| 1694 | CXXCastPath BasePath; |
| 1695 | BasePath.push_back(BaseSpec); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1696 | SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1697 | CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1698 | VK_LValue, &BasePath); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1699 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1700 | InitializationKind InitKind |
| 1701 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1702 | SourceLocation(), SourceLocation()); |
| 1703 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1704 | &CopyCtorArg, 1); |
| 1705 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1706 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1707 | break; |
| 1708 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1709 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1710 | case IIK_Move: |
| 1711 | assert(false && "Unhandled initializer kind!"); |
| 1712 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1714 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1715 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1716 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1717 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1718 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1719 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1720 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1721 | SourceLocation()), |
| 1722 | BaseSpec->isVirtual(), |
| 1723 | SourceLocation(), |
| 1724 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1725 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1726 | SourceLocation()); |
| 1727 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1728 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1731 | static bool |
| 1732 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1733 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1734 | FieldDecl *Field, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1735 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1736 | if (Field->isInvalidDecl()) |
| 1737 | return true; |
| 1738 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1739 | SourceLocation Loc = Constructor->getLocation(); |
| 1740 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1741 | if (ImplicitInitKind == IIK_Copy) { |
| 1742 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1743 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1744 | |
| 1745 | Expr *MemberExprBase = |
| 1746 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1747 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Build a reference to this field within the parameter. |
| 1750 | CXXScopeSpec SS; |
| 1751 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1752 | Sema::LookupMemberName); |
| 1753 | MemberLookup.addDecl(Field, AS_public); |
| 1754 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1755 | ExprResult CopyCtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1756 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1757 | ParamType, Loc, |
| 1758 | /*IsArrow=*/false, |
| 1759 | SS, |
| 1760 | /*FirstQualifierInScope=*/0, |
| 1761 | MemberLookup, |
| 1762 | /*TemplateArgs=*/0); |
| 1763 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1764 | return true; |
| 1765 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1766 | // When the field we are copying is an array, create index variables for |
| 1767 | // each dimension of the array. We use these index variables to subscript |
| 1768 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1769 | // necessary iteration with these index variables. |
| 1770 | llvm::SmallVector<VarDecl *, 4> IndexVariables; |
| 1771 | QualType BaseType = Field->getType(); |
| 1772 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1773 | while (const ConstantArrayType *Array |
| 1774 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1775 | // Create the iteration variable for this array index. |
| 1776 | IdentifierInfo *IterationVarName = 0; |
| 1777 | { |
| 1778 | llvm::SmallString<8> Str; |
| 1779 | llvm::raw_svector_ostream OS(Str); |
| 1780 | OS << "__i" << IndexVariables.size(); |
| 1781 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 1782 | } |
| 1783 | VarDecl *IterationVar |
| 1784 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, |
| 1785 | IterationVarName, SizeType, |
| 1786 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1787 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1788 | IndexVariables.push_back(IterationVar); |
| 1789 | |
| 1790 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1791 | ExprResult IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1792 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1793 | assert(!IterationVarRef.isInvalid() && |
| 1794 | "Reference to invented variable cannot fail!"); |
| 1795 | |
| 1796 | // Subscript the array with this iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1797 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1798 | Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1799 | IterationVarRef.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1800 | Loc); |
| 1801 | if (CopyCtorArg.isInvalid()) |
| 1802 | return true; |
| 1803 | |
| 1804 | BaseType = Array->getElementType(); |
| 1805 | } |
| 1806 | |
| 1807 | // Construct the entity that we will be initializing. For an array, this |
| 1808 | // will be first element in the array, which may require several levels |
| 1809 | // of array-subscript entities. |
| 1810 | llvm::SmallVector<InitializedEntity, 4> Entities; |
| 1811 | Entities.reserve(1 + IndexVariables.size()); |
| 1812 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| 1813 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 1814 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 1815 | 0, |
| 1816 | Entities.back())); |
| 1817 | |
| 1818 | // Direct-initialize to use the copy constructor. |
| 1819 | InitializationKind InitKind = |
| 1820 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 1821 | |
| 1822 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 1823 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 1824 | &CopyCtorArgE, 1); |
| 1825 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1826 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1827 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1828 | MultiExprArg(&CopyCtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1829 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1830 | if (MemberInit.isInvalid()) |
| 1831 | return true; |
| 1832 | |
| 1833 | CXXMemberInit |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1834 | = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1835 | MemberInit.takeAs<Expr>(), Loc, |
| 1836 | IndexVariables.data(), |
| 1837 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1838 | return false; |
| 1839 | } |
| 1840 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1841 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1842 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1843 | QualType FieldBaseElementType = |
| 1844 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1845 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1846 | if (FieldBaseElementType->isRecordType()) { |
| 1847 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1848 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1849 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1850 | |
| 1851 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1852 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1853 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1855 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1856 | if (MemberInit.isInvalid()) |
| 1857 | return true; |
| 1858 | |
| 1859 | CXXMemberInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1860 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1861 | Field, Loc, Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1862 | MemberInit.get(), |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1863 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1864 | return false; |
| 1865 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1866 | |
| 1867 | if (FieldBaseElementType->isReferenceType()) { |
| 1868 | SemaRef.Diag(Constructor->getLocation(), |
| 1869 | diag::err_uninitialized_member_in_ctor) |
| 1870 | << (int)Constructor->isImplicit() |
| 1871 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1872 | << 0 << Field->getDeclName(); |
| 1873 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
| 1877 | if (FieldBaseElementType.isConstQualified()) { |
| 1878 | SemaRef.Diag(Constructor->getLocation(), |
| 1879 | diag::err_uninitialized_member_in_ctor) |
| 1880 | << (int)Constructor->isImplicit() |
| 1881 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1882 | << 1 << Field->getDeclName(); |
| 1883 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1884 | return true; |
| 1885 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1886 | |
| 1887 | // Nothing to initialize. |
| 1888 | CXXMemberInit = 0; |
| 1889 | return false; |
| 1890 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1891 | |
| 1892 | namespace { |
| 1893 | struct BaseAndFieldInfo { |
| 1894 | Sema &S; |
| 1895 | CXXConstructorDecl *Ctor; |
| 1896 | bool AnyErrorsInInits; |
| 1897 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1898 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
| 1899 | llvm::SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1900 | |
| 1901 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 1902 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 1903 | // FIXME: Handle implicit move constructors. |
| 1904 | if (Ctor->isImplicit() && Ctor->isCopyConstructor()) |
| 1905 | IIK = IIK_Copy; |
| 1906 | else |
| 1907 | IIK = IIK_Default; |
| 1908 | } |
| 1909 | }; |
| 1910 | } |
| 1911 | |
| 1912 | static bool CollectFieldInitializer(BaseAndFieldInfo &Info, |
| 1913 | FieldDecl *Top, FieldDecl *Field) { |
| 1914 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1915 | // Overwhelmingly common case: we have a direct initializer for this field. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1916 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1917 | Info.AllToInit.push_back(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1918 | return false; |
| 1919 | } |
| 1920 | |
| 1921 | if (Info.IIK == IIK_Default && Field->isAnonymousStructOrUnion()) { |
| 1922 | const RecordType *FieldClassType = Field->getType()->getAs<RecordType>(); |
| 1923 | assert(FieldClassType && "anonymous struct/union without record type"); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1924 | CXXRecordDecl *FieldClassDecl |
| 1925 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1926 | |
| 1927 | // Even though union members never have non-trivial default |
| 1928 | // constructions in C++03, we still build member initializers for aggregate |
| 1929 | // record types which can be union members, and C++0x allows non-trivial |
| 1930 | // default constructors for union members, so we ensure that only one |
| 1931 | // member is initialized for these. |
| 1932 | if (FieldClassDecl->isUnion()) { |
| 1933 | // First check for an explicit initializer for one field. |
| 1934 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1935 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1936 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(*FA)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1937 | Info.AllToInit.push_back(Init); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Once we've initialized a field of an anonymous union, the union |
| 1940 | // field in the class is also initialized, so exit immediately. |
| 1941 | return false; |
Argyrios Kyrtzidis | 881b36c | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 1942 | } else if ((*FA)->isAnonymousStructOrUnion()) { |
| 1943 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1944 | return true; |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | // Fallthrough and construct a default initializer for the union as |
| 1949 | // a whole, which can call its default constructor if such a thing exists |
| 1950 | // (C++0x perhaps). FIXME: It's not clear that this is the correct |
| 1951 | // behavior going forward with C++0x, when anonymous unions there are |
| 1952 | // finalized, we should revisit this. |
| 1953 | } else { |
| 1954 | // For structs, we simply descend through to initialize all members where |
| 1955 | // necessary. |
| 1956 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1957 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1958 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1959 | return true; |
| 1960 | } |
| 1961 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | // Don't try to build an implicit initializer if there were semantic |
| 1965 | // errors in any of the initializers (and therefore we might be |
| 1966 | // missing some that the user actually wrote). |
| 1967 | if (Info.AnyErrorsInInits) |
| 1968 | return false; |
| 1969 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1970 | CXXCtorInitializer *Init = 0; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1971 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, Init)) |
| 1972 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1973 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1974 | if (Init) |
| 1975 | Info.AllToInit.push_back(Init); |
| 1976 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1977 | return false; |
| 1978 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1979 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1980 | bool |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1981 | Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 1982 | CXXCtorInitializer **Initializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1983 | unsigned NumInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1984 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1985 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1986 | // Just store the initializers as written, they will be checked during |
| 1987 | // instantiation. |
| 1988 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1989 | Constructor->setNumCtorInitializers(NumInitializers); |
| 1990 | CXXCtorInitializer **baseOrMemberInitializers = |
| 1991 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1992 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1993 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 1994 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | return false; |
| 1998 | } |
| 1999 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2000 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2001 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2002 | // We need to build the initializer AST according to order of construction |
| 2003 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2004 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 2005 | if (!ClassDecl) |
| 2006 | return true; |
| 2007 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2008 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2010 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2011 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2012 | |
| 2013 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2014 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2015 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2016 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2019 | // Keep track of the direct virtual bases. |
| 2020 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 2021 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 2022 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 2023 | if (I->isVirtual()) |
| 2024 | DirectVBases.insert(I); |
| 2025 | } |
| 2026 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2027 | // Push virtual bases before others. |
| 2028 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2029 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 2030 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2031 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2032 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 2033 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2034 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2035 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2036 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2037 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2038 | VBase, IsInheritedVirtualBase, |
| 2039 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2040 | HadError = true; |
| 2041 | continue; |
| 2042 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2043 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2044 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2045 | } |
| 2046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2048 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2049 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2050 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 2051 | // Virtuals are in the virtual base list and already constructed. |
| 2052 | if (Base->isVirtual()) |
| 2053 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2055 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2056 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 2057 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2058 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2059 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2060 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2061 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2062 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2063 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2064 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2065 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 2066 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2067 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2068 | } |
| 2069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2071 | // Fields. |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2072 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2073 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 2074 | if ((*Field)->getType()->isIncompleteArrayType()) { |
| 2075 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2076 | "Incomplete array type is not valid"); |
| 2077 | continue; |
| 2078 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2079 | if (CollectFieldInitializer(Info, *Field, *Field)) |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2080 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2081 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2083 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2084 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2085 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2086 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2087 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2088 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2089 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2090 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 2091 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2092 | // Constructors implicitly reference the base and member |
| 2093 | // destructors. |
| 2094 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 2095 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2096 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2097 | |
| 2098 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2101 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 2102 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2103 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2104 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 2105 | return static_cast<void *>(RT->getDecl()); |
| 2106 | } |
| 2107 | return static_cast<void *>(Field); |
| 2108 | } |
| 2109 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2110 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2111 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2114 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2115 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2116 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2117 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2118 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2119 | // For fields injected into the class via declaration of an anonymous union, |
| 2120 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2121 | FieldDecl *Field = Member->getAnyMember(); |
| 2122 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2123 | // If the field is a member of an anonymous struct or union, our key |
| 2124 | // is the anonymous record decl that's a direct child of the class. |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 2125 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2126 | if (RD->isAnonymousStructOrUnion()) { |
| 2127 | while (true) { |
| 2128 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 2129 | if (Parent->isAnonymousStructOrUnion()) |
| 2130 | RD = Parent; |
| 2131 | else |
| 2132 | break; |
| 2133 | } |
| 2134 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 2135 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2136 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2138 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2141 | static void |
| 2142 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2143 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2144 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2145 | unsigned NumInits) { |
| 2146 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 2147 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2149 | // Don't check initializers order unless the warning is enabled at the |
| 2150 | // location of at least one initializer. |
| 2151 | bool ShouldCheckOrder = false; |
| 2152 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2153 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2154 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 2155 | Init->getSourceLocation()) |
| 2156 | != Diagnostic::Ignored) { |
| 2157 | ShouldCheckOrder = true; |
| 2158 | break; |
| 2159 | } |
| 2160 | } |
| 2161 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2162 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2163 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2164 | // Build the list of bases and members in the order that they'll |
| 2165 | // actually be initialized. The explicit initializers should be in |
| 2166 | // this same order but may be missing things. |
| 2167 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2169 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2170 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2171 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2172 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2173 | ClassDecl->vbases_begin(), |
| 2174 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2175 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2177 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2178 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2179 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2180 | if (Base->isVirtual()) |
| 2181 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2182 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2185 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2186 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2187 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2188 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2190 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2191 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2192 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2193 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2194 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2195 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2196 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2197 | |
| 2198 | // Scan forward to try to find this initializer in the idealized |
| 2199 | // initializers list. |
| 2200 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2201 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2202 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2203 | |
| 2204 | // If we didn't find this initializer, it must be because we |
| 2205 | // scanned past it on a previous iteration. That can only |
| 2206 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2207 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2208 | Sema::SemaDiagnosticBuilder D = |
| 2209 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2210 | diag::warn_initializer_out_of_order); |
| 2211 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2212 | if (PrevInit->isAnyMemberInitializer()) |
| 2213 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2214 | else |
| 2215 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2216 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2217 | if (Init->isAnyMemberInitializer()) |
| 2218 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2219 | else |
| 2220 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2221 | |
| 2222 | // Move back to the initializer's location in the ideal list. |
| 2223 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2224 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2225 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2226 | |
| 2227 | assert(IdealIndex != NumIdealInits && |
| 2228 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2229 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2230 | |
| 2231 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2232 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2235 | namespace { |
| 2236 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2237 | CXXCtorInitializer *Init, |
| 2238 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2239 | if (!PrevInit) { |
| 2240 | PrevInit = Init; |
| 2241 | return false; |
| 2242 | } |
| 2243 | |
| 2244 | if (FieldDecl *Field = Init->getMember()) |
| 2245 | S.Diag(Init->getSourceLocation(), |
| 2246 | diag::err_multiple_mem_initialization) |
| 2247 | << Field->getDeclName() |
| 2248 | << Init->getSourceRange(); |
| 2249 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2250 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2251 | assert(BaseClass && "neither field nor base"); |
| 2252 | S.Diag(Init->getSourceLocation(), |
| 2253 | diag::err_multiple_base_initialization) |
| 2254 | << QualType(BaseClass, 0) |
| 2255 | << Init->getSourceRange(); |
| 2256 | } |
| 2257 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2258 | << 0 << PrevInit->getSourceRange(); |
| 2259 | |
| 2260 | return true; |
| 2261 | } |
| 2262 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2263 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2264 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2265 | |
| 2266 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2267 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2268 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2269 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2270 | RecordDecl *Parent = Field->getParent(); |
| 2271 | if (!Parent->isAnonymousStructOrUnion()) |
| 2272 | return false; |
| 2273 | |
| 2274 | NamedDecl *Child = Field; |
| 2275 | do { |
| 2276 | if (Parent->isUnion()) { |
| 2277 | UnionEntry &En = Unions[Parent]; |
| 2278 | if (En.first && En.first != Child) { |
| 2279 | S.Diag(Init->getSourceLocation(), |
| 2280 | diag::err_multiple_mem_union_initialization) |
| 2281 | << Field->getDeclName() |
| 2282 | << Init->getSourceRange(); |
| 2283 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2284 | << 0 << En.second->getSourceRange(); |
| 2285 | return true; |
| 2286 | } else if (!En.first) { |
| 2287 | En.first = Child; |
| 2288 | En.second = Init; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | Child = Parent; |
| 2293 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2294 | } while (Parent->isAnonymousStructOrUnion()); |
| 2295 | |
| 2296 | return false; |
| 2297 | } |
| 2298 | } |
| 2299 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2300 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2301 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2302 | SourceLocation ColonLoc, |
| 2303 | MemInitTy **meminits, unsigned NumMemInits, |
| 2304 | bool AnyErrors) { |
| 2305 | if (!ConstructorDecl) |
| 2306 | return; |
| 2307 | |
| 2308 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2309 | |
| 2310 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2311 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2312 | |
| 2313 | if (!Constructor) { |
| 2314 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2315 | return; |
| 2316 | } |
| 2317 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2318 | CXXCtorInitializer **MemInits = |
| 2319 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2320 | |
| 2321 | // Mapping for the duplicate initializers check. |
| 2322 | // For member initializers, this is keyed with a FieldDecl*. |
| 2323 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2324 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2325 | |
| 2326 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2327 | RedundantUnionMap MemberUnions; |
| 2328 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2329 | bool HadError = false; |
| 2330 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2331 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2332 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2333 | // Set the source order index. |
| 2334 | Init->setSourceOrder(i); |
| 2335 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2336 | if (Init->isAnyMemberInitializer()) { |
| 2337 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2338 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2339 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2340 | HadError = true; |
| 2341 | } else { |
| 2342 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2343 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2344 | HadError = true; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2345 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2348 | if (HadError) |
| 2349 | return; |
| 2350 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2351 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2352 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2353 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2356 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2357 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2358 | CXXRecordDecl *ClassDecl) { |
| 2359 | // Ignore dependent contexts. |
| 2360 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2361 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2362 | |
| 2363 | // FIXME: all the access-control diagnostics are positioned on the |
| 2364 | // field/base declaration. That's probably good; that said, the |
| 2365 | // user might reasonably want to know why the destructor is being |
| 2366 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2367 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2368 | // Non-static data members. |
| 2369 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2370 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2371 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2372 | if (Field->isInvalidDecl()) |
| 2373 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2374 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2375 | |
| 2376 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2377 | if (!RT) |
| 2378 | continue; |
| 2379 | |
| 2380 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2381 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2382 | continue; |
| 2383 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2384 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2385 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2386 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2387 | << Field->getDeclName() |
| 2388 | << FieldType); |
| 2389 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2390 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2393 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2394 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2395 | // Bases. |
| 2396 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2397 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2398 | // Bases are always records in a well-formed non-dependent class. |
| 2399 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2400 | |
| 2401 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2402 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2403 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2406 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2407 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2408 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2409 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2410 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2411 | |
| 2412 | // FIXME: caret should be on the start of the class name |
| 2413 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2414 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2415 | << Base->getType() |
| 2416 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2417 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2418 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2422 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2423 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2424 | |
| 2425 | // Bases are always records in a well-formed non-dependent class. |
| 2426 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2427 | |
| 2428 | // Ignore direct virtual bases. |
| 2429 | if (DirectVirtualBases.count(RT)) |
| 2430 | continue; |
| 2431 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2432 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2433 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2434 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2435 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2436 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2437 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2438 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2439 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2440 | << VBase->getType()); |
| 2441 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2442 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2446 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2447 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2448 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2451 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2452 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2456 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2457 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2458 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2459 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2460 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2463 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2464 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2465 | if (!getLangOptions().CPlusPlus) |
| 2466 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2467 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2468 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2469 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2471 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2472 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2473 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2474 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2476 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2477 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2478 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2479 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2480 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2481 | if (!RT) |
| 2482 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2484 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2485 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2486 | // We can't answer whether something is abstract until it has a |
| 2487 | // definition. If it's currently being defined, we'll walk back |
| 2488 | // over all the declarations when we have a full definition. |
| 2489 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2490 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2491 | return false; |
| 2492 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2493 | if (!RD->isAbstract()) |
| 2494 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2496 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2497 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2499 | return true; |
| 2500 | } |
| 2501 | |
| 2502 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2503 | // Check if we've already emitted the list of pure virtual functions |
| 2504 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2505 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2506 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2508 | CXXFinalOverriderMap FinalOverriders; |
| 2509 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2511 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2512 | // more than once. |
| 2513 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2514 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2515 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2516 | MEnd = FinalOverriders.end(); |
| 2517 | M != MEnd; |
| 2518 | ++M) { |
| 2519 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2520 | SOEnd = M->second.end(); |
| 2521 | SO != SOEnd; ++SO) { |
| 2522 | // C++ [class.abstract]p4: |
| 2523 | // A class is abstract if it contains or inherits at least one |
| 2524 | // pure virtual function for which the final overrider is pure |
| 2525 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2527 | // |
| 2528 | if (SO->second.size() != 1) |
| 2529 | continue; |
| 2530 | |
| 2531 | if (!SO->second.front().Method->isPure()) |
| 2532 | continue; |
| 2533 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2534 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2535 | continue; |
| 2536 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2537 | Diag(SO->second.front().Method->getLocation(), |
| 2538 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 2539 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2540 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | if (!PureVirtualClassDiagSet) |
| 2544 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2545 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2548 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2549 | struct AbstractUsageInfo { |
| 2550 | Sema &S; |
| 2551 | CXXRecordDecl *Record; |
| 2552 | CanQualType AbstractType; |
| 2553 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2555 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2556 | : S(S), Record(Record), |
| 2557 | AbstractType(S.Context.getCanonicalType( |
| 2558 | S.Context.getTypeDeclType(Record))), |
| 2559 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2560 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2561 | void DiagnoseAbstractType() { |
| 2562 | if (Invalid) return; |
| 2563 | S.DiagnoseAbstractType(Record); |
| 2564 | Invalid = true; |
| 2565 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2566 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2567 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2568 | }; |
| 2569 | |
| 2570 | struct CheckAbstractUsage { |
| 2571 | AbstractUsageInfo &Info; |
| 2572 | const NamedDecl *Ctx; |
| 2573 | |
| 2574 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2575 | : Info(Info), Ctx(Ctx) {} |
| 2576 | |
| 2577 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2578 | switch (TL.getTypeLocClass()) { |
| 2579 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2580 | #define TYPELOC(CLASS, PARENT) \ |
| 2581 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2582 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2583 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2586 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2587 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2588 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2589 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2590 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2591 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2592 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2593 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2594 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2595 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2596 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2598 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2599 | // Visit the type parameters from a permissive context. |
| 2600 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2601 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2602 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2603 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2604 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2605 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2606 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2609 | // Visit pointee types from a permissive context. |
| 2610 | #define CheckPolymorphic(Type) \ |
| 2611 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2612 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2613 | } |
| 2614 | CheckPolymorphic(PointerTypeLoc) |
| 2615 | CheckPolymorphic(ReferenceTypeLoc) |
| 2616 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2617 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2619 | /// Handle all the types we haven't given a more specific |
| 2620 | /// implementation for above. |
| 2621 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2622 | // Every other kind of type that we haven't called out already |
| 2623 | // that has an inner type is either (1) sugar or (2) contains that |
| 2624 | // inner type in some way as a subobject. |
| 2625 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2626 | return Visit(Next, Sel); |
| 2627 | |
| 2628 | // If there's no inner type and we're in a permissive context, |
| 2629 | // don't diagnose. |
| 2630 | if (Sel == Sema::AbstractNone) return; |
| 2631 | |
| 2632 | // Check whether the type matches the abstract type. |
| 2633 | QualType T = TL.getType(); |
| 2634 | if (T->isArrayType()) { |
| 2635 | Sel = Sema::AbstractArrayType; |
| 2636 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2637 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2638 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2639 | if (CT != Info.AbstractType) return; |
| 2640 | |
| 2641 | // It matched; do some magic. |
| 2642 | if (Sel == Sema::AbstractArrayType) { |
| 2643 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2644 | << T << TL.getSourceRange(); |
| 2645 | } else { |
| 2646 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2647 | << Sel << T << TL.getSourceRange(); |
| 2648 | } |
| 2649 | Info.DiagnoseAbstractType(); |
| 2650 | } |
| 2651 | }; |
| 2652 | |
| 2653 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2654 | Sema::AbstractDiagSelID Sel) { |
| 2655 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2656 | } |
| 2657 | |
| 2658 | } |
| 2659 | |
| 2660 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2661 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2662 | CXXMethodDecl *MD) { |
| 2663 | // No need to do the check on definitions, which require that |
| 2664 | // the return/param types be complete. |
| 2665 | if (MD->isThisDeclarationADefinition()) |
| 2666 | return; |
| 2667 | |
| 2668 | // For safety's sake, just ignore it if we don't have type source |
| 2669 | // information. This should never happen for non-implicit methods, |
| 2670 | // but... |
| 2671 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 2672 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 2673 | } |
| 2674 | |
| 2675 | /// Check for invalid uses of an abstract type within a class definition. |
| 2676 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2677 | CXXRecordDecl *RD) { |
| 2678 | for (CXXRecordDecl::decl_iterator |
| 2679 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 2680 | Decl *D = *I; |
| 2681 | if (D->isImplicit()) continue; |
| 2682 | |
| 2683 | // Methods and method templates. |
| 2684 | if (isa<CXXMethodDecl>(D)) { |
| 2685 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 2686 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 2687 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 2688 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 2689 | |
| 2690 | // Fields and static variables. |
| 2691 | } else if (isa<FieldDecl>(D)) { |
| 2692 | FieldDecl *FD = cast<FieldDecl>(D); |
| 2693 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 2694 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 2695 | } else if (isa<VarDecl>(D)) { |
| 2696 | VarDecl *VD = cast<VarDecl>(D); |
| 2697 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 2698 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 2699 | |
| 2700 | // Nested classes and class templates. |
| 2701 | } else if (isa<CXXRecordDecl>(D)) { |
| 2702 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 2703 | } else if (isa<ClassTemplateDecl>(D)) { |
| 2704 | CheckAbstractClassUsage(Info, |
| 2705 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 2706 | } |
| 2707 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2710 | /// \brief Perform semantic checks on a class definition that has been |
| 2711 | /// completing, introducing implicitly-declared members, checking for |
| 2712 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2713 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 2714 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2715 | return; |
| 2716 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2717 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 2718 | AbstractUsageInfo Info(*this, Record); |
| 2719 | CheckAbstractClassUsage(Info, Record); |
| 2720 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2721 | |
| 2722 | // If this is not an aggregate type and has no user-declared constructor, |
| 2723 | // complain about any non-static data members of reference or const scalar |
| 2724 | // type, since they will never get initializers. |
| 2725 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2726 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2727 | bool Complained = false; |
| 2728 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2729 | FEnd = Record->field_end(); |
| 2730 | F != FEnd; ++F) { |
| 2731 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2732 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2733 | if (!Complained) { |
| 2734 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2735 | << Record->getTagKind() << Record; |
| 2736 | Complained = true; |
| 2737 | } |
| 2738 | |
| 2739 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2740 | << F->getType()->isReferenceType() |
| 2741 | << F->getDeclName(); |
| 2742 | } |
| 2743 | } |
| 2744 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2745 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 2746 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2747 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2748 | |
| 2749 | if (Record->getIdentifier()) { |
| 2750 | // C++ [class.mem]p13: |
| 2751 | // If T is the name of a class, then each of the following shall have a |
| 2752 | // name different from T: |
| 2753 | // - every member of every anonymous union that is a member of class T. |
| 2754 | // |
| 2755 | // C++ [class.mem]p14: |
| 2756 | // In addition, if class T has a user-declared constructor (12.1), every |
| 2757 | // non-static data member of class T shall have a name different from T. |
| 2758 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2759 | R.first != R.second; ++R.first) { |
| 2760 | NamedDecl *D = *R.first; |
| 2761 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 2762 | isa<IndirectFieldDecl>(D)) { |
| 2763 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 2764 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2765 | break; |
| 2766 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2767 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2768 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 2769 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 2770 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 2771 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 2772 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 2773 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 2774 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 2775 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 2776 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2777 | |
| 2778 | // See if a method overloads virtual methods in a base |
| 2779 | /// class without overriding any. |
| 2780 | if (!Record->isDependentType()) { |
| 2781 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 2782 | MEnd = Record->method_end(); |
| 2783 | M != MEnd; ++M) { |
| 2784 | DiagnoseHiddenVirtualMethods(Record, *M); |
| 2785 | } |
| 2786 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 2787 | |
| 2788 | // Declare inherited constructors. We do this eagerly here because: |
| 2789 | // - The standard requires an eager diagnostic for conflicting inherited |
| 2790 | // constructors from different classes. |
| 2791 | // - The lazy declaration of the other implicit constructors is so as to not |
| 2792 | // waste space and performance on classes that are not meant to be |
| 2793 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 2794 | // have inherited constructors. |
| 2795 | DeclareInheritedConstructors(Record); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | /// \brief Data used with FindHiddenVirtualMethod |
| 2799 | struct FindHiddenVirtualMethodData { |
| 2800 | Sema *S; |
| 2801 | CXXMethodDecl *Method; |
| 2802 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
| 2803 | llvm::SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 2804 | }; |
| 2805 | |
| 2806 | /// \brief Member lookup function that determines whether a given C++ |
| 2807 | /// method overloads virtual methods in a base class without overriding any, |
| 2808 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 2809 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 2810 | CXXBasePath &Path, |
| 2811 | void *UserData) { |
| 2812 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 2813 | |
| 2814 | FindHiddenVirtualMethodData &Data |
| 2815 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 2816 | |
| 2817 | DeclarationName Name = Data.Method->getDeclName(); |
| 2818 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 2819 | |
| 2820 | bool foundSameNameMethod = false; |
| 2821 | llvm::SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
| 2822 | for (Path.Decls = BaseRecord->lookup(Name); |
| 2823 | Path.Decls.first != Path.Decls.second; |
| 2824 | ++Path.Decls.first) { |
| 2825 | NamedDecl *D = *Path.Decls.first; |
| 2826 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 2827 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2828 | foundSameNameMethod = true; |
| 2829 | // Interested only in hidden virtual methods. |
| 2830 | if (!MD->isVirtual()) |
| 2831 | continue; |
| 2832 | // If the method we are checking overrides a method from its base |
| 2833 | // don't warn about the other overloaded methods. |
| 2834 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 2835 | return true; |
| 2836 | // Collect the overload only if its hidden. |
| 2837 | if (!Data.OverridenAndUsingBaseMethods.count(MD)) |
| 2838 | overloadedMethods.push_back(MD); |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | if (foundSameNameMethod) |
| 2843 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 2844 | overloadedMethods.end()); |
| 2845 | return foundSameNameMethod; |
| 2846 | } |
| 2847 | |
| 2848 | /// \brief See if a method overloads virtual methods in a base class without |
| 2849 | /// overriding any. |
| 2850 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 2851 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
| 2852 | MD->getLocation()) == Diagnostic::Ignored) |
| 2853 | return; |
| 2854 | if (MD->getDeclName().getNameKind() != DeclarationName::Identifier) |
| 2855 | return; |
| 2856 | |
| 2857 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 2858 | /*bool RecordPaths=*/false, |
| 2859 | /*bool DetectVirtual=*/false); |
| 2860 | FindHiddenVirtualMethodData Data; |
| 2861 | Data.Method = MD; |
| 2862 | Data.S = this; |
| 2863 | |
| 2864 | // Keep the base methods that were overriden or introduced in the subclass |
| 2865 | // by 'using' in a set. A base method not in this set is hidden. |
| 2866 | for (DeclContext::lookup_result res = DC->lookup(MD->getDeclName()); |
| 2867 | res.first != res.second; ++res.first) { |
| 2868 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*res.first)) |
| 2869 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 2870 | E = MD->end_overridden_methods(); |
| 2871 | I != E; ++I) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 2872 | Data.OverridenAndUsingBaseMethods.insert((*I)->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2873 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*res.first)) |
| 2874 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(shad->getTargetDecl())) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 2875 | Data.OverridenAndUsingBaseMethods.insert(MD->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 2879 | !Data.OverloadedMethods.empty()) { |
| 2880 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 2881 | << MD << (Data.OverloadedMethods.size() > 1); |
| 2882 | |
| 2883 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 2884 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 2885 | Diag(overloadedMD->getLocation(), |
| 2886 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 2887 | } |
| 2888 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2891 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2892 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2893 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2894 | SourceLocation RBrac, |
| 2895 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2896 | if (!TagDecl) |
| 2897 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2899 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2900 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2901 | ActOnFields(S, RLoc, TagDecl, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2902 | // strict aliasing violation! |
| 2903 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2904 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2906 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2907 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2910 | namespace { |
| 2911 | /// \brief Helper class that collects exception specifications for |
| 2912 | /// implicitly-declared special member functions. |
| 2913 | class ImplicitExceptionSpecification { |
| 2914 | ASTContext &Context; |
| 2915 | bool AllowsAllExceptions; |
| 2916 | llvm::SmallPtrSet<CanQualType, 4> ExceptionsSeen; |
| 2917 | llvm::SmallVector<QualType, 4> Exceptions; |
| 2918 | |
| 2919 | public: |
| 2920 | explicit ImplicitExceptionSpecification(ASTContext &Context) |
| 2921 | : Context(Context), AllowsAllExceptions(false) { } |
| 2922 | |
| 2923 | /// \brief Whether the special member function should have any |
| 2924 | /// exception specification at all. |
| 2925 | bool hasExceptionSpecification() const { |
| 2926 | return !AllowsAllExceptions; |
| 2927 | } |
| 2928 | |
| 2929 | /// \brief Whether the special member function should have a |
| 2930 | /// throw(...) exception specification (a Microsoft extension). |
| 2931 | bool hasAnyExceptionSpecification() const { |
| 2932 | return false; |
| 2933 | } |
| 2934 | |
| 2935 | /// \brief The number of exceptions in the exception specification. |
| 2936 | unsigned size() const { return Exceptions.size(); } |
| 2937 | |
| 2938 | /// \brief The set of exceptions in the exception specification. |
| 2939 | const QualType *data() const { return Exceptions.data(); } |
| 2940 | |
| 2941 | /// \brief Note that |
| 2942 | void CalledDecl(CXXMethodDecl *Method) { |
| 2943 | // If we already know that we allow all exceptions, do nothing. |
Douglas Gregor | 4681ca8 | 2010-07-01 15:29:53 +0000 | [diff] [blame] | 2944 | if (AllowsAllExceptions || !Method) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2945 | return; |
| 2946 | |
| 2947 | const FunctionProtoType *Proto |
| 2948 | = Method->getType()->getAs<FunctionProtoType>(); |
| 2949 | |
| 2950 | // If this function can throw any exceptions, make a note of that. |
| 2951 | if (!Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec()) { |
| 2952 | AllowsAllExceptions = true; |
| 2953 | ExceptionsSeen.clear(); |
| 2954 | Exceptions.clear(); |
| 2955 | return; |
| 2956 | } |
| 2957 | |
| 2958 | // Record the exceptions in this function's exception specification. |
| 2959 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 2960 | EEnd = Proto->exception_end(); |
| 2961 | E != EEnd; ++E) |
| 2962 | if (ExceptionsSeen.insert(Context.getCanonicalType(*E))) |
| 2963 | Exceptions.push_back(*E); |
| 2964 | } |
| 2965 | }; |
| 2966 | } |
| 2967 | |
| 2968 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2969 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2970 | /// special functions, such as the default constructor, copy |
| 2971 | /// constructor, or destructor, to the given C++ class (C++ |
| 2972 | /// [special]p1). This routine can only be executed just before the |
| 2973 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2974 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 2975 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2976 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2977 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 2978 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 2979 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 2981 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2982 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 2983 | |
| 2984 | // If we have a dynamic class, then the copy assignment operator may be |
| 2985 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 2986 | // it shows up in the right place in the vtable and that we diagnose |
| 2987 | // problems with the implicit exception specification. |
| 2988 | if (ClassDecl->isDynamicClass()) |
| 2989 | DeclareImplicitCopyAssignment(ClassDecl); |
| 2990 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 2992 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 2993 | ++ASTContext::NumImplicitDestructors; |
| 2994 | |
| 2995 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 2996 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 2997 | // shows up in the right place in the vtable and that we diagnose problems |
| 2998 | // with the implicit exception specification. |
| 2999 | if (ClassDecl->isDynamicClass()) |
| 3000 | DeclareImplicitDestructor(ClassDecl); |
| 3001 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3004 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 3005 | if (!D) |
| 3006 | return; |
| 3007 | |
| 3008 | TemplateParameterList *Params = 0; |
| 3009 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 3010 | Params = Template->getTemplateParameters(); |
| 3011 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 3012 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 3013 | Params = PartialSpec->getTemplateParameters(); |
| 3014 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3015 | return; |
| 3016 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3017 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3018 | ParamEnd = Params->end(); |
| 3019 | Param != ParamEnd; ++Param) { |
| 3020 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 3021 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3022 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3023 | IdResolver.AddDecl(Named); |
| 3024 | } |
| 3025 | } |
| 3026 | } |
| 3027 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3028 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 3029 | if (!RecordD) return; |
| 3030 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3031 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 3032 | PushDeclContext(S, Record); |
| 3033 | } |
| 3034 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3035 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 3036 | if (!RecordD) return; |
| 3037 | PopDeclContext(); |
| 3038 | } |
| 3039 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3040 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 3041 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 3042 | /// parsing those parts of the given Method declaration that could |
| 3043 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 3044 | /// arguments. This action should enter the scope of the given |
| 3045 | /// Method declaration as if we had just parsed the qualified method |
| 3046 | /// name. However, it should not bring the parameters into scope; |
| 3047 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3048 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 3052 | /// C++ method declaration. We're (re-)introducing the given |
| 3053 | /// function parameter into scope for use in parsing later parts of |
| 3054 | /// the method declaration. For example, we could see an |
| 3055 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3056 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 3057 | if (!ParamD) |
| 3058 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3060 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3061 | |
| 3062 | // If this parameter has an unparsed default argument, clear it out |
| 3063 | // to make way for the parsed default argument. |
| 3064 | if (Param->hasUnparsedDefaultArg()) |
| 3065 | Param->setDefaultArg(0); |
| 3066 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3067 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3068 | if (Param->getDeclName()) |
| 3069 | IdResolver.AddDecl(Param); |
| 3070 | } |
| 3071 | |
| 3072 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 3073 | /// processing the delayed method declaration for Method. The method |
| 3074 | /// declaration is now considered finished. There may be a separate |
| 3075 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 3076 | /// immediately!) for this method, if it was also defined inside the |
| 3077 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3078 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 3079 | if (!MethodD) |
| 3080 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 3082 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3084 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3085 | |
| 3086 | // Now that we have our default arguments, check the constructor |
| 3087 | // again. It could produce additional diagnostics or affect whether |
| 3088 | // the class has implicitly-declared destructors, among other |
| 3089 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3090 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 3091 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3092 | |
| 3093 | // Check the default arguments, which we may have added. |
| 3094 | if (!Method->isInvalidDecl()) |
| 3095 | CheckCXXDefaultArguments(Method); |
| 3096 | } |
| 3097 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3098 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3099 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3100 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3101 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 3102 | /// will be updated to reflect a well-formed type for the constructor and |
| 3103 | /// returned. |
| 3104 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3105 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3106 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3107 | |
| 3108 | // C++ [class.ctor]p3: |
| 3109 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 3110 | // constructor can be invoked for a const, volatile or const |
| 3111 | // volatile object. A constructor shall not be declared const, |
| 3112 | // volatile, or const volatile (9.3.2). |
| 3113 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3114 | if (!D.isInvalidType()) |
| 3115 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 3116 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 3117 | << SourceRange(D.getIdentifierLoc()); |
| 3118 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3119 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3120 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3121 | if (!D.isInvalidType()) |
| 3122 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 3123 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3124 | << SourceRange(D.getIdentifierLoc()); |
| 3125 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3126 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3129 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3130 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3131 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3132 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 3133 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3134 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3135 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 3136 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3137 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3138 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 3139 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3140 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3143 | // C++0x [class.ctor]p4: |
| 3144 | // A constructor shall not be declared with a ref-qualifier. |
| 3145 | if (FTI.hasRefQualifier()) { |
| 3146 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 3147 | << FTI.RefQualifierIsLValueRef |
| 3148 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 3149 | D.setInvalidType(); |
| 3150 | } |
| 3151 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3152 | // Rebuild the function type "R" without any type qualifiers (in |
| 3153 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3154 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3155 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3156 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 3157 | return R; |
| 3158 | |
| 3159 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 3160 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3161 | EPI.RefQualifier = RQ_None; |
| 3162 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3163 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3164 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3167 | /// CheckConstructor - Checks a fully-formed constructor for |
| 3168 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 3169 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3170 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3171 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 3172 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 3173 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3174 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3175 | |
| 3176 | // C++ [class.copy]p3: |
| 3177 | // A declaration of a constructor for a class X is ill-formed if |
| 3178 | // its first parameter is of type (optionally cv-qualified) X and |
| 3179 | // either there are no other parameters or else all other |
| 3180 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 3181 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | ((Constructor->getNumParams() == 1) || |
| 3183 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3184 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 3185 | Constructor->getTemplateSpecializationKind() |
| 3186 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3187 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 3188 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 3189 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3190 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 3191 | const char *ConstRef |
| 3192 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 3193 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3194 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 3195 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3196 | |
| 3197 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 3198 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3199 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3200 | } |
| 3201 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 3204 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 3205 | /// well-formedness, issuing any diagnostics required. Returns true |
| 3206 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 3207 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3208 | CXXRecordDecl *RD = Destructor->getParent(); |
| 3209 | |
| 3210 | if (Destructor->isVirtual()) { |
| 3211 | SourceLocation Loc; |
| 3212 | |
| 3213 | if (!Destructor->isImplicit()) |
| 3214 | Loc = Destructor->getLocation(); |
| 3215 | else |
| 3216 | Loc = RD->getLocation(); |
| 3217 | |
| 3218 | // If we have a virtual destructor, look up the deallocation function |
| 3219 | FunctionDecl *OperatorDelete = 0; |
| 3220 | DeclarationName Name = |
| 3221 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 3222 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3223 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 3224 | |
| 3225 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3226 | |
| 3227 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3228 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3229 | |
| 3230 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3234 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 3235 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 3236 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3237 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3240 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 3241 | /// the well-formednes of the destructor declarator @p D with type @p |
| 3242 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3243 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 3244 | /// will be updated to reflect a well-formed type for the destructor and |
| 3245 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3246 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3247 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3248 | // C++ [class.dtor]p1: |
| 3249 | // [...] A typedef-name that names a class is a class-name |
| 3250 | // (7.1.3); however, a typedef-name that names a class shall not |
| 3251 | // be used as the identifier in the declarator for a destructor |
| 3252 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3253 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3254 | if (isa<TypedefType>(DeclaratorType)) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3255 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3256 | << DeclaratorType; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3257 | |
| 3258 | // C++ [class.dtor]p2: |
| 3259 | // A destructor is used to destroy objects of its class type. A |
| 3260 | // destructor takes no parameters, and no return type can be |
| 3261 | // specified for it (not even void). The address of a destructor |
| 3262 | // shall not be taken. A destructor shall not be static. A |
| 3263 | // destructor can be invoked for a const, volatile or const |
| 3264 | // volatile object. A destructor shall not be declared const, |
| 3265 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3266 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3267 | if (!D.isInvalidType()) |
| 3268 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 3269 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3270 | << SourceRange(D.getIdentifierLoc()) |
| 3271 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 3272 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3273 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3274 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3275 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3276 | // Destructors don't have return types, but the parser will |
| 3277 | // happily parse something like: |
| 3278 | // |
| 3279 | // class X { |
| 3280 | // float ~X(); |
| 3281 | // }; |
| 3282 | // |
| 3283 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3284 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 3285 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3286 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3287 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3288 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3289 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3290 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3291 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3292 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3293 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3294 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3295 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3296 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3297 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3298 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3299 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3300 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3303 | // C++0x [class.dtor]p2: |
| 3304 | // A destructor shall not be declared with a ref-qualifier. |
| 3305 | if (FTI.hasRefQualifier()) { |
| 3306 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 3307 | << FTI.RefQualifierIsLValueRef |
| 3308 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 3309 | D.setInvalidType(); |
| 3310 | } |
| 3311 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3312 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3313 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3314 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 3315 | |
| 3316 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3317 | FTI.freeArgs(); |
| 3318 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3322 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3323 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3324 | D.setInvalidType(); |
| 3325 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3326 | |
| 3327 | // Rebuild the function type "R" without any type qualifiers or |
| 3328 | // parameters (in case any of the errors above fired) and with |
| 3329 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3330 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3331 | if (!D.isInvalidType()) |
| 3332 | return R; |
| 3333 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3334 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3335 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 3336 | EPI.Variadic = false; |
| 3337 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3338 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3339 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3342 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 3343 | /// well-formednes of the conversion function declarator @p D with |
| 3344 | /// type @p R. If there are any errors in the declarator, this routine |
| 3345 | /// will emit diagnostics and return true. Otherwise, it will return |
| 3346 | /// false. Either way, the type @p R will be updated to reflect a |
| 3347 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3348 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3349 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3350 | // C++ [class.conv.fct]p1: |
| 3351 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3352 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3354 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3355 | if (!D.isInvalidType()) |
| 3356 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3357 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3358 | << SourceRange(D.getIdentifierLoc()); |
| 3359 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3360 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3361 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3362 | |
| 3363 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3364 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3365 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3366 | // Conversion functions don't have return types, but the parser will |
| 3367 | // happily parse something like: |
| 3368 | // |
| 3369 | // class X { |
| 3370 | // float operator bool(); |
| 3371 | // }; |
| 3372 | // |
| 3373 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3374 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3375 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3376 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3377 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3380 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3381 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3382 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3383 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3384 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3385 | |
| 3386 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3387 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3388 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3389 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3390 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3391 | D.setInvalidType(); |
| 3392 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3393 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3394 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3395 | // is actually a gcc extension which we don't support. |
| 3396 | if (Proto->getResultType() != ConvType) { |
| 3397 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3398 | << Proto->getResultType(); |
| 3399 | D.setInvalidType(); |
| 3400 | ConvType = Proto->getResultType(); |
| 3401 | } |
| 3402 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3403 | // C++ [class.conv.fct]p4: |
| 3404 | // The conversion-type-id shall not represent a function type nor |
| 3405 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3406 | if (ConvType->isArrayType()) { |
| 3407 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3408 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3409 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3410 | } else if (ConvType->isFunctionType()) { |
| 3411 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3412 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3413 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | // Rebuild the function type "R" without any parameters (in case any |
| 3417 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3419 | if (D.isInvalidType()) |
| 3420 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3422 | // C++0x explicit conversion operators. |
| 3423 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3425 | diag::warn_explicit_conversion_functions) |
| 3426 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3429 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3430 | /// the declaration of the given C++ conversion function. This routine |
| 3431 | /// is responsible for recording the conversion function in the C++ |
| 3432 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3433 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3434 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3435 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3436 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3437 | |
| 3438 | // Make sure we aren't redeclaring the conversion function. |
| 3439 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3440 | |
| 3441 | // C++ [class.conv.fct]p1: |
| 3442 | // [...] A conversion function is never used to convert a |
| 3443 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3444 | // same object type (or a reference to it), to a (possibly |
| 3445 | // cv-qualified) base class of that type (or a reference to it), |
| 3446 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3447 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3448 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3450 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3451 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3452 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3453 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 3454 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 3455 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3456 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3457 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3458 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3459 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3460 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3461 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3462 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3463 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3464 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3465 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3466 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3469 | if (FunctionTemplateDecl *ConversionTemplate |
| 3470 | = Conversion->getDescribedFunctionTemplate()) |
| 3471 | return ConversionTemplate; |
| 3472 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3473 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3476 | //===----------------------------------------------------------------------===// |
| 3477 | // Namespace Handling |
| 3478 | //===----------------------------------------------------------------------===// |
| 3479 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3480 | |
| 3481 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3482 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3483 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3484 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 3485 | SourceLocation InlineLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3486 | SourceLocation IdentLoc, |
| 3487 | IdentifierInfo *II, |
| 3488 | SourceLocation LBrace, |
| 3489 | AttributeList *AttrList) { |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 3490 | // anonymous namespace starts at its left brace |
| 3491 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
| 3492 | (II ? IdentLoc : LBrace) , II); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3493 | Namespc->setLBracLoc(LBrace); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3494 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3495 | |
| 3496 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3497 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3498 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3499 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 3500 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
| 3501 | PushNamespaceVisibilityAttr(Attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3502 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3503 | if (II) { |
| 3504 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 3505 | // The identifier in an original-namespace-definition shall not |
| 3506 | // have been previously defined in the declarative region in |
| 3507 | // which the original-namespace-definition appears. The |
| 3508 | // identifier in an original-namespace-definition is the name of |
| 3509 | // the namespace. Subsequently in that declarative region, it is |
| 3510 | // treated as an original-namespace-name. |
| 3511 | // |
| 3512 | // Since namespace names are unique in their scope, and we don't |
| 3513 | // look through using directives, just |
| 3514 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 3515 | NamedDecl *PrevDecl = R.first == R.second? 0 : *R.first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3517 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3518 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3519 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 3520 | // inline-ness must match |
| 3521 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3522 | << Namespc->isInline(); |
| 3523 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
| 3524 | Namespc->setInvalidDecl(); |
| 3525 | // Recover by ignoring the new namespace's inline status. |
| 3526 | Namespc->setInline(OrigNS->isInline()); |
| 3527 | } |
| 3528 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3529 | // Attach this namespace decl to the chain of extended namespace |
| 3530 | // definitions. |
| 3531 | OrigNS->setNextNamespace(Namespc); |
| 3532 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3533 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3535 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3536 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3537 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3538 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3539 | } else if (PrevDecl) { |
| 3540 | // This is an invalid name redefinition. |
| 3541 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3542 | << Namespc->getDeclName(); |
| 3543 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3544 | Namespc->setInvalidDecl(); |
| 3545 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3546 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3547 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3548 | // This is the first "real" definition of the namespace "std", so update |
| 3549 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3550 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3551 | // We had already defined a dummy namespace "std". Link this new |
| 3552 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3553 | StdNS->setNextNamespace(Namespc); |
| 3554 | StdNS->setLocation(IdentLoc); |
| 3555 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | // Make our StdNamespace cache point at the first real definition of the |
| 3559 | // "std" namespace. |
| 3560 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3561 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3562 | |
| 3563 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3564 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3565 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3566 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3567 | |
| 3568 | // Link the anonymous namespace into its parent. |
| 3569 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3570 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3571 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3572 | PrevDecl = TU->getAnonymousNamespace(); |
| 3573 | TU->setAnonymousNamespace(Namespc); |
| 3574 | } else { |
| 3575 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3576 | PrevDecl = ND->getAnonymousNamespace(); |
| 3577 | ND->setAnonymousNamespace(Namespc); |
| 3578 | } |
| 3579 | |
| 3580 | // Link the anonymous namespace with its previous declaration. |
| 3581 | if (PrevDecl) { |
| 3582 | assert(PrevDecl->isAnonymousNamespace()); |
| 3583 | assert(!PrevDecl->getNextNamespace()); |
| 3584 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3585 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3586 | |
| 3587 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 3588 | // inline-ness must match |
| 3589 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3590 | << Namespc->isInline(); |
| 3591 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3592 | Namespc->setInvalidDecl(); |
| 3593 | // Recover by ignoring the new namespace's inline status. |
| 3594 | Namespc->setInline(PrevDecl->isInline()); |
| 3595 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3596 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3597 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3598 | CurContext->addDecl(Namespc); |
| 3599 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3600 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3601 | // behaves as if it were replaced by |
| 3602 | // namespace unique { /* empty body */ } |
| 3603 | // using namespace unique; |
| 3604 | // namespace unique { namespace-body } |
| 3605 | // where all occurrences of 'unique' in a translation unit are |
| 3606 | // replaced by the same identifier and this identifier differs |
| 3607 | // from all other identifiers in the entire program. |
| 3608 | |
| 3609 | // We just create the namespace with an empty name and then add an |
| 3610 | // implicit using declaration, just like the standard suggests. |
| 3611 | // |
| 3612 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3613 | // declarations semantically contained within an anonymous |
| 3614 | // namespace internal linkage. |
| 3615 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3616 | if (!PrevDecl) { |
| 3617 | UsingDirectiveDecl* UD |
| 3618 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3619 | /* 'using' */ LBrace, |
| 3620 | /* 'namespace' */ SourceLocation(), |
| 3621 | /* qualifier */ SourceRange(), |
| 3622 | /* NNS */ NULL, |
| 3623 | /* identifier */ SourceLocation(), |
| 3624 | Namespc, |
| 3625 | /* Ancestor */ CurContext); |
| 3626 | UD->setImplicit(); |
| 3627 | CurContext->addDecl(UD); |
| 3628 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3632 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3633 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3634 | // for the namespace has the declarations that showed up in that particular |
| 3635 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3636 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3637 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3640 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3641 | /// is a namespace alias, returns the namespace it points to. |
| 3642 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3643 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3644 | return AD->getNamespace(); |
| 3645 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3646 | } |
| 3647 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3648 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3649 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3650 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3651 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3652 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3653 | Namespc->setRBracLoc(RBrace); |
| 3654 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3655 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 3656 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3657 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3658 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 3659 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 3660 | return cast_or_null<CXXRecordDecl>( |
| 3661 | StdBadAlloc.get(Context.getExternalSource())); |
| 3662 | } |
| 3663 | |
| 3664 | NamespaceDecl *Sema::getStdNamespace() const { |
| 3665 | return cast_or_null<NamespaceDecl>( |
| 3666 | StdNamespace.get(Context.getExternalSource())); |
| 3667 | } |
| 3668 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3669 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 3670 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3671 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3672 | if (!StdNamespace) { |
| 3673 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 3674 | StdNamespace = NamespaceDecl::Create(Context, |
| 3675 | Context.getTranslationUnitDecl(), |
| 3676 | SourceLocation(), |
| 3677 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3678 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3681 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3684 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3685 | SourceLocation UsingLoc, |
| 3686 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3687 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3688 | SourceLocation IdentLoc, |
| 3689 | IdentifierInfo *NamespcName, |
| 3690 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3691 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3692 | assert(NamespcName && "Invalid NamespcName."); |
| 3693 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3694 | |
| 3695 | // This can only happen along a recovery path. |
| 3696 | while (S->getFlags() & Scope::TemplateParamScope) |
| 3697 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3698 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3700 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3701 | NestedNameSpecifier *Qualifier = 0; |
| 3702 | if (SS.isSet()) |
| 3703 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3704 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3705 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3706 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3707 | LookupParsedName(R, S, &SS); |
| 3708 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3709 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3710 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3711 | if (R.empty()) { |
| 3712 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 3713 | // "std" hasn't been defined yet, for GCC compatibility. |
| 3714 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 3715 | NamespcName->isStr("std")) { |
| 3716 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3717 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3718 | R.resolveKind(); |
| 3719 | } |
| 3720 | // Otherwise, attempt typo correction. |
| 3721 | else if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 3722 | CTC_NoKeywords, 0)) { |
| 3723 | if (R.getAsSingle<NamespaceDecl>() || |
| 3724 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 3725 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 3726 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 3727 | << NamespcName << DC << Corrected << SS.getRange() |
| 3728 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3729 | else |
| 3730 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 3731 | << NamespcName << Corrected |
| 3732 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3733 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 3734 | << Corrected; |
| 3735 | |
| 3736 | NamespcName = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3737 | } else { |
| 3738 | R.clear(); |
| 3739 | R.setLookupName(NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3740 | } |
| 3741 | } |
| 3742 | } |
| 3743 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3744 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3745 | NamedDecl *Named = R.getFoundDecl(); |
| 3746 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3747 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3748 | // C++ [namespace.udir]p1: |
| 3749 | // A using-directive specifies that the names in the nominated |
| 3750 | // namespace can be used in the scope in which the |
| 3751 | // using-directive appears after the using-directive. During |
| 3752 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3753 | // were declared in the nearest enclosing namespace which |
| 3754 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3755 | // namespace. [Note: in this context, "contains" means "contains |
| 3756 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3757 | |
| 3758 | // Find enclosing context containing both using-directive and |
| 3759 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3760 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3761 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3762 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3763 | CommonAncestor = CommonAncestor->getParent(); |
| 3764 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3765 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3766 | SS.getRange(), |
| 3767 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3768 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3769 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3770 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3771 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3774 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3775 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3779 | // If scope has associated entity, then using directive is at namespace |
| 3780 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3781 | // it's lookup structure. |
| 3782 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3783 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3784 | else |
| 3785 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3786 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3787 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3788 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3789 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3790 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3791 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3792 | AccessSpecifier AS, |
| 3793 | bool HasUsingKeyword, |
| 3794 | SourceLocation UsingLoc, |
| 3795 | CXXScopeSpec &SS, |
| 3796 | UnqualifiedId &Name, |
| 3797 | AttributeList *AttrList, |
| 3798 | bool IsTypeName, |
| 3799 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3800 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3802 | switch (Name.getKind()) { |
| 3803 | case UnqualifiedId::IK_Identifier: |
| 3804 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3805 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3806 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3807 | break; |
| 3808 | |
| 3809 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3810 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3811 | // C++0x inherited constructors. |
| 3812 | if (getLangOptions().CPlusPlus0x) break; |
| 3813 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3814 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3815 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3816 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3817 | |
| 3818 | case UnqualifiedId::IK_DestructorName: |
| 3819 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3820 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3821 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3822 | |
| 3823 | case UnqualifiedId::IK_TemplateId: |
| 3824 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3825 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3826 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3827 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3828 | |
| 3829 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 3830 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3831 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3832 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3833 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3834 | // Warn about using declarations. |
| 3835 | // TODO: store that the declaration was written without 'using' and |
| 3836 | // talk about access decls instead of using decls in the |
| 3837 | // diagnostics. |
| 3838 | if (!HasUsingKeyword) { |
| 3839 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3840 | |
| 3841 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3842 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 3845 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 3846 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 3847 | return 0; |
| 3848 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3849 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3850 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3851 | /* IsInstantiation */ false, |
| 3852 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3853 | if (UD) |
| 3854 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3856 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3859 | /// \brief Determine whether a using declaration considers the given |
| 3860 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 3861 | /// the same entity or are both typedefs of the same type. |
| 3862 | static bool |
| 3863 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 3864 | bool &SuppressRedeclaration) { |
| 3865 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 3866 | SuppressRedeclaration = false; |
| 3867 | return true; |
| 3868 | } |
| 3869 | |
| 3870 | if (TypedefDecl *TD1 = dyn_cast<TypedefDecl>(D1)) |
| 3871 | if (TypedefDecl *TD2 = dyn_cast<TypedefDecl>(D2)) { |
| 3872 | SuppressRedeclaration = true; |
| 3873 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 3874 | TD2->getUnderlyingType()); |
| 3875 | } |
| 3876 | |
| 3877 | return false; |
| 3878 | } |
| 3879 | |
| 3880 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3881 | /// Determines whether to create a using shadow decl for a particular |
| 3882 | /// decl, given the set of decls existing prior to this using lookup. |
| 3883 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3884 | const LookupResult &Previous) { |
| 3885 | // Diagnose finding a decl which is not from a base class of the |
| 3886 | // current class. We do this now because there are cases where this |
| 3887 | // function will silently decide not to build a shadow decl, which |
| 3888 | // will pre-empt further diagnostics. |
| 3889 | // |
| 3890 | // We don't need to do this in C++0x because we do the check once on |
| 3891 | // the qualifier. |
| 3892 | // |
| 3893 | // FIXME: diagnose the following if we care enough: |
| 3894 | // struct A { int foo; }; |
| 3895 | // struct B : A { using A::foo; }; |
| 3896 | // template <class T> struct C : A {}; |
| 3897 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3898 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3899 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3900 | // We can't diagnose it immediately because C<T> is an unknown |
| 3901 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3902 | // to A::foo, which will look well-formed when we instantiate. |
| 3903 | // The right solution is to not collapse the shadow-decl chain. |
| 3904 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3905 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3906 | |
| 3907 | // Handle enums and anonymous structs. |
| 3908 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3909 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3910 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3911 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3912 | |
| 3913 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3914 | if (OrigDC == CurContext) { |
| 3915 | Diag(Using->getLocation(), |
| 3916 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3917 | << Using->getNestedNameRange(); |
| 3918 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3919 | return true; |
| 3920 | } |
| 3921 | |
| 3922 | Diag(Using->getNestedNameRange().getBegin(), |
| 3923 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3924 | << Using->getTargetNestedNameDecl() |
| 3925 | << cast<CXXRecordDecl>(CurContext) |
| 3926 | << Using->getNestedNameRange(); |
| 3927 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3928 | return true; |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | if (Previous.empty()) return false; |
| 3933 | |
| 3934 | NamedDecl *Target = Orig; |
| 3935 | if (isa<UsingShadowDecl>(Target)) |
| 3936 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3937 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3938 | // If the target happens to be one of the previous declarations, we |
| 3939 | // don't have a conflict. |
| 3940 | // |
| 3941 | // FIXME: but we might be increasing its access, in which case we |
| 3942 | // should redeclare it. |
| 3943 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3944 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3945 | I != E; ++I) { |
| 3946 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3947 | bool Result; |
| 3948 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 3949 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3950 | |
| 3951 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3952 | } |
| 3953 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3954 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3955 | FunctionDecl *FD; |
| 3956 | if (isa<FunctionTemplateDecl>(Target)) |
| 3957 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3958 | else |
| 3959 | FD = cast<FunctionDecl>(Target); |
| 3960 | |
| 3961 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3962 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3963 | case Ovl_Overload: |
| 3964 | return false; |
| 3965 | |
| 3966 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3967 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3968 | break; |
| 3969 | |
| 3970 | // We found a decl with the exact signature. |
| 3971 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3972 | // If we're in a record, we want to hide the target, so we |
| 3973 | // return true (without a diagnostic) to tell the caller not to |
| 3974 | // build a shadow decl. |
| 3975 | if (CurContext->isRecord()) |
| 3976 | return true; |
| 3977 | |
| 3978 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3979 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3980 | break; |
| 3981 | } |
| 3982 | |
| 3983 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3984 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3985 | return true; |
| 3986 | } |
| 3987 | |
| 3988 | // Target is not a function. |
| 3989 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3990 | if (isa<TagDecl>(Target)) { |
| 3991 | // No conflict between a tag and a non-tag. |
| 3992 | if (!Tag) return false; |
| 3993 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3994 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3995 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3996 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3997 | return true; |
| 3998 | } |
| 3999 | |
| 4000 | // No conflict between a tag and a non-tag. |
| 4001 | if (!NonTag) return false; |
| 4002 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 4003 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4004 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 4005 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 4006 | return true; |
| 4007 | } |
| 4008 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4009 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4010 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4011 | UsingDecl *UD, |
| 4012 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4013 | |
| 4014 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4015 | NamedDecl *Target = Orig; |
| 4016 | if (isa<UsingShadowDecl>(Target)) { |
| 4017 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 4018 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4022 | = UsingShadowDecl::Create(Context, CurContext, |
| 4023 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4024 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 4025 | |
| 4026 | Shadow->setAccess(UD->getAccess()); |
| 4027 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 4028 | Shadow->setInvalidDecl(); |
| 4029 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4030 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4031 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4032 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4033 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4034 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4035 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4036 | return Shadow; |
| 4037 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4038 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4039 | /// Hides a using shadow declaration. This is required by the current |
| 4040 | /// using-decl implementation when a resolvable using declaration in a |
| 4041 | /// class is followed by a declaration which would hide or override |
| 4042 | /// one or more of the using decl's targets; for example: |
| 4043 | /// |
| 4044 | /// struct Base { void foo(int); }; |
| 4045 | /// struct Derived : Base { |
| 4046 | /// using Base::foo; |
| 4047 | /// void foo(int); |
| 4048 | /// }; |
| 4049 | /// |
| 4050 | /// The governing language is C++03 [namespace.udecl]p12: |
| 4051 | /// |
| 4052 | /// When a using-declaration brings names from a base class into a |
| 4053 | /// derived class scope, member functions in the derived class |
| 4054 | /// override and/or hide member functions with the same name and |
| 4055 | /// parameter types in a base class (rather than conflicting). |
| 4056 | /// |
| 4057 | /// There are two ways to implement this: |
| 4058 | /// (1) optimistically create shadow decls when they're not hidden |
| 4059 | /// by existing declarations, or |
| 4060 | /// (2) don't create any shadow decls (or at least don't make them |
| 4061 | /// visible) until we've fully parsed/instantiated the class. |
| 4062 | /// The problem with (1) is that we might have to retroactively remove |
| 4063 | /// a shadow decl, which requires several O(n) operations because the |
| 4064 | /// decl structures are (very reasonably) not designed for removal. |
| 4065 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 4066 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4067 | if (Shadow->getDeclName().getNameKind() == |
| 4068 | DeclarationName::CXXConversionFunctionName) |
| 4069 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 4070 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4071 | // Remove it from the DeclContext... |
| 4072 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4073 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4074 | // ...and the scope, if applicable... |
| 4075 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4076 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4077 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4080 | // ...and the using decl. |
| 4081 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 4082 | |
| 4083 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4084 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4087 | /// Builds a using declaration. |
| 4088 | /// |
| 4089 | /// \param IsInstantiation - Whether this call arises from an |
| 4090 | /// instantiation of an unresolved using declaration. We treat |
| 4091 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4092 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 4093 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4094 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4095 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4096 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4097 | bool IsInstantiation, |
| 4098 | bool IsTypeName, |
| 4099 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4100 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4101 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4102 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 4103 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 4104 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4106 | if (SS.isEmpty()) { |
| 4107 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4108 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4110 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4111 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4112 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4113 | ForRedeclaration); |
| 4114 | Previous.setHideTags(false); |
| 4115 | if (S) { |
| 4116 | LookupName(Previous, S); |
| 4117 | |
| 4118 | // It is really dumb that we have to do this. |
| 4119 | LookupResult::Filter F = Previous.makeFilter(); |
| 4120 | while (F.hasNext()) { |
| 4121 | NamedDecl *D = F.next(); |
| 4122 | if (!isDeclInScope(D, CurContext, S)) |
| 4123 | F.erase(); |
| 4124 | } |
| 4125 | F.done(); |
| 4126 | } else { |
| 4127 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 4128 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 4129 | LookupQualifiedName(Previous, CurContext); |
| 4130 | } |
| 4131 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4132 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4133 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4134 | // Check for invalid redeclarations. |
| 4135 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 4136 | return 0; |
| 4137 | |
| 4138 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4139 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 4140 | return 0; |
| 4141 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 4142 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4143 | NamedDecl *D; |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 4144 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4145 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4146 | // FIXME: not all declaration name kinds are legal here |
| 4147 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 4148 | UsingLoc, TypenameLoc, |
| 4149 | SS.getRange(), NNS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4150 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4151 | } else { |
| 4152 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4153 | UsingLoc, SS.getRange(), |
| 4154 | NNS, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4155 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4156 | } else { |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4157 | D = UsingDecl::Create(Context, CurContext, |
| 4158 | SS.getRange(), UsingLoc, NNS, NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4159 | IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 4160 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4161 | D->setAccess(AS); |
| 4162 | CurContext->addDecl(D); |
| 4163 | |
| 4164 | if (!LookupContext) return D; |
| 4165 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4166 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4167 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4168 | UD->setInvalidDecl(); |
| 4169 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4172 | // Constructor inheriting using decls get special treatment. |
| 4173 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
| 4174 | if (CheckInheritedConstructorUsingDecl(UD)) |
| 4175 | UD->setInvalidDecl(); |
| 4176 | return UD; |
| 4177 | } |
| 4178 | |
| 4179 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4180 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4181 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4182 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4183 | // Unlike most lookups, we don't always want to hide tag |
| 4184 | // declarations: tag names are visible through the using declaration |
| 4185 | // even if hidden by ordinary names, *except* in a dependent context |
| 4186 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4187 | if (!IsInstantiation) |
| 4188 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4189 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4190 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4192 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4193 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4194 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4195 | UD->setInvalidDecl(); |
| 4196 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4199 | if (R.isAmbiguous()) { |
| 4200 | UD->setInvalidDecl(); |
| 4201 | return UD; |
| 4202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4204 | if (IsTypeName) { |
| 4205 | // If we asked for a typename and got a non-type decl, error out. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4206 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4207 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 4208 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 4209 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 4210 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4211 | UD->setInvalidDecl(); |
| 4212 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4213 | } |
| 4214 | } else { |
| 4215 | // If we asked for a non-typename and we got a type, error out, |
| 4216 | // but only if this is an instantiation of an unresolved using |
| 4217 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4218 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4219 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 4220 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4221 | UD->setInvalidDecl(); |
| 4222 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4223 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 4226 | // C++0x N2914 [namespace.udecl]p6: |
| 4227 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4228 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 4229 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 4230 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4231 | UD->setInvalidDecl(); |
| 4232 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 4233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4234 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4235 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 4236 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 4237 | BuildUsingShadowDecl(S, UD, *I); |
| 4238 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4239 | |
| 4240 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4243 | /// Additional checks for a using declaration referring to a constructor name. |
| 4244 | bool Sema::CheckInheritedConstructorUsingDecl(UsingDecl *UD) { |
| 4245 | if (UD->isTypeName()) { |
| 4246 | // FIXME: Cannot specify typename when specifying constructor |
| 4247 | return true; |
| 4248 | } |
| 4249 | |
| 4250 | const Type *SourceType = UD->getTargetNestedNameDecl()->getAsType(); |
| 4251 | assert(SourceType && |
| 4252 | "Using decl naming constructor doesn't have type in scope spec."); |
| 4253 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 4254 | |
| 4255 | // Check whether the named type is a direct base class. |
| 4256 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 4257 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 4258 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 4259 | BaseIt != BaseE; ++BaseIt) { |
| 4260 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 4261 | if (CanonicalSourceType == BaseType) |
| 4262 | break; |
| 4263 | } |
| 4264 | |
| 4265 | if (BaseIt == BaseE) { |
| 4266 | // Did not find SourceType in the bases. |
| 4267 | Diag(UD->getUsingLocation(), |
| 4268 | diag::err_using_decl_constructor_not_in_direct_base) |
| 4269 | << UD->getNameInfo().getSourceRange() |
| 4270 | << QualType(SourceType, 0) << TargetClass; |
| 4271 | return true; |
| 4272 | } |
| 4273 | |
| 4274 | BaseIt->setInheritConstructors(); |
| 4275 | |
| 4276 | return false; |
| 4277 | } |
| 4278 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4279 | /// Checks that the given using declaration is not an invalid |
| 4280 | /// redeclaration. Note that this is checking only for the using decl |
| 4281 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 4282 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 4283 | bool isTypeName, |
| 4284 | const CXXScopeSpec &SS, |
| 4285 | SourceLocation NameLoc, |
| 4286 | const LookupResult &Prev) { |
| 4287 | // C++03 [namespace.udecl]p8: |
| 4288 | // C++0x [namespace.udecl]p10: |
| 4289 | // A using-declaration is a declaration and can therefore be used |
| 4290 | // repeatedly where (and only where) multiple declarations are |
| 4291 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 4292 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 4293 | // That's in non-member contexts. |
| 4294 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4295 | return false; |
| 4296 | |
| 4297 | NestedNameSpecifier *Qual |
| 4298 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 4299 | |
| 4300 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 4301 | NamedDecl *D = *I; |
| 4302 | |
| 4303 | bool DTypename; |
| 4304 | NestedNameSpecifier *DQual; |
| 4305 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 4306 | DTypename = UD->isTypeName(); |
| 4307 | DQual = UD->getTargetNestedNameDecl(); |
| 4308 | } else if (UnresolvedUsingValueDecl *UD |
| 4309 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 4310 | DTypename = false; |
| 4311 | DQual = UD->getTargetNestedNameSpecifier(); |
| 4312 | } else if (UnresolvedUsingTypenameDecl *UD |
| 4313 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 4314 | DTypename = true; |
| 4315 | DQual = UD->getTargetNestedNameSpecifier(); |
| 4316 | } else continue; |
| 4317 | |
| 4318 | // using decls differ if one says 'typename' and the other doesn't. |
| 4319 | // FIXME: non-dependent using decls? |
| 4320 | if (isTypeName != DTypename) continue; |
| 4321 | |
| 4322 | // using decls differ if they name different scopes (but note that |
| 4323 | // template instantiation can cause this check to trigger when it |
| 4324 | // didn't before instantiation). |
| 4325 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 4326 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 4327 | continue; |
| 4328 | |
| 4329 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 4330 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4331 | return true; |
| 4332 | } |
| 4333 | |
| 4334 | return false; |
| 4335 | } |
| 4336 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4337 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4338 | /// Checks that the given nested-name qualifier used in a using decl |
| 4339 | /// in the current context is appropriately related to the current |
| 4340 | /// scope. If an error is found, diagnoses it and returns true. |
| 4341 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 4342 | const CXXScopeSpec &SS, |
| 4343 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4344 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4345 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4346 | if (!CurContext->isRecord()) { |
| 4347 | // C++03 [namespace.udecl]p3: |
| 4348 | // C++0x [namespace.udecl]p8: |
| 4349 | // A using-declaration for a class member shall be a member-declaration. |
| 4350 | |
| 4351 | // If we weren't able to compute a valid scope, it must be a |
| 4352 | // dependent class scope. |
| 4353 | if (!NamedContext || NamedContext->isRecord()) { |
| 4354 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 4355 | << SS.getRange(); |
| 4356 | return true; |
| 4357 | } |
| 4358 | |
| 4359 | // Otherwise, everything is known to be fine. |
| 4360 | return false; |
| 4361 | } |
| 4362 | |
| 4363 | // The current scope is a record. |
| 4364 | |
| 4365 | // If the named context is dependent, we can't decide much. |
| 4366 | if (!NamedContext) { |
| 4367 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 4368 | // nested-name-specifier does not refer to a base class, which is |
| 4369 | // still possible in some cases. |
| 4370 | |
| 4371 | // Otherwise we have to conservatively report that things might be |
| 4372 | // okay. |
| 4373 | return false; |
| 4374 | } |
| 4375 | |
| 4376 | if (!NamedContext->isRecord()) { |
| 4377 | // Ideally this would point at the last name in the specifier, |
| 4378 | // but we don't have that level of source info. |
| 4379 | Diag(SS.getRange().getBegin(), |
| 4380 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 4381 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 4382 | return true; |
| 4383 | } |
| 4384 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 4385 | if (!NamedContext->isDependentContext() && |
| 4386 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 4387 | return true; |
| 4388 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4389 | if (getLangOptions().CPlusPlus0x) { |
| 4390 | // C++0x [namespace.udecl]p3: |
| 4391 | // In a using-declaration used as a member-declaration, the |
| 4392 | // nested-name-specifier shall name a base class of the class |
| 4393 | // being defined. |
| 4394 | |
| 4395 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 4396 | cast<CXXRecordDecl>(NamedContext))) { |
| 4397 | if (CurContext == NamedContext) { |
| 4398 | Diag(NameLoc, |
| 4399 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 4400 | << SS.getRange(); |
| 4401 | return true; |
| 4402 | } |
| 4403 | |
| 4404 | Diag(SS.getRange().getBegin(), |
| 4405 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4406 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4407 | << cast<CXXRecordDecl>(CurContext) |
| 4408 | << SS.getRange(); |
| 4409 | return true; |
| 4410 | } |
| 4411 | |
| 4412 | return false; |
| 4413 | } |
| 4414 | |
| 4415 | // C++03 [namespace.udecl]p4: |
| 4416 | // A using-declaration used as a member-declaration shall refer |
| 4417 | // to a member of a base class of the class being defined [etc.]. |
| 4418 | |
| 4419 | // Salient point: SS doesn't have to name a base class as long as |
| 4420 | // lookup only finds members from base classes. Therefore we can |
| 4421 | // diagnose here only if we can prove that that can't happen, |
| 4422 | // i.e. if the class hierarchies provably don't intersect. |
| 4423 | |
| 4424 | // TODO: it would be nice if "definitely valid" results were cached |
| 4425 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 4426 | // need to be repeated. |
| 4427 | |
| 4428 | struct UserData { |
| 4429 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 4430 | |
| 4431 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4432 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4433 | Data->Bases.insert(Base); |
| 4434 | return true; |
| 4435 | } |
| 4436 | |
| 4437 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 4438 | return !Class->forallBases(collect, this); |
| 4439 | } |
| 4440 | |
| 4441 | /// Returns true if the base is dependent or is one of the |
| 4442 | /// accumulated base classes. |
| 4443 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4444 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4445 | return !Data->Bases.count(Base); |
| 4446 | } |
| 4447 | |
| 4448 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 4449 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 4450 | } |
| 4451 | }; |
| 4452 | |
| 4453 | UserData Data; |
| 4454 | |
| 4455 | // Returns false if we find a dependent base. |
| 4456 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 4457 | return false; |
| 4458 | |
| 4459 | // Returns false if the class has a dependent base or if it or one |
| 4460 | // of its bases is present in the base set of the current context. |
| 4461 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 4462 | return false; |
| 4463 | |
| 4464 | Diag(SS.getRange().getBegin(), |
| 4465 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4466 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4467 | << cast<CXXRecordDecl>(CurContext) |
| 4468 | << SS.getRange(); |
| 4469 | |
| 4470 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4473 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4474 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4475 | SourceLocation AliasLoc, |
| 4476 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4477 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4478 | SourceLocation IdentLoc, |
| 4479 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4481 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4482 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4483 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4484 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4485 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 4486 | NamedDecl *PrevDecl |
| 4487 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4488 | ForRedeclaration); |
| 4489 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 4490 | PrevDecl = 0; |
| 4491 | |
| 4492 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4493 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | // We already have an alias with the same name that points to the same |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4495 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4496 | // FIXME: At some point, we'll want to create the (redundant) |
| 4497 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4498 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4499 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4500 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4502 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4503 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4504 | diag::err_redefinition_different_kind; |
| 4505 | Diag(AliasLoc, DiagID) << Alias; |
| 4506 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4507 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4510 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4511 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4512 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4513 | if (R.empty()) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4514 | if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 4515 | CTC_NoKeywords, 0)) { |
| 4516 | if (R.getAsSingle<NamespaceDecl>() || |
| 4517 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 4518 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 4519 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4520 | << Ident << DC << Corrected << SS.getRange() |
| 4521 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4522 | else |
| 4523 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4524 | << Ident << Corrected |
| 4525 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4526 | |
| 4527 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 4528 | << Corrected; |
| 4529 | |
| 4530 | Ident = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 4531 | } else { |
| 4532 | R.clear(); |
| 4533 | R.setLookupName(Ident); |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | if (R.empty()) { |
| 4538 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4539 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4540 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4541 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4542 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4543 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4545 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4546 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4547 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4548 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4549 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4550 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4553 | namespace { |
| 4554 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4555 | /// to implicitly define the body of a C++ member function; |
| 4556 | class ImplicitlyDefinedFunctionScope { |
| 4557 | Sema &S; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4558 | Sema::ContextRAII SavedContext; |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4559 | |
| 4560 | public: |
| 4561 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4562 | : S(S), SavedContext(S, Method) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4563 | { |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4564 | S.PushFunctionScope(); |
| 4565 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4566 | } |
| 4567 | |
| 4568 | ~ImplicitlyDefinedFunctionScope() { |
| 4569 | S.PopExpressionEvaluationContext(); |
| 4570 | S.PopFunctionOrBlockScope(); |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4571 | } |
| 4572 | }; |
| 4573 | } |
| 4574 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4575 | static CXXConstructorDecl *getDefaultConstructorUnsafe(Sema &Self, |
| 4576 | CXXRecordDecl *D) { |
| 4577 | ASTContext &Context = Self.Context; |
| 4578 | QualType ClassType = Context.getTypeDeclType(D); |
| 4579 | DeclarationName ConstructorName |
| 4580 | = Context.DeclarationNames.getCXXConstructorName( |
| 4581 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 4582 | |
| 4583 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 4584 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 4585 | Con != ConEnd; ++Con) { |
| 4586 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 4587 | if (isa<FunctionTemplateDecl>(*Con)) |
| 4588 | continue; |
| 4589 | |
| 4590 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 4591 | if (Constructor->isDefaultConstructor()) |
| 4592 | return Constructor; |
| 4593 | } |
| 4594 | return 0; |
| 4595 | } |
| 4596 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4597 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 4598 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4599 | // C++ [class.ctor]p5: |
| 4600 | // A default constructor for a class X is a constructor of class X |
| 4601 | // that can be called without an argument. If there is no |
| 4602 | // user-declared constructor for class X, a default constructor is |
| 4603 | // implicitly declared. An implicitly-declared default constructor |
| 4604 | // is an inline public member of its class. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4605 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 4606 | "Should not build implicit default constructor!"); |
| 4607 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4608 | // C++ [except.spec]p14: |
| 4609 | // An implicitly declared special member function (Clause 12) shall have an |
| 4610 | // exception-specification. [...] |
| 4611 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4612 | |
| 4613 | // Direct base-class destructors. |
| 4614 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4615 | BEnd = ClassDecl->bases_end(); |
| 4616 | B != BEnd; ++B) { |
| 4617 | if (B->isVirtual()) // Handled below. |
| 4618 | continue; |
| 4619 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4620 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4621 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4622 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4623 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4624 | else if (CXXConstructorDecl *Constructor |
| 4625 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4626 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4627 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | // Virtual base-class destructors. |
| 4631 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4632 | BEnd = ClassDecl->vbases_end(); |
| 4633 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4634 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4635 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4636 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4637 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
| 4638 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4639 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4640 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4641 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | // Field destructors. |
| 4645 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4646 | FEnd = ClassDecl->field_end(); |
| 4647 | F != FEnd; ++F) { |
| 4648 | if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4649 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 4650 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4651 | if (!FieldClassDecl->hasDeclaredDefaultConstructor()) |
| 4652 | ExceptSpec.CalledDecl( |
| 4653 | DeclareImplicitDefaultConstructor(FieldClassDecl)); |
| 4654 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4655 | = getDefaultConstructorUnsafe(*this, FieldClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4656 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4657 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4658 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4659 | |
| 4660 | FunctionProtoType::ExtProtoInfo EPI; |
| 4661 | EPI.HasExceptionSpec = ExceptSpec.hasExceptionSpecification(); |
| 4662 | EPI.HasAnyExceptionSpec = ExceptSpec.hasAnyExceptionSpecification(); |
| 4663 | EPI.NumExceptions = ExceptSpec.size(); |
| 4664 | EPI.Exceptions = ExceptSpec.data(); |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4665 | |
| 4666 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4667 | CanQualType ClassType |
| 4668 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4669 | DeclarationName Name |
| 4670 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4671 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4672 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4673 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4674 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4675 | 0, 0, EPI), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4676 | /*TInfo=*/0, |
| 4677 | /*isExplicit=*/false, |
| 4678 | /*isInline=*/true, |
| 4679 | /*isImplicitlyDeclared=*/true); |
| 4680 | DefaultCon->setAccess(AS_public); |
| 4681 | DefaultCon->setImplicit(); |
| 4682 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4683 | |
| 4684 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4685 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 4686 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4687 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4688 | PushOnScopeChains(DefaultCon, S, false); |
| 4689 | ClassDecl->addDecl(DefaultCon); |
| 4690 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4691 | return DefaultCon; |
| 4692 | } |
| 4693 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4694 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4695 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4696 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4697 | !Constructor->isUsed(false)) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4698 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4699 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4700 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4701 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4702 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4703 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 4704 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4705 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4706 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4707 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4708 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4709 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4710 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4711 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4712 | |
| 4713 | SourceLocation Loc = Constructor->getLocation(); |
| 4714 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4715 | |
| 4716 | Constructor->setUsed(); |
| 4717 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4720 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 4721 | // We start with an initial pass over the base classes to collect those that |
| 4722 | // inherit constructors from. If there are none, we can forgo all further |
| 4723 | // processing. |
| 4724 | typedef llvm::SmallVector<const RecordType *, 4> BasesVector; |
| 4725 | BasesVector BasesToInheritFrom; |
| 4726 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 4727 | BaseE = ClassDecl->bases_end(); |
| 4728 | BaseIt != BaseE; ++BaseIt) { |
| 4729 | if (BaseIt->getInheritConstructors()) { |
| 4730 | QualType Base = BaseIt->getType(); |
| 4731 | if (Base->isDependentType()) { |
| 4732 | // If we inherit constructors from anything that is dependent, just |
| 4733 | // abort processing altogether. We'll get another chance for the |
| 4734 | // instantiations. |
| 4735 | return; |
| 4736 | } |
| 4737 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 4738 | } |
| 4739 | } |
| 4740 | if (BasesToInheritFrom.empty()) |
| 4741 | return; |
| 4742 | |
| 4743 | // Now collect the constructors that we already have in the current class. |
| 4744 | // Those take precedence over inherited constructors. |
| 4745 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 4746 | // unless there is a user-declared constructor with the same signature in |
| 4747 | // the class where the using-declaration appears. |
| 4748 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 4749 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 4750 | CtorE = ClassDecl->ctor_end(); |
| 4751 | CtorIt != CtorE; ++CtorIt) { |
| 4752 | ExistingConstructors.insert( |
| 4753 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 4754 | } |
| 4755 | |
| 4756 | Scope *S = getScopeForContext(ClassDecl); |
| 4757 | DeclarationName CreatedCtorName = |
| 4758 | Context.DeclarationNames.getCXXConstructorName( |
| 4759 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 4760 | |
| 4761 | // Now comes the true work. |
| 4762 | // First, we keep a map from constructor types to the base that introduced |
| 4763 | // them. Needed for finding conflicting constructors. We also keep the |
| 4764 | // actually inserted declarations in there, for pretty diagnostics. |
| 4765 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 4766 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 4767 | ConstructorToSourceMap InheritedConstructors; |
| 4768 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 4769 | BaseE = BasesToInheritFrom.end(); |
| 4770 | BaseIt != BaseE; ++BaseIt) { |
| 4771 | const RecordType *Base = *BaseIt; |
| 4772 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 4773 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 4774 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 4775 | CtorE = BaseDecl->ctor_end(); |
| 4776 | CtorIt != CtorE; ++CtorIt) { |
| 4777 | // Find the using declaration for inheriting this base's constructors. |
| 4778 | DeclarationName Name = |
| 4779 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
| 4780 | UsingDecl *UD = dyn_cast_or_null<UsingDecl>( |
| 4781 | LookupSingleName(S, Name,SourceLocation(), LookupUsingDeclName)); |
| 4782 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 4783 | ClassDecl->getLocation(); |
| 4784 | |
| 4785 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 4786 | // from the class X named in the using-declaration consists of actual |
| 4787 | // constructors and notional constructors that result from the |
| 4788 | // transformation of defaulted parameters as follows: |
| 4789 | // - all non-template default constructors of X, and |
| 4790 | // - for each non-template constructor of X that has at least one |
| 4791 | // parameter with a default argument, the set of constructors that |
| 4792 | // results from omitting any ellipsis parameter specification and |
| 4793 | // successively omitting parameters with a default argument from the |
| 4794 | // end of the parameter-type-list. |
| 4795 | CXXConstructorDecl *BaseCtor = *CtorIt; |
| 4796 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 4797 | const FunctionProtoType *BaseCtorType = |
| 4798 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 4799 | |
| 4800 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 4801 | maxParams = BaseCtor->getNumParams(); |
| 4802 | params <= maxParams; ++params) { |
| 4803 | // Skip default constructors. They're never inherited. |
| 4804 | if (params == 0) |
| 4805 | continue; |
| 4806 | // Skip copy and move constructors for the same reason. |
| 4807 | if (CanBeCopyOrMove && params == 1) |
| 4808 | continue; |
| 4809 | |
| 4810 | // Build up a function type for this particular constructor. |
| 4811 | // FIXME: The working paper does not consider that the exception spec |
| 4812 | // for the inheriting constructor might be larger than that of the |
| 4813 | // source. This code doesn't yet, either. |
| 4814 | const Type *NewCtorType; |
| 4815 | if (params == maxParams) |
| 4816 | NewCtorType = BaseCtorType; |
| 4817 | else { |
| 4818 | llvm::SmallVector<QualType, 16> Args; |
| 4819 | for (unsigned i = 0; i < params; ++i) { |
| 4820 | Args.push_back(BaseCtorType->getArgType(i)); |
| 4821 | } |
| 4822 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 4823 | BaseCtorType->getExtProtoInfo(); |
| 4824 | ExtInfo.Variadic = false; |
| 4825 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 4826 | Args.data(), params, ExtInfo) |
| 4827 | .getTypePtr(); |
| 4828 | } |
| 4829 | const Type *CanonicalNewCtorType = |
| 4830 | Context.getCanonicalType(NewCtorType); |
| 4831 | |
| 4832 | // Now that we have the type, first check if the class already has a |
| 4833 | // constructor with this signature. |
| 4834 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 4835 | continue; |
| 4836 | |
| 4837 | // Then we check if we have already declared an inherited constructor |
| 4838 | // with this signature. |
| 4839 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 4840 | InheritedConstructors.insert(std::make_pair( |
| 4841 | CanonicalNewCtorType, |
| 4842 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 4843 | if (!result.second) { |
| 4844 | // Already in the map. If it came from a different class, that's an |
| 4845 | // error. Not if it's from the same. |
| 4846 | CanQualType PreviousBase = result.first->second.first; |
| 4847 | if (CanonicalBase != PreviousBase) { |
| 4848 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 4849 | const CXXConstructorDecl *PrevBaseCtor = |
| 4850 | PrevCtor->getInheritedConstructor(); |
| 4851 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 4852 | |
| 4853 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 4854 | Diag(BaseCtor->getLocation(), |
| 4855 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 4856 | Diag(PrevBaseCtor->getLocation(), |
| 4857 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 4858 | Diag(PrevCtor->getLocation(), |
| 4859 | diag::note_using_decl_constructor_conflict_previous_using); |
| 4860 | } |
| 4861 | continue; |
| 4862 | } |
| 4863 | |
| 4864 | // OK, we're there, now add the constructor. |
| 4865 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
| 4866 | // user-writtern inline constructor [...] |
| 4867 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 4868 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
| 4869 | Context, ClassDecl, DNI, QualType(NewCtorType, 0), /*TInfo=*/0, |
| 4870 | BaseCtor->isExplicit(), /*Inline=*/true, |
| 4871 | /*ImplicitlyDeclared=*/true); |
| 4872 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 4873 | |
| 4874 | // Build up the parameter decls and add them. |
| 4875 | llvm::SmallVector<ParmVarDecl *, 16> ParamDecls; |
| 4876 | for (unsigned i = 0; i < params; ++i) { |
| 4877 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, UsingLoc, |
| 4878 | /*IdentifierInfo=*/0, |
| 4879 | BaseCtorType->getArgType(i), |
| 4880 | /*TInfo=*/0, SC_None, |
| 4881 | SC_None, /*DefaultArg=*/0)); |
| 4882 | } |
| 4883 | NewCtor->setParams(ParamDecls.data(), ParamDecls.size()); |
| 4884 | NewCtor->setInheritedConstructor(BaseCtor); |
| 4885 | |
| 4886 | PushOnScopeChains(NewCtor, S, false); |
| 4887 | ClassDecl->addDecl(NewCtor); |
| 4888 | result.first->second.second = NewCtor; |
| 4889 | } |
| 4890 | } |
| 4891 | } |
| 4892 | } |
| 4893 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4894 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4895 | // C++ [class.dtor]p2: |
| 4896 | // If a class has no user-declared destructor, a destructor is |
| 4897 | // declared implicitly. An implicitly-declared destructor is an |
| 4898 | // inline public member of its class. |
| 4899 | |
| 4900 | // C++ [except.spec]p14: |
| 4901 | // An implicitly declared special member function (Clause 12) shall have |
| 4902 | // an exception-specification. |
| 4903 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4904 | |
| 4905 | // Direct base-class destructors. |
| 4906 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4907 | BEnd = ClassDecl->bases_end(); |
| 4908 | B != BEnd; ++B) { |
| 4909 | if (B->isVirtual()) // Handled below. |
| 4910 | continue; |
| 4911 | |
| 4912 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4913 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4914 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | // Virtual base-class destructors. |
| 4918 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4919 | BEnd = ClassDecl->vbases_end(); |
| 4920 | B != BEnd; ++B) { |
| 4921 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4922 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4923 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | // Field destructors. |
| 4927 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4928 | FEnd = ClassDecl->field_end(); |
| 4929 | F != FEnd; ++F) { |
| 4930 | if (const RecordType *RecordTy |
| 4931 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 4932 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4933 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4936 | // Create the actual destructor declaration. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4937 | FunctionProtoType::ExtProtoInfo EPI; |
| 4938 | EPI.HasExceptionSpec = ExceptSpec.hasExceptionSpecification(); |
| 4939 | EPI.HasAnyExceptionSpec = ExceptSpec.hasAnyExceptionSpecification(); |
| 4940 | EPI.NumExceptions = ExceptSpec.size(); |
| 4941 | EPI.Exceptions = ExceptSpec.data(); |
| 4942 | QualType Ty = Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4943 | |
| 4944 | CanQualType ClassType |
| 4945 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4946 | DeclarationName Name |
| 4947 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4948 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4949 | CXXDestructorDecl *Destructor |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 4950 | = CXXDestructorDecl::Create(Context, ClassDecl, NameInfo, Ty, 0, |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4951 | /*isInline=*/true, |
| 4952 | /*isImplicitlyDeclared=*/true); |
| 4953 | Destructor->setAccess(AS_public); |
| 4954 | Destructor->setImplicit(); |
| 4955 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4956 | |
| 4957 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4958 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 4959 | |
| 4960 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4961 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4962 | PushOnScopeChains(Destructor, S, false); |
| 4963 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4964 | |
| 4965 | // This could be uniqued if it ever proves significant. |
| 4966 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
| 4967 | |
| 4968 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4969 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4970 | return Destructor; |
| 4971 | } |
| 4972 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4973 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4974 | CXXDestructorDecl *Destructor) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4975 | assert((Destructor->isImplicit() && !Destructor->isUsed(false)) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4976 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4977 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4978 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4979 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4980 | if (Destructor->isInvalidDecl()) |
| 4981 | return; |
| 4982 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4983 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4984 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 4985 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4986 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4987 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4988 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4989 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4990 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4991 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4992 | |
| 4993 | Destructor->setInvalidDecl(); |
| 4994 | return; |
| 4995 | } |
| 4996 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4997 | SourceLocation Loc = Destructor->getLocation(); |
| 4998 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4999 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5000 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5001 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5004 | /// \brief Builds a statement that copies the given entity from \p From to |
| 5005 | /// \c To. |
| 5006 | /// |
| 5007 | /// This routine is used to copy the members of a class with an |
| 5008 | /// implicitly-declared copy assignment operator. When the entities being |
| 5009 | /// copied are arrays, this routine builds for loops to copy them. |
| 5010 | /// |
| 5011 | /// \param S The Sema object used for type-checking. |
| 5012 | /// |
| 5013 | /// \param Loc The location where the implicit copy is being generated. |
| 5014 | /// |
| 5015 | /// \param T The type of the expressions being copied. Both expressions must |
| 5016 | /// have this type. |
| 5017 | /// |
| 5018 | /// \param To The expression we are copying to. |
| 5019 | /// |
| 5020 | /// \param From The expression we are copying from. |
| 5021 | /// |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5022 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 5023 | /// Otherwise, it's a non-static member subobject. |
| 5024 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5025 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 5026 | /// |
| 5027 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5028 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5029 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5030 | Expr *To, Expr *From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5031 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5032 | // C++0x [class.copy]p30: |
| 5033 | // Each subobject is assigned in the manner appropriate to its type: |
| 5034 | // |
| 5035 | // - if the subobject is of class type, the copy assignment operator |
| 5036 | // for the class is used (as if by explicit qualification; that is, |
| 5037 | // ignoring any possible virtual overriding functions in more derived |
| 5038 | // classes); |
| 5039 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 5040 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 5041 | |
| 5042 | // Look for operator=. |
| 5043 | DeclarationName Name |
| 5044 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 5045 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 5046 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 5047 | |
| 5048 | // Filter out any result that isn't a copy-assignment operator. |
| 5049 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 5050 | while (F.hasNext()) { |
| 5051 | NamedDecl *D = F.next(); |
| 5052 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 5053 | if (Method->isCopyAssignmentOperator()) |
| 5054 | continue; |
| 5055 | |
| 5056 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 5057 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5058 | F.done(); |
| 5059 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5060 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 5061 | // assignment operators we found. This strange dance is required when |
| 5062 | // we're assigning via a base classes's copy-assignment operator. To |
| 5063 | // ensure that we're getting the right base class subobject (without |
| 5064 | // ambiguities), we need to cast "this" to that subobject type; to |
| 5065 | // ensure that we don't go through the virtual call mechanism, we need |
| 5066 | // to qualify the operator= name with the base class (see below). However, |
| 5067 | // this means that if the base class has a protected copy assignment |
| 5068 | // operator, the protected member access check will fail. So, we |
| 5069 | // rewrite "protected" access to "public" access in this case, since we |
| 5070 | // know by construction that we're calling from a derived class. |
| 5071 | if (CopyingBaseSubobject) { |
| 5072 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 5073 | L != LEnd; ++L) { |
| 5074 | if (L.getAccess() == AS_protected) |
| 5075 | L.setAccess(AS_public); |
| 5076 | } |
| 5077 | } |
| 5078 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5079 | // Create the nested-name-specifier that will be used to qualify the |
| 5080 | // reference to operator=; this is required to suppress the virtual |
| 5081 | // call mechanism. |
| 5082 | CXXScopeSpec SS; |
| 5083 | SS.setRange(Loc); |
| 5084 | SS.setScopeRep(NestedNameSpecifier::Create(S.Context, 0, false, |
| 5085 | T.getTypePtr())); |
| 5086 | |
| 5087 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5088 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5089 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5090 | /*FirstQualifierInScope=*/0, OpLookup, |
| 5091 | /*TemplateArgs=*/0, |
| 5092 | /*SuppressQualifierCheck=*/true); |
| 5093 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5094 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5095 | |
| 5096 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5097 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5098 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5099 | OpEqualRef.takeAs<Expr>(), |
| 5100 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5101 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5102 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5103 | |
| 5104 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5105 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 5106 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5107 | // - if the subobject is of scalar type, the built-in assignment |
| 5108 | // operator is used. |
| 5109 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 5110 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5111 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5112 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5113 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5114 | |
| 5115 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5116 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5117 | |
| 5118 | // - if the subobject is an array, each element is assigned, in the |
| 5119 | // manner appropriate to the element type; |
| 5120 | |
| 5121 | // Construct a loop over the array bounds, e.g., |
| 5122 | // |
| 5123 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 5124 | // |
| 5125 | // that will copy each of the array elements. |
| 5126 | QualType SizeType = S.Context.getSizeType(); |
| 5127 | |
| 5128 | // Create the iteration variable. |
| 5129 | IdentifierInfo *IterationVarName = 0; |
| 5130 | { |
| 5131 | llvm::SmallString<8> Str; |
| 5132 | llvm::raw_svector_ostream OS(Str); |
| 5133 | OS << "__i" << Depth; |
| 5134 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 5135 | } |
| 5136 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, |
| 5137 | IterationVarName, SizeType, |
| 5138 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5139 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5140 | |
| 5141 | // Initialize the iteration variable to zero. |
| 5142 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5143 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5144 | |
| 5145 | // Create a reference to the iteration variable; we'll use this several |
| 5146 | // times throughout. |
| 5147 | Expr *IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5148 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5149 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 5150 | |
| 5151 | // Create the DeclStmt that holds the iteration variable. |
| 5152 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 5153 | |
| 5154 | // Create the comparison against the array bound. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5155 | llvm::APInt Upper |
| 5156 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5157 | Expr *Comparison |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5158 | = new (S.Context) BinaryOperator(IterationVarRef, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5159 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 5160 | BO_NE, S.Context.BoolTy, |
| 5161 | VK_RValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5162 | |
| 5163 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5164 | Expr *Increment |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5165 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 5166 | VK_LValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5167 | |
| 5168 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5169 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 5170 | IterationVarRef, Loc)); |
| 5171 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 5172 | IterationVarRef, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5173 | |
| 5174 | // Build the copy for an individual element of the array. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5175 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, ArrayTy->getElementType(), |
| 5176 | To, From, CopyingBaseSubobject, |
| 5177 | Depth + 1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 5178 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5179 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5180 | |
| 5181 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5182 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5183 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5184 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5185 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5188 | /// \brief Determine whether the given class has a copy assignment operator |
| 5189 | /// that accepts a const-qualified argument. |
| 5190 | static bool hasConstCopyAssignment(Sema &S, const CXXRecordDecl *CClass) { |
| 5191 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(CClass); |
| 5192 | |
| 5193 | if (!Class->hasDeclaredCopyAssignment()) |
| 5194 | S.DeclareImplicitCopyAssignment(Class); |
| 5195 | |
| 5196 | QualType ClassType = S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)); |
| 5197 | DeclarationName OpName |
| 5198 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 5199 | |
| 5200 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 5201 | for (llvm::tie(Op, OpEnd) = Class->lookup(OpName); Op != OpEnd; ++Op) { |
| 5202 | // C++ [class.copy]p9: |
| 5203 | // A user-declared copy assignment operator is a non-static non-template |
| 5204 | // member function of class X with exactly one parameter of type X, X&, |
| 5205 | // const X&, volatile X& or const volatile X&. |
| 5206 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 5207 | if (!Method) |
| 5208 | continue; |
| 5209 | |
| 5210 | if (Method->isStatic()) |
| 5211 | continue; |
| 5212 | if (Method->getPrimaryTemplate()) |
| 5213 | continue; |
| 5214 | const FunctionProtoType *FnType = |
| 5215 | Method->getType()->getAs<FunctionProtoType>(); |
| 5216 | assert(FnType && "Overloaded operator has no prototype."); |
| 5217 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 5218 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 5219 | continue; |
| 5220 | bool AcceptsConst = true; |
| 5221 | QualType ArgType = FnType->getArgType(0); |
| 5222 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()){ |
| 5223 | ArgType = Ref->getPointeeType(); |
| 5224 | // Is it a non-const lvalue reference? |
| 5225 | if (!ArgType.isConstQualified()) |
| 5226 | AcceptsConst = false; |
| 5227 | } |
| 5228 | if (!S.Context.hasSameUnqualifiedType(ArgType, ClassType)) |
| 5229 | continue; |
| 5230 | |
| 5231 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 5232 | // copy assignment operator. Return whether it accepts const arguments. |
| 5233 | return AcceptsConst; |
| 5234 | } |
| 5235 | assert(Class->isInvalidDecl() && |
| 5236 | "No copy assignment operator declared in valid code."); |
| 5237 | return false; |
| 5238 | } |
| 5239 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5240 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5241 | // Note: The following rules are largely analoguous to the copy |
| 5242 | // constructor rules. Note that virtual bases are not taken into account |
| 5243 | // for determining the argument type of the operator. Note also that |
| 5244 | // operators taking an object instead of a reference are allowed. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5245 | |
| 5246 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5247 | // C++ [class.copy]p10: |
| 5248 | // If the class definition does not explicitly declare a copy |
| 5249 | // assignment operator, one is declared implicitly. |
| 5250 | // The implicitly-defined copy assignment operator for a class X |
| 5251 | // will have the form |
| 5252 | // |
| 5253 | // X& X::operator=(const X&) |
| 5254 | // |
| 5255 | // if |
| 5256 | bool HasConstCopyAssignment = true; |
| 5257 | |
| 5258 | // -- each direct base class B of X has a copy assignment operator |
| 5259 | // whose parameter is of type const B&, const volatile B& or B, |
| 5260 | // and |
| 5261 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5262 | BaseEnd = ClassDecl->bases_end(); |
| 5263 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 5264 | assert(!Base->getType()->isDependentType() && |
| 5265 | "Cannot generate implicit members for class with dependent bases."); |
| 5266 | const CXXRecordDecl *BaseClassDecl |
| 5267 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5268 | HasConstCopyAssignment = hasConstCopyAssignment(*this, BaseClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | // -- for all the nonstatic data members of X that are of a class |
| 5272 | // type M (or array thereof), each such class type has a copy |
| 5273 | // assignment operator whose parameter is of type const M&, |
| 5274 | // const volatile M& or M. |
| 5275 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5276 | FieldEnd = ClassDecl->field_end(); |
| 5277 | HasConstCopyAssignment && Field != FieldEnd; |
| 5278 | ++Field) { |
| 5279 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5280 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 5281 | const CXXRecordDecl *FieldClassDecl |
| 5282 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5283 | HasConstCopyAssignment = hasConstCopyAssignment(*this, FieldClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5284 | } |
| 5285 | } |
| 5286 | |
| 5287 | // Otherwise, the implicitly declared copy assignment operator will |
| 5288 | // have the form |
| 5289 | // |
| 5290 | // X& X::operator=(X&) |
| 5291 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 5292 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 5293 | if (HasConstCopyAssignment) |
| 5294 | ArgType = ArgType.withConst(); |
| 5295 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5296 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5297 | // C++ [except.spec]p14: |
| 5298 | // An implicitly declared special member function (Clause 12) shall have an |
| 5299 | // exception-specification. [...] |
| 5300 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5301 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5302 | BaseEnd = ClassDecl->bases_end(); |
| 5303 | Base != BaseEnd; ++Base) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5304 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5305 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5306 | |
| 5307 | if (!BaseClassDecl->hasDeclaredCopyAssignment()) |
| 5308 | DeclareImplicitCopyAssignment(BaseClassDecl); |
| 5309 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5310 | if (CXXMethodDecl *CopyAssign |
| 5311 | = BaseClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 5312 | ExceptSpec.CalledDecl(CopyAssign); |
| 5313 | } |
| 5314 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5315 | FieldEnd = ClassDecl->field_end(); |
| 5316 | Field != FieldEnd; |
| 5317 | ++Field) { |
| 5318 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5319 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5320 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5321 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5322 | |
| 5323 | if (!FieldClassDecl->hasDeclaredCopyAssignment()) |
| 5324 | DeclareImplicitCopyAssignment(FieldClassDecl); |
| 5325 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5326 | if (CXXMethodDecl *CopyAssign |
| 5327 | = FieldClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 5328 | ExceptSpec.CalledDecl(CopyAssign); |
| 5329 | } |
| 5330 | } |
| 5331 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5332 | // An implicitly-declared copy assignment operator is an inline public |
| 5333 | // member of its class. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5334 | FunctionProtoType::ExtProtoInfo EPI; |
| 5335 | EPI.HasExceptionSpec = ExceptSpec.hasExceptionSpecification(); |
| 5336 | EPI.HasAnyExceptionSpec = ExceptSpec.hasAnyExceptionSpecification(); |
| 5337 | EPI.NumExceptions = ExceptSpec.size(); |
| 5338 | EPI.Exceptions = ExceptSpec.data(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5339 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5340 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5341 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5342 | = CXXMethodDecl::Create(Context, ClassDecl, NameInfo, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5343 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5344 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5345 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5346 | /*isInline=*/true); |
| 5347 | CopyAssignment->setAccess(AS_public); |
| 5348 | CopyAssignment->setImplicit(); |
| 5349 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5350 | |
| 5351 | // Add the parameter to the operator. |
| 5352 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 5353 | ClassDecl->getLocation(), |
| 5354 | /*Id=*/0, |
| 5355 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5356 | SC_None, |
| 5357 | SC_None, 0); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5358 | CopyAssignment->setParams(&FromParam, 1); |
| 5359 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5360 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5361 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 5362 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5363 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5364 | PushOnScopeChains(CopyAssignment, S, false); |
| 5365 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5366 | |
| 5367 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 5368 | return CopyAssignment; |
| 5369 | } |
| 5370 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5371 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 5372 | CXXMethodDecl *CopyAssignOperator) { |
| 5373 | assert((CopyAssignOperator->isImplicit() && |
| 5374 | CopyAssignOperator->isOverloadedOperator() && |
| 5375 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5376 | !CopyAssignOperator->isUsed(false)) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5377 | "DefineImplicitCopyAssignment called for wrong function"); |
| 5378 | |
| 5379 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 5380 | |
| 5381 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 5382 | CopyAssignOperator->setInvalidDecl(); |
| 5383 | return; |
| 5384 | } |
| 5385 | |
| 5386 | CopyAssignOperator->setUsed(); |
| 5387 | |
| 5388 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 5389 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5390 | |
| 5391 | // C++0x [class.copy]p30: |
| 5392 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 5393 | // for a non-union class X performs memberwise copy assignment of its |
| 5394 | // subobjects. The direct base classes of X are assigned first, in the |
| 5395 | // order of their declaration in the base-specifier-list, and then the |
| 5396 | // immediate non-static data members of X are assigned, in the order in |
| 5397 | // which they were declared in the class definition. |
| 5398 | |
| 5399 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5400 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5401 | |
| 5402 | // The parameter for the "other" object, which we are copying from. |
| 5403 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 5404 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 5405 | QualType OtherRefType = Other->getType(); |
| 5406 | if (const LValueReferenceType *OtherRef |
| 5407 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 5408 | OtherRefType = OtherRef->getPointeeType(); |
| 5409 | OtherQuals = OtherRefType.getQualifiers(); |
| 5410 | } |
| 5411 | |
| 5412 | // Our location for everything implicitly-generated. |
| 5413 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 5414 | |
| 5415 | // Construct a reference to the "other" object. We'll be using this |
| 5416 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5417 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5418 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 5419 | |
| 5420 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 5421 | // ASTs. |
| 5422 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 5423 | assert(This && "Reference to this cannot fail!"); |
| 5424 | |
| 5425 | // Assign base classes. |
| 5426 | bool Invalid = false; |
| 5427 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5428 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 5429 | // Form the assignment: |
| 5430 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 5431 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 5432 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5433 | Invalid = true; |
| 5434 | continue; |
| 5435 | } |
| 5436 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5437 | CXXCastPath BasePath; |
| 5438 | BasePath.push_back(Base); |
| 5439 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5440 | // Construct the "from" expression, which is an implicit cast to the |
| 5441 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5442 | Expr *From = OtherRef; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5443 | ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5444 | CK_UncheckedDerivedToBase, |
| 5445 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5446 | |
| 5447 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5448 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5449 | |
| 5450 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 5451 | Expr *ToE = To.takeAs<Expr>(); |
| 5452 | ImpCastExprToType(ToE, |
| 5453 | Context.getCVRQualifiedType(BaseType, |
| 5454 | CopyAssignOperator->getTypeQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5455 | CK_UncheckedDerivedToBase, |
| 5456 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5457 | To = Owned(ToE); |
| 5458 | |
| 5459 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5460 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5461 | To.get(), From, |
| 5462 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5463 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5464 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5465 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5466 | CopyAssignOperator->setInvalidDecl(); |
| 5467 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
| 5470 | // Success! Record the copy. |
| 5471 | Statements.push_back(Copy.takeAs<Expr>()); |
| 5472 | } |
| 5473 | |
| 5474 | // \brief Reference to the __builtin_memcpy function. |
| 5475 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 5476 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5477 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5478 | |
| 5479 | // Assign non-static members. |
| 5480 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5481 | FieldEnd = ClassDecl->field_end(); |
| 5482 | Field != FieldEnd; ++Field) { |
| 5483 | // Check for members of reference type; we can't copy those. |
| 5484 | if (Field->getType()->isReferenceType()) { |
| 5485 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 5486 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 5487 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5488 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5489 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5490 | Invalid = true; |
| 5491 | continue; |
| 5492 | } |
| 5493 | |
| 5494 | // Check for members of const-qualified, non-class type. |
| 5495 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 5496 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 5497 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 5498 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 5499 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5500 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5501 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5502 | Invalid = true; |
| 5503 | continue; |
| 5504 | } |
| 5505 | |
| 5506 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 5507 | if (FieldType->isIncompleteArrayType()) { |
| 5508 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 5509 | "Incomplete array type is not valid"); |
| 5510 | continue; |
| 5511 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5512 | |
| 5513 | // Build references to the field in the object we're copying from and to. |
| 5514 | CXXScopeSpec SS; // Intentionally empty |
| 5515 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 5516 | LookupMemberName); |
| 5517 | MemberLookup.addDecl(*Field); |
| 5518 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5519 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5520 | Loc, /*IsArrow=*/false, |
| 5521 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5522 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5523 | Loc, /*IsArrow=*/true, |
| 5524 | SS, 0, MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5525 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 5526 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 5527 | |
| 5528 | // If the field should be copied with __builtin_memcpy rather than via |
| 5529 | // explicit assignments, do so. This optimization only applies for arrays |
| 5530 | // of scalars and arrays of class type with trivial copy-assignment |
| 5531 | // operators. |
| 5532 | if (FieldType->isArrayType() && |
| 5533 | (!BaseType->isRecordType() || |
| 5534 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()) |
| 5535 | ->hasTrivialCopyAssignment())) { |
| 5536 | // Compute the size of the memory buffer to be copied. |
| 5537 | QualType SizeType = Context.getSizeType(); |
| 5538 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 5539 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 5540 | for (const ConstantArrayType *Array |
| 5541 | = Context.getAsConstantArrayType(FieldType); |
| 5542 | Array; |
| 5543 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5544 | llvm::APInt ArraySize |
| 5545 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5546 | Size *= ArraySize; |
| 5547 | } |
| 5548 | |
| 5549 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5550 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 5551 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5552 | |
| 5553 | bool NeedsCollectableMemCpy = |
| 5554 | (BaseType->isRecordType() && |
| 5555 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 5556 | |
| 5557 | if (NeedsCollectableMemCpy) { |
| 5558 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 5559 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 5560 | LookupResult R(*this, |
| 5561 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5562 | Loc, LookupOrdinaryName); |
| 5563 | LookupName(R, TUScope, true); |
| 5564 | |
| 5565 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5566 | if (!CollectableMemCpy) { |
| 5567 | // Something went horribly wrong earlier, and we will have |
| 5568 | // complained about it. |
| 5569 | Invalid = true; |
| 5570 | continue; |
| 5571 | } |
| 5572 | |
| 5573 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 5574 | CollectableMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5575 | VK_LValue, Loc, 0).take(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5576 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 5577 | } |
| 5578 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5579 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5580 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5581 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 5582 | LookupOrdinaryName); |
| 5583 | LookupName(R, TUScope, true); |
| 5584 | |
| 5585 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5586 | if (!BuiltinMemCpy) { |
| 5587 | // Something went horribly wrong earlier, and we will have complained |
| 5588 | // about it. |
| 5589 | Invalid = true; |
| 5590 | continue; |
| 5591 | } |
| 5592 | |
| 5593 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 5594 | BuiltinMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5595 | VK_LValue, Loc, 0).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5596 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 5597 | } |
| 5598 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5599 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5600 | CallArgs.push_back(To.takeAs<Expr>()); |
| 5601 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5602 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5603 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5604 | if (NeedsCollectableMemCpy) |
| 5605 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5606 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5607 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5608 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5609 | else |
| 5610 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5611 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5612 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5613 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5615 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 5616 | Statements.push_back(Call.takeAs<Expr>()); |
| 5617 | continue; |
| 5618 | } |
| 5619 | |
| 5620 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5621 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5622 | To.get(), From.get(), |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5623 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5624 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5625 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5626 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5627 | CopyAssignOperator->setInvalidDecl(); |
| 5628 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | // Success! Record the copy. |
| 5632 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 5633 | } |
| 5634 | |
| 5635 | if (!Invalid) { |
| 5636 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5637 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5638 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5639 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5640 | if (Return.isInvalid()) |
| 5641 | Invalid = true; |
| 5642 | else { |
| 5643 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5644 | |
| 5645 | if (Trap.hasErrorOccurred()) { |
| 5646 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5647 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5648 | Invalid = true; |
| 5649 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | if (Invalid) { |
| 5654 | CopyAssignOperator->setInvalidDecl(); |
| 5655 | return; |
| 5656 | } |
| 5657 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5658 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5659 | /*isStmtExpr=*/false); |
| 5660 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 5661 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5664 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 5665 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5666 | // C++ [class.copy]p4: |
| 5667 | // If the class definition does not explicitly declare a copy |
| 5668 | // constructor, one is declared implicitly. |
| 5669 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5670 | // C++ [class.copy]p5: |
| 5671 | // The implicitly-declared copy constructor for a class X will |
| 5672 | // have the form |
| 5673 | // |
| 5674 | // X::X(const X&) |
| 5675 | // |
| 5676 | // if |
| 5677 | bool HasConstCopyConstructor = true; |
| 5678 | |
| 5679 | // -- each direct or virtual base class B of X has a copy |
| 5680 | // constructor whose first parameter is of type const B& or |
| 5681 | // const volatile B&, and |
| 5682 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5683 | BaseEnd = ClassDecl->bases_end(); |
| 5684 | HasConstCopyConstructor && Base != BaseEnd; |
| 5685 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5686 | // Virtual bases are handled below. |
| 5687 | if (Base->isVirtual()) |
| 5688 | continue; |
| 5689 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5690 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5691 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5692 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5693 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5694 | |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5695 | HasConstCopyConstructor |
| 5696 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5697 | } |
| 5698 | |
| 5699 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5700 | BaseEnd = ClassDecl->vbases_end(); |
| 5701 | HasConstCopyConstructor && Base != BaseEnd; |
| 5702 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5703 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5704 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5705 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5706 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5707 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5708 | HasConstCopyConstructor |
| 5709 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5710 | } |
| 5711 | |
| 5712 | // -- for all the nonstatic data members of X that are of a |
| 5713 | // class type M (or array thereof), each such class type |
| 5714 | // has a copy constructor whose first parameter is of type |
| 5715 | // const M& or const volatile M&. |
| 5716 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5717 | FieldEnd = ClassDecl->field_end(); |
| 5718 | HasConstCopyConstructor && Field != FieldEnd; |
| 5719 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5720 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5721 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5722 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5723 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5724 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5725 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5726 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5727 | HasConstCopyConstructor |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5728 | = FieldClassDecl->hasConstCopyConstructor(Context); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5729 | } |
| 5730 | } |
| 5731 | |
| 5732 | // Otherwise, the implicitly declared copy constructor will have |
| 5733 | // the form |
| 5734 | // |
| 5735 | // X::X(X&) |
| 5736 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 5737 | QualType ArgType = ClassType; |
| 5738 | if (HasConstCopyConstructor) |
| 5739 | ArgType = ArgType.withConst(); |
| 5740 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5741 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5742 | // C++ [except.spec]p14: |
| 5743 | // An implicitly declared special member function (Clause 12) shall have an |
| 5744 | // exception-specification. [...] |
| 5745 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5746 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 5747 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5748 | BaseEnd = ClassDecl->bases_end(); |
| 5749 | Base != BaseEnd; |
| 5750 | ++Base) { |
| 5751 | // Virtual bases are handled below. |
| 5752 | if (Base->isVirtual()) |
| 5753 | continue; |
| 5754 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5755 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5756 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5757 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5758 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5759 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5760 | if (CXXConstructorDecl *CopyConstructor |
| 5761 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5762 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5763 | } |
| 5764 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5765 | BaseEnd = ClassDecl->vbases_end(); |
| 5766 | Base != BaseEnd; |
| 5767 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5768 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5769 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5770 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5771 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5772 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5773 | if (CXXConstructorDecl *CopyConstructor |
| 5774 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5775 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5776 | } |
| 5777 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5778 | FieldEnd = ClassDecl->field_end(); |
| 5779 | Field != FieldEnd; |
| 5780 | ++Field) { |
| 5781 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5782 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5783 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5784 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5785 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5786 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5787 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5788 | if (CXXConstructorDecl *CopyConstructor |
| 5789 | = FieldClassDecl->getCopyConstructor(Context, Quals)) |
| 5790 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5791 | } |
| 5792 | } |
| 5793 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5794 | // An implicitly-declared copy constructor is an inline public |
| 5795 | // member of its class. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5796 | FunctionProtoType::ExtProtoInfo EPI; |
| 5797 | EPI.HasExceptionSpec = ExceptSpec.hasExceptionSpecification(); |
| 5798 | EPI.HasAnyExceptionSpec = ExceptSpec.hasAnyExceptionSpecification(); |
| 5799 | EPI.NumExceptions = ExceptSpec.size(); |
| 5800 | EPI.Exceptions = ExceptSpec.data(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5801 | DeclarationName Name |
| 5802 | = Context.DeclarationNames.getCXXConstructorName( |
| 5803 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5804 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5805 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5806 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5807 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5808 | &ArgType, 1, EPI), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5809 | /*TInfo=*/0, |
| 5810 | /*isExplicit=*/false, |
| 5811 | /*isInline=*/true, |
| 5812 | /*isImplicitlyDeclared=*/true); |
| 5813 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5814 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 5815 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5816 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5817 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 5818 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5819 | // Add the parameter to the constructor. |
| 5820 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 5821 | ClassDecl->getLocation(), |
| 5822 | /*IdentifierInfo=*/0, |
| 5823 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5824 | SC_None, |
| 5825 | SC_None, 0); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5826 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5827 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5828 | PushOnScopeChains(CopyConstructor, S, false); |
| 5829 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5830 | |
| 5831 | return CopyConstructor; |
| 5832 | } |
| 5833 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5834 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 5835 | CXXConstructorDecl *CopyConstructor, |
| 5836 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5837 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 5838 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5839 | !CopyConstructor->isUsed(false)) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5840 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5841 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 5842 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5843 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5844 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5845 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 5846 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5847 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 5848 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5849 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5850 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5851 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5852 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5853 | } else { |
| 5854 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 5855 | CopyConstructor->getLocation(), |
| 5856 | MultiStmtArg(*this, 0, 0), |
| 5857 | /*isStmtExpr=*/false) |
| 5858 | .takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 5859 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5860 | |
| 5861 | CopyConstructor->setUsed(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5862 | } |
| 5863 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5864 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5865 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5866 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5867 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5868 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5869 | unsigned ConstructKind, |
| 5870 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5871 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5872 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5873 | // C++0x [class.copy]p34: |
| 5874 | // When certain criteria are met, an implementation is allowed to |
| 5875 | // omit the copy/move construction of a class object, even if the |
| 5876 | // copy/move constructor and/or destructor for the object have |
| 5877 | // side effects. [...] |
| 5878 | // - when a temporary class object that has not been bound to a |
| 5879 | // reference (12.2) would be copied/moved to a class object |
| 5880 | // with the same cv-unqualified type, the copy/move operation |
| 5881 | // can be omitted by constructing the temporary object |
| 5882 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5883 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | 70a21de | 2011-01-27 23:24:55 +0000 | [diff] [blame] | 5884 | Constructor->isCopyOrMoveConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5885 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5886 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5887 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5888 | |
| 5889 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5890 | Elidable, move(ExprArgs), RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5891 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5894 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 5895 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5896 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5897 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 5898 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5899 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5900 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5901 | unsigned ConstructKind, |
| 5902 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5903 | unsigned NumExprs = ExprArgs.size(); |
| 5904 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5906 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5907 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5908 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5909 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5910 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 5911 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5912 | } |
| 5913 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5914 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5915 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5916 | MultiExprArg Exprs) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5917 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5918 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 5919 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5920 | move(Exprs), false, CXXConstructExpr::CK_Complete, |
| 5921 | SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5922 | if (TempResult.isInvalid()) |
| 5923 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5924 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 5925 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5926 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5927 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 5928 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5929 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5930 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5931 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5934 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 5935 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 333de06 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 5936 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
Douglas Gregor | fb2db46 | 2010-05-22 17:12:29 +0000 | [diff] [blame] | 5937 | !ClassDecl->hasTrivialDestructor() && !ClassDecl->isDependentContext()) { |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5938 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5939 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5940 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5941 | PDiag(diag::err_access_dtor_var) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5942 | << VD->getDeclName() |
| 5943 | << VD->getType()); |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5944 | |
John McCall | ae79222 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 5945 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 5946 | if (!VD->isInvalidDecl() && VD->hasGlobalStorage() && !VD->isStaticLocal()) |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5947 | Diag(VD->getLocation(), diag::warn_global_destructor); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5948 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5951 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5952 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 5953 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5954 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5955 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5956 | MultiExprArg Exprs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5957 | SourceLocation RParenLoc, |
| 5958 | bool TypeMayContainAuto) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 5959 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5960 | |
| 5961 | // If there is no declaration, there was an error parsing it. Just ignore |
| 5962 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5963 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5964 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5965 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5966 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 5967 | if (!VDecl) { |
| 5968 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 5969 | RealDecl->setInvalidDecl(); |
| 5970 | return; |
| 5971 | } |
| 5972 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5973 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 5974 | if (TypeMayContainAuto && VDecl->getType()->getContainedAutoType()) { |
| 5975 | VDecl->setParsingAutoInit(false); |
| 5976 | |
| 5977 | // FIXME: n3225 doesn't actually seem to indicate this is ill-formed |
| 5978 | if (Exprs.size() > 1) { |
| 5979 | Diag(Exprs.get()[1]->getSourceRange().getBegin(), |
| 5980 | diag::err_auto_var_init_multiple_expressions) |
| 5981 | << VDecl->getDeclName() << VDecl->getType() |
| 5982 | << VDecl->getSourceRange(); |
| 5983 | RealDecl->setInvalidDecl(); |
| 5984 | return; |
| 5985 | } |
| 5986 | |
| 5987 | Expr *Init = Exprs.get()[0]; |
| 5988 | QualType DeducedType; |
| 5989 | if (!DeduceAutoType(VDecl->getType(), Init, DeducedType)) { |
| 5990 | Diag(VDecl->getLocation(), diag::err_auto_var_deduction_failure) |
| 5991 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 5992 | << Init->getSourceRange(); |
| 5993 | RealDecl->setInvalidDecl(); |
| 5994 | return; |
| 5995 | } |
| 5996 | VDecl->setType(DeducedType); |
| 5997 | |
| 5998 | // If this is a redeclaration, check that the type we just deduced matches |
| 5999 | // the previously declared type. |
| 6000 | if (VarDecl *Old = VDecl->getPreviousDeclaration()) |
| 6001 | MergeVarDeclTypes(VDecl, Old); |
| 6002 | } |
| 6003 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 6004 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 6005 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6006 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 6007 | // |
| 6008 | // Clients that want to distinguish between the two forms, can check for |
| 6009 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 6010 | // A major benefit is that clients that don't particularly care about which |
| 6011 | // exactly form was it (like the CodeGen) can handle both cases without |
| 6012 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 6013 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6014 | // C++ 8.5p11: |
| 6015 | // The form of initialization (using parentheses or '=') is generally |
| 6016 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 6017 | // class type. |
| 6018 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6019 | if (!VDecl->getType()->isDependentType() && |
| 6020 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 6021 | diag::err_typecheck_decl_incomplete_type)) { |
| 6022 | VDecl->setInvalidDecl(); |
| 6023 | return; |
| 6024 | } |
| 6025 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6026 | // The variable can not have an abstract class type. |
| 6027 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 6028 | diag::err_abstract_type_in_decl, |
| 6029 | AbstractVariableType)) |
| 6030 | VDecl->setInvalidDecl(); |
| 6031 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 6032 | const VarDecl *Def; |
| 6033 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6034 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 6035 | << VDecl->getDeclName(); |
| 6036 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6037 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 6038 | return; |
| 6039 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6040 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 6041 | // C++ [class.static.data]p4 |
| 6042 | // If a static data member is of const integral or const |
| 6043 | // enumeration type, its declaration in the class definition can |
| 6044 | // specify a constant-initializer which shall be an integral |
| 6045 | // constant expression (5.19). In that case, the member can appear |
| 6046 | // in integral constant expressions. The member shall still be |
| 6047 | // defined in a namespace scope if it is used in the program and the |
| 6048 | // namespace scope definition shall not contain an initializer. |
| 6049 | // |
| 6050 | // We already performed a redefinition check above, but for static |
| 6051 | // data members we also need to check whether there was an in-class |
| 6052 | // declaration with an initializer. |
| 6053 | const VarDecl* PrevInit = 0; |
| 6054 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 6055 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 6056 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 6057 | return; |
| 6058 | } |
| 6059 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 6060 | bool IsDependent = false; |
| 6061 | for (unsigned I = 0, N = Exprs.size(); I != N; ++I) { |
| 6062 | if (DiagnoseUnexpandedParameterPack(Exprs.get()[I], UPPC_Expression)) { |
| 6063 | VDecl->setInvalidDecl(); |
| 6064 | return; |
| 6065 | } |
| 6066 | |
| 6067 | if (Exprs.get()[I]->isTypeDependent()) |
| 6068 | IsDependent = true; |
| 6069 | } |
| 6070 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6071 | // If either the declaration has a dependent type or if any of the |
| 6072 | // expressions is type-dependent, we represent the initialization |
| 6073 | // via a ParenListExpr for later use during template instantiation. |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 6074 | if (VDecl->getType()->isDependentType() || IsDependent) { |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6075 | // Let clients know that initialization was done with a direct initializer. |
| 6076 | VDecl->setCXXDirectInitializer(true); |
| 6077 | |
| 6078 | // Store the initialization expressions as a ParenListExpr. |
| 6079 | unsigned NumExprs = Exprs.size(); |
| 6080 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 6081 | (Expr **)Exprs.release(), |
| 6082 | NumExprs, RParenLoc)); |
| 6083 | return; |
| 6084 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6085 | |
| 6086 | // Capture the variable that is being initialized and the style of |
| 6087 | // initialization. |
| 6088 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 6089 | |
| 6090 | // FIXME: Poor source location information. |
| 6091 | InitializationKind Kind |
| 6092 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 6093 | LParenLoc, RParenLoc); |
| 6094 | |
| 6095 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6096 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6097 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6098 | if (Result.isInvalid()) { |
| 6099 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6100 | return; |
| 6101 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6102 | |
| 6103 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6104 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 6105 | Result = MaybeCreateExprWithCleanups(Result); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6106 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6107 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 6108 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 6109 | CheckCompleteVariableDeclaration(VDecl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6110 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 6111 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6112 | /// \brief Given a constructor and the set of arguments provided for the |
| 6113 | /// constructor, convert the arguments and add any required default arguments |
| 6114 | /// to form a proper call to this constructor. |
| 6115 | /// |
| 6116 | /// \returns true if an error occurred, false otherwise. |
| 6117 | bool |
| 6118 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 6119 | MultiExprArg ArgsPtr, |
| 6120 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6121 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6122 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 6123 | unsigned NumArgs = ArgsPtr.size(); |
| 6124 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 6125 | |
| 6126 | const FunctionProtoType *Proto |
| 6127 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 6128 | assert(Proto && "Constructor without a prototype?"); |
| 6129 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6130 | |
| 6131 | // If too few arguments are available, we'll fill in the rest with defaults. |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 6132 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6133 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 6134 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6135 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 6136 | |
| 6137 | VariadicCallType CallType = |
| 6138 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 6139 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 6140 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 6141 | Proto, 0, Args, NumArgs, AllArgs, |
| 6142 | CallType); |
| 6143 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 6144 | ConvertedArgs.push_back(AllArgs[i]); |
| 6145 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 6146 | } |
| 6147 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6148 | static inline bool |
| 6149 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 6150 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6151 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6152 | if (isa<NamespaceDecl>(DC)) { |
| 6153 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6154 | diag::err_operator_new_delete_declared_in_namespace) |
| 6155 | << FnDecl->getDeclName(); |
| 6156 | } |
| 6157 | |
| 6158 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6159 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6160 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6161 | diag::err_operator_new_delete_declared_static) |
| 6162 | << FnDecl->getDeclName(); |
| 6163 | } |
| 6164 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 6165 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6168 | static inline bool |
| 6169 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 6170 | CanQualType ExpectedResultType, |
| 6171 | CanQualType ExpectedFirstParamType, |
| 6172 | unsigned DependentParamTypeDiag, |
| 6173 | unsigned InvalidParamTypeDiag) { |
| 6174 | QualType ResultType = |
| 6175 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 6176 | |
| 6177 | // Check that the result type is not dependent. |
| 6178 | if (ResultType->isDependentType()) |
| 6179 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6180 | diag::err_operator_new_delete_dependent_result_type) |
| 6181 | << FnDecl->getDeclName() << ExpectedResultType; |
| 6182 | |
| 6183 | // Check that the result type is what we expect. |
| 6184 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 6185 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6186 | diag::err_operator_new_delete_invalid_result_type) |
| 6187 | << FnDecl->getDeclName() << ExpectedResultType; |
| 6188 | |
| 6189 | // A function template must have at least 2 parameters. |
| 6190 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 6191 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6192 | diag::err_operator_new_delete_template_too_few_parameters) |
| 6193 | << FnDecl->getDeclName(); |
| 6194 | |
| 6195 | // The function decl must have at least 1 parameter. |
| 6196 | if (FnDecl->getNumParams() == 0) |
| 6197 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6198 | diag::err_operator_new_delete_too_few_parameters) |
| 6199 | << FnDecl->getDeclName(); |
| 6200 | |
| 6201 | // Check the the first parameter type is not dependent. |
| 6202 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 6203 | if (FirstParamType->isDependentType()) |
| 6204 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 6205 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 6206 | |
| 6207 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 6208 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6209 | ExpectedFirstParamType) |
| 6210 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 6211 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 6212 | |
| 6213 | return false; |
| 6214 | } |
| 6215 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6216 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6217 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6218 | // C++ [basic.stc.dynamic.allocation]p1: |
| 6219 | // A program is ill-formed if an allocation function is declared in a |
| 6220 | // namespace scope other than global scope or declared static in global |
| 6221 | // scope. |
| 6222 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 6223 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6224 | |
| 6225 | CanQualType SizeTy = |
| 6226 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 6227 | |
| 6228 | // C++ [basic.stc.dynamic.allocation]p1: |
| 6229 | // The return type shall be void*. The first parameter shall have type |
| 6230 | // std::size_t. |
| 6231 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 6232 | SizeTy, |
| 6233 | diag::err_operator_new_dependent_param_type, |
| 6234 | diag::err_operator_new_param_type)) |
| 6235 | return true; |
| 6236 | |
| 6237 | // C++ [basic.stc.dynamic.allocation]p1: |
| 6238 | // The first parameter shall not have an associated default argument. |
| 6239 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 6240 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6241 | diag::err_operator_new_default_arg) |
| 6242 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 6243 | |
| 6244 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6248 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 6249 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 6250 | // A program is ill-formed if deallocation functions are declared in a |
| 6251 | // namespace scope other than global scope or declared static in global |
| 6252 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6253 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 6254 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6255 | |
| 6256 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 6257 | // Each deallocation function shall return void and its first parameter |
| 6258 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6259 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 6260 | SemaRef.Context.VoidPtrTy, |
| 6261 | diag::err_operator_delete_dependent_param_type, |
| 6262 | diag::err_operator_delete_param_type)) |
| 6263 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6264 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6265 | return false; |
| 6266 | } |
| 6267 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6268 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 6269 | /// of this overloaded operator is well-formed. If so, returns false; |
| 6270 | /// otherwise, emits appropriate diagnostics and returns true. |
| 6271 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6272 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6273 | "Expected an overloaded operator declaration"); |
| 6274 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6275 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 6276 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6277 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6278 | // The allocation and deallocation functions, operator new, |
| 6279 | // operator new[], operator delete and operator delete[], are |
| 6280 | // described completely in 3.7.3. The attributes and restrictions |
| 6281 | // found in the rest of this subclause do not apply to them unless |
| 6282 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 6283 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6284 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 6285 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 6286 | if (Op == OO_New || Op == OO_Array_New) |
| 6287 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6288 | |
| 6289 | // C++ [over.oper]p6: |
| 6290 | // An operator function shall either be a non-static member |
| 6291 | // function or be a non-member function and have at least one |
| 6292 | // parameter whose type is a class, a reference to a class, an |
| 6293 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6294 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 6295 | if (MethodDecl->isStatic()) |
| 6296 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6297 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6298 | } else { |
| 6299 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6300 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 6301 | ParamEnd = FnDecl->param_end(); |
| 6302 | Param != ParamEnd; ++Param) { |
| 6303 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 6304 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 6305 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6306 | ClassOrEnumParam = true; |
| 6307 | break; |
| 6308 | } |
| 6309 | } |
| 6310 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6311 | if (!ClassOrEnumParam) |
| 6312 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 6313 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6314 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
| 6317 | // C++ [over.oper]p8: |
| 6318 | // An operator function cannot have default arguments (8.3.6), |
| 6319 | // except where explicitly stated below. |
| 6320 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6321 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6322 | // (C++ [over.call]p1). |
| 6323 | if (Op != OO_Call) { |
| 6324 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 6325 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6326 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6327 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6328 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6329 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6330 | } |
| 6331 | } |
| 6332 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6333 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 6334 | { false, false, false } |
| 6335 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 6336 | , { Unary, Binary, MemberOnly } |
| 6337 | #include "clang/Basic/OperatorKinds.def" |
| 6338 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6339 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6340 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 6341 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 6342 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6343 | |
| 6344 | // C++ [over.oper]p8: |
| 6345 | // [...] Operator functions cannot have more or fewer parameters |
| 6346 | // than the number required for the corresponding operator, as |
| 6347 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6348 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6349 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6350 | if (Op != OO_Call && |
| 6351 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 6352 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 6353 | (NumParams < 1) || (NumParams > 2))) { |
| 6354 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6355 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6356 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6357 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6358 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6359 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6360 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 6361 | assert(CanBeBinaryOperator && |
| 6362 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6363 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6364 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6365 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6366 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6367 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6368 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 6369 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6370 | // Overloaded operators other than operator() cannot be variadic. |
| 6371 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6372 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 6373 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6374 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6378 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 6379 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 6380 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6381 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
| 6384 | // C++ [over.inc]p1: |
| 6385 | // The user-defined function called operator++ implements the |
| 6386 | // prefix and postfix ++ operator. If this function is a member |
| 6387 | // function with no parameters, or a non-member function with one |
| 6388 | // parameter of class or enumeration type, it defines the prefix |
| 6389 | // increment operator ++ for objects of that type. If the function |
| 6390 | // is a member function with one parameter (which shall be of type |
| 6391 | // int) or a non-member function with two parameters (the second |
| 6392 | // of which shall be of type int), it defines the postfix |
| 6393 | // increment operator ++ for objects of that type. |
| 6394 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 6395 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 6396 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6397 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6398 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 6399 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 6400 | if (!ParamIsInt) |
| 6401 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6402 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6403 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6406 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6407 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 6408 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6409 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 6410 | /// of this literal operator function is well-formed. If so, returns |
| 6411 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 6412 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 6413 | DeclContext *DC = FnDecl->getDeclContext(); |
| 6414 | Decl::Kind Kind = DC->getDeclKind(); |
| 6415 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 6416 | Kind != Decl::LinkageSpec) { |
| 6417 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 6418 | << FnDecl->getDeclName(); |
| 6419 | return true; |
| 6420 | } |
| 6421 | |
| 6422 | bool Valid = false; |
| 6423 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6424 | // template <char...> type operator "" name() is the only valid template |
| 6425 | // signature, and the only valid signature with no parameters. |
| 6426 | if (FnDecl->param_size() == 0) { |
| 6427 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 6428 | // Must have only one template parameter |
| 6429 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 6430 | if (Params->size() == 1) { |
| 6431 | NonTypeTemplateParmDecl *PmDecl = |
| 6432 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6433 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6434 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6435 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 6436 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 6437 | Valid = true; |
| 6438 | } |
| 6439 | } |
| 6440 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6441 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6442 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 6443 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6444 | QualType T = (*Param)->getType(); |
| 6445 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 6446 | // unsigned long long int, long double, and any character type are allowed |
| 6447 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6448 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 6449 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 6450 | Context.hasSameType(T, Context.CharTy) || |
| 6451 | Context.hasSameType(T, Context.WCharTy) || |
| 6452 | Context.hasSameType(T, Context.Char16Ty) || |
| 6453 | Context.hasSameType(T, Context.Char32Ty)) { |
| 6454 | if (++Param == FnDecl->param_end()) |
| 6455 | Valid = true; |
| 6456 | goto FinishedParams; |
| 6457 | } |
| 6458 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 6459 | // Otherwise it must be a pointer to const; let's strip those qualifiers. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6460 | const PointerType *PT = T->getAs<PointerType>(); |
| 6461 | if (!PT) |
| 6462 | goto FinishedParams; |
| 6463 | T = PT->getPointeeType(); |
| 6464 | if (!T.isConstQualified()) |
| 6465 | goto FinishedParams; |
| 6466 | T = T.getUnqualifiedType(); |
| 6467 | |
| 6468 | // Move on to the second parameter; |
| 6469 | ++Param; |
| 6470 | |
| 6471 | // If there is no second parameter, the first must be a const char * |
| 6472 | if (Param == FnDecl->param_end()) { |
| 6473 | if (Context.hasSameType(T, Context.CharTy)) |
| 6474 | Valid = true; |
| 6475 | goto FinishedParams; |
| 6476 | } |
| 6477 | |
| 6478 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 6479 | // are allowed as the first parameter to a two-parameter function |
| 6480 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 6481 | Context.hasSameType(T, Context.WCharTy) || |
| 6482 | Context.hasSameType(T, Context.Char16Ty) || |
| 6483 | Context.hasSameType(T, Context.Char32Ty))) |
| 6484 | goto FinishedParams; |
| 6485 | |
| 6486 | // The second and final parameter must be an std::size_t |
| 6487 | T = (*Param)->getType().getUnqualifiedType(); |
| 6488 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 6489 | ++Param == FnDecl->param_end()) |
| 6490 | Valid = true; |
| 6491 | } |
| 6492 | |
| 6493 | // FIXME: This diagnostic is absolutely terrible. |
| 6494 | FinishedParams: |
| 6495 | if (!Valid) { |
| 6496 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 6497 | << FnDecl->getDeclName(); |
| 6498 | return true; |
| 6499 | } |
| 6500 | |
| 6501 | return false; |
| 6502 | } |
| 6503 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6504 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 6505 | /// linkage specification, including the language and (if present) |
| 6506 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 6507 | /// the location of the language string literal, which is provided |
| 6508 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 6509 | /// the '{' brace. Otherwise, this linkage specification does not |
| 6510 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 6511 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 6512 | SourceLocation LangLoc, |
| 6513 | llvm::StringRef Lang, |
| 6514 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6515 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 6516 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6517 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 6518 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6519 | Language = LinkageSpecDecl::lang_cxx; |
| 6520 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6521 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6522 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6524 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6525 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6526 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6527 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6528 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6529 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6530 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6531 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6532 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6533 | } |
| 6534 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 6535 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6536 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 6537 | /// valid, it's the position of the closing '}' brace in a linkage |
| 6538 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6539 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 6540 | Decl *LinkageSpec, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6541 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6542 | if (LinkageSpec) |
| 6543 | PopDeclContext(); |
| 6544 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6547 | /// \brief Perform semantic analysis for the variable declaration that |
| 6548 | /// occurs within a C++ catch clause, returning the newly-created |
| 6549 | /// variable. |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6550 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6551 | TypeSourceInfo *TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6552 | IdentifierInfo *Name, |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6553 | SourceLocation Loc) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6554 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6555 | QualType ExDeclType = TInfo->getType(); |
| 6556 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6557 | // Arrays and functions decay. |
| 6558 | if (ExDeclType->isArrayType()) |
| 6559 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 6560 | else if (ExDeclType->isFunctionType()) |
| 6561 | ExDeclType = Context.getPointerType(ExDeclType); |
| 6562 | |
| 6563 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 6564 | // The exception-declaration shall not denote a pointer or reference to an |
| 6565 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6566 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6567 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6568 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6569 | Invalid = true; |
| 6570 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6571 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6572 | // GCC allows catching pointers and references to incomplete types |
| 6573 | // as an extension; so do we, but we warn by default. |
| 6574 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6575 | QualType BaseType = ExDeclType; |
| 6576 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6577 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6578 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6579 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6580 | BaseType = Ptr->getPointeeType(); |
| 6581 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6582 | DK = diag::ext_catch_incomplete_ptr; |
| 6583 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6584 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6585 | // For the purpose of error recovery, we treat rvalue refs like lvalue refs. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6586 | BaseType = Ref->getPointeeType(); |
| 6587 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6588 | DK = diag::ext_catch_incomplete_ref; |
| 6589 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6590 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6591 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6592 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 6593 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6594 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6595 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6596 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6597 | RequireNonAbstractType(Loc, ExDeclType, |
| 6598 | diag::err_abstract_type_in_decl, |
| 6599 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 6600 | Invalid = true; |
| 6601 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6602 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 6603 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 6604 | if (!Invalid && getLangOptions().ObjC1) { |
| 6605 | QualType T = ExDeclType; |
| 6606 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 6607 | T = RT->getPointeeType(); |
| 6608 | |
| 6609 | if (T->isObjCObjectType()) { |
| 6610 | Diag(Loc, diag::err_objc_object_catch); |
| 6611 | Invalid = true; |
| 6612 | } else if (T->isObjCObjectPointerType()) { |
| 6613 | if (!getLangOptions().NeXTRuntime) { |
| 6614 | Diag(Loc, diag::err_objc_pointer_cxx_catch_gnu); |
| 6615 | Invalid = true; |
| 6616 | } else if (!getLangOptions().ObjCNonFragileABI) { |
| 6617 | Diag(Loc, diag::err_objc_pointer_cxx_catch_fragile); |
| 6618 | Invalid = true; |
| 6619 | } |
| 6620 | } |
| 6621 | } |
| 6622 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6623 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6624 | Name, ExDeclType, TInfo, SC_None, |
| 6625 | SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 6626 | ExDecl->setExceptionVariable(true); |
| 6627 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6628 | if (!Invalid) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 6629 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6630 | // C++ [except.handle]p16: |
| 6631 | // The object declared in an exception-declaration or, if the |
| 6632 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 6633 | // copy-initialized (8.5) from the exception object. [...] |
| 6634 | // The object is destroyed when the handler exits, after the destruction |
| 6635 | // of any automatic objects initialized within the handler. |
| 6636 | // |
| 6637 | // We just pretend to initialize the object with itself, then make sure |
| 6638 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 6639 | QualType initType = ExDeclType; |
| 6640 | |
| 6641 | InitializedEntity entity = |
| 6642 | InitializedEntity::InitializeVariable(ExDecl); |
| 6643 | InitializationKind initKind = |
| 6644 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 6645 | |
| 6646 | Expr *opaqueValue = |
| 6647 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 6648 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 6649 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 6650 | MultiExprArg(&opaqueValue, 1)); |
| 6651 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6652 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 6653 | else { |
| 6654 | // If the constructor used was non-trivial, set this as the |
| 6655 | // "initializer". |
| 6656 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 6657 | if (!construct->getConstructor()->isTrivial()) { |
| 6658 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 6659 | ExDecl->setInit(init); |
| 6660 | } |
| 6661 | |
| 6662 | // And make sure it's destructable. |
| 6663 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 6664 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6665 | } |
| 6666 | } |
| 6667 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6668 | if (Invalid) |
| 6669 | ExDecl->setInvalidDecl(); |
| 6670 | |
| 6671 | return ExDecl; |
| 6672 | } |
| 6673 | |
| 6674 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 6675 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6676 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6677 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 6678 | bool Invalid = D.isInvalidType(); |
| 6679 | |
| 6680 | // Check for unexpanded parameter packs. |
| 6681 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 6682 | UPPC_ExceptionType)) { |
| 6683 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 6684 | D.getIdentifierLoc()); |
| 6685 | Invalid = true; |
| 6686 | } |
| 6687 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6688 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6689 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 6690 | LookupOrdinaryName, |
| 6691 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6692 | // The scope should be freshly made just for us. There is just no way |
| 6693 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6694 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6695 | if (PrevDecl->isTemplateParameter()) { |
| 6696 | // Maybe we will complain about the shadowed template parameter. |
| 6697 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6698 | } |
| 6699 | } |
| 6700 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6701 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6702 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 6703 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6704 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6707 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6708 | D.getIdentifier(), |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6709 | D.getIdentifierLoc()); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6710 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6711 | if (Invalid) |
| 6712 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6713 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6714 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6715 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6716 | PushOnScopeChains(ExDecl, S); |
| 6717 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6718 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6719 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6720 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6721 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6722 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6723 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6724 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6725 | Expr *AssertExpr, |
| 6726 | Expr *AssertMessageExpr_) { |
| 6727 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6728 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6729 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 6730 | llvm::APSInt Value(32); |
| 6731 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 6732 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 6733 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6734 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6735 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6736 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6737 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6738 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 6739 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6740 | } |
| 6741 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6742 | |
Douglas Gregor | 399ad97 | 2010-12-15 23:55:21 +0000 | [diff] [blame] | 6743 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 6744 | return 0; |
| 6745 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6746 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6747 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6748 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6749 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6750 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6751 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6752 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6753 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 6754 | /// |
| 6755 | /// \returns A friend declaration that. |
| 6756 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 6757 | TypeSourceInfo *TSInfo) { |
| 6758 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 6759 | |
| 6760 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6761 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6762 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6763 | if (!getLangOptions().CPlusPlus0x) { |
| 6764 | // C++03 [class.friend]p2: |
| 6765 | // An elaborated-type-specifier shall be used in a friend declaration |
| 6766 | // for a class.* |
| 6767 | // |
| 6768 | // * The class-key of the elaborated-type-specifier is required. |
| 6769 | if (!ActiveTemplateInstantiations.empty()) { |
| 6770 | // Do not complain about the form of friend template types during |
| 6771 | // template instantiation; we will already have complained when the |
| 6772 | // template was declared. |
| 6773 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 6774 | // If we evaluated the type to a record type, suggest putting |
| 6775 | // a tag in front. |
| 6776 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 6777 | RecordDecl *RD = RT->getDecl(); |
| 6778 | |
| 6779 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 6780 | |
| 6781 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 6782 | << (unsigned) RD->getTagKind() |
| 6783 | << T |
| 6784 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 6785 | InsertionText); |
| 6786 | } else { |
| 6787 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 6788 | << T |
| 6789 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 6790 | } |
| 6791 | } else if (T->getAs<EnumType>()) { |
| 6792 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6793 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6794 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6795 | } |
| 6796 | } |
| 6797 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6798 | // C++0x [class.friend]p3: |
| 6799 | // If the type specifier in a friend declaration designates a (possibly |
| 6800 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 6801 | // the friend declaration is ignored. |
| 6802 | |
| 6803 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 6804 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6805 | |
| 6806 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 6807 | } |
| 6808 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 6809 | /// Handle a friend tag declaration where the scope specifier was |
| 6810 | /// templated. |
| 6811 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 6812 | unsigned TagSpec, SourceLocation TagLoc, |
| 6813 | CXXScopeSpec &SS, |
| 6814 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 6815 | AttributeList *Attr, |
| 6816 | MultiTemplateParamsArg TempParamLists) { |
| 6817 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6818 | |
| 6819 | bool isExplicitSpecialization = false; |
| 6820 | unsigned NumMatchedTemplateParamLists = TempParamLists.size(); |
| 6821 | bool Invalid = false; |
| 6822 | |
| 6823 | if (TemplateParameterList *TemplateParams |
| 6824 | = MatchTemplateParametersToScopeSpecifier(TagLoc, SS, |
| 6825 | TempParamLists.get(), |
| 6826 | TempParamLists.size(), |
| 6827 | /*friend*/ true, |
| 6828 | isExplicitSpecialization, |
| 6829 | Invalid)) { |
| 6830 | --NumMatchedTemplateParamLists; |
| 6831 | |
| 6832 | if (TemplateParams->size() > 0) { |
| 6833 | // This is a declaration of a class template. |
| 6834 | if (Invalid) |
| 6835 | return 0; |
| 6836 | |
| 6837 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 6838 | SS, Name, NameLoc, Attr, |
| 6839 | TemplateParams, AS_public).take(); |
| 6840 | } else { |
| 6841 | // The "template<>" header is extraneous. |
| 6842 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 6843 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 6844 | isExplicitSpecialization = true; |
| 6845 | } |
| 6846 | } |
| 6847 | |
| 6848 | if (Invalid) return 0; |
| 6849 | |
| 6850 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 6851 | |
| 6852 | bool isAllExplicitSpecializations = true; |
| 6853 | for (unsigned I = 0; I != NumMatchedTemplateParamLists; ++I) { |
| 6854 | if (TempParamLists.get()[I]->size()) { |
| 6855 | isAllExplicitSpecializations = false; |
| 6856 | break; |
| 6857 | } |
| 6858 | } |
| 6859 | |
| 6860 | // FIXME: don't ignore attributes. |
| 6861 | |
| 6862 | // If it's explicit specializations all the way down, just forget |
| 6863 | // about the template header and build an appropriate non-templated |
| 6864 | // friend. TODO: for source fidelity, remember the headers. |
| 6865 | if (isAllExplicitSpecializations) { |
| 6866 | ElaboratedTypeKeyword Keyword |
| 6867 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 6868 | QualType T = CheckTypenameType(Keyword, SS.getScopeRep(), *Name, |
| 6869 | TagLoc, SS.getRange(), NameLoc); |
| 6870 | if (T.isNull()) |
| 6871 | return 0; |
| 6872 | |
| 6873 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6874 | if (isa<DependentNameType>(T)) { |
| 6875 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 6876 | TL.setKeywordLoc(TagLoc); |
| 6877 | TL.setQualifierRange(SS.getRange()); |
| 6878 | TL.setNameLoc(NameLoc); |
| 6879 | } else { |
| 6880 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
| 6881 | TL.setKeywordLoc(TagLoc); |
| 6882 | TL.setQualifierRange(SS.getRange()); |
| 6883 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 6884 | } |
| 6885 | |
| 6886 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 6887 | TSI, FriendLoc); |
| 6888 | Friend->setAccess(AS_public); |
| 6889 | CurContext->addDecl(Friend); |
| 6890 | return Friend; |
| 6891 | } |
| 6892 | |
| 6893 | // Handle the case of a templated-scope friend class. e.g. |
| 6894 | // template <class T> class A<T>::B; |
| 6895 | // FIXME: we don't support these right now. |
| 6896 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 6897 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 6898 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6899 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 6900 | TL.setKeywordLoc(TagLoc); |
| 6901 | TL.setQualifierRange(SS.getRange()); |
| 6902 | TL.setNameLoc(NameLoc); |
| 6903 | |
| 6904 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 6905 | TSI, FriendLoc); |
| 6906 | Friend->setAccess(AS_public); |
| 6907 | Friend->setUnsupportedFriend(true); |
| 6908 | CurContext->addDecl(Friend); |
| 6909 | return Friend; |
| 6910 | } |
| 6911 | |
| 6912 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6913 | /// Handle a friend type declaration. This works in tandem with |
| 6914 | /// ActOnTag. |
| 6915 | /// |
| 6916 | /// Notes on friend class templates: |
| 6917 | /// |
| 6918 | /// We generally treat friend class declarations as if they were |
| 6919 | /// declaring a class. So, for example, the elaborated type specifier |
| 6920 | /// in a friend declaration is required to obey the restrictions of a |
| 6921 | /// class-head (i.e. no typedefs in the scope chain), template |
| 6922 | /// parameters are required to match up with simple template-ids, &c. |
| 6923 | /// However, unlike when declaring a template specialization, it's |
| 6924 | /// okay to refer to a template specialization without an empty |
| 6925 | /// template parameter declaration, e.g. |
| 6926 | /// friend class A<T>::B<unsigned>; |
| 6927 | /// We permit this as a special case; if there are any template |
| 6928 | /// parameters present at all, require proper matching, i.e. |
| 6929 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6930 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 6931 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6932 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6933 | |
| 6934 | assert(DS.isFriendSpecified()); |
| 6935 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6936 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6937 | // Try to convert the decl specifier to a type. This works for |
| 6938 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 6939 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6940 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6941 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 6942 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6943 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6944 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6945 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 6946 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 6947 | return 0; |
| 6948 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6949 | // This is definitely an error in C++98. It's probably meant to |
| 6950 | // be forbidden in C++0x, too, but the specification is just |
| 6951 | // poorly written. |
| 6952 | // |
| 6953 | // The problem is with declarations like the following: |
| 6954 | // template <T> friend A<T>::foo; |
| 6955 | // where deciding whether a class C is a friend or not now hinges |
| 6956 | // on whether there exists an instantiation of A that causes |
| 6957 | // 'foo' to equal C. There are restrictions on class-heads |
| 6958 | // (which we declare (by fiat) elaborated friend declarations to |
| 6959 | // be) that makes this tractable. |
| 6960 | // |
| 6961 | // FIXME: handle "template <> friend class A<T>;", which |
| 6962 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 6963 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6964 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 6965 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6966 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6967 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6968 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6969 | // C++98 [class.friend]p1: A friend of a class is a function |
| 6970 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 6971 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 6972 | // deadline. It's also a very silly restriction that seriously |
| 6973 | // affects inner classes and which nobody else seems to implement; |
| 6974 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6975 | // |
| 6976 | // But note that we could warn about it: it's always useless to |
| 6977 | // friend one of your own members (it's not, however, worthless to |
| 6978 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6979 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6980 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6981 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6982 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6983 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 6984 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6985 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6986 | DS.getFriendSpecLoc()); |
| 6987 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6988 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 6989 | |
| 6990 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6991 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6992 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6993 | D->setAccess(AS_public); |
| 6994 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6995 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6996 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6997 | } |
| 6998 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6999 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 7000 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7001 | const DeclSpec &DS = D.getDeclSpec(); |
| 7002 | |
| 7003 | assert(DS.isFriendSpecified()); |
| 7004 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 7005 | |
| 7006 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7007 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 7008 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7009 | |
| 7010 | // C++ [class.friend]p1 |
| 7011 | // A friend of a class is a function or class.... |
| 7012 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7013 | // It *doesn't* see through dependent types, which is correct |
| 7014 | // according to [temp.arg.type]p3: |
| 7015 | // If a declaration acquires a function type through a |
| 7016 | // type dependent on a template-parameter and this causes |
| 7017 | // a declaration that does not use the syntactic form of a |
| 7018 | // function declarator to have a function type, the program |
| 7019 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7020 | if (!T->isFunctionType()) { |
| 7021 | Diag(Loc, diag::err_unexpected_friend); |
| 7022 | |
| 7023 | // It might be worthwhile to try to recover by creating an |
| 7024 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7025 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
| 7028 | // C++ [namespace.memdef]p3 |
| 7029 | // - If a friend declaration in a non-local class first declares a |
| 7030 | // class or function, the friend class or function is a member |
| 7031 | // of the innermost enclosing namespace. |
| 7032 | // - The name of the friend is not found by simple name lookup |
| 7033 | // until a matching declaration is provided in that namespace |
| 7034 | // scope (either before or after the class declaration granting |
| 7035 | // friendship). |
| 7036 | // - If a friend function is called, its name may be found by the |
| 7037 | // name lookup that considers functions from namespaces and |
| 7038 | // classes associated with the types of the function arguments. |
| 7039 | // - When looking for a prior declaration of a class or a function |
| 7040 | // declared as a friend, scopes outside the innermost enclosing |
| 7041 | // namespace scope are not considered. |
| 7042 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7043 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7044 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7045 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7046 | assert(Name); |
| 7047 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 7048 | // Check for unexpanded parameter packs. |
| 7049 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 7050 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 7051 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 7052 | return 0; |
| 7053 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7054 | // The context we found the declaration in, or in which we should |
| 7055 | // create the declaration. |
| 7056 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 7057 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7058 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7059 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7060 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7061 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7062 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7063 | // There are four cases here. |
| 7064 | // - There's no scope specifier, in which case we just go to the |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7065 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7066 | // there as appropriate. |
| 7067 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 7068 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7069 | // C++0x [namespace.memdef]p3: |
| 7070 | // If the name in a friend declaration is neither qualified nor |
| 7071 | // a template-id and the declaration is a function or an |
| 7072 | // elaborated-type-specifier, the lookup to determine whether |
| 7073 | // the entity has been previously declared shall not consider |
| 7074 | // any scopes outside the innermost enclosing namespace. |
| 7075 | // C++0x [class.friend]p11: |
| 7076 | // If a friend declaration appears in a local class and the name |
| 7077 | // specified is an unqualified name, a prior declaration is |
| 7078 | // looked up without considering scopes that are outside the |
| 7079 | // innermost enclosing non-class scope. For a friend function |
| 7080 | // declaration, if there is no prior declaration, the program is |
| 7081 | // ill-formed. |
| 7082 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 7083 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7084 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7085 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7086 | DC = CurContext; |
| 7087 | while (true) { |
| 7088 | // Skip class contexts. If someone can cite chapter and verse |
| 7089 | // for this behavior, that would be nice --- it's what GCC and |
| 7090 | // EDG do, and it seems like a reasonable intent, but the spec |
| 7091 | // really only says that checks for unqualified existing |
| 7092 | // declarations should stop at the nearest enclosing namespace, |
| 7093 | // not that they should only consider the nearest enclosing |
| 7094 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7095 | while (DC->isRecord()) |
| 7096 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7097 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7098 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7099 | |
| 7100 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7101 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7102 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7103 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 7104 | if (isTemplateId) { |
| 7105 | if (isa<TranslationUnitDecl>(DC)) break; |
| 7106 | } else { |
| 7107 | if (DC->isFileContext()) break; |
| 7108 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7109 | DC = DC->getParent(); |
| 7110 | } |
| 7111 | |
| 7112 | // C++ [class.friend]p1: A friend of a class is a function or |
| 7113 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 7114 | // C++0x changes this for both friend types and functions. |
| 7115 | // Most C++ 98 compilers do seem to give an error here, so |
| 7116 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7117 | if (!Previous.empty() && DC->Equals(CurContext) |
| 7118 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7119 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7120 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 7121 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7122 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7123 | // - There's a non-dependent scope specifier, in which case we |
| 7124 | // compute it and do a previous lookup there for a function |
| 7125 | // or function template. |
| 7126 | } else if (!SS.getScopeRep()->isDependent()) { |
| 7127 | DC = computeDeclContext(SS); |
| 7128 | if (!DC) return 0; |
| 7129 | |
| 7130 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 7131 | |
| 7132 | LookupQualifiedName(Previous, DC); |
| 7133 | |
| 7134 | // Ignore things found implicitly in the wrong scope. |
| 7135 | // TODO: better diagnostics for this case. Suggesting the right |
| 7136 | // qualified scope would be nice... |
| 7137 | LookupResult::Filter F = Previous.makeFilter(); |
| 7138 | while (F.hasNext()) { |
| 7139 | NamedDecl *D = F.next(); |
| 7140 | if (!DC->InEnclosingNamespaceSetOf( |
| 7141 | D->getDeclContext()->getRedeclContext())) |
| 7142 | F.erase(); |
| 7143 | } |
| 7144 | F.done(); |
| 7145 | |
| 7146 | if (Previous.empty()) { |
| 7147 | D.setInvalidType(); |
| 7148 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 7149 | return 0; |
| 7150 | } |
| 7151 | |
| 7152 | // C++ [class.friend]p1: A friend of a class is a function or |
| 7153 | // class that is not a member of the class . . . |
| 7154 | if (DC->Equals(CurContext)) |
| 7155 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 7156 | |
| 7157 | // - There's a scope specifier that does not match any template |
| 7158 | // parameter lists, in which case we use some arbitrary context, |
| 7159 | // create a method or method template, and wait for instantiation. |
| 7160 | // - There's a scope specifier that does match some template |
| 7161 | // parameter lists, which we don't handle right now. |
| 7162 | } else { |
| 7163 | DC = CurContext; |
| 7164 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7165 | } |
| 7166 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7167 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7168 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7169 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 7170 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 7171 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7172 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7173 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 7174 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7175 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7176 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7179 | bool Redeclaration = false; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 7180 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7181 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 7182 | IsDefinition, |
| 7183 | Redeclaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7184 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7185 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7186 | assert(ND->getDeclContext() == DC); |
| 7187 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 7188 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7189 | // Add the function declaration to the appropriate lookup tables, |
| 7190 | // adjusting the redeclarations list as necessary. We don't |
| 7191 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7192 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7193 | // Also update the scope-based lookup if the target context's |
| 7194 | // lookup context is in lexical scope. |
| 7195 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7196 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7197 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7198 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7199 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7200 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7201 | |
| 7202 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7203 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7204 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 7205 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7206 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7207 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7208 | if (ND->isInvalidDecl()) |
| 7209 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 7210 | else { |
| 7211 | FunctionDecl *FD; |
| 7212 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 7213 | FD = FTD->getTemplatedDecl(); |
| 7214 | else |
| 7215 | FD = cast<FunctionDecl>(ND); |
| 7216 | |
| 7217 | // Mark templated-scope function declarations as unsupported. |
| 7218 | if (FD->getNumTemplateParameterLists()) |
| 7219 | FrD->setUnsupportedFriend(true); |
| 7220 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7221 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7222 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 7223 | } |
| 7224 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7225 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 7226 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7227 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 7228 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 7229 | if (!Fn) { |
| 7230 | Diag(DelLoc, diag::err_deleted_non_function); |
| 7231 | return; |
| 7232 | } |
| 7233 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 7234 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 7235 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 7236 | // If the declaration wasn't the first, we delete the function anyway for |
| 7237 | // recovery. |
| 7238 | } |
| 7239 | Fn->setDeleted(); |
| 7240 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 7241 | |
| 7242 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 7243 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 7244 | Stmt *SubStmt = *CI; |
| 7245 | if (!SubStmt) |
| 7246 | continue; |
| 7247 | if (isa<ReturnStmt>(SubStmt)) |
| 7248 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 7249 | diag::err_return_in_constructor_handler); |
| 7250 | if (!isa<Expr>(SubStmt)) |
| 7251 | SearchForReturnInStmt(Self, SubStmt); |
| 7252 | } |
| 7253 | } |
| 7254 | |
| 7255 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 7256 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 7257 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 7258 | SearchForReturnInStmt(*this, Handler); |
| 7259 | } |
| 7260 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7261 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7262 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7263 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7264 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 7265 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7266 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 7267 | if (Context.hasSameType(NewTy, OldTy) || |
| 7268 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7269 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7270 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7271 | // Check if the return types are covariant |
| 7272 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7273 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7274 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 7275 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 7276 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7277 | NewClassTy = NewPT->getPointeeType(); |
| 7278 | OldClassTy = OldPT->getPointeeType(); |
| 7279 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 7280 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 7281 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 7282 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 7283 | NewClassTy = NewRT->getPointeeType(); |
| 7284 | OldClassTy = OldRT->getPointeeType(); |
| 7285 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7286 | } |
| 7287 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7288 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7289 | // The return types aren't either both pointers or references to a class type. |
| 7290 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7291 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7292 | diag::err_different_return_type_for_overriding_virtual_function) |
| 7293 | << New->getDeclName() << NewTy << OldTy; |
| 7294 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7295 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7296 | return true; |
| 7297 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7298 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 7299 | // C++ [class.virtual]p6: |
| 7300 | // If the return type of D::f differs from the return type of B::f, the |
| 7301 | // class type in the return type of D::f shall be complete at the point of |
| 7302 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 7303 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 7304 | if (!RT->isBeingDefined() && |
| 7305 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 7306 | PDiag(diag::err_covariant_return_incomplete) |
| 7307 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 7308 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 7309 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 7310 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 7311 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7312 | // Check if the new class derives from the old class. |
| 7313 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 7314 | Diag(New->getLocation(), |
| 7315 | diag::err_covariant_return_not_derived) |
| 7316 | << New->getDeclName() << NewTy << OldTy; |
| 7317 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7318 | return true; |
| 7319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7320 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7321 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 7322 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 7323 | diag::err_covariant_return_inaccessible_base, |
| 7324 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 7325 | // FIXME: Should this point to the return type? |
| 7326 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7327 | // FIXME: this note won't trigger for delayed access control |
| 7328 | // diagnostics, and it's impossible to get an undelayed error |
| 7329 | // here from access control during the original parse because |
| 7330 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7331 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7332 | return true; |
| 7333 | } |
| 7334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7335 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7336 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 7337 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7338 | Diag(New->getLocation(), |
| 7339 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7340 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7341 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7342 | return true; |
| 7343 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7344 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7345 | |
| 7346 | // The new class type must have the same or less qualifiers as the old type. |
| 7347 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 7348 | Diag(New->getLocation(), |
| 7349 | diag::err_covariant_return_type_class_type_more_qualified) |
| 7350 | << New->getDeclName() << NewTy << OldTy; |
| 7351 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7352 | return true; |
| 7353 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7354 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7355 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7356 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7357 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7358 | /// \brief Mark the given method pure. |
| 7359 | /// |
| 7360 | /// \param Method the method to be marked pure. |
| 7361 | /// |
| 7362 | /// \param InitRange the source range that covers the "0" initializer. |
| 7363 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 7364 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 7365 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7366 | return false; |
| 7367 | } |
| 7368 | |
| 7369 | if (!Method->isInvalidDecl()) |
| 7370 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 7371 | << Method->getDeclName() << InitRange; |
| 7372 | return true; |
| 7373 | } |
| 7374 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7375 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 7376 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 7377 | /// is a fresh scope pushed for just this purpose. |
| 7378 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7379 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 7380 | /// static data member of class X, names should be looked up in the scope of |
| 7381 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7382 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7383 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7384 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7385 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7386 | // We should only get called for declarations with scope specifiers, like: |
| 7387 | // int foo::bar; |
| 7388 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 7389 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7393 | /// initializer for the out-of-line declaration 'D'. |
| 7394 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7395 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7396 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7397 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7398 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 7399 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7400 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7401 | |
| 7402 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 7403 | /// C++ if/switch/while/for statement. |
| 7404 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7405 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7406 | // C++ 6.4p2: |
| 7407 | // The declarator shall not specify a function or an array. |
| 7408 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 7409 | // new class or enumeration. |
| 7410 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 7411 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 7412 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7413 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7414 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 7415 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7416 | |
| 7417 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 7418 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 7419 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 7420 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 7421 | << D.getSourceRange(); |
| 7422 | return DeclResult(); |
| 7423 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 7424 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 7425 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 7426 | } |
| 7427 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7428 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7429 | if (!Dcl) |
| 7430 | return DeclResult(); |
| 7431 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7432 | return Dcl; |
| 7433 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 7434 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7435 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 7436 | bool DefinitionRequired) { |
| 7437 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 7438 | // not have a vtable. |
| 7439 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 7440 | CurContext->isDependentContext() || |
| 7441 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 7442 | return; |
| 7443 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7444 | // Try to insert this class into the map. |
| 7445 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 7446 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 7447 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 7448 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 7449 | // If we already had an entry, check to see if we are promoting this vtable |
| 7450 | // to required a definition. If so, we need to reappend to the VTableUses |
| 7451 | // list, since we may have already processed the first entry. |
| 7452 | if (DefinitionRequired && !Pos.first->second) { |
| 7453 | Pos.first->second = true; |
| 7454 | } else { |
| 7455 | // Otherwise, we can early exit. |
| 7456 | return; |
| 7457 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
| 7460 | // Local classes need to have their virtual members marked |
| 7461 | // immediately. For all other classes, we mark their virtual members |
| 7462 | // at the end of the translation unit. |
| 7463 | if (Class->isLocalClass()) |
| 7464 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 7465 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7466 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 7467 | } |
| 7468 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7469 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7470 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7471 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 7472 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7473 | // Note: The VTableUses vector could grow as a result of marking |
| 7474 | // the members of a class as "used", so we check the size each |
| 7475 | // time through the loop and prefer indices (with are stable) to |
| 7476 | // iterators (which are not). |
| 7477 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 7478 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7479 | if (!Class) |
| 7480 | continue; |
| 7481 | |
| 7482 | SourceLocation Loc = VTableUses[I].second; |
| 7483 | |
| 7484 | // If this class has a key function, but that key function is |
| 7485 | // defined in another translation unit, we don't need to emit the |
| 7486 | // vtable even though we're using it. |
| 7487 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 7488 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7489 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 7490 | case TSK_Undeclared: |
| 7491 | case TSK_ExplicitSpecialization: |
| 7492 | case TSK_ExplicitInstantiationDeclaration: |
| 7493 | // The key function is in another translation unit. |
| 7494 | continue; |
| 7495 | |
| 7496 | case TSK_ExplicitInstantiationDefinition: |
| 7497 | case TSK_ImplicitInstantiation: |
| 7498 | // We will be instantiating the key function. |
| 7499 | break; |
| 7500 | } |
| 7501 | } else if (!KeyFunction) { |
| 7502 | // If we have a class with no key function that is the subject |
| 7503 | // of an explicit instantiation declaration, suppress the |
| 7504 | // vtable; it will live with the explicit instantiation |
| 7505 | // definition. |
| 7506 | bool IsExplicitInstantiationDeclaration |
| 7507 | = Class->getTemplateSpecializationKind() |
| 7508 | == TSK_ExplicitInstantiationDeclaration; |
| 7509 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 7510 | REnd = Class->redecls_end(); |
| 7511 | R != REnd; ++R) { |
| 7512 | TemplateSpecializationKind TSK |
| 7513 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 7514 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 7515 | IsExplicitInstantiationDeclaration = true; |
| 7516 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 7517 | IsExplicitInstantiationDeclaration = false; |
| 7518 | break; |
| 7519 | } |
| 7520 | } |
| 7521 | |
| 7522 | if (IsExplicitInstantiationDeclaration) |
| 7523 | continue; |
| 7524 | } |
| 7525 | |
| 7526 | // Mark all of the virtual members of this class as referenced, so |
| 7527 | // that we can build a vtable. Then, tell the AST consumer that a |
| 7528 | // vtable for this class is required. |
| 7529 | MarkVirtualMembersReferenced(Loc, Class); |
| 7530 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 7531 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 7532 | |
| 7533 | // Optionally warn if we're emitting a weak vtable. |
| 7534 | if (Class->getLinkage() == ExternalLinkage && |
| 7535 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 7536 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7537 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 7538 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 7539 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7540 | VTableUses.clear(); |
| 7541 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7542 | return true; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 7543 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7544 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7545 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 7546 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7547 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 7548 | e = RD->method_end(); i != e; ++i) { |
| 7549 | CXXMethodDecl *MD = *i; |
| 7550 | |
| 7551 | // C++ [basic.def.odr]p2: |
| 7552 | // [...] A virtual member function is used if it is not pure. [...] |
| 7553 | if (MD->isVirtual() && !MD->isPure()) |
| 7554 | MarkDeclarationReferenced(Loc, MD); |
| 7555 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7556 | |
| 7557 | // Only classes that have virtual bases need a VTT. |
| 7558 | if (RD->getNumVBases() == 0) |
| 7559 | return; |
| 7560 | |
| 7561 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 7562 | e = RD->bases_end(); i != e; ++i) { |
| 7563 | const CXXRecordDecl *Base = |
| 7564 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7565 | if (Base->getNumVBases() == 0) |
| 7566 | continue; |
| 7567 | MarkVirtualMembersReferenced(Loc, Base); |
| 7568 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7569 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7570 | |
| 7571 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 7572 | /// Objective-C implementation whose ivars need be initialized. |
| 7573 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 7574 | if (!getLangOptions().CPlusPlus) |
| 7575 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 7576 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7577 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 7578 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 7579 | if (ivars.empty()) |
| 7580 | return; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7581 | llvm::SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7582 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 7583 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7584 | if (Field->isInvalidDecl()) |
| 7585 | continue; |
| 7586 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7587 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7588 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 7589 | InitializationKind InitKind = |
| 7590 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 7591 | |
| 7592 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7593 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7594 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 7595 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7596 | // Note, MemberInit could actually come back empty if no initialization |
| 7597 | // is required (e.g., because it would call a trivial default constructor) |
| 7598 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 7599 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7600 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7601 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7602 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 7603 | SourceLocation(), |
| 7604 | MemberInit.takeAs<Expr>(), |
| 7605 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7606 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7607 | |
| 7608 | // Be sure that the destructor is accessible and is marked as referenced. |
| 7609 | if (const RecordType *RecordTy |
| 7610 | = Context.getBaseElementType(Field->getType()) |
| 7611 | ->getAs<RecordType>()) { |
| 7612 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7613 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7614 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 7615 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 7616 | PDiag(diag::err_access_dtor_ivar) |
| 7617 | << Context.getBaseElementType(Field->getType())); |
| 7618 | } |
| 7619 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7620 | } |
| 7621 | ObjCImplementation->setIvarInitializers(Context, |
| 7622 | AllToInit.data(), AllToInit.size()); |
| 7623 | } |
| 7624 | } |