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" |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 26 | #include "clang/AST/RecordLayout.h" |
| 27 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 29 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 30 | #include "clang/Sema/DeclSpec.h" |
| 31 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 32 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Preprocessor.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 36 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 37 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace clang; |
| 40 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // CheckDefaultArgumentVisitor |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 47 | /// the default argument of a parameter to determine whether it |
| 48 | /// contains any ill-formed subexpressions. For example, this will |
| 49 | /// diagnose the use of local variables or parameters within the |
| 50 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 51 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 52 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 53 | Expr *DefaultArg; |
| 54 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 60 | bool VisitExpr(Expr *Node); |
| 61 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 62 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 65 | /// VisitExpr - Visit all of the children of this expression. |
| 66 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 67 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 68 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 69 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 70 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 73 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 74 | /// determine whether this declaration can be used in the default |
| 75 | /// argument expression. |
| 76 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 77 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 78 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 79 | // C++ [dcl.fct.default]p9 |
| 80 | // Default arguments are evaluated each time the function is |
| 81 | // called. The order of evaluation of function arguments is |
| 82 | // unspecified. Consequently, parameters of a function shall not |
| 83 | // be used in default argument expressions, even if they are not |
| 84 | // evaluated. Parameters of a function declared before a default |
| 85 | // argument expression are in scope and can hide namespace and |
| 86 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 88 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 89 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 90 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 91 | // C++ [dcl.fct.default]p7 |
| 92 | // Local variables shall not be used in default argument |
| 93 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 94 | if (VDecl->isLocalVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 96 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 97 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 98 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 102 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 103 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 104 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 105 | // C++ [dcl.fct.default]p8: |
| 106 | // The keyword this shall not be used in a default argument of a |
| 107 | // member function. |
| 108 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 109 | diag::err_param_default_argument_references_this) |
| 110 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 114 | void Sema::ImplicitExceptionSpecification::CalledDecl(CXXMethodDecl *Method) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 115 | assert(Context && "ImplicitExceptionSpecification without an ASTContext"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 116 | // If we have an MSAny or unknown spec already, don't bother. |
| 117 | if (!Method || ComputedEST == EST_MSAny || ComputedEST == EST_Delayed) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 118 | return; |
| 119 | |
| 120 | const FunctionProtoType *Proto |
| 121 | = Method->getType()->getAs<FunctionProtoType>(); |
| 122 | |
| 123 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 124 | |
| 125 | // If this function can throw any exceptions, make a note of that. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 126 | if (EST == EST_Delayed || EST == EST_MSAny || EST == EST_None) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 127 | ClearExceptions(); |
| 128 | ComputedEST = EST; |
| 129 | return; |
| 130 | } |
| 131 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 132 | // FIXME: If the call to this decl is using any of its default arguments, we |
| 133 | // need to search them for potentially-throwing calls. |
| 134 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 135 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 136 | if (EST == EST_BasicNoexcept) |
| 137 | return; |
| 138 | |
| 139 | // If we have a throw-all spec at this point, ignore the function. |
| 140 | if (ComputedEST == EST_None) |
| 141 | return; |
| 142 | |
| 143 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 144 | // change to that specification. |
| 145 | if (EST == EST_DynamicNone) { |
| 146 | if (ComputedEST == EST_BasicNoexcept) |
| 147 | ComputedEST = EST_DynamicNone; |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Check out noexcept specs. |
| 152 | if (EST == EST_ComputedNoexcept) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 153 | FunctionProtoType::NoexceptResult NR = Proto->getNoexceptSpec(*Context); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 154 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 155 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 156 | assert(NR != FunctionProtoType::NR_Dependent && |
| 157 | "Should not generate implicit declarations for dependent cases, " |
| 158 | "and don't know how to handle them anyway."); |
| 159 | |
| 160 | // noexcept(false) -> no spec on the new function |
| 161 | if (NR == FunctionProtoType::NR_Throw) { |
| 162 | ClearExceptions(); |
| 163 | ComputedEST = EST_None; |
| 164 | } |
| 165 | // noexcept(true) won't change anything either. |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 170 | assert(ComputedEST != EST_None && |
| 171 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 172 | ComputedEST = EST_Dynamic; |
| 173 | // Record the exceptions in this function's exception specification. |
| 174 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 175 | EEnd = Proto->exception_end(); |
| 176 | E != EEnd; ++E) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 177 | if (ExceptionsSeen.insert(Context->getCanonicalType(*E))) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 178 | Exceptions.push_back(*E); |
| 179 | } |
| 180 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 181 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
| 182 | if (!E || ComputedEST == EST_MSAny || ComputedEST == EST_Delayed) |
| 183 | return; |
| 184 | |
| 185 | // FIXME: |
| 186 | // |
| 187 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 4857947 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 188 | // [An] implicit exception-specification specifies the type-id T if and |
| 189 | // only if T is allowed by the exception-specification of a function directly |
| 190 | // invoked by f's implicit definition; f shall allow all exceptions if any |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 191 | // function it directly invokes allows all exceptions, and f shall allow no |
| 192 | // exceptions if every function it directly invokes allows no exceptions. |
| 193 | // |
| 194 | // Note in particular that if an implicit exception-specification is generated |
| 195 | // for a function containing a throw-expression, that specification can still |
| 196 | // be noexcept(true). |
| 197 | // |
| 198 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 199 | // is no indication that we should only consider potentially-evaluated calls. |
| 200 | // |
| 201 | // Ultimately we should implement the intent of the standard: the exception |
| 202 | // specification should be the set of exceptions which can be thrown by the |
| 203 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 204 | // throw any exception. |
| 205 | |
| 206 | if (E->CanThrow(*Context)) |
| 207 | ComputedEST = EST_None; |
| 208 | } |
| 209 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 210 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 211 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 213 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 214 | diag::err_typecheck_decl_incomplete_type)) { |
| 215 | Param->setInvalidDecl(); |
| 216 | return true; |
| 217 | } |
| 218 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 219 | // C++ [dcl.fct.default]p5 |
| 220 | // A default argument expression is implicitly converted (clause |
| 221 | // 4) to the parameter type. The default argument expression has |
| 222 | // the same semantic constraints as the initializer expression in |
| 223 | // a declaration of a variable of the parameter type, using the |
| 224 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 225 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 226 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 227 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 228 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 229 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 230 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 231 | MultiExprArg(*this, &Arg, 1)); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 232 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 233 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 234 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 235 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 236 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 237 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 239 | // Okay: add the default argument to the parameter |
| 240 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 242 | // We have already instantiated this parameter; provide each of the |
| 243 | // instantiations with the uninstantiated default argument. |
| 244 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 245 | = UnparsedDefaultArgInstantiations.find(Param); |
| 246 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 247 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 248 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 249 | |
| 250 | // We're done tracking this parameter's instantiations. |
| 251 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 252 | } |
| 253 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 254 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 257 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 258 | /// provided for a function parameter is well-formed. If so, attach it |
| 259 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 260 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 261 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 262 | Expr *DefaultArg) { |
| 263 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 264 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 266 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 267 | UnparsedDefaultArgLocs.erase(Param); |
| 268 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 269 | // Default arguments are only permitted in C++ |
| 270 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 271 | Diag(EqualLoc, diag::err_param_default_argument) |
| 272 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 273 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 277 | // Check for unexpanded parameter packs. |
| 278 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 279 | Param->setInvalidDecl(); |
| 280 | return; |
| 281 | } |
| 282 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 283 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 284 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 285 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 286 | Param->setInvalidDecl(); |
| 287 | return; |
| 288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 290 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 293 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 294 | /// argument for a function parameter, but we can't parse it yet |
| 295 | /// because we're inside a class definition. Note that this default |
| 296 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 297 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 298 | SourceLocation EqualLoc, |
| 299 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 300 | if (!param) |
| 301 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 303 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 304 | if (Param) |
| 305 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 307 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 310 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 311 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 312 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 313 | if (!param) |
| 314 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 316 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 318 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 320 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 323 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 324 | /// arguments in the declarator, which is not a function declaration |
| 325 | /// or definition and therefore is not permitted to have default |
| 326 | /// arguments. This routine should be invoked for every declarator |
| 327 | /// that is not a function declaration or definition. |
| 328 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 329 | // C++ [dcl.fct.default]p3 |
| 330 | // A default argument expression shall be specified only in the |
| 331 | // parameter-declaration-clause of a function declaration or in a |
| 332 | // template-parameter (14.1). It shall not be specified for a |
| 333 | // parameter pack. If it is specified in a |
| 334 | // parameter-declaration-clause, it shall not occur within a |
| 335 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 336 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 337 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 338 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 339 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 340 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 341 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 342 | if (Param->hasUnparsedDefaultArg()) { |
| 343 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 344 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 345 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 346 | delete Toks; |
| 347 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 348 | } else if (Param->getDefaultArg()) { |
| 349 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 350 | << Param->getDefaultArg()->getSourceRange(); |
| 351 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 358 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 359 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 360 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 361 | // error, false otherwise. |
| 362 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 363 | bool Invalid = false; |
| 364 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 365 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 366 | // For non-template functions, default arguments can be added in |
| 367 | // later declarations of a function in the same |
| 368 | // scope. Declarations in different scopes have completely |
| 369 | // distinct sets of default arguments. That is, declarations in |
| 370 | // inner scopes do not acquire default arguments from |
| 371 | // declarations in outer scopes, and vice versa. In a given |
| 372 | // function declaration, all parameters subsequent to a |
| 373 | // parameter with a default argument shall have default |
| 374 | // arguments supplied in this or previous declarations. A |
| 375 | // default argument shall not be redefined by a later |
| 376 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 377 | // |
| 378 | // C++ [dcl.fct.default]p6: |
| 379 | // Except for member functions of class templates, the default arguments |
| 380 | // in a member function definition that appears outside of the class |
| 381 | // definition are added to the set of default arguments provided by the |
| 382 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 383 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 384 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 385 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 386 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 387 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 388 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 389 | unsigned DiagDefaultParamID = |
| 390 | diag::err_param_default_argument_redefinition; |
| 391 | |
| 392 | // MSVC accepts that default parameters be redefined for member functions |
| 393 | // of template class. The new default parameter's value is ignored. |
| 394 | Invalid = true; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 395 | if (getLangOptions().MicrosoftExt) { |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 396 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 397 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 398 | // Merge the old default argument into the new parameter. |
| 399 | NewParam->setHasInheritedDefaultArg(); |
| 400 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 401 | NewParam->setUninstantiatedDefaultArg( |
| 402 | OldParam->getUninstantiatedDefaultArg()); |
| 403 | else |
| 404 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 405 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 406 | Invalid = false; |
| 407 | } |
| 408 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 409 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 410 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 411 | // hint here. Alternatively, we could walk the type-source information |
| 412 | // for NewParam to find the last source location in the type... but it |
| 413 | // isn't worth the effort right now. This is the kind of test case that |
| 414 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 415 | // int f(int); |
| 416 | // void g(int (*fp)(int) = f); |
| 417 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 418 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 419 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 420 | |
| 421 | // Look for the function declaration where the default argument was |
| 422 | // actually written, which may be a declaration prior to Old. |
| 423 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 424 | Older; Older = Older->getPreviousDeclaration()) { |
| 425 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 426 | break; |
| 427 | |
| 428 | OldParam = Older->getParamDecl(p); |
| 429 | } |
| 430 | |
| 431 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 432 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 433 | } else if (OldParam->hasDefaultArg()) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 434 | // Merge the old default argument into the new parameter. |
| 435 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 436 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 437 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 438 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 439 | NewParam->setUninstantiatedDefaultArg( |
| 440 | OldParam->getUninstantiatedDefaultArg()); |
| 441 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 442 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 443 | } else if (NewParam->hasDefaultArg()) { |
| 444 | if (New->getDescribedFunctionTemplate()) { |
| 445 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 446 | Diag(NewParam->getLocation(), |
| 447 | diag::err_param_default_argument_template_redecl) |
| 448 | << NewParam->getDefaultArgRange(); |
| 449 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 450 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 451 | } else if (New->getTemplateSpecializationKind() |
| 452 | != TSK_ImplicitInstantiation && |
| 453 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 454 | // C++ [temp.expr.spec]p21: |
| 455 | // Default function arguments shall not be specified in a declaration |
| 456 | // or a definition for one of the following explicit specializations: |
| 457 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 458 | // - the explicit specialization of a member function template; |
| 459 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 460 | // template where the class template specialization to which the |
| 461 | // member function specialization belongs is implicitly |
| 462 | // instantiated. |
| 463 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 464 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 465 | << New->getDeclName() |
| 466 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 467 | } else if (New->getDeclContext()->isDependentContext()) { |
| 468 | // C++ [dcl.fct.default]p6 (DR217): |
| 469 | // Default arguments for a member function of a class template shall |
| 470 | // be specified on the initial declaration of the member function |
| 471 | // within the class template. |
| 472 | // |
| 473 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 474 | // leads me to the conclusion that one cannot add default function |
| 475 | // arguments for an out-of-line definition of a member function of a |
| 476 | // dependent type. |
| 477 | int WhichKind = 2; |
| 478 | if (CXXRecordDecl *Record |
| 479 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 480 | if (Record->getDescribedClassTemplate()) |
| 481 | WhichKind = 0; |
| 482 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 483 | WhichKind = 1; |
| 484 | else |
| 485 | WhichKind = 2; |
| 486 | } |
| 487 | |
| 488 | Diag(NewParam->getLocation(), |
| 489 | diag::err_param_default_argument_member_template_redecl) |
| 490 | << WhichKind |
| 491 | << NewParam->getDefaultArgRange(); |
Sean Hunt | 9ae60d5 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 492 | } else if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(New)) { |
| 493 | CXXSpecialMember NewSM = getSpecialMember(Ctor), |
| 494 | OldSM = getSpecialMember(cast<CXXConstructorDecl>(Old)); |
| 495 | if (NewSM != OldSM) { |
| 496 | Diag(NewParam->getLocation(),diag::warn_default_arg_makes_ctor_special) |
| 497 | << NewParam->getDefaultArgRange() << NewSM; |
| 498 | Diag(Old->getLocation(), diag::note_previous_declaration_special) |
| 499 | << OldSM; |
| 500 | } |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 501 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 505 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 506 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 507 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 508 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 511 | /// \brief Merge the exception specifications of two variable declarations. |
| 512 | /// |
| 513 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 514 | /// checks if the redeclaration might have an exception specification and |
| 515 | /// validates compatibility and merges the specs if necessary. |
| 516 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 517 | // Shortcut if exceptions are disabled. |
| 518 | if (!getLangOptions().CXXExceptions) |
| 519 | return; |
| 520 | |
| 521 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 522 | "Should only be called if types are otherwise the same."); |
| 523 | |
| 524 | QualType NewType = New->getType(); |
| 525 | QualType OldType = Old->getType(); |
| 526 | |
| 527 | // We're only interested in pointers and references to functions, as well |
| 528 | // as pointers to member functions. |
| 529 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 530 | NewType = R->getPointeeType(); |
| 531 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 532 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 533 | NewType = P->getPointeeType(); |
| 534 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 535 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 536 | NewType = M->getPointeeType(); |
| 537 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 538 | } |
| 539 | |
| 540 | if (!NewType->isFunctionProtoType()) |
| 541 | return; |
| 542 | |
| 543 | // There's lots of special cases for functions. For function pointers, system |
| 544 | // libraries are hopefully not as broken so that we don't need these |
| 545 | // workarounds. |
| 546 | if (CheckEquivalentExceptionSpec( |
| 547 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 548 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 549 | New->setInvalidDecl(); |
| 550 | } |
| 551 | } |
| 552 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 553 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 554 | /// function declaration are well-formed according to C++ |
| 555 | /// [dcl.fct.default]. |
| 556 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 557 | unsigned NumParams = FD->getNumParams(); |
| 558 | unsigned p; |
| 559 | |
| 560 | // Find first parameter with a default argument |
| 561 | for (p = 0; p < NumParams; ++p) { |
| 562 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 563 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | |
| 567 | // C++ [dcl.fct.default]p4: |
| 568 | // In a given function declaration, all parameters |
| 569 | // subsequent to a parameter with a default argument shall |
| 570 | // have default arguments supplied in this or previous |
| 571 | // declarations. A default argument shall not be redefined |
| 572 | // by a later declaration (not even to the same value). |
| 573 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 575 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 576 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 577 | if (Param->isInvalidDecl()) |
| 578 | /* We already complained about this parameter. */; |
| 579 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 581 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 582 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 583 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 585 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 587 | LastMissingDefaultArg = p; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (LastMissingDefaultArg > 0) { |
| 592 | // Some default arguments were missing. Clear out all of the |
| 593 | // default arguments up to (and including) the last missing |
| 594 | // default argument, so that we leave the function parameters |
| 595 | // in a semantically valid state. |
| 596 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 597 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 598 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 599 | Param->setDefaultArg(0); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 605 | /// isCurrentClassName - Determine whether the identifier II is the |
| 606 | /// name of the class type currently being defined. In the case of |
| 607 | /// nested classes, this will only return true if II is the name of |
| 608 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 609 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 610 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 611 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 612 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 613 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 614 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 615 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 616 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 617 | } else |
| 618 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 619 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 620 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 621 | return &II == CurDecl->getIdentifier(); |
| 622 | else |
| 623 | return false; |
| 624 | } |
| 625 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 627 | /// |
| 628 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 629 | /// and returns NULL otherwise. |
| 630 | CXXBaseSpecifier * |
| 631 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 632 | SourceRange SpecifierRange, |
| 633 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 634 | TypeSourceInfo *TInfo, |
| 635 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 636 | QualType BaseType = TInfo->getType(); |
| 637 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 638 | // C++ [class.union]p1: |
| 639 | // A union shall not have base classes. |
| 640 | if (Class->isUnion()) { |
| 641 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 642 | << SpecifierRange; |
| 643 | return 0; |
| 644 | } |
| 645 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 646 | if (EllipsisLoc.isValid() && |
| 647 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 648 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 649 | << TInfo->getTypeLoc().getSourceRange(); |
| 650 | EllipsisLoc = SourceLocation(); |
| 651 | } |
| 652 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 653 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 655 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 656 | Access, TInfo, EllipsisLoc); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 657 | |
| 658 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 659 | |
| 660 | // Base specifiers must be record types. |
| 661 | if (!BaseType->isRecordType()) { |
| 662 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | // C++ [class.union]p1: |
| 667 | // A union shall not be used as a base class. |
| 668 | if (BaseType->isUnionType()) { |
| 669 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | // C++ [class.derived]p2: |
| 674 | // The class-name in a base-specifier shall not be an incompletely |
| 675 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 677 | PDiag(diag::err_incomplete_base_class) |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 678 | << SpecifierRange)) { |
| 679 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 680 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 681 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 682 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 683 | // 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] | 684 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 685 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 686 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 687 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 688 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 689 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 690 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 691 | // C++ [class]p3: |
| 692 | // If a class is marked final and it appears as a base-type-specifier in |
| 693 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 694 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 695 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 696 | << CXXBaseDecl->getDeclName(); |
| 697 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 698 | << CXXBaseDecl->getDeclName(); |
| 699 | return 0; |
| 700 | } |
| 701 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 702 | if (BaseDecl->isInvalidDecl()) |
| 703 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 704 | |
| 705 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 706 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 707 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 708 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 711 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 712 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | /// example: |
| 714 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 715 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 716 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 717 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 718 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 719 | ParsedType basetype, SourceLocation BaseLoc, |
| 720 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 721 | if (!classdecl) |
| 722 | return true; |
| 723 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 724 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 725 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 726 | if (!Class) |
| 727 | return true; |
| 728 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 729 | TypeSourceInfo *TInfo = 0; |
| 730 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 732 | if (EllipsisLoc.isInvalid() && |
| 733 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 734 | UPPC_BaseType)) |
| 735 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 737 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 738 | Virtual, Access, TInfo, |
| 739 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 740 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 742 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 745 | /// \brief Performs the actual work of attaching the given base class |
| 746 | /// specifiers to a C++ class. |
| 747 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 748 | unsigned NumBases) { |
| 749 | if (NumBases == 0) |
| 750 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 751 | |
| 752 | // Used to keep track of which base types we have already seen, so |
| 753 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 754 | // that the key is always the unqualified canonical type of the base |
| 755 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 756 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 757 | |
| 758 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 759 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 760 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 761 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 763 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 764 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 765 | if (KnownBaseTypes[NewBaseType]) { |
| 766 | // C++ [class.mi]p3: |
| 767 | // A class shall not be specified as a direct base class of a |
| 768 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 769 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 770 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 771 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 772 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 773 | |
| 774 | // Delete the duplicate base class specifier; we're going to |
| 775 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 776 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 777 | |
| 778 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 779 | } else { |
| 780 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 781 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 782 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
| 786 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 787 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 788 | |
| 789 | // Delete the remaining (good) base class specifiers, since their |
| 790 | // data has been copied into the CXXRecordDecl. |
| 791 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 792 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 793 | |
| 794 | return Invalid; |
| 795 | } |
| 796 | |
| 797 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 798 | /// class, after checking whether there are any duplicate base |
| 799 | /// classes. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 800 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 801 | unsigned NumBases) { |
| 802 | if (!ClassDecl || !Bases || !NumBases) |
| 803 | return; |
| 804 | |
| 805 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 806 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 807 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 808 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 809 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 810 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 811 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 812 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 813 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 814 | return ICT->getDecl(); |
| 815 | else |
| 816 | return 0; |
| 817 | } |
| 818 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 819 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 820 | /// derived from the type \p Base. |
| 821 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 822 | if (!getLangOptions().CPlusPlus) |
| 823 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 824 | |
| 825 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 826 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 827 | return false; |
| 828 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 829 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 830 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 831 | return false; |
| 832 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 833 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 834 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 838 | /// derived from the type \p Base. |
| 839 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 840 | if (!getLangOptions().CPlusPlus) |
| 841 | return false; |
| 842 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 843 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 844 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 845 | return false; |
| 846 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 847 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 848 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 849 | return false; |
| 850 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 851 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 852 | } |
| 853 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 854 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 855 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 856 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 857 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 858 | |
| 859 | const CXXBasePath &Path = Paths.front(); |
| 860 | |
| 861 | // We first go backward and check if we have a virtual base. |
| 862 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 863 | // the nearest virtual base. |
| 864 | unsigned Start = 0; |
| 865 | for (unsigned I = Path.size(); I != 0; --I) { |
| 866 | if (Path[I - 1].Base->isVirtual()) { |
| 867 | Start = I - 1; |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | // Now add all bases. |
| 873 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 874 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 877 | /// \brief Determine whether the given base path includes a virtual |
| 878 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 879 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 880 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 881 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 882 | B != BEnd; ++B) |
| 883 | if ((*B)->isVirtual()) |
| 884 | return true; |
| 885 | |
| 886 | return false; |
| 887 | } |
| 888 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 889 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 890 | /// conversion (where Derived and Base are class types) is |
| 891 | /// well-formed, meaning that the conversion is unambiguous (and |
| 892 | /// that all of the base classes are accessible). Returns true |
| 893 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 894 | /// otherwise. Loc is the location where this routine should point to |
| 895 | /// if there is an error, and Range is the source range to highlight |
| 896 | /// if there is an error. |
| 897 | bool |
| 898 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 899 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 900 | unsigned AmbigiousBaseConvID, |
| 901 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 902 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 903 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 904 | // First, determine whether the path from Derived to Base is |
| 905 | // ambiguous. This is slightly more expensive than checking whether |
| 906 | // the Derived to Base conversion exists, because here we need to |
| 907 | // explore multiple paths to determine if there is an ambiguity. |
| 908 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 909 | /*DetectVirtual=*/false); |
| 910 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 911 | assert(DerivationOkay && |
| 912 | "Can only be used with a derived-to-base conversion"); |
| 913 | (void)DerivationOkay; |
| 914 | |
| 915 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 916 | if (InaccessibleBaseID) { |
| 917 | // Check that the base class can be accessed. |
| 918 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 919 | InaccessibleBaseID)) { |
| 920 | case AR_inaccessible: |
| 921 | return true; |
| 922 | case AR_accessible: |
| 923 | case AR_dependent: |
| 924 | case AR_delayed: |
| 925 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 926 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 927 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 928 | |
| 929 | // Build a base path if necessary. |
| 930 | if (BasePath) |
| 931 | BuildBasePathArray(Paths, *BasePath); |
| 932 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // We know that the derived-to-base conversion is ambiguous, and |
| 936 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 937 | // search just one more time to compute all of the possible paths so |
| 938 | // that we can print them out. This is more expensive than any of |
| 939 | // the previous derived-to-base checks we've done, but at this point |
| 940 | // performance isn't as much of an issue. |
| 941 | Paths.clear(); |
| 942 | Paths.setRecordingPaths(true); |
| 943 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 944 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 945 | (void)StillOkay; |
| 946 | |
| 947 | // Build up a textual representation of the ambiguous paths, e.g., |
| 948 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 949 | // conversions in the diagnostic. We only print one of the paths |
| 950 | // to each base class subobject. |
| 951 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 952 | |
| 953 | Diag(Loc, AmbigiousBaseConvID) |
| 954 | << Derived << Base << PathDisplayStr << Range << Name; |
| 955 | return true; |
| 956 | } |
| 957 | |
| 958 | bool |
| 959 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 960 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 961 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 962 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 963 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 964 | IgnoreAccess ? 0 |
| 965 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 966 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 967 | Loc, Range, DeclarationName(), |
| 968 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | |
| 972 | /// @brief Builds a string representing ambiguous paths from a |
| 973 | /// specific derived class to different subobjects of the same base |
| 974 | /// class. |
| 975 | /// |
| 976 | /// This function builds a string that can be used in error messages |
| 977 | /// to show the different paths that one can take through the |
| 978 | /// inheritance hierarchy to go from the derived class to different |
| 979 | /// subobjects of a base class. The result looks something like this: |
| 980 | /// @code |
| 981 | /// struct D -> struct B -> struct A |
| 982 | /// struct D -> struct C -> struct A |
| 983 | /// @endcode |
| 984 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 985 | std::string PathDisplayStr; |
| 986 | std::set<unsigned> DisplayedPaths; |
| 987 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 988 | Path != Paths.end(); ++Path) { |
| 989 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 990 | // We haven't displayed a path to this particular base |
| 991 | // class subobject yet. |
| 992 | PathDisplayStr += "\n "; |
| 993 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 994 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 995 | Element != Path->end(); ++Element) |
| 996 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | return PathDisplayStr; |
| 1001 | } |
| 1002 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1003 | //===----------------------------------------------------------------------===// |
| 1004 | // C++ class member Handling |
| 1005 | //===----------------------------------------------------------------------===// |
| 1006 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1007 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1008 | Decl *Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1009 | SourceLocation ASLoc, |
| 1010 | SourceLocation ColonLoc) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1011 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1012 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1013 | ASLoc, ColonLoc); |
| 1014 | CurContext->addHiddenDecl(ASDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1015 | return ASDecl; |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1018 | /// CheckOverrideControl - Check C++0x override control semantics. |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1019 | void Sema::CheckOverrideControl(const Decl *D) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1020 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1021 | if (!MD || !MD->isVirtual()) |
| 1022 | return; |
| 1023 | |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1024 | if (MD->isDependentContext()) |
| 1025 | return; |
| 1026 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1027 | // C++0x [class.virtual]p3: |
| 1028 | // If a virtual function is marked with the virt-specifier override and does |
| 1029 | // not override a member function of a base class, |
| 1030 | // the program is ill-formed. |
| 1031 | bool HasOverriddenMethods = |
| 1032 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1033 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) { |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1034 | Diag(MD->getLocation(), |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1035 | diag::err_function_marked_override_not_overriding) |
| 1036 | << MD->getDeclName(); |
| 1037 | return; |
| 1038 | } |
| 1039 | } |
| 1040 | |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1041 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
| 1042 | /// function overrides a virtual member function marked 'final', according to |
| 1043 | /// C++0x [class.virtual]p3. |
| 1044 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1045 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1046 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1047 | return false; |
| 1048 | |
| 1049 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 1050 | << New->getDeclName(); |
| 1051 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1052 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1055 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1056 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1057 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
| 1058 | /// one has been parsed, and 'HasDeferredInit' is true if an initializer is |
| 1059 | /// present but parsing it has been deferred. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1060 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1061 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1062 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1063 | Expr *BW, const VirtSpecifiers &VS, |
| 1064 | Expr *InitExpr, bool HasDeferredInit, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1065 | bool IsDefinition) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1066 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1067 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1068 | DeclarationName Name = NameInfo.getName(); |
| 1069 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1070 | |
| 1071 | // For anonymous bitfields, the location should point to the type. |
| 1072 | if (Loc.isInvalid()) |
| 1073 | Loc = D.getSourceRange().getBegin(); |
| 1074 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1075 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 1076 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1077 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1078 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1079 | assert(!DS.isFriendSpecified()); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1080 | assert(!Init || !HasDeferredInit); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1081 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1082 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1083 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1084 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1085 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1086 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1087 | // data members and cannot be applied to names declared const or static, |
| 1088 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1089 | switch (DS.getStorageClassSpec()) { |
| 1090 | case DeclSpec::SCS_unspecified: |
| 1091 | case DeclSpec::SCS_typedef: |
| 1092 | case DeclSpec::SCS_static: |
| 1093 | // FALL THROUGH. |
| 1094 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1095 | case DeclSpec::SCS_mutable: |
| 1096 | if (isFunc) { |
| 1097 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1098 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1099 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1100 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1102 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1103 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1104 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1105 | } |
| 1106 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1107 | default: |
| 1108 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1109 | Diag(DS.getStorageClassSpecLoc(), |
| 1110 | diag::err_storageclass_invalid_for_member); |
| 1111 | else |
| 1112 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 1113 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1114 | } |
| 1115 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1116 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1117 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1118 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1119 | |
| 1120 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1121 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1122 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 1123 | |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1124 | // FIXME: Check that the name is an identifier! |
| 1125 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1126 | |
| 1127 | // Member field could not be with "template" keyword. |
| 1128 | // So TemplateParameterLists should be empty in this case. |
| 1129 | if (TemplateParameterLists.size()) { |
| 1130 | TemplateParameterList* TemplateParams = TemplateParameterLists.get()[0]; |
| 1131 | if (TemplateParams->size()) { |
| 1132 | // There is no such thing as a member field template. |
| 1133 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 1134 | << II |
| 1135 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1136 | TemplateParams->getRAngleLoc()); |
| 1137 | } else { |
| 1138 | // There is an extraneous 'template<>' for this member. |
| 1139 | Diag(TemplateParams->getTemplateLoc(), |
| 1140 | diag::err_template_member_noparams) |
| 1141 | << II |
| 1142 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1143 | TemplateParams->getRAngleLoc()); |
| 1144 | } |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1148 | if (SS.isSet() && !SS.isInvalid()) { |
| 1149 | // The user provided a superfluous scope specifier inside a class |
| 1150 | // definition: |
| 1151 | // |
| 1152 | // class X { |
| 1153 | // int X::member; |
| 1154 | // }; |
| 1155 | DeclContext *DC = 0; |
| 1156 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 1157 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 1158 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 1159 | else |
| 1160 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1161 | << Name << SS.getRange(); |
| 1162 | |
| 1163 | SS.clear(); |
| 1164 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1166 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1167 | HasDeferredInit, AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1168 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1169 | } else { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1170 | assert(!HasDeferredInit); |
| 1171 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1172 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1173 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1174 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1175 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1176 | |
| 1177 | // Non-instance-fields can't have a bitfield. |
| 1178 | if (BitWidth) { |
| 1179 | if (Member->isInvalidDecl()) { |
| 1180 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1181 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1182 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1183 | // "static member 'A' cannot be a bit-field" |
| 1184 | Diag(Loc, diag::err_static_not_bitfield) |
| 1185 | << Name << BitWidth->getSourceRange(); |
| 1186 | } else if (isa<TypedefDecl>(Member)) { |
| 1187 | // "typedef member 'x' cannot be a bit-field" |
| 1188 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1189 | << Name << BitWidth->getSourceRange(); |
| 1190 | } else { |
| 1191 | // A function typedef ("typedef int f(); f a;"). |
| 1192 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1193 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1195 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1198 | BitWidth = 0; |
| 1199 | Member->setInvalidDecl(); |
| 1200 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1201 | |
| 1202 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1204 | // If we have declared a member function template, set the access of the |
| 1205 | // templated declaration as well. |
| 1206 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1207 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1208 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1209 | |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1210 | if (VS.isOverrideSpecified()) { |
| 1211 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1212 | if (!MD || !MD->isVirtual()) { |
| 1213 | Diag(Member->getLocStart(), |
| 1214 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1215 | << "override" << FixItHint::CreateRemoval(VS.getOverrideLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1216 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1217 | MD->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1218 | } |
| 1219 | if (VS.isFinalSpecified()) { |
| 1220 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1221 | if (!MD || !MD->isVirtual()) { |
| 1222 | Diag(Member->getLocStart(), |
| 1223 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1224 | << "final" << FixItHint::CreateRemoval(VS.getFinalLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1225 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1226 | MD->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1227 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1228 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1229 | if (VS.getLastLocation().isValid()) { |
| 1230 | // Update the end location of a method that has a virt-specifiers. |
| 1231 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 1232 | MD->setRangeEnd(VS.getLastLocation()); |
| 1233 | } |
| 1234 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1235 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1237 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1238 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 1239 | if (Init) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1240 | AddInitializerToDecl(Member, Init, false, |
| 1241 | DS.getTypeSpecType() == DeclSpec::TST_auto); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1242 | else if (DS.getTypeSpecType() == DeclSpec::TST_auto && |
| 1243 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1244 | // C++0x [dcl.spec.auto]p4: 'auto' can only be used in the type of a static |
| 1245 | // data member if a brace-or-equal-initializer is provided. |
| 1246 | Diag(Loc, diag::err_auto_var_requires_init) |
| 1247 | << Name << cast<ValueDecl>(Member)->getType(); |
| 1248 | Member->setInvalidDecl(); |
| 1249 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1250 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1251 | FinalizeDeclaration(Member); |
| 1252 | |
John McCall | b25b295 | 2011-02-15 07:12:36 +0000 | [diff] [blame] | 1253 | if (isInstField) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1254 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1255 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1258 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
Richard Smith | 0ff6f8f | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 1259 | /// in-class initializer for a non-static C++ class member, and after |
| 1260 | /// instantiating an in-class initializer in a class template. Such actions |
| 1261 | /// are deferred until the class is complete. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1262 | void |
| 1263 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation EqualLoc, |
| 1264 | Expr *InitExpr) { |
| 1265 | FieldDecl *FD = cast<FieldDecl>(D); |
| 1266 | |
| 1267 | if (!InitExpr) { |
| 1268 | FD->setInvalidDecl(); |
| 1269 | FD->removeInClassInitializer(); |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | ExprResult Init = InitExpr; |
| 1274 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
| 1275 | // FIXME: if there is no EqualLoc, this is list-initialization. |
| 1276 | Init = PerformCopyInitialization( |
| 1277 | InitializedEntity::InitializeMember(FD), EqualLoc, InitExpr); |
| 1278 | if (Init.isInvalid()) { |
| 1279 | FD->setInvalidDecl(); |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | CheckImplicitConversions(Init.get(), EqualLoc); |
| 1284 | } |
| 1285 | |
| 1286 | // C++0x [class.base.init]p7: |
| 1287 | // The initialization of each base and member constitutes a |
| 1288 | // full-expression. |
| 1289 | Init = MaybeCreateExprWithCleanups(Init); |
| 1290 | if (Init.isInvalid()) { |
| 1291 | FD->setInvalidDecl(); |
| 1292 | return; |
| 1293 | } |
| 1294 | |
| 1295 | InitExpr = Init.release(); |
| 1296 | |
| 1297 | FD->setInClassInitializer(InitExpr); |
| 1298 | } |
| 1299 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1300 | /// \brief Find the direct and/or virtual base specifiers that |
| 1301 | /// correspond to the given base type, for use in base initialization |
| 1302 | /// within a constructor. |
| 1303 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1304 | CXXRecordDecl *ClassDecl, |
| 1305 | QualType BaseType, |
| 1306 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1307 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1308 | // First, check for a direct base class. |
| 1309 | DirectBaseSpec = 0; |
| 1310 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1311 | = ClassDecl->bases_begin(); |
| 1312 | Base != ClassDecl->bases_end(); ++Base) { |
| 1313 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1314 | // We found a direct base of this type. That's what we're |
| 1315 | // initializing. |
| 1316 | DirectBaseSpec = &*Base; |
| 1317 | break; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | // Check for a virtual base class. |
| 1322 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1323 | // there are no virtual bases. |
| 1324 | VirtualBaseSpec = 0; |
| 1325 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1326 | // We haven't found a base yet; search the class hierarchy for a |
| 1327 | // virtual base class. |
| 1328 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1329 | /*DetectVirtual=*/false); |
| 1330 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1331 | BaseType, Paths)) { |
| 1332 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1333 | Path != Paths.end(); ++Path) { |
| 1334 | if (Path->back().Base->isVirtual()) { |
| 1335 | VirtualBaseSpec = Path->back().Base; |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | return DirectBaseSpec || VirtualBaseSpec; |
| 1343 | } |
| 1344 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1345 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1346 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1347 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1348 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1349 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1350 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1351 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1352 | SourceLocation IdLoc, |
| 1353 | SourceLocation LParenLoc, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1354 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1355 | SourceLocation RParenLoc, |
| 1356 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1357 | if (!ConstructorD) |
| 1358 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1360 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
| 1362 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1363 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1364 | if (!Constructor) { |
| 1365 | // The user wrote a constructor initializer on a function that is |
| 1366 | // not a C++ constructor. Ignore the error for now, because we may |
| 1367 | // have more member initializers coming; we'll diagnose it just |
| 1368 | // once in ActOnMemInitializers. |
| 1369 | return true; |
| 1370 | } |
| 1371 | |
| 1372 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1373 | |
| 1374 | // C++ [class.base.init]p2: |
| 1375 | // 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] | 1376 | // constructor's class and, if not found in that scope, are looked |
| 1377 | // up in the scope containing the constructor's definition. |
| 1378 | // [Note: if the constructor's class contains a member with the |
| 1379 | // same name as a direct or virtual base class of the class, a |
| 1380 | // mem-initializer-id naming the member or base class and composed |
| 1381 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1382 | // mem-initializer-id for the hidden base class may be specified |
| 1383 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1384 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1385 | // Look for a member, first. |
| 1386 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1388 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1389 | if (Result.first != Result.second) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1390 | Member = dyn_cast<FieldDecl>(*Result.first); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1391 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1392 | if (Member) { |
| 1393 | if (EllipsisLoc.isValid()) |
| 1394 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1395 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1396 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1397 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1398 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1401 | // Handle anonymous union case. |
| 1402 | if (IndirectFieldDecl* IndirectField |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1403 | = dyn_cast<IndirectFieldDecl>(*Result.first)) { |
| 1404 | if (EllipsisLoc.isValid()) |
| 1405 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1406 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1407 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1408 | return BuildMemberInitializer(IndirectField, (Expr**)Args, |
| 1409 | NumArgs, IdLoc, |
| 1410 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1411 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1412 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1413 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1414 | // 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] | 1415 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1416 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1417 | |
| 1418 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1419 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1420 | } else { |
| 1421 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1422 | LookupParsedName(R, S, &SS); |
| 1423 | |
| 1424 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1425 | if (!TyD) { |
| 1426 | if (R.isAmbiguous()) return true; |
| 1427 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1428 | // We don't want access-control diagnostics here. |
| 1429 | R.suppressDiagnostics(); |
| 1430 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1431 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1432 | bool NotUnknownSpecialization = false; |
| 1433 | DeclContext *DC = computeDeclContext(SS, false); |
| 1434 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1435 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1436 | |
| 1437 | if (!NotUnknownSpecialization) { |
| 1438 | // When the scope specifier can refer to a member of an unknown |
| 1439 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 1440 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 1441 | SS.getWithLocInContext(Context), |
| 1442 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1443 | if (BaseType.isNull()) |
| 1444 | return true; |
| 1445 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1446 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1447 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1451 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1452 | TypoCorrection Corr; |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1453 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1454 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
| 1455 | ClassDecl, false, CTC_NoKeywords))) { |
| 1456 | std::string CorrectedStr(Corr.getAsString(getLangOptions())); |
| 1457 | std::string CorrectedQuotedStr(Corr.getQuoted(getLangOptions())); |
| 1458 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1459 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1460 | // We have found a non-static data member with a similar |
| 1461 | // name to what was typed; complain and initialize that |
| 1462 | // member. |
| 1463 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1464 | << MemberOrBase << true << CorrectedQuotedStr |
| 1465 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1466 | Diag(Member->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1467 | << CorrectedQuotedStr; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1468 | |
| 1469 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1470 | LParenLoc, RParenLoc); |
| 1471 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1472 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1473 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1474 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1475 | if (FindBaseInitializer(*this, ClassDecl, |
| 1476 | Context.getTypeDeclType(Type), |
| 1477 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1478 | // We have found a direct or virtual base class with a |
| 1479 | // similar name to what was typed; complain and initialize |
| 1480 | // that base class. |
| 1481 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1482 | << MemberOrBase << false << CorrectedQuotedStr |
| 1483 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1484 | |
| 1485 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1486 | : VirtualBaseSpec; |
| 1487 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1488 | diag::note_base_class_specified_here) |
| 1489 | << BaseSpec->getType() |
| 1490 | << BaseSpec->getSourceRange(); |
| 1491 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1492 | TyD = Type; |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1497 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1498 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1499 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1500 | return true; |
| 1501 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1504 | if (BaseType.isNull()) { |
| 1505 | BaseType = Context.getTypeDeclType(TyD); |
| 1506 | if (SS.isSet()) { |
| 1507 | NestedNameSpecifier *Qualifier = |
| 1508 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1509 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1510 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1511 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1512 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1516 | if (!TInfo) |
| 1517 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1518 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1519 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1520 | LParenLoc, RParenLoc, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1523 | /// Checks a member initializer expression for cases where reference (or |
| 1524 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1525 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 1526 | Expr *Init, |
| 1527 | SourceLocation IdLoc) { |
| 1528 | QualType MemberTy = Member->getType(); |
| 1529 | |
| 1530 | // We only handle pointers and references currently. |
| 1531 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 1532 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 1533 | return; |
| 1534 | |
| 1535 | const bool IsPointer = MemberTy->isPointerType(); |
| 1536 | if (IsPointer) { |
| 1537 | if (const UnaryOperator *Op |
| 1538 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 1539 | // The only case we're worried about with pointers requires taking the |
| 1540 | // address. |
| 1541 | if (Op->getOpcode() != UO_AddrOf) |
| 1542 | return; |
| 1543 | |
| 1544 | Init = Op->getSubExpr(); |
| 1545 | } else { |
| 1546 | // We only handle address-of expression initializers for pointers. |
| 1547 | return; |
| 1548 | } |
| 1549 | } |
| 1550 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1551 | if (isa<MaterializeTemporaryExpr>(Init->IgnoreParens())) { |
| 1552 | // Taking the address of a temporary will be diagnosed as a hard error. |
| 1553 | if (IsPointer) |
| 1554 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1555 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1556 | S.Diag(Init->getExprLoc(), diag::warn_bind_ref_member_to_temporary) |
| 1557 | << Member << Init->getSourceRange(); |
| 1558 | } else if (const DeclRefExpr *DRE |
| 1559 | = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
| 1560 | // We only warn when referring to a non-reference parameter declaration. |
| 1561 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 1562 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | |
| 1565 | S.Diag(Init->getExprLoc(), |
| 1566 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 1567 | : diag::warn_bind_ref_member_to_parameter) |
| 1568 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1569 | } else { |
| 1570 | // Other initializers are fine. |
| 1571 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1572 | } |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1573 | |
| 1574 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 1575 | << (unsigned)IsPointer; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1578 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1579 | /// containing the field that is being initialized. Returns true if there is an |
| 1580 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1581 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1582 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1583 | const ValueDecl *LhsField, |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1584 | SourceLocation *L) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1585 | assert(isa<FieldDecl>(LhsField) || isa<IndirectFieldDecl>(LhsField)); |
| 1586 | |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1587 | if (isa<CallExpr>(S)) { |
| 1588 | // Do not descend into function calls or constructors, as the use |
| 1589 | // of an uninitialized field may be valid. One would have to inspect |
| 1590 | // the contents of the function/ctor to determine if it is safe or not. |
| 1591 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1592 | // may be safe, depending on what the function/ctor does. |
| 1593 | return false; |
| 1594 | } |
| 1595 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1596 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1597 | |
| 1598 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1599 | // The member expression points to a static data member. |
| 1600 | assert(VD->isStaticDataMember() && |
| 1601 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1602 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1603 | return false; |
| 1604 | } |
| 1605 | |
| 1606 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1607 | // The member expression points to an enum. |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1611 | if (RhsField == LhsField) { |
| 1612 | // Initializing a field with itself. Throw a warning. |
| 1613 | // But wait; there are exceptions! |
| 1614 | // Exception #1: The field may not belong to this record. |
| 1615 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1616 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1617 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1618 | // Even though the field matches, it does not belong to this record. |
| 1619 | return false; |
| 1620 | } |
| 1621 | // None of the exceptions triggered; return true to indicate an |
| 1622 | // uninitialized field was used. |
| 1623 | *L = ME->getMemberLoc(); |
| 1624 | return true; |
| 1625 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1626 | } else if (isa<UnaryExprOrTypeTraitExpr>(S)) { |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1627 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1628 | return false; |
| 1629 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1630 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1631 | // in the same expression but it would be rare; and weird). |
| 1632 | if (UOE->getOpcode() == UO_AddrOf) |
| 1633 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1634 | } |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1635 | for (Stmt::const_child_range it = S->children(); it; ++it) { |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1636 | if (!*it) { |
| 1637 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1638 | continue; |
| 1639 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1640 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1641 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1642 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1643 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1646 | MemInitResult |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1647 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr **Args, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1648 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1649 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1650 | SourceLocation RParenLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1651 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 1652 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 1653 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1654 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 1655 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 1656 | if (Member->isInvalidDecl()) |
| 1657 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1658 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1659 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1660 | // foo(foo) |
| 1661 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1662 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1663 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1664 | SourceLocation L; |
| 1665 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1666 | // FIXME: Return true in the case when other fields are used before being |
| 1667 | // uninitialized. For example, let this field be the i'th field. When |
| 1668 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1669 | // fields are used, as they are not yet initialized. |
| 1670 | // Right now we are only handling the case where the i'th field uses |
| 1671 | // itself in its initializer. |
| 1672 | Diag(L, diag::warn_field_is_uninit); |
| 1673 | } |
| 1674 | } |
| 1675 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1676 | bool HasDependentArg = false; |
| 1677 | for (unsigned i = 0; i < NumArgs; i++) |
| 1678 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1679 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1680 | Expr *Init; |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1681 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1682 | // Can't check initialization for a member of dependent type or when |
| 1683 | // any of the arguments are type-dependent expressions. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1684 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1685 | RParenLoc, |
| 1686 | Member->getType().getNonReferenceType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1687 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1688 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1689 | } else { |
| 1690 | // Initialize the member. |
| 1691 | InitializedEntity MemberEntity = |
| 1692 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 1693 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 1694 | InitializationKind Kind = |
| 1695 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1696 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1697 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1698 | |
| 1699 | ExprResult MemberInit = |
| 1700 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1701 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1702 | if (MemberInit.isInvalid()) |
| 1703 | return true; |
| 1704 | |
| 1705 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
| 1706 | |
| 1707 | // C++0x [class.base.init]p7: |
| 1708 | // The initialization of each base and member constitutes a |
| 1709 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1710 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1711 | if (MemberInit.isInvalid()) |
| 1712 | return true; |
| 1713 | |
| 1714 | // If we are in a dependent context, template instantiation will |
| 1715 | // perform this type-checking again. Just save the arguments that we |
| 1716 | // received in a ParenListExpr. |
| 1717 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1718 | // of the information that we have about the member |
| 1719 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1720 | // and this approach is far more likely to get the corner cases right. |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1721 | if (CurContext->isDependentContext()) { |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1722 | Init = new (Context) ParenListExpr( |
| 1723 | Context, LParenLoc, Args, NumArgs, RParenLoc, |
| 1724 | Member->getType().getNonReferenceType()); |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1725 | } else { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1726 | Init = MemberInit.get(); |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1727 | CheckForDanglingReferenceOrPointer(*this, Member, Init, IdLoc); |
| 1728 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1731 | if (DirectMember) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1732 | return new (Context) CXXCtorInitializer(Context, DirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1733 | IdLoc, LParenLoc, Init, |
| 1734 | RParenLoc); |
| 1735 | } else { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1736 | return new (Context) CXXCtorInitializer(Context, IndirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1737 | IdLoc, LParenLoc, Init, |
| 1738 | RParenLoc); |
| 1739 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1742 | MemInitResult |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1743 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, |
| 1744 | Expr **Args, unsigned NumArgs, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1745 | SourceLocation NameLoc, |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1746 | SourceLocation LParenLoc, |
| 1747 | SourceLocation RParenLoc, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1748 | CXXRecordDecl *ClassDecl) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1749 | SourceLocation Loc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1750 | if (!LangOpts.CPlusPlus0x) |
| 1751 | return Diag(Loc, diag::err_delegation_0x_only) |
| 1752 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 1753 | |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1754 | // Initialize the object. |
| 1755 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 1756 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 1757 | InitializationKind Kind = |
| 1758 | InitializationKind::CreateDirect(NameLoc, LParenLoc, RParenLoc); |
| 1759 | |
| 1760 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args, NumArgs); |
| 1761 | |
| 1762 | ExprResult DelegationInit = |
| 1763 | InitSeq.Perform(*this, DelegationEntity, Kind, |
| 1764 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1765 | if (DelegationInit.isInvalid()) |
| 1766 | return true; |
| 1767 | |
| 1768 | CXXConstructExpr *ConExpr = cast<CXXConstructExpr>(DelegationInit.get()); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 1769 | CXXConstructorDecl *Constructor |
| 1770 | = ConExpr->getConstructor(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1771 | assert(Constructor && "Delegating constructor with no target?"); |
| 1772 | |
| 1773 | CheckImplicitConversions(DelegationInit.get(), LParenLoc); |
| 1774 | |
| 1775 | // C++0x [class.base.init]p7: |
| 1776 | // The initialization of each base and member constitutes a |
| 1777 | // full-expression. |
| 1778 | DelegationInit = MaybeCreateExprWithCleanups(DelegationInit); |
| 1779 | if (DelegationInit.isInvalid()) |
| 1780 | return true; |
| 1781 | |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1782 | assert(!CurContext->isDependentContext()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1783 | return new (Context) CXXCtorInitializer(Context, Loc, LParenLoc, Constructor, |
| 1784 | DelegationInit.takeAs<Expr>(), |
| 1785 | RParenLoc); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1789 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1790 | Expr **Args, unsigned NumArgs, |
| 1791 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1792 | CXXRecordDecl *ClassDecl, |
| 1793 | SourceLocation EllipsisLoc) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1794 | bool HasDependentArg = false; |
| 1795 | for (unsigned i = 0; i < NumArgs; i++) |
| 1796 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1797 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1798 | SourceLocation BaseLoc |
| 1799 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1800 | |
| 1801 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1802 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1803 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1804 | |
| 1805 | // C++ [class.base.init]p2: |
| 1806 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1807 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1808 | // of that class, the mem-initializer is ill-formed. A |
| 1809 | // mem-initializer-list can initialize a base class using any |
| 1810 | // name that denotes that base class type. |
| 1811 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1812 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1813 | if (EllipsisLoc.isValid()) { |
| 1814 | // This is a pack expansion. |
| 1815 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 1816 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1817 | << SourceRange(BaseLoc, RParenLoc); |
| 1818 | |
| 1819 | EllipsisLoc = SourceLocation(); |
| 1820 | } |
| 1821 | } else { |
| 1822 | // Check for any unexpanded parameter packs. |
| 1823 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 1824 | return true; |
| 1825 | |
| 1826 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1827 | if (DiagnoseUnexpandedParameterPack(Args[I])) |
| 1828 | return true; |
| 1829 | } |
| 1830 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1831 | // Check for direct and virtual base classes. |
| 1832 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1833 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1834 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1835 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 1836 | BaseType)) |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1837 | return BuildDelegatingInitializer(BaseTInfo, Args, NumArgs, BaseLoc, |
| 1838 | LParenLoc, RParenLoc, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1839 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1840 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1841 | VirtualBaseSpec); |
| 1842 | |
| 1843 | // C++ [base.class.init]p2: |
| 1844 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1845 | // constructor's class or a direct or virtual base of that class, the |
| 1846 | // mem-initializer is ill-formed. |
| 1847 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1848 | // If the class has any dependent bases, then it's possible that |
| 1849 | // one of those types will resolve to the same type as |
| 1850 | // BaseType. Therefore, just treat this as a dependent base |
| 1851 | // class initialization. FIXME: Should we try to check the |
| 1852 | // initialization anyway? It seems odd. |
| 1853 | if (ClassDecl->hasAnyDependentBases()) |
| 1854 | Dependent = true; |
| 1855 | else |
| 1856 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1857 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1858 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1863 | // Can't check initialization for a base of dependent type or when |
| 1864 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1865 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1866 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1867 | RParenLoc, BaseType)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1868 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1869 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1871 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1872 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1873 | LParenLoc, |
| 1874 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1875 | RParenLoc, |
| 1876 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1877 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1878 | |
| 1879 | // C++ [base.class.init]p2: |
| 1880 | // If a mem-initializer-id is ambiguous because it designates both |
| 1881 | // a direct non-virtual base class and an inherited virtual base |
| 1882 | // class, the mem-initializer is ill-formed. |
| 1883 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1884 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1885 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1886 | |
| 1887 | CXXBaseSpecifier *BaseSpec |
| 1888 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1889 | if (!BaseSpec) |
| 1890 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1891 | |
| 1892 | // Initialize the base. |
| 1893 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1894 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1895 | InitializationKind Kind = |
| 1896 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1897 | |
| 1898 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1899 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1900 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1901 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1902 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1903 | if (BaseInit.isInvalid()) |
| 1904 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1905 | |
| 1906 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1907 | |
| 1908 | // C++0x [class.base.init]p7: |
| 1909 | // The initialization of each base and member constitutes a |
| 1910 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1911 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1912 | if (BaseInit.isInvalid()) |
| 1913 | return true; |
| 1914 | |
| 1915 | // If we are in a dependent context, template instantiation will |
| 1916 | // perform this type-checking again. Just save the arguments that we |
| 1917 | // received in a ParenListExpr. |
| 1918 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1919 | // of the information that we have about the base |
| 1920 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1921 | // and this approach is far more likely to get the corner cases right. |
| 1922 | if (CurContext->isDependentContext()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1923 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1924 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1925 | RParenLoc, BaseType)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1926 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1927 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1928 | LParenLoc, |
| 1929 | Init.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1930 | RParenLoc, |
| 1931 | EllipsisLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1934 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1935 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1936 | LParenLoc, |
| 1937 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1938 | RParenLoc, |
| 1939 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1942 | // Create a static_cast\<T&&>(expr). |
| 1943 | static Expr *CastForMoving(Sema &SemaRef, Expr *E) { |
| 1944 | QualType ExprType = E->getType(); |
| 1945 | QualType TargetType = SemaRef.Context.getRValueReferenceType(ExprType); |
| 1946 | SourceLocation ExprLoc = E->getLocStart(); |
| 1947 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 1948 | TargetType, ExprLoc); |
| 1949 | |
| 1950 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 1951 | SourceRange(ExprLoc, ExprLoc), |
| 1952 | E->getSourceRange()).take(); |
| 1953 | } |
| 1954 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1955 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1956 | /// initialize its base or member. |
| 1957 | enum ImplicitInitializerKind { |
| 1958 | IIK_Default, |
| 1959 | IIK_Copy, |
| 1960 | IIK_Move |
| 1961 | }; |
| 1962 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1963 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1964 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1965 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1966 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1967 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1968 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1969 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1970 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1971 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1972 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1973 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1974 | |
| 1975 | switch (ImplicitInitKind) { |
| 1976 | case IIK_Default: { |
| 1977 | InitializationKind InitKind |
| 1978 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1979 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1980 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1981 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1982 | break; |
| 1983 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1984 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1985 | case IIK_Move: |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1986 | case IIK_Copy: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1987 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1988 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1989 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1990 | |
| 1991 | Expr *CopyCtorArg = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1992 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1993 | Constructor->getLocation(), ParamType, |
| 1994 | VK_LValue, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1995 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1996 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1997 | QualType ArgTy = |
| 1998 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1999 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2000 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2001 | if (Moving) { |
| 2002 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 2003 | } |
| 2004 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2005 | CXXCastPath BasePath; |
| 2006 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2007 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 2008 | CK_UncheckedDerivedToBase, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2009 | Moving ? VK_XValue : VK_LValue, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2010 | &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2011 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2012 | InitializationKind InitKind |
| 2013 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 2014 | SourceLocation(), SourceLocation()); |
| 2015 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 2016 | &CopyCtorArg, 1); |
| 2017 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2018 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2019 | break; |
| 2020 | } |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2021 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2023 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2024 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2025 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2026 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2027 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2028 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2029 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 2030 | SourceLocation()), |
| 2031 | BaseSpec->isVirtual(), |
| 2032 | SourceLocation(), |
| 2033 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2034 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2035 | SourceLocation()); |
| 2036 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2037 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2040 | static bool RefersToRValueRef(Expr *MemRef) { |
| 2041 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 2042 | return Referenced->getType()->isRValueReferenceType(); |
| 2043 | } |
| 2044 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2045 | static bool |
| 2046 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2047 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2048 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2049 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2050 | if (Field->isInvalidDecl()) |
| 2051 | return true; |
| 2052 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2053 | SourceLocation Loc = Constructor->getLocation(); |
| 2054 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2055 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 2056 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2057 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2058 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2059 | |
| 2060 | // Suppress copying zero-width bitfields. |
| 2061 | if (const Expr *Width = Field->getBitWidth()) |
| 2062 | if (Width->EvaluateAsInt(SemaRef.Context) == 0) |
| 2063 | return false; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2064 | |
| 2065 | Expr *MemberExprBase = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2066 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2067 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2068 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2069 | if (Moving) { |
| 2070 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 2071 | } |
| 2072 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2073 | // Build a reference to this field within the parameter. |
| 2074 | CXXScopeSpec SS; |
| 2075 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 2076 | Sema::LookupMemberName); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2077 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 2078 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2079 | MemberLookup.resolveKind(); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2080 | ExprResult CtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2081 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2082 | ParamType, Loc, |
| 2083 | /*IsArrow=*/false, |
| 2084 | SS, |
| 2085 | /*FirstQualifierInScope=*/0, |
| 2086 | MemberLookup, |
| 2087 | /*TemplateArgs=*/0); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2088 | if (CtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2089 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2090 | |
| 2091 | // C++11 [class.copy]p15: |
| 2092 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 2093 | // with static_cast<T&&>(x.m); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2094 | if (RefersToRValueRef(CtorArg.get())) { |
| 2095 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2098 | // When the field we are copying is an array, create index variables for |
| 2099 | // each dimension of the array. We use these index variables to subscript |
| 2100 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 2101 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2102 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2103 | QualType BaseType = Field->getType(); |
| 2104 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2105 | bool InitializingArray = false; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2106 | while (const ConstantArrayType *Array |
| 2107 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2108 | InitializingArray = true; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2109 | // Create the iteration variable for this array index. |
| 2110 | IdentifierInfo *IterationVarName = 0; |
| 2111 | { |
| 2112 | llvm::SmallString<8> Str; |
| 2113 | llvm::raw_svector_ostream OS(Str); |
| 2114 | OS << "__i" << IndexVariables.size(); |
| 2115 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 2116 | } |
| 2117 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2118 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2119 | IterationVarName, SizeType, |
| 2120 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2121 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2122 | IndexVariables.push_back(IterationVar); |
| 2123 | |
| 2124 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2125 | ExprResult IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2126 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2127 | assert(!IterationVarRef.isInvalid() && |
| 2128 | "Reference to invented variable cannot fail!"); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2129 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2130 | // Subscript the array with this iteration variable. |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2131 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2132 | IterationVarRef.take(), |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2133 | Loc); |
| 2134 | if (CtorArg.isInvalid()) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2135 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2137 | BaseType = Array->getElementType(); |
| 2138 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2139 | |
| 2140 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 2141 | if (Moving && InitializingArray) |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2142 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2143 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2144 | // Construct the entity that we will be initializing. For an array, this |
| 2145 | // will be first element in the array, which may require several levels |
| 2146 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2147 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2148 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2149 | if (Indirect) |
| 2150 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 2151 | else |
| 2152 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2153 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 2154 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 2155 | 0, |
| 2156 | Entities.back())); |
| 2157 | |
| 2158 | // Direct-initialize to use the copy constructor. |
| 2159 | InitializationKind InitKind = |
| 2160 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 2161 | |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2162 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2163 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2164 | &CtorArgE, 1); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2165 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2166 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2167 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2168 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2169 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2170 | if (MemberInit.isInvalid()) |
| 2171 | return true; |
| 2172 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2173 | if (Indirect) { |
| 2174 | assert(IndexVariables.size() == 0 && |
| 2175 | "Indirect field improperly initialized"); |
| 2176 | CXXMemberInit |
| 2177 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2178 | Loc, Loc, |
| 2179 | MemberInit.takeAs<Expr>(), |
| 2180 | Loc); |
| 2181 | } else |
| 2182 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 2183 | Loc, MemberInit.takeAs<Expr>(), |
| 2184 | Loc, |
| 2185 | IndexVariables.data(), |
| 2186 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2187 | return false; |
| 2188 | } |
| 2189 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2190 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 2191 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2192 | QualType FieldBaseElementType = |
| 2193 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 2194 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2195 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2196 | InitializedEntity InitEntity |
| 2197 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 2198 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2199 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2200 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2201 | |
| 2202 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2203 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2204 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2205 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2206 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2207 | if (MemberInit.isInvalid()) |
| 2208 | return true; |
| 2209 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2210 | if (Indirect) |
| 2211 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2212 | Indirect, Loc, |
| 2213 | Loc, |
| 2214 | MemberInit.get(), |
| 2215 | Loc); |
| 2216 | else |
| 2217 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2218 | Field, Loc, Loc, |
| 2219 | MemberInit.get(), |
| 2220 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2221 | return false; |
| 2222 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2223 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2224 | if (!Field->getParent()->isUnion()) { |
| 2225 | if (FieldBaseElementType->isReferenceType()) { |
| 2226 | SemaRef.Diag(Constructor->getLocation(), |
| 2227 | diag::err_uninitialized_member_in_ctor) |
| 2228 | << (int)Constructor->isImplicit() |
| 2229 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2230 | << 0 << Field->getDeclName(); |
| 2231 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2232 | return true; |
| 2233 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2234 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2235 | if (FieldBaseElementType.isConstQualified()) { |
| 2236 | SemaRef.Diag(Constructor->getLocation(), |
| 2237 | diag::err_uninitialized_member_in_ctor) |
| 2238 | << (int)Constructor->isImplicit() |
| 2239 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2240 | << 1 << Field->getDeclName(); |
| 2241 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2242 | return true; |
| 2243 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2244 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2245 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2246 | if (SemaRef.getLangOptions().ObjCAutoRefCount && |
| 2247 | FieldBaseElementType->isObjCRetainableType() && |
| 2248 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 2249 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
| 2250 | // Instant objects: |
| 2251 | // Default-initialize Objective-C pointers to NULL. |
| 2252 | CXXMemberInit |
| 2253 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2254 | Loc, Loc, |
| 2255 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 2256 | Loc); |
| 2257 | return false; |
| 2258 | } |
| 2259 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2260 | // Nothing to initialize. |
| 2261 | CXXMemberInit = 0; |
| 2262 | return false; |
| 2263 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2264 | |
| 2265 | namespace { |
| 2266 | struct BaseAndFieldInfo { |
| 2267 | Sema &S; |
| 2268 | CXXConstructorDecl *Ctor; |
| 2269 | bool AnyErrorsInInits; |
| 2270 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2271 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2272 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2273 | |
| 2274 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 2275 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2276 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 2277 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2278 | IIK = IIK_Copy; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2279 | else if (Generated && Ctor->isMoveConstructor()) |
| 2280 | IIK = IIK_Move; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2281 | else |
| 2282 | IIK = IIK_Default; |
| 2283 | } |
| 2284 | }; |
| 2285 | } |
| 2286 | |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 2287 | /// \brief Determine whether the given indirect field declaration is somewhere |
| 2288 | /// within an anonymous union. |
| 2289 | static bool isWithinAnonymousUnion(IndirectFieldDecl *F) { |
| 2290 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 2291 | CEnd = F->chain_end(); |
| 2292 | C != CEnd; ++C) |
| 2293 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>((*C)->getDeclContext())) |
| 2294 | if (Record->isUnion()) |
| 2295 | return true; |
| 2296 | |
| 2297 | return false; |
| 2298 | } |
| 2299 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2300 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2301 | FieldDecl *Field, |
| 2302 | IndirectFieldDecl *Indirect = 0) { |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2303 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2304 | // Overwhelmingly common case: we have a direct initializer for this field. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2305 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2306 | Info.AllToInit.push_back(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2307 | return false; |
| 2308 | } |
| 2309 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2310 | // C++0x [class.base.init]p8: if the entity is a non-static data member that |
| 2311 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 2312 | // in [dcl.init]. |
| 2313 | if (Field->hasInClassInitializer()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2314 | CXXCtorInitializer *Init; |
| 2315 | if (Indirect) |
| 2316 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2317 | SourceLocation(), |
| 2318 | SourceLocation(), 0, |
| 2319 | SourceLocation()); |
| 2320 | else |
| 2321 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2322 | SourceLocation(), |
| 2323 | SourceLocation(), 0, |
| 2324 | SourceLocation()); |
| 2325 | Info.AllToInit.push_back(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2326 | return false; |
| 2327 | } |
| 2328 | |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 2329 | // Don't build an implicit initializer for union members if none was |
| 2330 | // explicitly specified. |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 2331 | if (Field->getParent()->isUnion() || |
| 2332 | (Indirect && isWithinAnonymousUnion(Indirect))) |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 2333 | return false; |
| 2334 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2335 | // Don't try to build an implicit initializer if there were semantic |
| 2336 | // errors in any of the initializers (and therefore we might be |
| 2337 | // missing some that the user actually wrote). |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2338 | if (Info.AnyErrorsInInits || Field->isInvalidDecl()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2339 | return false; |
| 2340 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2341 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2342 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 2343 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2344 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2345 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2346 | if (Init) |
| 2347 | Info.AllToInit.push_back(Init); |
| 2348 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2349 | return false; |
| 2350 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2351 | |
| 2352 | bool |
| 2353 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 2354 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2355 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 2356 | Constructor->setNumCtorInitializers(1); |
| 2357 | CXXCtorInitializer **initializer = |
| 2358 | new (Context) CXXCtorInitializer*[1]; |
| 2359 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 2360 | Constructor->setCtorInitializers(initializer); |
| 2361 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2362 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
| 2363 | MarkDeclarationReferenced(Initializer->getSourceLocation(), Dtor); |
| 2364 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 2365 | } |
| 2366 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 2367 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2368 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2369 | return false; |
| 2370 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2371 | |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2372 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 2373 | CXXCtorInitializer **Initializers, |
| 2374 | unsigned NumInitializers, |
| 2375 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2376 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2377 | // Just store the initializers as written, they will be checked during |
| 2378 | // instantiation. |
| 2379 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2380 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2381 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2382 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2383 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2384 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2385 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | return false; |
| 2389 | } |
| 2390 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2391 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2392 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2393 | // We need to build the initializer AST according to order of construction |
| 2394 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2395 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 2396 | if (!ClassDecl) |
| 2397 | return true; |
| 2398 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2399 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2401 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2402 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2403 | |
| 2404 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2405 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2406 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2407 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2410 | // Keep track of the direct virtual bases. |
| 2411 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 2412 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 2413 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 2414 | if (I->isVirtual()) |
| 2415 | DirectVBases.insert(I); |
| 2416 | } |
| 2417 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2418 | // Push virtual bases before others. |
| 2419 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2420 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 2421 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2422 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2423 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 2424 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2425 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2426 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2427 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2428 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2429 | VBase, IsInheritedVirtualBase, |
| 2430 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2431 | HadError = true; |
| 2432 | continue; |
| 2433 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2434 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2435 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2439 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2440 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2441 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 2442 | // Virtuals are in the virtual base list and already constructed. |
| 2443 | if (Base->isVirtual()) |
| 2444 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2446 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2447 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 2448 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2449 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2450 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2451 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2452 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2453 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2454 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2455 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2456 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 2457 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2458 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2462 | // Fields. |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2463 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 2464 | MemEnd = ClassDecl->decls_end(); |
| 2465 | Mem != MemEnd; ++Mem) { |
| 2466 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
| 2467 | if (F->getType()->isIncompleteArrayType()) { |
| 2468 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2469 | "Incomplete array type is not valid"); |
| 2470 | continue; |
| 2471 | } |
| 2472 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2473 | // If we're not generating the implicit copy/move constructor, then we'll |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2474 | // handle anonymous struct/union fields based on their individual |
| 2475 | // indirect fields. |
| 2476 | if (F->isAnonymousStructOrUnion() && Info.IIK == IIK_Default) |
| 2477 | continue; |
| 2478 | |
| 2479 | if (CollectFieldInitializer(*this, Info, F)) |
| 2480 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2481 | continue; |
| 2482 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2483 | |
| 2484 | // Beyond this point, we only consider default initialization. |
| 2485 | if (Info.IIK != IIK_Default) |
| 2486 | continue; |
| 2487 | |
| 2488 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 2489 | if (F->getType()->isIncompleteArrayType()) { |
| 2490 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2491 | "Incomplete array type is not valid"); |
| 2492 | continue; |
| 2493 | } |
| 2494 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2495 | // Initialize each field of an anonymous struct individually. |
| 2496 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 2497 | HadError = true; |
| 2498 | |
| 2499 | continue; |
| 2500 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2502 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2503 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2504 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2505 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2506 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2507 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2508 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2509 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2510 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 2511 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2512 | // Constructors implicitly reference the base and member |
| 2513 | // destructors. |
| 2514 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 2515 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2516 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2517 | |
| 2518 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2521 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 2522 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2523 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2524 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 2525 | return static_cast<void *>(RT->getDecl()); |
| 2526 | } |
| 2527 | return static_cast<void *>(Field); |
| 2528 | } |
| 2529 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2530 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2531 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2534 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2535 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2536 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2537 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2538 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2539 | // For fields injected into the class via declaration of an anonymous union, |
| 2540 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2541 | FieldDecl *Field = Member->getAnyMember(); |
| 2542 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2543 | // If the field is a member of an anonymous struct or union, our key |
| 2544 | // 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] | 2545 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2546 | if (RD->isAnonymousStructOrUnion()) { |
| 2547 | while (true) { |
| 2548 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 2549 | if (Parent->isAnonymousStructOrUnion()) |
| 2550 | RD = Parent; |
| 2551 | else |
| 2552 | break; |
| 2553 | } |
| 2554 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 2555 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2556 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2558 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2561 | static void |
| 2562 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2563 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2564 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2565 | unsigned NumInits) { |
| 2566 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 2567 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2569 | // Don't check initializers order unless the warning is enabled at the |
| 2570 | // location of at least one initializer. |
| 2571 | bool ShouldCheckOrder = false; |
| 2572 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2573 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2574 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 2575 | Init->getSourceLocation()) |
| 2576 | != Diagnostic::Ignored) { |
| 2577 | ShouldCheckOrder = true; |
| 2578 | break; |
| 2579 | } |
| 2580 | } |
| 2581 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2582 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2583 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2584 | // Build the list of bases and members in the order that they'll |
| 2585 | // actually be initialized. The explicit initializers should be in |
| 2586 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2587 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2589 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2590 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2591 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2592 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2593 | ClassDecl->vbases_begin(), |
| 2594 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2595 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2597 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2598 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2599 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2600 | if (Base->isVirtual()) |
| 2601 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2602 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2603 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2605 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2606 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2607 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2608 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2610 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2611 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2612 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2613 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2614 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2615 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2616 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2617 | |
| 2618 | // Scan forward to try to find this initializer in the idealized |
| 2619 | // initializers list. |
| 2620 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2621 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2622 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2623 | |
| 2624 | // If we didn't find this initializer, it must be because we |
| 2625 | // scanned past it on a previous iteration. That can only |
| 2626 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2627 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2628 | Sema::SemaDiagnosticBuilder D = |
| 2629 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2630 | diag::warn_initializer_out_of_order); |
| 2631 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2632 | if (PrevInit->isAnyMemberInitializer()) |
| 2633 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2634 | else |
| 2635 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2636 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2637 | if (Init->isAnyMemberInitializer()) |
| 2638 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2639 | else |
| 2640 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2641 | |
| 2642 | // Move back to the initializer's location in the ideal list. |
| 2643 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2644 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2645 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2646 | |
| 2647 | assert(IdealIndex != NumIdealInits && |
| 2648 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2649 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2650 | |
| 2651 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2652 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2655 | namespace { |
| 2656 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2657 | CXXCtorInitializer *Init, |
| 2658 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2659 | if (!PrevInit) { |
| 2660 | PrevInit = Init; |
| 2661 | return false; |
| 2662 | } |
| 2663 | |
| 2664 | if (FieldDecl *Field = Init->getMember()) |
| 2665 | S.Diag(Init->getSourceLocation(), |
| 2666 | diag::err_multiple_mem_initialization) |
| 2667 | << Field->getDeclName() |
| 2668 | << Init->getSourceRange(); |
| 2669 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2670 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2671 | assert(BaseClass && "neither field nor base"); |
| 2672 | S.Diag(Init->getSourceLocation(), |
| 2673 | diag::err_multiple_base_initialization) |
| 2674 | << QualType(BaseClass, 0) |
| 2675 | << Init->getSourceRange(); |
| 2676 | } |
| 2677 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2678 | << 0 << PrevInit->getSourceRange(); |
| 2679 | |
| 2680 | return true; |
| 2681 | } |
| 2682 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2683 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2684 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2685 | |
| 2686 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2687 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2688 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2689 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2690 | RecordDecl *Parent = Field->getParent(); |
| 2691 | if (!Parent->isAnonymousStructOrUnion()) |
| 2692 | return false; |
| 2693 | |
| 2694 | NamedDecl *Child = Field; |
| 2695 | do { |
| 2696 | if (Parent->isUnion()) { |
| 2697 | UnionEntry &En = Unions[Parent]; |
| 2698 | if (En.first && En.first != Child) { |
| 2699 | S.Diag(Init->getSourceLocation(), |
| 2700 | diag::err_multiple_mem_union_initialization) |
| 2701 | << Field->getDeclName() |
| 2702 | << Init->getSourceRange(); |
| 2703 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2704 | << 0 << En.second->getSourceRange(); |
| 2705 | return true; |
| 2706 | } else if (!En.first) { |
| 2707 | En.first = Child; |
| 2708 | En.second = Init; |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | Child = Parent; |
| 2713 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2714 | } while (Parent->isAnonymousStructOrUnion()); |
| 2715 | |
| 2716 | return false; |
| 2717 | } |
| 2718 | } |
| 2719 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2720 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2721 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2722 | SourceLocation ColonLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 2723 | CXXCtorInitializer **meminits, |
| 2724 | unsigned NumMemInits, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2725 | bool AnyErrors) { |
| 2726 | if (!ConstructorDecl) |
| 2727 | return; |
| 2728 | |
| 2729 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2730 | |
| 2731 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2732 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2733 | |
| 2734 | if (!Constructor) { |
| 2735 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2736 | return; |
| 2737 | } |
| 2738 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2739 | CXXCtorInitializer **MemInits = |
| 2740 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2741 | |
| 2742 | // Mapping for the duplicate initializers check. |
| 2743 | // For member initializers, this is keyed with a FieldDecl*. |
| 2744 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2745 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2746 | |
| 2747 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2748 | RedundantUnionMap MemberUnions; |
| 2749 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2750 | bool HadError = false; |
| 2751 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2752 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2753 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2754 | // Set the source order index. |
| 2755 | Init->setSourceOrder(i); |
| 2756 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2757 | if (Init->isAnyMemberInitializer()) { |
| 2758 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2759 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2760 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2761 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2762 | } else if (Init->isBaseInitializer()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2763 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2764 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2765 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2766 | } else { |
| 2767 | assert(Init->isDelegatingInitializer()); |
| 2768 | // This must be the only initializer |
| 2769 | if (i != 0 || NumMemInits > 1) { |
| 2770 | Diag(MemInits[0]->getSourceLocation(), |
| 2771 | diag::err_delegating_initializer_alone) |
| 2772 | << MemInits[0]->getSourceRange(); |
| 2773 | HadError = true; |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2774 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2775 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2776 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2777 | // Return immediately as the initializer is set. |
| 2778 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2779 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2782 | if (HadError) |
| 2783 | return; |
| 2784 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2785 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2786 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2787 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2790 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2791 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2792 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 2793 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 2794 | // have destructors implicitly called. |
| 2795 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2796 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2797 | |
| 2798 | // FIXME: all the access-control diagnostics are positioned on the |
| 2799 | // field/base declaration. That's probably good; that said, the |
| 2800 | // user might reasonably want to know why the destructor is being |
| 2801 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2802 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2803 | // Non-static data members. |
| 2804 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2805 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2806 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2807 | if (Field->isInvalidDecl()) |
| 2808 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2809 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2810 | |
| 2811 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2812 | if (!RT) |
| 2813 | continue; |
| 2814 | |
| 2815 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2816 | if (FieldClassDecl->isInvalidDecl()) |
| 2817 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2818 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2819 | continue; |
| 2820 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2821 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2822 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2823 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2824 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2825 | << Field->getDeclName() |
| 2826 | << FieldType); |
| 2827 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2828 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2831 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2832 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2833 | // Bases. |
| 2834 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2835 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2836 | // Bases are always records in a well-formed non-dependent class. |
| 2837 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2838 | |
| 2839 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2840 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2841 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2842 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2843 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2844 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2845 | if (BaseClassDecl->isInvalidDecl()) |
| 2846 | continue; |
| 2847 | // Ignore trivial destructors. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2848 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2849 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2851 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2852 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2853 | |
| 2854 | // FIXME: caret should be on the start of the class name |
| 2855 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2856 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2857 | << Base->getType() |
| 2858 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2859 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2860 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2864 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2865 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2866 | |
| 2867 | // Bases are always records in a well-formed non-dependent class. |
| 2868 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2869 | |
| 2870 | // Ignore direct virtual bases. |
| 2871 | if (DirectVirtualBases.count(RT)) |
| 2872 | continue; |
| 2873 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2874 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2875 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2876 | if (BaseClassDecl->isInvalidDecl()) |
| 2877 | continue; |
| 2878 | // Ignore trivial destructors. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2879 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2880 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2882 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2883 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2884 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2885 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2886 | << VBase->getType()); |
| 2887 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2888 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2892 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2893 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2894 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2897 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2898 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2902 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2903 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2904 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2905 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2906 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2909 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2910 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2911 | if (!getLangOptions().CPlusPlus) |
| 2912 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2914 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2915 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2917 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2918 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2919 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2920 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2922 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2923 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2924 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2926 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2927 | if (!RT) |
| 2928 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2930 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2931 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2932 | // We can't answer whether something is abstract until it has a |
| 2933 | // definition. If it's currently being defined, we'll walk back |
| 2934 | // over all the declarations when we have a full definition. |
| 2935 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2936 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2937 | return false; |
| 2938 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2939 | if (!RD->isAbstract()) |
| 2940 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2942 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2943 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2945 | return true; |
| 2946 | } |
| 2947 | |
| 2948 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2949 | // Check if we've already emitted the list of pure virtual functions |
| 2950 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2951 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2952 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2954 | CXXFinalOverriderMap FinalOverriders; |
| 2955 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2957 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2958 | // more than once. |
| 2959 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2960 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2961 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2962 | MEnd = FinalOverriders.end(); |
| 2963 | M != MEnd; |
| 2964 | ++M) { |
| 2965 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2966 | SOEnd = M->second.end(); |
| 2967 | SO != SOEnd; ++SO) { |
| 2968 | // C++ [class.abstract]p4: |
| 2969 | // A class is abstract if it contains or inherits at least one |
| 2970 | // pure virtual function for which the final overrider is pure |
| 2971 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2973 | // |
| 2974 | if (SO->second.size() != 1) |
| 2975 | continue; |
| 2976 | |
| 2977 | if (!SO->second.front().Method->isPure()) |
| 2978 | continue; |
| 2979 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2980 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2981 | continue; |
| 2982 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2983 | Diag(SO->second.front().Method->getLocation(), |
| 2984 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 2985 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2986 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | if (!PureVirtualClassDiagSet) |
| 2990 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2991 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2994 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2995 | struct AbstractUsageInfo { |
| 2996 | Sema &S; |
| 2997 | CXXRecordDecl *Record; |
| 2998 | CanQualType AbstractType; |
| 2999 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3001 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 3002 | : S(S), Record(Record), |
| 3003 | AbstractType(S.Context.getCanonicalType( |
| 3004 | S.Context.getTypeDeclType(Record))), |
| 3005 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3006 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3007 | void DiagnoseAbstractType() { |
| 3008 | if (Invalid) return; |
| 3009 | S.DiagnoseAbstractType(Record); |
| 3010 | Invalid = true; |
| 3011 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3012 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3013 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 3014 | }; |
| 3015 | |
| 3016 | struct CheckAbstractUsage { |
| 3017 | AbstractUsageInfo &Info; |
| 3018 | const NamedDecl *Ctx; |
| 3019 | |
| 3020 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 3021 | : Info(Info), Ctx(Ctx) {} |
| 3022 | |
| 3023 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3024 | switch (TL.getTypeLocClass()) { |
| 3025 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 3026 | #define TYPELOC(CLASS, PARENT) \ |
| 3027 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 3028 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3029 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3030 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3032 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3033 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 3034 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 3035 | if (!TL.getArg(I)) |
| 3036 | continue; |
| 3037 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3038 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 3039 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3040 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3041 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3042 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3043 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3044 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 3045 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3047 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3048 | // Visit the type parameters from a permissive context. |
| 3049 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 3050 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 3051 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 3052 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 3053 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 3054 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3055 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3057 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3058 | // Visit pointee types from a permissive context. |
| 3059 | #define CheckPolymorphic(Type) \ |
| 3060 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 3061 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 3062 | } |
| 3063 | CheckPolymorphic(PointerTypeLoc) |
| 3064 | CheckPolymorphic(ReferenceTypeLoc) |
| 3065 | CheckPolymorphic(MemberPointerTypeLoc) |
| 3066 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3068 | /// Handle all the types we haven't given a more specific |
| 3069 | /// implementation for above. |
| 3070 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3071 | // Every other kind of type that we haven't called out already |
| 3072 | // that has an inner type is either (1) sugar or (2) contains that |
| 3073 | // inner type in some way as a subobject. |
| 3074 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 3075 | return Visit(Next, Sel); |
| 3076 | |
| 3077 | // If there's no inner type and we're in a permissive context, |
| 3078 | // don't diagnose. |
| 3079 | if (Sel == Sema::AbstractNone) return; |
| 3080 | |
| 3081 | // Check whether the type matches the abstract type. |
| 3082 | QualType T = TL.getType(); |
| 3083 | if (T->isArrayType()) { |
| 3084 | Sel = Sema::AbstractArrayType; |
| 3085 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3086 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3087 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 3088 | if (CT != Info.AbstractType) return; |
| 3089 | |
| 3090 | // It matched; do some magic. |
| 3091 | if (Sel == Sema::AbstractArrayType) { |
| 3092 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 3093 | << T << TL.getSourceRange(); |
| 3094 | } else { |
| 3095 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 3096 | << Sel << T << TL.getSourceRange(); |
| 3097 | } |
| 3098 | Info.DiagnoseAbstractType(); |
| 3099 | } |
| 3100 | }; |
| 3101 | |
| 3102 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 3103 | Sema::AbstractDiagSelID Sel) { |
| 3104 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 3105 | } |
| 3106 | |
| 3107 | } |
| 3108 | |
| 3109 | /// Check for invalid uses of an abstract type in a method declaration. |
| 3110 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3111 | CXXMethodDecl *MD) { |
| 3112 | // No need to do the check on definitions, which require that |
| 3113 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3114 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3115 | return; |
| 3116 | |
| 3117 | // For safety's sake, just ignore it if we don't have type source |
| 3118 | // information. This should never happen for non-implicit methods, |
| 3119 | // but... |
| 3120 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 3121 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 3122 | } |
| 3123 | |
| 3124 | /// Check for invalid uses of an abstract type within a class definition. |
| 3125 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3126 | CXXRecordDecl *RD) { |
| 3127 | for (CXXRecordDecl::decl_iterator |
| 3128 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 3129 | Decl *D = *I; |
| 3130 | if (D->isImplicit()) continue; |
| 3131 | |
| 3132 | // Methods and method templates. |
| 3133 | if (isa<CXXMethodDecl>(D)) { |
| 3134 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 3135 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 3136 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 3137 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 3138 | |
| 3139 | // Fields and static variables. |
| 3140 | } else if (isa<FieldDecl>(D)) { |
| 3141 | FieldDecl *FD = cast<FieldDecl>(D); |
| 3142 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 3143 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 3144 | } else if (isa<VarDecl>(D)) { |
| 3145 | VarDecl *VD = cast<VarDecl>(D); |
| 3146 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 3147 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 3148 | |
| 3149 | // Nested classes and class templates. |
| 3150 | } else if (isa<CXXRecordDecl>(D)) { |
| 3151 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 3152 | } else if (isa<ClassTemplateDecl>(D)) { |
| 3153 | CheckAbstractClassUsage(Info, |
| 3154 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 3155 | } |
| 3156 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3159 | /// \brief Perform semantic checks on a class definition that has been |
| 3160 | /// completing, introducing implicitly-declared members, checking for |
| 3161 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3162 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 3163 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3164 | return; |
| 3165 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3166 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 3167 | AbstractUsageInfo Info(*this, Record); |
| 3168 | CheckAbstractClassUsage(Info, Record); |
| 3169 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3170 | |
| 3171 | // If this is not an aggregate type and has no user-declared constructor, |
| 3172 | // complain about any non-static data members of reference or const scalar |
| 3173 | // type, since they will never get initializers. |
| 3174 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 3175 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 3176 | bool Complained = false; |
| 3177 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 3178 | FEnd = Record->field_end(); |
| 3179 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3180 | if (F->hasInClassInitializer()) |
| 3181 | continue; |
| 3182 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3183 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 3184 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3185 | if (!Complained) { |
| 3186 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 3187 | << Record->getTagKind() << Record; |
| 3188 | Complained = true; |
| 3189 | } |
| 3190 | |
| 3191 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 3192 | << F->getType()->isReferenceType() |
| 3193 | << F->getDeclName(); |
| 3194 | } |
| 3195 | } |
| 3196 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3197 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 3198 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3199 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3200 | |
| 3201 | if (Record->getIdentifier()) { |
| 3202 | // C++ [class.mem]p13: |
| 3203 | // If T is the name of a class, then each of the following shall have a |
| 3204 | // name different from T: |
| 3205 | // - every member of every anonymous union that is a member of class T. |
| 3206 | // |
| 3207 | // C++ [class.mem]p14: |
| 3208 | // In addition, if class T has a user-declared constructor (12.1), every |
| 3209 | // non-static data member of class T shall have a name different from T. |
| 3210 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3211 | R.first != R.second; ++R.first) { |
| 3212 | NamedDecl *D = *R.first; |
| 3213 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 3214 | isa<IndirectFieldDecl>(D)) { |
| 3215 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 3216 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3217 | break; |
| 3218 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3219 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3220 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3221 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3222 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 3223 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3224 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3225 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3226 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 3227 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 3228 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3229 | |
| 3230 | // See if a method overloads virtual methods in a base |
| 3231 | /// class without overriding any. |
| 3232 | if (!Record->isDependentType()) { |
| 3233 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 3234 | MEnd = Record->method_end(); |
| 3235 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 0266aa3 | 2011-03-03 22:58:57 +0000 | [diff] [blame] | 3236 | if (!(*M)->isStatic()) |
| 3237 | DiagnoseHiddenVirtualMethods(Record, *M); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3238 | } |
| 3239 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 3240 | |
| 3241 | // Declare inherited constructors. We do this eagerly here because: |
| 3242 | // - The standard requires an eager diagnostic for conflicting inherited |
| 3243 | // constructors from different classes. |
| 3244 | // - The lazy declaration of the other implicit constructors is so as to not |
| 3245 | // waste space and performance on classes that are not meant to be |
| 3246 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 3247 | // have inherited constructors. |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 3248 | DeclareInheritedConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3249 | |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 3250 | if (!Record->isDependentType()) |
| 3251 | CheckExplicitlyDefaultedMethods(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | void Sema::CheckExplicitlyDefaultedMethods(CXXRecordDecl *Record) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3255 | for (CXXRecordDecl::method_iterator MI = Record->method_begin(), |
| 3256 | ME = Record->method_end(); |
| 3257 | MI != ME; ++MI) { |
| 3258 | if (!MI->isInvalidDecl() && MI->isExplicitlyDefaulted()) { |
| 3259 | switch (getSpecialMember(*MI)) { |
| 3260 | case CXXDefaultConstructor: |
| 3261 | CheckExplicitlyDefaultedDefaultConstructor( |
| 3262 | cast<CXXConstructorDecl>(*MI)); |
| 3263 | break; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3264 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3265 | case CXXDestructor: |
| 3266 | CheckExplicitlyDefaultedDestructor(cast<CXXDestructorDecl>(*MI)); |
| 3267 | break; |
| 3268 | |
| 3269 | case CXXCopyConstructor: |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3270 | CheckExplicitlyDefaultedCopyConstructor(cast<CXXConstructorDecl>(*MI)); |
| 3271 | break; |
| 3272 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3273 | case CXXCopyAssignment: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3274 | CheckExplicitlyDefaultedCopyAssignment(*MI); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3275 | break; |
| 3276 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3277 | case CXXMoveConstructor: |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3278 | CheckExplicitlyDefaultedMoveConstructor(cast<CXXConstructorDecl>(*MI)); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3279 | break; |
| 3280 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3281 | case CXXMoveAssignment: |
| 3282 | CheckExplicitlyDefaultedMoveAssignment(*MI); |
| 3283 | break; |
| 3284 | |
| 3285 | case CXXInvalid: |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3286 | llvm_unreachable("non-special member explicitly defaulted!"); |
| 3287 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | void Sema::CheckExplicitlyDefaultedDefaultConstructor(CXXConstructorDecl *CD) { |
| 3294 | assert(CD->isExplicitlyDefaulted() && CD->isDefaultConstructor()); |
| 3295 | |
| 3296 | // Whether this was the first-declared instance of the constructor. |
| 3297 | // This affects whether we implicitly add an exception spec (and, eventually, |
| 3298 | // constexpr). It is also ill-formed to explicitly default a constructor such |
| 3299 | // that it would be deleted. (C++0x [decl.fct.def.default]) |
| 3300 | bool First = CD == CD->getCanonicalDecl(); |
| 3301 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3302 | bool HadError = false; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3303 | if (CD->getNumParams() != 0) { |
| 3304 | Diag(CD->getLocation(), diag::err_defaulted_default_ctor_params) |
| 3305 | << CD->getSourceRange(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3306 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | ImplicitExceptionSpecification Spec |
| 3310 | = ComputeDefaultedDefaultCtorExceptionSpec(CD->getParent()); |
| 3311 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3312 | if (EPI.ExceptionSpecType == EST_Delayed) { |
| 3313 | // Exception specification depends on some deferred part of the class. We'll |
| 3314 | // try again when the class's definition has been fully processed. |
| 3315 | return; |
| 3316 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3317 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3318 | *ExceptionType = Context.getFunctionType( |
| 3319 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3320 | |
| 3321 | if (CtorType->hasExceptionSpec()) { |
| 3322 | if (CheckEquivalentExceptionSpec( |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3323 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3324 | << CXXDefaultConstructor, |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3325 | PDiag(), |
| 3326 | ExceptionType, SourceLocation(), |
| 3327 | CtorType, CD->getLocation())) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3328 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3329 | } |
| 3330 | } else if (First) { |
| 3331 | // We set the declaration to have the computed exception spec here. |
| 3332 | // We know there are no parameters. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3333 | EPI.ExtInfo = CtorType->getExtInfo(); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3334 | CD->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 3335 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3336 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3337 | if (HadError) { |
| 3338 | CD->setInvalidDecl(); |
| 3339 | return; |
| 3340 | } |
| 3341 | |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3342 | if (ShouldDeleteDefaultConstructor(CD)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3343 | if (First) { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3344 | CD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3345 | } else { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3346 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3347 | << CXXDefaultConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3348 | CD->setInvalidDecl(); |
| 3349 | } |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | void Sema::CheckExplicitlyDefaultedCopyConstructor(CXXConstructorDecl *CD) { |
| 3354 | assert(CD->isExplicitlyDefaulted() && CD->isCopyConstructor()); |
| 3355 | |
| 3356 | // Whether this was the first-declared instance of the constructor. |
| 3357 | bool First = CD == CD->getCanonicalDecl(); |
| 3358 | |
| 3359 | bool HadError = false; |
| 3360 | if (CD->getNumParams() != 1) { |
| 3361 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_params) |
| 3362 | << CD->getSourceRange(); |
| 3363 | HadError = true; |
| 3364 | } |
| 3365 | |
| 3366 | ImplicitExceptionSpecification Spec(Context); |
| 3367 | bool Const; |
| 3368 | llvm::tie(Spec, Const) = |
| 3369 | ComputeDefaultedCopyCtorExceptionSpecAndConst(CD->getParent()); |
| 3370 | |
| 3371 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3372 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3373 | *ExceptionType = Context.getFunctionType( |
| 3374 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3375 | |
| 3376 | // Check for parameter type matching. |
| 3377 | // This is a copy ctor so we know it's a cv-qualified reference to T. |
| 3378 | QualType ArgType = CtorType->getArgType(0); |
| 3379 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3380 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_volatile_param); |
| 3381 | HadError = true; |
| 3382 | } |
| 3383 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 3384 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_const_param); |
| 3385 | HadError = true; |
| 3386 | } |
| 3387 | |
| 3388 | if (CtorType->hasExceptionSpec()) { |
| 3389 | if (CheckEquivalentExceptionSpec( |
| 3390 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3391 | << CXXCopyConstructor, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3392 | PDiag(), |
| 3393 | ExceptionType, SourceLocation(), |
| 3394 | CtorType, CD->getLocation())) { |
| 3395 | HadError = true; |
| 3396 | } |
| 3397 | } else if (First) { |
| 3398 | // We set the declaration to have the computed exception spec here. |
| 3399 | // We duplicate the one parameter type. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3400 | EPI.ExtInfo = CtorType->getExtInfo(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3401 | CD->setType(Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 3402 | } |
| 3403 | |
| 3404 | if (HadError) { |
| 3405 | CD->setInvalidDecl(); |
| 3406 | return; |
| 3407 | } |
| 3408 | |
| 3409 | if (ShouldDeleteCopyConstructor(CD)) { |
| 3410 | if (First) { |
| 3411 | CD->setDeletedAsWritten(); |
| 3412 | } else { |
| 3413 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3414 | << CXXCopyConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3415 | CD->setInvalidDecl(); |
| 3416 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3417 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3418 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3419 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3420 | void Sema::CheckExplicitlyDefaultedCopyAssignment(CXXMethodDecl *MD) { |
| 3421 | assert(MD->isExplicitlyDefaulted()); |
| 3422 | |
| 3423 | // Whether this was the first-declared instance of the operator |
| 3424 | bool First = MD == MD->getCanonicalDecl(); |
| 3425 | |
| 3426 | bool HadError = false; |
| 3427 | if (MD->getNumParams() != 1) { |
| 3428 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_params) |
| 3429 | << MD->getSourceRange(); |
| 3430 | HadError = true; |
| 3431 | } |
| 3432 | |
| 3433 | QualType ReturnType = |
| 3434 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 3435 | if (!ReturnType->isLValueReferenceType() || |
| 3436 | !Context.hasSameType( |
| 3437 | Context.getCanonicalType(ReturnType->getPointeeType()), |
| 3438 | Context.getCanonicalType(Context.getTypeDeclType(MD->getParent())))) { |
| 3439 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_return_type); |
| 3440 | HadError = true; |
| 3441 | } |
| 3442 | |
| 3443 | ImplicitExceptionSpecification Spec(Context); |
| 3444 | bool Const; |
| 3445 | llvm::tie(Spec, Const) = |
| 3446 | ComputeDefaultedCopyCtorExceptionSpecAndConst(MD->getParent()); |
| 3447 | |
| 3448 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3449 | const FunctionProtoType *OperType = MD->getType()->getAs<FunctionProtoType>(), |
| 3450 | *ExceptionType = Context.getFunctionType( |
| 3451 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3452 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3453 | QualType ArgType = OperType->getArgType(0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3454 | if (!ArgType->isLValueReferenceType()) { |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3455 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3456 | HadError = true; |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3457 | } else { |
| 3458 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3459 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_volatile_param); |
| 3460 | HadError = true; |
| 3461 | } |
| 3462 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 3463 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_const_param); |
| 3464 | HadError = true; |
| 3465 | } |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3466 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3467 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3468 | if (OperType->getTypeQuals()) { |
| 3469 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_quals); |
| 3470 | HadError = true; |
| 3471 | } |
| 3472 | |
| 3473 | if (OperType->hasExceptionSpec()) { |
| 3474 | if (CheckEquivalentExceptionSpec( |
| 3475 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3476 | << CXXCopyAssignment, |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3477 | PDiag(), |
| 3478 | ExceptionType, SourceLocation(), |
| 3479 | OperType, MD->getLocation())) { |
| 3480 | HadError = true; |
| 3481 | } |
| 3482 | } else if (First) { |
| 3483 | // We set the declaration to have the computed exception spec here. |
| 3484 | // We duplicate the one parameter type. |
| 3485 | EPI.RefQualifier = OperType->getRefQualifier(); |
| 3486 | EPI.ExtInfo = OperType->getExtInfo(); |
| 3487 | MD->setType(Context.getFunctionType(ReturnType, &ArgType, 1, EPI)); |
| 3488 | } |
| 3489 | |
| 3490 | if (HadError) { |
| 3491 | MD->setInvalidDecl(); |
| 3492 | return; |
| 3493 | } |
| 3494 | |
| 3495 | if (ShouldDeleteCopyAssignmentOperator(MD)) { |
| 3496 | if (First) { |
| 3497 | MD->setDeletedAsWritten(); |
| 3498 | } else { |
| 3499 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3500 | << CXXCopyAssignment; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3501 | MD->setInvalidDecl(); |
| 3502 | } |
| 3503 | } |
| 3504 | } |
| 3505 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3506 | void Sema::CheckExplicitlyDefaultedMoveConstructor(CXXConstructorDecl *CD) { |
| 3507 | assert(CD->isExplicitlyDefaulted() && CD->isMoveConstructor()); |
| 3508 | |
| 3509 | // Whether this was the first-declared instance of the constructor. |
| 3510 | bool First = CD == CD->getCanonicalDecl(); |
| 3511 | |
| 3512 | bool HadError = false; |
| 3513 | if (CD->getNumParams() != 1) { |
| 3514 | Diag(CD->getLocation(), diag::err_defaulted_move_ctor_params) |
| 3515 | << CD->getSourceRange(); |
| 3516 | HadError = true; |
| 3517 | } |
| 3518 | |
| 3519 | ImplicitExceptionSpecification Spec( |
| 3520 | ComputeDefaultedMoveCtorExceptionSpec(CD->getParent())); |
| 3521 | |
| 3522 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3523 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3524 | *ExceptionType = Context.getFunctionType( |
| 3525 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3526 | |
| 3527 | // Check for parameter type matching. |
| 3528 | // This is a move ctor so we know it's a cv-qualified rvalue reference to T. |
| 3529 | QualType ArgType = CtorType->getArgType(0); |
| 3530 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3531 | Diag(CD->getLocation(), diag::err_defaulted_move_ctor_volatile_param); |
| 3532 | HadError = true; |
| 3533 | } |
| 3534 | if (ArgType->getPointeeType().isConstQualified()) { |
| 3535 | Diag(CD->getLocation(), diag::err_defaulted_move_ctor_const_param); |
| 3536 | HadError = true; |
| 3537 | } |
| 3538 | |
| 3539 | if (CtorType->hasExceptionSpec()) { |
| 3540 | if (CheckEquivalentExceptionSpec( |
| 3541 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 3542 | << CXXMoveConstructor, |
| 3543 | PDiag(), |
| 3544 | ExceptionType, SourceLocation(), |
| 3545 | CtorType, CD->getLocation())) { |
| 3546 | HadError = true; |
| 3547 | } |
| 3548 | } else if (First) { |
| 3549 | // We set the declaration to have the computed exception spec here. |
| 3550 | // We duplicate the one parameter type. |
| 3551 | EPI.ExtInfo = CtorType->getExtInfo(); |
| 3552 | CD->setType(Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 3553 | } |
| 3554 | |
| 3555 | if (HadError) { |
| 3556 | CD->setInvalidDecl(); |
| 3557 | return; |
| 3558 | } |
| 3559 | |
| 3560 | if (ShouldDeleteMoveConstructor(CD)) { |
| 3561 | if (First) { |
| 3562 | CD->setDeletedAsWritten(); |
| 3563 | } else { |
| 3564 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
| 3565 | << CXXMoveConstructor; |
| 3566 | CD->setInvalidDecl(); |
| 3567 | } |
| 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | void Sema::CheckExplicitlyDefaultedMoveAssignment(CXXMethodDecl *MD) { |
| 3572 | assert(MD->isExplicitlyDefaulted()); |
| 3573 | |
| 3574 | // Whether this was the first-declared instance of the operator |
| 3575 | bool First = MD == MD->getCanonicalDecl(); |
| 3576 | |
| 3577 | bool HadError = false; |
| 3578 | if (MD->getNumParams() != 1) { |
| 3579 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_params) |
| 3580 | << MD->getSourceRange(); |
| 3581 | HadError = true; |
| 3582 | } |
| 3583 | |
| 3584 | QualType ReturnType = |
| 3585 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 3586 | if (!ReturnType->isLValueReferenceType() || |
| 3587 | !Context.hasSameType( |
| 3588 | Context.getCanonicalType(ReturnType->getPointeeType()), |
| 3589 | Context.getCanonicalType(Context.getTypeDeclType(MD->getParent())))) { |
| 3590 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_return_type); |
| 3591 | HadError = true; |
| 3592 | } |
| 3593 | |
| 3594 | ImplicitExceptionSpecification Spec( |
| 3595 | ComputeDefaultedMoveCtorExceptionSpec(MD->getParent())); |
| 3596 | |
| 3597 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3598 | const FunctionProtoType *OperType = MD->getType()->getAs<FunctionProtoType>(), |
| 3599 | *ExceptionType = Context.getFunctionType( |
| 3600 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3601 | |
| 3602 | QualType ArgType = OperType->getArgType(0); |
| 3603 | if (!ArgType->isRValueReferenceType()) { |
| 3604 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_not_ref); |
| 3605 | HadError = true; |
| 3606 | } else { |
| 3607 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3608 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_volatile_param); |
| 3609 | HadError = true; |
| 3610 | } |
| 3611 | if (ArgType->getPointeeType().isConstQualified()) { |
| 3612 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_const_param); |
| 3613 | HadError = true; |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | if (OperType->getTypeQuals()) { |
| 3618 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_quals); |
| 3619 | HadError = true; |
| 3620 | } |
| 3621 | |
| 3622 | if (OperType->hasExceptionSpec()) { |
| 3623 | if (CheckEquivalentExceptionSpec( |
| 3624 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 3625 | << CXXMoveAssignment, |
| 3626 | PDiag(), |
| 3627 | ExceptionType, SourceLocation(), |
| 3628 | OperType, MD->getLocation())) { |
| 3629 | HadError = true; |
| 3630 | } |
| 3631 | } else if (First) { |
| 3632 | // We set the declaration to have the computed exception spec here. |
| 3633 | // We duplicate the one parameter type. |
| 3634 | EPI.RefQualifier = OperType->getRefQualifier(); |
| 3635 | EPI.ExtInfo = OperType->getExtInfo(); |
| 3636 | MD->setType(Context.getFunctionType(ReturnType, &ArgType, 1, EPI)); |
| 3637 | } |
| 3638 | |
| 3639 | if (HadError) { |
| 3640 | MD->setInvalidDecl(); |
| 3641 | return; |
| 3642 | } |
| 3643 | |
| 3644 | if (ShouldDeleteMoveAssignmentOperator(MD)) { |
| 3645 | if (First) { |
| 3646 | MD->setDeletedAsWritten(); |
| 3647 | } else { |
| 3648 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) |
| 3649 | << CXXMoveAssignment; |
| 3650 | MD->setInvalidDecl(); |
| 3651 | } |
| 3652 | } |
| 3653 | } |
| 3654 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3655 | void Sema::CheckExplicitlyDefaultedDestructor(CXXDestructorDecl *DD) { |
| 3656 | assert(DD->isExplicitlyDefaulted()); |
| 3657 | |
| 3658 | // Whether this was the first-declared instance of the destructor. |
| 3659 | bool First = DD == DD->getCanonicalDecl(); |
| 3660 | |
| 3661 | ImplicitExceptionSpecification Spec |
| 3662 | = ComputeDefaultedDtorExceptionSpec(DD->getParent()); |
| 3663 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3664 | const FunctionProtoType *DtorType = DD->getType()->getAs<FunctionProtoType>(), |
| 3665 | *ExceptionType = Context.getFunctionType( |
| 3666 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3667 | |
| 3668 | if (DtorType->hasExceptionSpec()) { |
| 3669 | if (CheckEquivalentExceptionSpec( |
| 3670 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3671 | << CXXDestructor, |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3672 | PDiag(), |
| 3673 | ExceptionType, SourceLocation(), |
| 3674 | DtorType, DD->getLocation())) { |
| 3675 | DD->setInvalidDecl(); |
| 3676 | return; |
| 3677 | } |
| 3678 | } else if (First) { |
| 3679 | // We set the declaration to have the computed exception spec here. |
| 3680 | // There are no parameters. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3681 | EPI.ExtInfo = DtorType->getExtInfo(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3682 | DD->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 3683 | } |
| 3684 | |
| 3685 | if (ShouldDeleteDestructor(DD)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3686 | if (First) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3687 | DD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3688 | } else { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3689 | Diag(DD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3690 | << CXXDestructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3691 | DD->setInvalidDecl(); |
| 3692 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3693 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3696 | bool Sema::ShouldDeleteDefaultConstructor(CXXConstructorDecl *CD) { |
| 3697 | CXXRecordDecl *RD = CD->getParent(); |
| 3698 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 3699 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3700 | return false; |
| 3701 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3702 | SourceLocation Loc = CD->getLocation(); |
| 3703 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3704 | // Do access control from the constructor |
| 3705 | ContextRAII CtorContext(*this, CD); |
| 3706 | |
| 3707 | bool Union = RD->isUnion(); |
| 3708 | bool AllConst = true; |
| 3709 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3710 | // We do this because we should never actually use an anonymous |
| 3711 | // union's constructor. |
| 3712 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3713 | return false; |
| 3714 | |
| 3715 | // FIXME: We should put some diagnostic logic right into this function. |
| 3716 | |
| 3717 | // C++0x [class.ctor]/5 |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3718 | // A defaulted default constructor for class X is defined as deleted if: |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3719 | |
| 3720 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3721 | BE = RD->bases_end(); |
| 3722 | BI != BE; ++BI) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3723 | // We'll handle this one later |
| 3724 | if (BI->isVirtual()) |
| 3725 | continue; |
| 3726 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3727 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3728 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3729 | |
| 3730 | // -- any [direct base class] has a type with a destructor that is |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3731 | // deleted or inaccessible from the defaulted default constructor |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3732 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3733 | if (BaseDtor->isDeleted()) |
| 3734 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3735 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3736 | AR_accessible) |
| 3737 | return true; |
| 3738 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3739 | // -- any [direct base class either] has no default constructor or |
| 3740 | // overload resolution as applied to [its] default constructor |
| 3741 | // results in an ambiguity or in a function that is deleted or |
| 3742 | // inaccessible from the defaulted default constructor |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3743 | CXXConstructorDecl *BaseDefault = LookupDefaultConstructor(BaseDecl); |
| 3744 | if (!BaseDefault || BaseDefault->isDeleted()) |
| 3745 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3746 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3747 | if (CheckConstructorAccess(Loc, BaseDefault, BaseDefault->getAccess(), |
| 3748 | PDiag()) != AR_accessible) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3749 | return true; |
| 3750 | } |
| 3751 | |
| 3752 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3753 | BE = RD->vbases_end(); |
| 3754 | BI != BE; ++BI) { |
| 3755 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3756 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3757 | |
| 3758 | // -- any [virtual base class] has a type with a destructor that is |
| 3759 | // delete or inaccessible from the defaulted default constructor |
| 3760 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3761 | if (BaseDtor->isDeleted()) |
| 3762 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3763 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3764 | AR_accessible) |
| 3765 | return true; |
| 3766 | |
| 3767 | // -- any [virtual base class either] has no default constructor or |
| 3768 | // overload resolution as applied to [its] default constructor |
| 3769 | // results in an ambiguity or in a function that is deleted or |
| 3770 | // inaccessible from the defaulted default constructor |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3771 | CXXConstructorDecl *BaseDefault = LookupDefaultConstructor(BaseDecl); |
| 3772 | if (!BaseDefault || BaseDefault->isDeleted()) |
| 3773 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3774 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3775 | if (CheckConstructorAccess(Loc, BaseDefault, BaseDefault->getAccess(), |
| 3776 | PDiag()) != AR_accessible) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3777 | return true; |
| 3778 | } |
| 3779 | |
| 3780 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3781 | FE = RD->field_end(); |
| 3782 | FI != FE; ++FI) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3783 | if (FI->isInvalidDecl()) |
| 3784 | continue; |
| 3785 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3786 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3787 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3788 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3789 | // -- any non-static data member with no brace-or-equal-initializer is of |
| 3790 | // reference type |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3791 | if (FieldType->isReferenceType() && !FI->hasInClassInitializer()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3792 | return true; |
| 3793 | |
| 3794 | // -- X is a union and all its variant members are of const-qualified type |
| 3795 | // (or array thereof) |
| 3796 | if (Union && !FieldType.isConstQualified()) |
| 3797 | AllConst = false; |
| 3798 | |
| 3799 | if (FieldRecord) { |
| 3800 | // -- X is a union-like class that has a variant member with a non-trivial |
| 3801 | // default constructor |
| 3802 | if (Union && !FieldRecord->hasTrivialDefaultConstructor()) |
| 3803 | return true; |
| 3804 | |
| 3805 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 3806 | if (FieldDtor->isDeleted()) |
| 3807 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3808 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3809 | AR_accessible) |
| 3810 | return true; |
| 3811 | |
| 3812 | // -- any non-variant non-static data member of const-qualified type (or |
| 3813 | // array thereof) with no brace-or-equal-initializer does not have a |
| 3814 | // user-provided default constructor |
| 3815 | if (FieldType.isConstQualified() && |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3816 | !FI->hasInClassInitializer() && |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3817 | !FieldRecord->hasUserProvidedDefaultConstructor()) |
| 3818 | return true; |
| 3819 | |
| 3820 | if (!Union && FieldRecord->isUnion() && |
| 3821 | FieldRecord->isAnonymousStructOrUnion()) { |
| 3822 | // We're okay to reuse AllConst here since we only care about the |
| 3823 | // value otherwise if we're in a union. |
| 3824 | AllConst = true; |
| 3825 | |
| 3826 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3827 | UE = FieldRecord->field_end(); |
| 3828 | UI != UE; ++UI) { |
| 3829 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3830 | CXXRecordDecl *UnionFieldRecord = |
| 3831 | UnionFieldType->getAsCXXRecordDecl(); |
| 3832 | |
| 3833 | if (!UnionFieldType.isConstQualified()) |
| 3834 | AllConst = false; |
| 3835 | |
| 3836 | if (UnionFieldRecord && |
| 3837 | !UnionFieldRecord->hasTrivialDefaultConstructor()) |
| 3838 | return true; |
| 3839 | } |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 3840 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3841 | if (AllConst) |
| 3842 | return true; |
| 3843 | |
| 3844 | // Don't try to initialize the anonymous union |
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 3845 | // This is technically non-conformant, but sanity demands it. |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3846 | continue; |
| 3847 | } |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3848 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3849 | // -- any non-static data member with no brace-or-equal-initializer has |
| 3850 | // class type M (or array thereof) and either M has no default |
| 3851 | // constructor or overload resolution as applied to M's default |
| 3852 | // constructor results in an ambiguity or in a function that is deleted |
| 3853 | // or inaccessible from the defaulted default constructor. |
| 3854 | if (!FI->hasInClassInitializer()) { |
| 3855 | CXXConstructorDecl *FieldDefault = LookupDefaultConstructor(FieldRecord); |
| 3856 | if (!FieldDefault || FieldDefault->isDeleted()) |
| 3857 | return true; |
| 3858 | if (CheckConstructorAccess(Loc, FieldDefault, FieldDefault->getAccess(), |
| 3859 | PDiag()) != AR_accessible) |
| 3860 | return true; |
| 3861 | } |
| 3862 | } else if (!Union && FieldType.isConstQualified() && |
| 3863 | !FI->hasInClassInitializer()) { |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 3864 | // -- any non-variant non-static data member of const-qualified type (or |
| 3865 | // array thereof) with no brace-or-equal-initializer does not have a |
| 3866 | // user-provided default constructor |
| 3867 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3868 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | if (Union && AllConst) |
| 3872 | return true; |
| 3873 | |
| 3874 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3877 | bool Sema::ShouldDeleteCopyConstructor(CXXConstructorDecl *CD) { |
Sean Hunt | 493ff72 | 2011-05-18 20:57:13 +0000 | [diff] [blame] | 3878 | CXXRecordDecl *RD = CD->getParent(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3879 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 3880 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3881 | return false; |
| 3882 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3883 | SourceLocation Loc = CD->getLocation(); |
| 3884 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3885 | // Do access control from the constructor |
| 3886 | ContextRAII CtorContext(*this, CD); |
| 3887 | |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3888 | bool Union = RD->isUnion(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3889 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3890 | assert(!CD->getParamDecl(0)->getType()->getPointeeType().isNull() && |
| 3891 | "copy assignment arg has no pointee type"); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3892 | unsigned ArgQuals = |
| 3893 | CD->getParamDecl(0)->getType()->getPointeeType().isConstQualified() ? |
| 3894 | Qualifiers::Const : 0; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3895 | |
| 3896 | // We do this because we should never actually use an anonymous |
| 3897 | // union's constructor. |
| 3898 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3899 | return false; |
| 3900 | |
| 3901 | // FIXME: We should put some diagnostic logic right into this function. |
| 3902 | |
| 3903 | // C++0x [class.copy]/11 |
| 3904 | // A defaulted [copy] constructor for class X is defined as delete if X has: |
| 3905 | |
| 3906 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3907 | BE = RD->bases_end(); |
| 3908 | BI != BE; ++BI) { |
| 3909 | // We'll handle this one later |
| 3910 | if (BI->isVirtual()) |
| 3911 | continue; |
| 3912 | |
| 3913 | QualType BaseType = BI->getType(); |
| 3914 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3915 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3916 | |
| 3917 | // -- any [direct base class] of a type with a destructor that is deleted or |
| 3918 | // inaccessible from the defaulted constructor |
| 3919 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3920 | if (BaseDtor->isDeleted()) |
| 3921 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3922 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3923 | AR_accessible) |
| 3924 | return true; |
| 3925 | |
| 3926 | // -- a [direct base class] B that cannot be [copied] because overload |
| 3927 | // resolution, as applied to B's [copy] constructor, results in an |
| 3928 | // ambiguity or a function that is deleted or inaccessible from the |
| 3929 | // defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3930 | CXXConstructorDecl *BaseCtor = LookupCopyingConstructor(BaseDecl, ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3931 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 3932 | return true; |
| 3933 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 3934 | AR_accessible) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3935 | return true; |
| 3936 | } |
| 3937 | |
| 3938 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3939 | BE = RD->vbases_end(); |
| 3940 | BI != BE; ++BI) { |
| 3941 | QualType BaseType = BI->getType(); |
| 3942 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3943 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3944 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3945 | // -- any [virtual base class] of a type with a destructor that is deleted or |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3946 | // inaccessible from the defaulted constructor |
| 3947 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3948 | if (BaseDtor->isDeleted()) |
| 3949 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3950 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3951 | AR_accessible) |
| 3952 | return true; |
| 3953 | |
| 3954 | // -- a [virtual base class] B that cannot be [copied] because overload |
| 3955 | // resolution, as applied to B's [copy] constructor, results in an |
| 3956 | // ambiguity or a function that is deleted or inaccessible from the |
| 3957 | // defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3958 | CXXConstructorDecl *BaseCtor = LookupCopyingConstructor(BaseDecl, ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3959 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 3960 | return true; |
| 3961 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 3962 | AR_accessible) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3963 | return true; |
| 3964 | } |
| 3965 | |
| 3966 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3967 | FE = RD->field_end(); |
| 3968 | FI != FE; ++FI) { |
| 3969 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3970 | |
| 3971 | // -- for a copy constructor, a non-static data member of rvalue reference |
| 3972 | // type |
| 3973 | if (FieldType->isRValueReferenceType()) |
| 3974 | return true; |
| 3975 | |
| 3976 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 3977 | |
| 3978 | if (FieldRecord) { |
| 3979 | // This is an anonymous union |
| 3980 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 3981 | // Anonymous unions inside unions do not variant members create |
| 3982 | if (!Union) { |
| 3983 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3984 | UE = FieldRecord->field_end(); |
| 3985 | UI != UE; ++UI) { |
| 3986 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3987 | CXXRecordDecl *UnionFieldRecord = |
| 3988 | UnionFieldType->getAsCXXRecordDecl(); |
| 3989 | |
| 3990 | // -- a variant member with a non-trivial [copy] constructor and X |
| 3991 | // is a union-like class |
| 3992 | if (UnionFieldRecord && |
| 3993 | !UnionFieldRecord->hasTrivialCopyConstructor()) |
| 3994 | return true; |
| 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | // Don't try to initalize an anonymous union |
| 3999 | continue; |
| 4000 | } else { |
| 4001 | // -- a variant member with a non-trivial [copy] constructor and X is a |
| 4002 | // union-like class |
| 4003 | if (Union && !FieldRecord->hasTrivialCopyConstructor()) |
| 4004 | return true; |
| 4005 | |
| 4006 | // -- any [non-static data member] of a type with a destructor that is |
| 4007 | // deleted or inaccessible from the defaulted constructor |
| 4008 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 4009 | if (FieldDtor->isDeleted()) |
| 4010 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4011 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4012 | AR_accessible) |
| 4013 | return true; |
| 4014 | } |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 4015 | |
| 4016 | // -- a [non-static data member of class type (or array thereof)] B that |
| 4017 | // cannot be [copied] because overload resolution, as applied to B's |
| 4018 | // [copy] constructor, results in an ambiguity or a function that is |
| 4019 | // deleted or inaccessible from the defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 4020 | CXXConstructorDecl *FieldCtor = LookupCopyingConstructor(FieldRecord, |
| 4021 | ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 4022 | if (!FieldCtor || FieldCtor->isDeleted()) |
| 4023 | return true; |
| 4024 | if (CheckConstructorAccess(Loc, FieldCtor, FieldCtor->getAccess(), |
| 4025 | PDiag()) != AR_accessible) |
| 4026 | return true; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4027 | } |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | return false; |
| 4031 | } |
| 4032 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4033 | bool Sema::ShouldDeleteCopyAssignmentOperator(CXXMethodDecl *MD) { |
| 4034 | CXXRecordDecl *RD = MD->getParent(); |
| 4035 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 4036 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4037 | return false; |
| 4038 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4039 | SourceLocation Loc = MD->getLocation(); |
| 4040 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4041 | // Do access control from the constructor |
| 4042 | ContextRAII MethodContext(*this, MD); |
| 4043 | |
| 4044 | bool Union = RD->isUnion(); |
| 4045 | |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 4046 | unsigned ArgQuals = |
| 4047 | MD->getParamDecl(0)->getType()->getPointeeType().isConstQualified() ? |
| 4048 | Qualifiers::Const : 0; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4049 | |
| 4050 | // We do this because we should never actually use an anonymous |
| 4051 | // union's constructor. |
| 4052 | if (Union && RD->isAnonymousStructOrUnion()) |
| 4053 | return false; |
| 4054 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4055 | // FIXME: We should put some diagnostic logic right into this function. |
| 4056 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4057 | // C++0x [class.copy]/20 |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4058 | // A defaulted [copy] assignment operator for class X is defined as deleted |
| 4059 | // if X has: |
| 4060 | |
| 4061 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 4062 | BE = RD->bases_end(); |
| 4063 | BI != BE; ++BI) { |
| 4064 | // We'll handle this one later |
| 4065 | if (BI->isVirtual()) |
| 4066 | continue; |
| 4067 | |
| 4068 | QualType BaseType = BI->getType(); |
| 4069 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 4070 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 4071 | |
| 4072 | // -- a [direct base class] B that cannot be [copied] because overload |
| 4073 | // resolution, as applied to B's [copy] assignment operator, results in |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4074 | // an ambiguity or a function that is deleted or inaccessible from the |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4075 | // assignment operator |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 4076 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(BaseDecl, ArgQuals, false, |
| 4077 | 0); |
| 4078 | if (!CopyOper || CopyOper->isDeleted()) |
| 4079 | return true; |
| 4080 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4081 | return true; |
| 4082 | } |
| 4083 | |
| 4084 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 4085 | BE = RD->vbases_end(); |
| 4086 | BI != BE; ++BI) { |
| 4087 | QualType BaseType = BI->getType(); |
| 4088 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 4089 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 4090 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4091 | // -- a [virtual base class] B that cannot be [copied] because overload |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4092 | // resolution, as applied to B's [copy] assignment operator, results in |
| 4093 | // an ambiguity or a function that is deleted or inaccessible from the |
| 4094 | // assignment operator |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 4095 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(BaseDecl, ArgQuals, false, |
| 4096 | 0); |
| 4097 | if (!CopyOper || CopyOper->isDeleted()) |
| 4098 | return true; |
| 4099 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4100 | return true; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 4104 | FE = RD->field_end(); |
| 4105 | FI != FE; ++FI) { |
| 4106 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 4107 | |
| 4108 | // -- a non-static data member of reference type |
| 4109 | if (FieldType->isReferenceType()) |
| 4110 | return true; |
| 4111 | |
| 4112 | // -- a non-static data member of const non-class type (or array thereof) |
| 4113 | if (FieldType.isConstQualified() && !FieldType->isRecordType()) |
| 4114 | return true; |
| 4115 | |
| 4116 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4117 | |
| 4118 | if (FieldRecord) { |
| 4119 | // This is an anonymous union |
| 4120 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 4121 | // Anonymous unions inside unions do not variant members create |
| 4122 | if (!Union) { |
| 4123 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4124 | UE = FieldRecord->field_end(); |
| 4125 | UI != UE; ++UI) { |
| 4126 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 4127 | CXXRecordDecl *UnionFieldRecord = |
| 4128 | UnionFieldType->getAsCXXRecordDecl(); |
| 4129 | |
| 4130 | // -- a variant member with a non-trivial [copy] assignment operator |
| 4131 | // and X is a union-like class |
| 4132 | if (UnionFieldRecord && |
| 4133 | !UnionFieldRecord->hasTrivialCopyAssignment()) |
| 4134 | return true; |
| 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | // Don't try to initalize an anonymous union |
| 4139 | continue; |
| 4140 | // -- a variant member with a non-trivial [copy] assignment operator |
| 4141 | // and X is a union-like class |
| 4142 | } else if (Union && !FieldRecord->hasTrivialCopyAssignment()) { |
| 4143 | return true; |
| 4144 | } |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4145 | |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 4146 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(FieldRecord, ArgQuals, |
| 4147 | false, 0); |
| 4148 | if (!CopyOper || CopyOper->isDeleted()) |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4149 | return true; |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 4150 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4151 | return true; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | return false; |
| 4156 | } |
| 4157 | |
| 4158 | bool Sema::ShouldDeleteMoveConstructor(CXXConstructorDecl *CD) { |
| 4159 | CXXRecordDecl *RD = CD->getParent(); |
| 4160 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| 4161 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
| 4162 | return false; |
| 4163 | |
| 4164 | SourceLocation Loc = CD->getLocation(); |
| 4165 | |
| 4166 | // Do access control from the constructor |
| 4167 | ContextRAII CtorContext(*this, CD); |
| 4168 | |
| 4169 | bool Union = RD->isUnion(); |
| 4170 | |
| 4171 | assert(!CD->getParamDecl(0)->getType()->getPointeeType().isNull() && |
| 4172 | "copy assignment arg has no pointee type"); |
| 4173 | |
| 4174 | // We do this because we should never actually use an anonymous |
| 4175 | // union's constructor. |
| 4176 | if (Union && RD->isAnonymousStructOrUnion()) |
| 4177 | return false; |
| 4178 | |
| 4179 | // C++0x [class.copy]/11 |
| 4180 | // A defaulted [move] constructor for class X is defined as deleted |
| 4181 | // if X has: |
| 4182 | |
| 4183 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 4184 | BE = RD->bases_end(); |
| 4185 | BI != BE; ++BI) { |
| 4186 | // We'll handle this one later |
| 4187 | if (BI->isVirtual()) |
| 4188 | continue; |
| 4189 | |
| 4190 | QualType BaseType = BI->getType(); |
| 4191 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 4192 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 4193 | |
| 4194 | // -- any [direct base class] of a type with a destructor that is deleted or |
| 4195 | // inaccessible from the defaulted constructor |
| 4196 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 4197 | if (BaseDtor->isDeleted()) |
| 4198 | return true; |
| 4199 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
| 4200 | AR_accessible) |
| 4201 | return true; |
| 4202 | |
| 4203 | // -- a [direct base class] B that cannot be [moved] because overload |
| 4204 | // resolution, as applied to B's [move] constructor, results in an |
| 4205 | // ambiguity or a function that is deleted or inaccessible from the |
| 4206 | // defaulted constructor |
| 4207 | CXXConstructorDecl *BaseCtor = LookupMovingConstructor(BaseDecl); |
| 4208 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 4209 | return true; |
| 4210 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 4211 | AR_accessible) |
| 4212 | return true; |
| 4213 | |
| 4214 | // -- for a move constructor, a [direct base class] with a type that |
| 4215 | // does not have a move constructor and is not trivially copyable. |
| 4216 | // If the field isn't a record, it's always trivially copyable. |
| 4217 | // A moving constructor could be a copy constructor instead. |
| 4218 | if (!BaseCtor->isMoveConstructor() && |
| 4219 | !BaseDecl->isTriviallyCopyable()) |
| 4220 | return true; |
| 4221 | } |
| 4222 | |
| 4223 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 4224 | BE = RD->vbases_end(); |
| 4225 | BI != BE; ++BI) { |
| 4226 | QualType BaseType = BI->getType(); |
| 4227 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 4228 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 4229 | |
| 4230 | // -- any [virtual base class] of a type with a destructor that is deleted |
| 4231 | // or inaccessible from the defaulted constructor |
| 4232 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 4233 | if (BaseDtor->isDeleted()) |
| 4234 | return true; |
| 4235 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
| 4236 | AR_accessible) |
| 4237 | return true; |
| 4238 | |
| 4239 | // -- a [virtual base class] B that cannot be [moved] because overload |
| 4240 | // resolution, as applied to B's [move] constructor, results in an |
| 4241 | // ambiguity or a function that is deleted or inaccessible from the |
| 4242 | // defaulted constructor |
| 4243 | CXXConstructorDecl *BaseCtor = LookupMovingConstructor(BaseDecl); |
| 4244 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 4245 | return true; |
| 4246 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 4247 | AR_accessible) |
| 4248 | return true; |
| 4249 | |
| 4250 | // -- for a move constructor, a [virtual base class] with a type that |
| 4251 | // does not have a move constructor and is not trivially copyable. |
| 4252 | // If the field isn't a record, it's always trivially copyable. |
| 4253 | // A moving constructor could be a copy constructor instead. |
| 4254 | if (!BaseCtor->isMoveConstructor() && |
| 4255 | !BaseDecl->isTriviallyCopyable()) |
| 4256 | return true; |
| 4257 | } |
| 4258 | |
| 4259 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 4260 | FE = RD->field_end(); |
| 4261 | FI != FE; ++FI) { |
| 4262 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 4263 | |
| 4264 | if (CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl()) { |
| 4265 | // This is an anonymous union |
| 4266 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 4267 | // Anonymous unions inside unions do not variant members create |
| 4268 | if (!Union) { |
| 4269 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4270 | UE = FieldRecord->field_end(); |
| 4271 | UI != UE; ++UI) { |
| 4272 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 4273 | CXXRecordDecl *UnionFieldRecord = |
| 4274 | UnionFieldType->getAsCXXRecordDecl(); |
| 4275 | |
| 4276 | // -- a variant member with a non-trivial [move] constructor and X |
| 4277 | // is a union-like class |
| 4278 | if (UnionFieldRecord && |
| 4279 | !UnionFieldRecord->hasTrivialMoveConstructor()) |
| 4280 | return true; |
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | // Don't try to initalize an anonymous union |
| 4285 | continue; |
| 4286 | } else { |
| 4287 | // -- a variant member with a non-trivial [move] constructor and X is a |
| 4288 | // union-like class |
| 4289 | if (Union && !FieldRecord->hasTrivialMoveConstructor()) |
| 4290 | return true; |
| 4291 | |
| 4292 | // -- any [non-static data member] of a type with a destructor that is |
| 4293 | // deleted or inaccessible from the defaulted constructor |
| 4294 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 4295 | if (FieldDtor->isDeleted()) |
| 4296 | return true; |
| 4297 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
| 4298 | AR_accessible) |
| 4299 | return true; |
| 4300 | } |
| 4301 | |
| 4302 | // -- a [non-static data member of class type (or array thereof)] B that |
| 4303 | // cannot be [moved] because overload resolution, as applied to B's |
| 4304 | // [move] constructor, results in an ambiguity or a function that is |
| 4305 | // deleted or inaccessible from the defaulted constructor |
| 4306 | CXXConstructorDecl *FieldCtor = LookupMovingConstructor(FieldRecord); |
| 4307 | if (!FieldCtor || FieldCtor->isDeleted()) |
| 4308 | return true; |
| 4309 | if (CheckConstructorAccess(Loc, FieldCtor, FieldCtor->getAccess(), |
| 4310 | PDiag()) != AR_accessible) |
| 4311 | return true; |
| 4312 | |
| 4313 | // -- for a move constructor, a [non-static data member] with a type that |
| 4314 | // does not have a move constructor and is not trivially copyable. |
| 4315 | // If the field isn't a record, it's always trivially copyable. |
| 4316 | // A moving constructor could be a copy constructor instead. |
| 4317 | if (!FieldCtor->isMoveConstructor() && |
| 4318 | !FieldRecord->isTriviallyCopyable()) |
| 4319 | return true; |
| 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | return false; |
| 4324 | } |
| 4325 | |
| 4326 | bool Sema::ShouldDeleteMoveAssignmentOperator(CXXMethodDecl *MD) { |
| 4327 | CXXRecordDecl *RD = MD->getParent(); |
| 4328 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| 4329 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
| 4330 | return false; |
| 4331 | |
| 4332 | SourceLocation Loc = MD->getLocation(); |
| 4333 | |
| 4334 | // Do access control from the constructor |
| 4335 | ContextRAII MethodContext(*this, MD); |
| 4336 | |
| 4337 | bool Union = RD->isUnion(); |
| 4338 | |
| 4339 | // We do this because we should never actually use an anonymous |
| 4340 | // union's constructor. |
| 4341 | if (Union && RD->isAnonymousStructOrUnion()) |
| 4342 | return false; |
| 4343 | |
| 4344 | // C++0x [class.copy]/20 |
| 4345 | // A defaulted [move] assignment operator for class X is defined as deleted |
| 4346 | // if X has: |
| 4347 | |
| 4348 | // -- for the move constructor, [...] any direct or indirect virtual base |
| 4349 | // class. |
| 4350 | if (RD->getNumVBases() != 0) |
| 4351 | return true; |
| 4352 | |
| 4353 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 4354 | BE = RD->bases_end(); |
| 4355 | BI != BE; ++BI) { |
| 4356 | |
| 4357 | QualType BaseType = BI->getType(); |
| 4358 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 4359 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 4360 | |
| 4361 | // -- a [direct base class] B that cannot be [moved] because overload |
| 4362 | // resolution, as applied to B's [move] assignment operator, results in |
| 4363 | // an ambiguity or a function that is deleted or inaccessible from the |
| 4364 | // assignment operator |
| 4365 | CXXMethodDecl *MoveOper = LookupMovingAssignment(BaseDecl, false, 0); |
| 4366 | if (!MoveOper || MoveOper->isDeleted()) |
| 4367 | return true; |
| 4368 | if (CheckDirectMemberAccess(Loc, MoveOper, PDiag()) != AR_accessible) |
| 4369 | return true; |
| 4370 | |
| 4371 | // -- for the move assignment operator, a [direct base class] with a type |
| 4372 | // that does not have a move assignment operator and is not trivially |
| 4373 | // copyable. |
| 4374 | if (!MoveOper->isMoveAssignmentOperator() && |
| 4375 | !BaseDecl->isTriviallyCopyable()) |
| 4376 | return true; |
| 4377 | } |
| 4378 | |
| 4379 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 4380 | FE = RD->field_end(); |
| 4381 | FI != FE; ++FI) { |
| 4382 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 4383 | |
| 4384 | // -- a non-static data member of reference type |
| 4385 | if (FieldType->isReferenceType()) |
| 4386 | return true; |
| 4387 | |
| 4388 | // -- a non-static data member of const non-class type (or array thereof) |
| 4389 | if (FieldType.isConstQualified() && !FieldType->isRecordType()) |
| 4390 | return true; |
| 4391 | |
| 4392 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4393 | |
| 4394 | if (FieldRecord) { |
| 4395 | // This is an anonymous union |
| 4396 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 4397 | // Anonymous unions inside unions do not variant members create |
| 4398 | if (!Union) { |
| 4399 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4400 | UE = FieldRecord->field_end(); |
| 4401 | UI != UE; ++UI) { |
| 4402 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 4403 | CXXRecordDecl *UnionFieldRecord = |
| 4404 | UnionFieldType->getAsCXXRecordDecl(); |
| 4405 | |
| 4406 | // -- a variant member with a non-trivial [move] assignment operator |
| 4407 | // and X is a union-like class |
| 4408 | if (UnionFieldRecord && |
| 4409 | !UnionFieldRecord->hasTrivialMoveAssignment()) |
| 4410 | return true; |
| 4411 | } |
| 4412 | } |
| 4413 | |
| 4414 | // Don't try to initalize an anonymous union |
| 4415 | continue; |
| 4416 | // -- a variant member with a non-trivial [move] assignment operator |
| 4417 | // and X is a union-like class |
| 4418 | } else if (Union && !FieldRecord->hasTrivialMoveAssignment()) { |
| 4419 | return true; |
| 4420 | } |
| 4421 | |
| 4422 | CXXMethodDecl *MoveOper = LookupMovingAssignment(FieldRecord, false, 0); |
| 4423 | if (!MoveOper || MoveOper->isDeleted()) |
| 4424 | return true; |
| 4425 | if (CheckDirectMemberAccess(Loc, MoveOper, PDiag()) != AR_accessible) |
| 4426 | return true; |
| 4427 | |
| 4428 | // -- for the move assignment operator, a [non-static data member] with a |
| 4429 | // type that does not have a move assignment operator and is not |
| 4430 | // trivially copyable. |
| 4431 | if (!MoveOper->isMoveAssignmentOperator() && |
| 4432 | !FieldRecord->isTriviallyCopyable()) |
| 4433 | return true; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4434 | } |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | return false; |
| 4438 | } |
| 4439 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4440 | bool Sema::ShouldDeleteDestructor(CXXDestructorDecl *DD) { |
| 4441 | CXXRecordDecl *RD = DD->getParent(); |
| 4442 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 4443 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4444 | return false; |
| 4445 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4446 | SourceLocation Loc = DD->getLocation(); |
| 4447 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4448 | // Do access control from the destructor |
| 4449 | ContextRAII CtorContext(*this, DD); |
| 4450 | |
| 4451 | bool Union = RD->isUnion(); |
| 4452 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4453 | // We do this because we should never actually use an anonymous |
| 4454 | // union's destructor. |
| 4455 | if (Union && RD->isAnonymousStructOrUnion()) |
| 4456 | return false; |
| 4457 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4458 | // C++0x [class.dtor]p5 |
| 4459 | // A defaulted destructor for a class X is defined as deleted if: |
| 4460 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 4461 | BE = RD->bases_end(); |
| 4462 | BI != BE; ++BI) { |
| 4463 | // We'll handle this one later |
| 4464 | if (BI->isVirtual()) |
| 4465 | continue; |
| 4466 | |
| 4467 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 4468 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 4469 | assert(BaseDtor && "base has no destructor"); |
| 4470 | |
| 4471 | // -- any direct or virtual base class has a deleted destructor or |
| 4472 | // a destructor that is inaccessible from the defaulted destructor |
| 4473 | if (BaseDtor->isDeleted()) |
| 4474 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4475 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4476 | AR_accessible) |
| 4477 | return true; |
| 4478 | } |
| 4479 | |
| 4480 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 4481 | BE = RD->vbases_end(); |
| 4482 | BI != BE; ++BI) { |
| 4483 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 4484 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 4485 | assert(BaseDtor && "base has no destructor"); |
| 4486 | |
| 4487 | // -- any direct or virtual base class has a deleted destructor or |
| 4488 | // a destructor that is inaccessible from the defaulted destructor |
| 4489 | if (BaseDtor->isDeleted()) |
| 4490 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4491 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4492 | AR_accessible) |
| 4493 | return true; |
| 4494 | } |
| 4495 | |
| 4496 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 4497 | FE = RD->field_end(); |
| 4498 | FI != FE; ++FI) { |
| 4499 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 4500 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4501 | if (FieldRecord) { |
| 4502 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 4503 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4504 | UE = FieldRecord->field_end(); |
| 4505 | UI != UE; ++UI) { |
| 4506 | QualType UnionFieldType = Context.getBaseElementType(FI->getType()); |
| 4507 | CXXRecordDecl *UnionFieldRecord = |
| 4508 | UnionFieldType->getAsCXXRecordDecl(); |
| 4509 | |
| 4510 | // -- X is a union-like class that has a variant member with a non- |
| 4511 | // trivial destructor. |
| 4512 | if (UnionFieldRecord && !UnionFieldRecord->hasTrivialDestructor()) |
| 4513 | return true; |
| 4514 | } |
| 4515 | // Technically we are supposed to do this next check unconditionally. |
| 4516 | // But that makes absolutely no sense. |
| 4517 | } else { |
| 4518 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 4519 | |
| 4520 | // -- any of the non-static data members has class type M (or array |
| 4521 | // thereof) and M has a deleted destructor or a destructor that is |
| 4522 | // inaccessible from the defaulted destructor |
| 4523 | if (FieldDtor->isDeleted()) |
| 4524 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4525 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4526 | AR_accessible) |
| 4527 | return true; |
| 4528 | |
| 4529 | // -- X is a union-like class that has a variant member with a non- |
| 4530 | // trivial destructor. |
| 4531 | if (Union && !FieldDtor->isTrivial()) |
| 4532 | return true; |
| 4533 | } |
| 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | if (DD->isVirtual()) { |
| 4538 | FunctionDecl *OperatorDelete = 0; |
| 4539 | DeclarationName Name = |
| 4540 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4541 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete, |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4542 | false)) |
| 4543 | return true; |
| 4544 | } |
| 4545 | |
| 4546 | |
| 4547 | return false; |
| 4548 | } |
| 4549 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4550 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4551 | namespace { |
| 4552 | struct FindHiddenVirtualMethodData { |
| 4553 | Sema *S; |
| 4554 | CXXMethodDecl *Method; |
| 4555 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4556 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4557 | }; |
| 4558 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4559 | |
| 4560 | /// \brief Member lookup function that determines whether a given C++ |
| 4561 | /// method overloads virtual methods in a base class without overriding any, |
| 4562 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 4563 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 4564 | CXXBasePath &Path, |
| 4565 | void *UserData) { |
| 4566 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 4567 | |
| 4568 | FindHiddenVirtualMethodData &Data |
| 4569 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 4570 | |
| 4571 | DeclarationName Name = Data.Method->getDeclName(); |
| 4572 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 4573 | |
| 4574 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4575 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4576 | for (Path.Decls = BaseRecord->lookup(Name); |
| 4577 | Path.Decls.first != Path.Decls.second; |
| 4578 | ++Path.Decls.first) { |
| 4579 | NamedDecl *D = *Path.Decls.first; |
| 4580 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4581 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4582 | foundSameNameMethod = true; |
| 4583 | // Interested only in hidden virtual methods. |
| 4584 | if (!MD->isVirtual()) |
| 4585 | continue; |
| 4586 | // If the method we are checking overrides a method from its base |
| 4587 | // don't warn about the other overloaded methods. |
| 4588 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 4589 | return true; |
| 4590 | // Collect the overload only if its hidden. |
| 4591 | if (!Data.OverridenAndUsingBaseMethods.count(MD)) |
| 4592 | overloadedMethods.push_back(MD); |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | if (foundSameNameMethod) |
| 4597 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 4598 | overloadedMethods.end()); |
| 4599 | return foundSameNameMethod; |
| 4600 | } |
| 4601 | |
| 4602 | /// \brief See if a method overloads virtual methods in a base class without |
| 4603 | /// overriding any. |
| 4604 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 4605 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
| 4606 | MD->getLocation()) == Diagnostic::Ignored) |
| 4607 | return; |
| 4608 | if (MD->getDeclName().getNameKind() != DeclarationName::Identifier) |
| 4609 | return; |
| 4610 | |
| 4611 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 4612 | /*bool RecordPaths=*/false, |
| 4613 | /*bool DetectVirtual=*/false); |
| 4614 | FindHiddenVirtualMethodData Data; |
| 4615 | Data.Method = MD; |
| 4616 | Data.S = this; |
| 4617 | |
| 4618 | // Keep the base methods that were overriden or introduced in the subclass |
| 4619 | // by 'using' in a set. A base method not in this set is hidden. |
| 4620 | for (DeclContext::lookup_result res = DC->lookup(MD->getDeclName()); |
| 4621 | res.first != res.second; ++res.first) { |
| 4622 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*res.first)) |
| 4623 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 4624 | E = MD->end_overridden_methods(); |
| 4625 | I != E; ++I) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4626 | Data.OverridenAndUsingBaseMethods.insert((*I)->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4627 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*res.first)) |
| 4628 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(shad->getTargetDecl())) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4629 | Data.OverridenAndUsingBaseMethods.insert(MD->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 4633 | !Data.OverloadedMethods.empty()) { |
| 4634 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 4635 | << MD << (Data.OverloadedMethods.size() > 1); |
| 4636 | |
| 4637 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 4638 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 4639 | Diag(overloadedMD->getLocation(), |
| 4640 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 4641 | } |
| 4642 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4645 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4646 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4647 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 4648 | SourceLocation RBrac, |
| 4649 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4650 | if (!TagDecl) |
| 4651 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4653 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4654 | |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 4655 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4656 | // strict aliasing violation! |
| 4657 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 4658 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4659 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4660 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4661 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4664 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 4665 | /// special functions, such as the default constructor, copy |
| 4666 | /// constructor, or destructor, to the given C++ class (C++ |
| 4667 | /// [special]p1). This routine can only be executed just before the |
| 4668 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4669 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4670 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4671 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4672 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 4673 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 4674 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4676 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 4677 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 4678 | |
| 4679 | // If we have a dynamic class, then the copy assignment operator may be |
| 4680 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 4681 | // it shows up in the right place in the vtable and that we diagnose |
| 4682 | // problems with the implicit exception specification. |
| 4683 | if (ClassDecl->isDynamicClass()) |
| 4684 | DeclareImplicitCopyAssignment(ClassDecl); |
| 4685 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4686 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4687 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 4688 | ++ASTContext::NumImplicitDestructors; |
| 4689 | |
| 4690 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 4691 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 4692 | // shows up in the right place in the vtable and that we diagnose problems |
| 4693 | // with the implicit exception specification. |
| 4694 | if (ClassDecl->isDynamicClass()) |
| 4695 | DeclareImplicitDestructor(ClassDecl); |
| 4696 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4699 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 4700 | if (!D) |
| 4701 | return; |
| 4702 | |
| 4703 | int NumParamList = D->getNumTemplateParameterLists(); |
| 4704 | for (int i = 0; i < NumParamList; i++) { |
| 4705 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 4706 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4707 | ParamEnd = Params->end(); |
| 4708 | Param != ParamEnd; ++Param) { |
| 4709 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4710 | if (Named->getDeclName()) { |
| 4711 | S->AddDecl(Named); |
| 4712 | IdResolver.AddDecl(Named); |
| 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | } |
| 4717 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4718 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 4719 | if (!D) |
| 4720 | return; |
| 4721 | |
| 4722 | TemplateParameterList *Params = 0; |
| 4723 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 4724 | Params = Template->getTemplateParameters(); |
| 4725 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4726 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 4727 | Params = PartialSpec->getTemplateParameters(); |
| 4728 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4729 | return; |
| 4730 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4731 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4732 | ParamEnd = Params->end(); |
| 4733 | Param != ParamEnd; ++Param) { |
| 4734 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4735 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4736 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4737 | IdResolver.AddDecl(Named); |
| 4738 | } |
| 4739 | } |
| 4740 | } |
| 4741 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4742 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4743 | if (!RecordD) return; |
| 4744 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4745 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4746 | PushDeclContext(S, Record); |
| 4747 | } |
| 4748 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4749 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4750 | if (!RecordD) return; |
| 4751 | PopDeclContext(); |
| 4752 | } |
| 4753 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4754 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 4755 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 4756 | /// parsing those parts of the given Method declaration that could |
| 4757 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 4758 | /// arguments. This action should enter the scope of the given |
| 4759 | /// Method declaration as if we had just parsed the qualified method |
| 4760 | /// name. However, it should not bring the parameters into scope; |
| 4761 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4762 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 4766 | /// C++ method declaration. We're (re-)introducing the given |
| 4767 | /// function parameter into scope for use in parsing later parts of |
| 4768 | /// the method declaration. For example, we could see an |
| 4769 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4770 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4771 | if (!ParamD) |
| 4772 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4774 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4775 | |
| 4776 | // If this parameter has an unparsed default argument, clear it out |
| 4777 | // to make way for the parsed default argument. |
| 4778 | if (Param->hasUnparsedDefaultArg()) |
| 4779 | Param->setDefaultArg(0); |
| 4780 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4781 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4782 | if (Param->getDeclName()) |
| 4783 | IdResolver.AddDecl(Param); |
| 4784 | } |
| 4785 | |
| 4786 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 4787 | /// processing the delayed method declaration for Method. The method |
| 4788 | /// declaration is now considered finished. There may be a separate |
| 4789 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 4790 | /// immediately!) for this method, if it was also defined inside the |
| 4791 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4792 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4793 | if (!MethodD) |
| 4794 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4796 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4798 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4799 | |
| 4800 | // Now that we have our default arguments, check the constructor |
| 4801 | // again. It could produce additional diagnostics or affect whether |
| 4802 | // the class has implicitly-declared destructors, among other |
| 4803 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4804 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 4805 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4806 | |
| 4807 | // Check the default arguments, which we may have added. |
| 4808 | if (!Method->isInvalidDecl()) |
| 4809 | CheckCXXDefaultArguments(Method); |
| 4810 | } |
| 4811 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4812 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4813 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4814 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4815 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 4816 | /// will be updated to reflect a well-formed type for the constructor and |
| 4817 | /// returned. |
| 4818 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4819 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4820 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4821 | |
| 4822 | // C++ [class.ctor]p3: |
| 4823 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 4824 | // constructor can be invoked for a const, volatile or const |
| 4825 | // volatile object. A constructor shall not be declared const, |
| 4826 | // volatile, or const volatile (9.3.2). |
| 4827 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4828 | if (!D.isInvalidType()) |
| 4829 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4830 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 4831 | << SourceRange(D.getIdentifierLoc()); |
| 4832 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4833 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4834 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4835 | if (!D.isInvalidType()) |
| 4836 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4837 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 4838 | << SourceRange(D.getIdentifierLoc()); |
| 4839 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4840 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4843 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4844 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4845 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4846 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4847 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4848 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4849 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4850 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4851 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4852 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4853 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4854 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4855 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4856 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4857 | // C++0x [class.ctor]p4: |
| 4858 | // A constructor shall not be declared with a ref-qualifier. |
| 4859 | if (FTI.hasRefQualifier()) { |
| 4860 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 4861 | << FTI.RefQualifierIsLValueRef |
| 4862 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 4863 | D.setInvalidType(); |
| 4864 | } |
| 4865 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4866 | // Rebuild the function type "R" without any type qualifiers (in |
| 4867 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4868 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4869 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4870 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 4871 | return R; |
| 4872 | |
| 4873 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 4874 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4875 | EPI.RefQualifier = RQ_None; |
| 4876 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4877 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4878 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4881 | /// CheckConstructor - Checks a fully-formed constructor for |
| 4882 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 4883 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4884 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4885 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 4886 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 4887 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4888 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4889 | |
| 4890 | // C++ [class.copy]p3: |
| 4891 | // A declaration of a constructor for a class X is ill-formed if |
| 4892 | // its first parameter is of type (optionally cv-qualified) X and |
| 4893 | // either there are no other parameters or else all other |
| 4894 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 4895 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4896 | ((Constructor->getNumParams() == 1) || |
| 4897 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4898 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 4899 | Constructor->getTemplateSpecializationKind() |
| 4900 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4901 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 4902 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 4903 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4904 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 4905 | const char *ConstRef |
| 4906 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 4907 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4908 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 4909 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4910 | |
| 4911 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 4912 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4913 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4914 | } |
| 4915 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 4918 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 4919 | /// well-formedness, issuing any diagnostics required. Returns true |
| 4920 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 4921 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4922 | CXXRecordDecl *RD = Destructor->getParent(); |
| 4923 | |
| 4924 | if (Destructor->isVirtual()) { |
| 4925 | SourceLocation Loc; |
| 4926 | |
| 4927 | if (!Destructor->isImplicit()) |
| 4928 | Loc = Destructor->getLocation(); |
| 4929 | else |
| 4930 | Loc = RD->getLocation(); |
| 4931 | |
| 4932 | // If we have a virtual destructor, look up the deallocation function |
| 4933 | FunctionDecl *OperatorDelete = 0; |
| 4934 | DeclarationName Name = |
| 4935 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 4936 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4937 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 4938 | |
| 4939 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4940 | |
| 4941 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4942 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4943 | |
| 4944 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4948 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 4949 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4950 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4951 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4954 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 4955 | /// the well-formednes of the destructor declarator @p D with type @p |
| 4956 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4957 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 4958 | /// will be updated to reflect a well-formed type for the destructor and |
| 4959 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4960 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4961 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4962 | // C++ [class.dtor]p1: |
| 4963 | // [...] A typedef-name that names a class is a class-name |
| 4964 | // (7.1.3); however, a typedef-name that names a class shall not |
| 4965 | // be used as the identifier in the declarator for a destructor |
| 4966 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4967 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4968 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4969 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4970 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4971 | else if (const TemplateSpecializationType *TST = |
| 4972 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 4973 | if (TST->isTypeAlias()) |
| 4974 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 4975 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4976 | |
| 4977 | // C++ [class.dtor]p2: |
| 4978 | // A destructor is used to destroy objects of its class type. A |
| 4979 | // destructor takes no parameters, and no return type can be |
| 4980 | // specified for it (not even void). The address of a destructor |
| 4981 | // shall not be taken. A destructor shall not be static. A |
| 4982 | // destructor can be invoked for a const, volatile or const |
| 4983 | // volatile object. A destructor shall not be declared const, |
| 4984 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4985 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4986 | if (!D.isInvalidType()) |
| 4987 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 4988 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4989 | << SourceRange(D.getIdentifierLoc()) |
| 4990 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 4991 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4992 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4993 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4994 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4995 | // Destructors don't have return types, but the parser will |
| 4996 | // happily parse something like: |
| 4997 | // |
| 4998 | // class X { |
| 4999 | // float ~X(); |
| 5000 | // }; |
| 5001 | // |
| 5002 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5003 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 5004 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5005 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5007 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5008 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5009 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5010 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5011 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5012 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5013 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5014 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5015 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5016 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5017 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5018 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5019 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5022 | // C++0x [class.dtor]p2: |
| 5023 | // A destructor shall not be declared with a ref-qualifier. |
| 5024 | if (FTI.hasRefQualifier()) { |
| 5025 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 5026 | << FTI.RefQualifierIsLValueRef |
| 5027 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5028 | D.setInvalidType(); |
| 5029 | } |
| 5030 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5031 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5032 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5033 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 5034 | |
| 5035 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5036 | FTI.freeArgs(); |
| 5037 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5040 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5041 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5042 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5043 | D.setInvalidType(); |
| 5044 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5045 | |
| 5046 | // Rebuild the function type "R" without any type qualifiers or |
| 5047 | // parameters (in case any of the errors above fired) and with |
| 5048 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5049 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5050 | if (!D.isInvalidType()) |
| 5051 | return R; |
| 5052 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5053 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5054 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5055 | EPI.Variadic = false; |
| 5056 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5057 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5058 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5061 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 5062 | /// well-formednes of the conversion function declarator @p D with |
| 5063 | /// type @p R. If there are any errors in the declarator, this routine |
| 5064 | /// will emit diagnostics and return true. Otherwise, it will return |
| 5065 | /// false. Either way, the type @p R will be updated to reflect a |
| 5066 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5067 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5068 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5069 | // C++ [class.conv.fct]p1: |
| 5070 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5071 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5072 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5073 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5074 | if (!D.isInvalidType()) |
| 5075 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 5076 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5077 | << SourceRange(D.getIdentifierLoc()); |
| 5078 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5079 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5080 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5081 | |
| 5082 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 5083 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5084 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5085 | // Conversion functions don't have return types, but the parser will |
| 5086 | // happily parse something like: |
| 5087 | // |
| 5088 | // class X { |
| 5089 | // float operator bool(); |
| 5090 | // }; |
| 5091 | // |
| 5092 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5093 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 5094 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5095 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5096 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5099 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 5100 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5101 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5102 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5103 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 5104 | |
| 5105 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5106 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5107 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5108 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5109 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5110 | D.setInvalidType(); |
| 5111 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5112 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5113 | // Diagnose "&operator bool()" and other such nonsense. This |
| 5114 | // is actually a gcc extension which we don't support. |
| 5115 | if (Proto->getResultType() != ConvType) { |
| 5116 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 5117 | << Proto->getResultType(); |
| 5118 | D.setInvalidType(); |
| 5119 | ConvType = Proto->getResultType(); |
| 5120 | } |
| 5121 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5122 | // C++ [class.conv.fct]p4: |
| 5123 | // The conversion-type-id shall not represent a function type nor |
| 5124 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5125 | if (ConvType->isArrayType()) { |
| 5126 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 5127 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5128 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5129 | } else if (ConvType->isFunctionType()) { |
| 5130 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 5131 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5132 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | // Rebuild the function type "R" without any parameters (in case any |
| 5136 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5137 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5138 | if (D.isInvalidType()) |
| 5139 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5140 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5141 | // C++0x explicit conversion operators. |
| 5142 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5143 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5144 | diag::warn_explicit_conversion_functions) |
| 5145 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5148 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 5149 | /// the declaration of the given C++ conversion function. This routine |
| 5150 | /// is responsible for recording the conversion function in the C++ |
| 5151 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5152 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5153 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 5154 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 5155 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5156 | |
| 5157 | // Make sure we aren't redeclaring the conversion function. |
| 5158 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5159 | |
| 5160 | // C++ [class.conv.fct]p1: |
| 5161 | // [...] A conversion function is never used to convert a |
| 5162 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 5163 | // same object type (or a reference to it), to a (possibly |
| 5164 | // cv-qualified) base class of that type (or a reference to it), |
| 5165 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5166 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 5167 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5168 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5169 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5170 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5171 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5172 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 5173 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 5174 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5175 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5176 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 5177 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5178 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5179 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5180 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5181 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5182 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5183 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5184 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5185 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 5188 | if (FunctionTemplateDecl *ConversionTemplate |
| 5189 | = Conversion->getDescribedFunctionTemplate()) |
| 5190 | return ConversionTemplate; |
| 5191 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5192 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5195 | //===----------------------------------------------------------------------===// |
| 5196 | // Namespace Handling |
| 5197 | //===----------------------------------------------------------------------===// |
| 5198 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5199 | |
| 5200 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5201 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 5202 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5203 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 5204 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5205 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5206 | SourceLocation IdentLoc, |
| 5207 | IdentifierInfo *II, |
| 5208 | SourceLocation LBrace, |
| 5209 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5210 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 5211 | // For anonymous namespace, take the location of the left brace. |
| 5212 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 5213 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5214 | StartLoc, Loc, II); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5215 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5216 | |
| 5217 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 5218 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 5219 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 5220 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 5221 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
| 5222 | PushNamespaceVisibilityAttr(Attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 5223 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5224 | if (II) { |
| 5225 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 5226 | // The identifier in an original-namespace-definition shall not |
| 5227 | // have been previously defined in the declarative region in |
| 5228 | // which the original-namespace-definition appears. The |
| 5229 | // identifier in an original-namespace-definition is the name of |
| 5230 | // the namespace. Subsequently in that declarative region, it is |
| 5231 | // treated as an original-namespace-name. |
| 5232 | // |
| 5233 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 5234 | // look through using directives, just look for any ordinary names. |
| 5235 | |
| 5236 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
| 5237 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 5238 | Decl::IDNS_Namespace; |
| 5239 | NamedDecl *PrevDecl = 0; |
| 5240 | for (DeclContext::lookup_result R |
| 5241 | = CurContext->getRedeclContext()->lookup(II); |
| 5242 | R.first != R.second; ++R.first) { |
| 5243 | if ((*R.first)->getIdentifierNamespace() & IDNS) { |
| 5244 | PrevDecl = *R.first; |
| 5245 | break; |
| 5246 | } |
| 5247 | } |
| 5248 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5249 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 5250 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5251 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 5252 | // inline-ness must match |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 5253 | if (OrigNS->isInline()) { |
| 5254 | // The user probably just forgot the 'inline', so suggest that it |
| 5255 | // be added back. |
| 5256 | Diag(Namespc->getLocation(), |
| 5257 | diag::warn_inline_namespace_reopened_noninline) |
| 5258 | << FixItHint::CreateInsertion(NamespaceLoc, "inline "); |
| 5259 | } else { |
| 5260 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 5261 | << Namespc->isInline(); |
| 5262 | } |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5263 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 5264 | |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5265 | // Recover by ignoring the new namespace's inline status. |
| 5266 | Namespc->setInline(OrigNS->isInline()); |
| 5267 | } |
| 5268 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5269 | // Attach this namespace decl to the chain of extended namespace |
| 5270 | // definitions. |
| 5271 | OrigNS->setNextNamespace(Namespc); |
| 5272 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5273 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5274 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5275 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 5276 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5277 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5278 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5279 | } else if (PrevDecl) { |
| 5280 | // This is an invalid name redefinition. |
| 5281 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 5282 | << Namespc->getDeclName(); |
| 5283 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 5284 | Namespc->setInvalidDecl(); |
| 5285 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 5286 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5287 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 5288 | // This is the first "real" definition of the namespace "std", so update |
| 5289 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5290 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 5291 | // We had already defined a dummy namespace "std". Link this new |
| 5292 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5293 | StdNS->setNextNamespace(Namespc); |
| 5294 | StdNS->setLocation(IdentLoc); |
| 5295 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | // Make our StdNamespace cache point at the first real definition of the |
| 5299 | // "std" namespace. |
| 5300 | StdNamespace = Namespc; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5301 | |
| 5302 | // Add this instance of "std" to the set of known namespaces |
| 5303 | KnownNamespaces[Namespc] = false; |
| 5304 | } else if (!Namespc->isInline()) { |
| 5305 | // Since this is an "original" namespace, add it to the known set of |
| 5306 | // namespaces if it is not an inline namespace. |
| 5307 | KnownNamespaces[Namespc] = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5308 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5309 | |
| 5310 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 5311 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5312 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5313 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5314 | |
| 5315 | // Link the anonymous namespace into its parent. |
| 5316 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5317 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5318 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 5319 | PrevDecl = TU->getAnonymousNamespace(); |
| 5320 | TU->setAnonymousNamespace(Namespc); |
| 5321 | } else { |
| 5322 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 5323 | PrevDecl = ND->getAnonymousNamespace(); |
| 5324 | ND->setAnonymousNamespace(Namespc); |
| 5325 | } |
| 5326 | |
| 5327 | // Link the anonymous namespace with its previous declaration. |
| 5328 | if (PrevDecl) { |
| 5329 | assert(PrevDecl->isAnonymousNamespace()); |
| 5330 | assert(!PrevDecl->getNextNamespace()); |
| 5331 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 5332 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5333 | |
| 5334 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 5335 | // inline-ness must match |
| 5336 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 5337 | << Namespc->isInline(); |
| 5338 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 5339 | Namespc->setInvalidDecl(); |
| 5340 | // Recover by ignoring the new namespace's inline status. |
| 5341 | Namespc->setInline(PrevDecl->isInline()); |
| 5342 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5343 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5344 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 5345 | CurContext->addDecl(Namespc); |
| 5346 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5347 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 5348 | // behaves as if it were replaced by |
| 5349 | // namespace unique { /* empty body */ } |
| 5350 | // using namespace unique; |
| 5351 | // namespace unique { namespace-body } |
| 5352 | // where all occurrences of 'unique' in a translation unit are |
| 5353 | // replaced by the same identifier and this identifier differs |
| 5354 | // from all other identifiers in the entire program. |
| 5355 | |
| 5356 | // We just create the namespace with an empty name and then add an |
| 5357 | // implicit using declaration, just like the standard suggests. |
| 5358 | // |
| 5359 | // CodeGen enforces the "universally unique" aspect by giving all |
| 5360 | // declarations semantically contained within an anonymous |
| 5361 | // namespace internal linkage. |
| 5362 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5363 | if (!PrevDecl) { |
| 5364 | UsingDirectiveDecl* UD |
| 5365 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 5366 | /* 'using' */ LBrace, |
| 5367 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 5368 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5369 | /* identifier */ SourceLocation(), |
| 5370 | Namespc, |
| 5371 | /* Ancestor */ CurContext); |
| 5372 | UD->setImplicit(); |
| 5373 | CurContext->addDecl(UD); |
| 5374 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 5378 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5379 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 5380 | // for the namespace has the declarations that showed up in that particular |
| 5381 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5382 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5383 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5384 | } |
| 5385 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5386 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 5387 | /// is a namespace alias, returns the namespace it points to. |
| 5388 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 5389 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 5390 | return AD->getNamespace(); |
| 5391 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 5392 | } |
| 5393 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5394 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 5395 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5396 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5397 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 5398 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5399 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5400 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 5401 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 5402 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5403 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5404 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 5405 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 5406 | return cast_or_null<CXXRecordDecl>( |
| 5407 | StdBadAlloc.get(Context.getExternalSource())); |
| 5408 | } |
| 5409 | |
| 5410 | NamespaceDecl *Sema::getStdNamespace() const { |
| 5411 | return cast_or_null<NamespaceDecl>( |
| 5412 | StdNamespace.get(Context.getExternalSource())); |
| 5413 | } |
| 5414 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5415 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 5416 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 5417 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5418 | if (!StdNamespace) { |
| 5419 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 5420 | StdNamespace = NamespaceDecl::Create(Context, |
| 5421 | Context.getTranslationUnitDecl(), |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5422 | SourceLocation(), SourceLocation(), |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5423 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5424 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5427 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 5430 | /// \brief Determine whether a using statement is in a context where it will be |
| 5431 | /// apply in all contexts. |
| 5432 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 5433 | switch (CurContext->getDeclKind()) { |
| 5434 | case Decl::TranslationUnit: |
| 5435 | return true; |
| 5436 | case Decl::LinkageSpec: |
| 5437 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 5438 | default: |
| 5439 | return false; |
| 5440 | } |
| 5441 | } |
| 5442 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5443 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 5444 | CXXScopeSpec &SS, |
| 5445 | SourceLocation IdentLoc, |
| 5446 | IdentifierInfo *Ident) { |
| 5447 | R.clear(); |
| 5448 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
| 5449 | R.getLookupKind(), Sc, &SS, NULL, |
| 5450 | false, S.CTC_NoKeywords, NULL)) { |
| 5451 | if (Corrected.getCorrectionDeclAs<NamespaceDecl>() || |
| 5452 | Corrected.getCorrectionDeclAs<NamespaceAliasDecl>()) { |
| 5453 | std::string CorrectedStr(Corrected.getAsString(S.getLangOptions())); |
| 5454 | std::string CorrectedQuotedStr(Corrected.getQuoted(S.getLangOptions())); |
| 5455 | if (DeclContext *DC = S.computeDeclContext(SS, false)) |
| 5456 | S.Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 5457 | << Ident << DC << CorrectedQuotedStr << SS.getRange() |
| 5458 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
| 5459 | else |
| 5460 | S.Diag(IdentLoc, diag::err_using_directive_suggest) |
| 5461 | << Ident << CorrectedQuotedStr |
| 5462 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
| 5463 | |
| 5464 | S.Diag(Corrected.getCorrectionDecl()->getLocation(), |
| 5465 | diag::note_namespace_defined_here) << CorrectedQuotedStr; |
| 5466 | |
| 5467 | Ident = Corrected.getCorrectionAsIdentifierInfo(); |
| 5468 | R.addDecl(Corrected.getCorrectionDecl()); |
| 5469 | return true; |
| 5470 | } |
| 5471 | R.setLookupName(Ident); |
| 5472 | } |
| 5473 | return false; |
| 5474 | } |
| 5475 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5476 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5477 | SourceLocation UsingLoc, |
| 5478 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5479 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5480 | SourceLocation IdentLoc, |
| 5481 | IdentifierInfo *NamespcName, |
| 5482 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5483 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 5484 | assert(NamespcName && "Invalid NamespcName."); |
| 5485 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 5486 | |
| 5487 | // This can only happen along a recovery path. |
| 5488 | while (S->getFlags() & Scope::TemplateParamScope) |
| 5489 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5490 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5491 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5492 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5493 | NestedNameSpecifier *Qualifier = 0; |
| 5494 | if (SS.isSet()) |
| 5495 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5496 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 5497 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5498 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 5499 | LookupParsedName(R, S, &SS); |
| 5500 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5501 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5502 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5503 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5504 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5505 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 5506 | // "std" hasn't been defined yet, for GCC compatibility. |
| 5507 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 5508 | NamespcName->isStr("std")) { |
| 5509 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 5510 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5511 | R.resolveKind(); |
| 5512 | } |
| 5513 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5514 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5517 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5518 | NamedDecl *Named = R.getFoundDecl(); |
| 5519 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 5520 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5521 | // C++ [namespace.udir]p1: |
| 5522 | // A using-directive specifies that the names in the nominated |
| 5523 | // namespace can be used in the scope in which the |
| 5524 | // using-directive appears after the using-directive. During |
| 5525 | // unqualified name lookup (3.4.1), the names appear as if they |
| 5526 | // were declared in the nearest enclosing namespace which |
| 5527 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5528 | // namespace. [Note: in this context, "contains" means "contains |
| 5529 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5530 | |
| 5531 | // Find enclosing context containing both using-directive and |
| 5532 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5533 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5534 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 5535 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 5536 | CommonAncestor = CommonAncestor->getParent(); |
| 5537 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5538 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 5539 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5540 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 5541 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 5542 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5543 | !SourceMgr.isFromMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 5544 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 5545 | } |
| 5546 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5547 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5548 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 5549 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5550 | } |
| 5551 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5552 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5553 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 5557 | // If scope has associated entity, then using directive is at namespace |
| 5558 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 5559 | // it's lookup structure. |
| 5560 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5561 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5562 | else |
| 5563 | // Otherwise it is block-sope. using-directives will affect lookup |
| 5564 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5565 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5566 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5567 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5568 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5569 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 5570 | AccessSpecifier AS, |
| 5571 | bool HasUsingKeyword, |
| 5572 | SourceLocation UsingLoc, |
| 5573 | CXXScopeSpec &SS, |
| 5574 | UnqualifiedId &Name, |
| 5575 | AttributeList *AttrList, |
| 5576 | bool IsTypeName, |
| 5577 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5578 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5579 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5580 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 5581 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5582 | case UnqualifiedId::IK_Identifier: |
| 5583 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 5584 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5585 | case UnqualifiedId::IK_ConversionFunctionId: |
| 5586 | break; |
| 5587 | |
| 5588 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5589 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5590 | // C++0x inherited constructors. |
| 5591 | if (getLangOptions().CPlusPlus0x) break; |
| 5592 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5593 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 5594 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5595 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5596 | |
| 5597 | case UnqualifiedId::IK_DestructorName: |
| 5598 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 5599 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5600 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5601 | |
| 5602 | case UnqualifiedId::IK_TemplateId: |
| 5603 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 5604 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5605 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5606 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5607 | |
| 5608 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 5609 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5610 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5611 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5612 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5613 | // Warn about using declarations. |
| 5614 | // TODO: store that the declaration was written without 'using' and |
| 5615 | // talk about access decls instead of using decls in the |
| 5616 | // diagnostics. |
| 5617 | if (!HasUsingKeyword) { |
| 5618 | UsingLoc = Name.getSourceRange().getBegin(); |
| 5619 | |
| 5620 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5621 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 5624 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 5625 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 5626 | return 0; |
| 5627 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5628 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5629 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5630 | /* IsInstantiation */ false, |
| 5631 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5632 | if (UD) |
| 5633 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5634 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5635 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5638 | /// \brief Determine whether a using declaration considers the given |
| 5639 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 5640 | /// the same entity or are both typedefs of the same type. |
| 5641 | static bool |
| 5642 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 5643 | bool &SuppressRedeclaration) { |
| 5644 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 5645 | SuppressRedeclaration = false; |
| 5646 | return true; |
| 5647 | } |
| 5648 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5649 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 5650 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5651 | SuppressRedeclaration = true; |
| 5652 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 5653 | TD2->getUnderlyingType()); |
| 5654 | } |
| 5655 | |
| 5656 | return false; |
| 5657 | } |
| 5658 | |
| 5659 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5660 | /// Determines whether to create a using shadow decl for a particular |
| 5661 | /// decl, given the set of decls existing prior to this using lookup. |
| 5662 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 5663 | const LookupResult &Previous) { |
| 5664 | // Diagnose finding a decl which is not from a base class of the |
| 5665 | // current class. We do this now because there are cases where this |
| 5666 | // function will silently decide not to build a shadow decl, which |
| 5667 | // will pre-empt further diagnostics. |
| 5668 | // |
| 5669 | // We don't need to do this in C++0x because we do the check once on |
| 5670 | // the qualifier. |
| 5671 | // |
| 5672 | // FIXME: diagnose the following if we care enough: |
| 5673 | // struct A { int foo; }; |
| 5674 | // struct B : A { using A::foo; }; |
| 5675 | // template <class T> struct C : A {}; |
| 5676 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 5677 | // This is invalid (during instantiation) in C++03 because B::foo |
| 5678 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 5679 | // We can't diagnose it immediately because C<T> is an unknown |
| 5680 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 5681 | // to A::foo, which will look well-formed when we instantiate. |
| 5682 | // The right solution is to not collapse the shadow-decl chain. |
| 5683 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 5684 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 5685 | |
| 5686 | // Handle enums and anonymous structs. |
| 5687 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 5688 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 5689 | while (OrigRec->isAnonymousStructOrUnion()) |
| 5690 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 5691 | |
| 5692 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 5693 | if (OrigDC == CurContext) { |
| 5694 | Diag(Using->getLocation(), |
| 5695 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5696 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5697 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5698 | return true; |
| 5699 | } |
| 5700 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5701 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5702 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5703 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5704 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5705 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5706 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5707 | return true; |
| 5708 | } |
| 5709 | } |
| 5710 | |
| 5711 | if (Previous.empty()) return false; |
| 5712 | |
| 5713 | NamedDecl *Target = Orig; |
| 5714 | if (isa<UsingShadowDecl>(Target)) |
| 5715 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 5716 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 5717 | // If the target happens to be one of the previous declarations, we |
| 5718 | // don't have a conflict. |
| 5719 | // |
| 5720 | // FIXME: but we might be increasing its access, in which case we |
| 5721 | // should redeclare it. |
| 5722 | NamedDecl *NonTag = 0, *Tag = 0; |
| 5723 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5724 | I != E; ++I) { |
| 5725 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5726 | bool Result; |
| 5727 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 5728 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 5729 | |
| 5730 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 5731 | } |
| 5732 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5733 | if (Target->isFunctionOrFunctionTemplate()) { |
| 5734 | FunctionDecl *FD; |
| 5735 | if (isa<FunctionTemplateDecl>(Target)) |
| 5736 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 5737 | else |
| 5738 | FD = cast<FunctionDecl>(Target); |
| 5739 | |
| 5740 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5741 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5742 | case Ovl_Overload: |
| 5743 | return false; |
| 5744 | |
| 5745 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5746 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5747 | break; |
| 5748 | |
| 5749 | // We found a decl with the exact signature. |
| 5750 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5751 | // If we're in a record, we want to hide the target, so we |
| 5752 | // return true (without a diagnostic) to tell the caller not to |
| 5753 | // build a shadow decl. |
| 5754 | if (CurContext->isRecord()) |
| 5755 | return true; |
| 5756 | |
| 5757 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5758 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5759 | break; |
| 5760 | } |
| 5761 | |
| 5762 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5763 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 5764 | return true; |
| 5765 | } |
| 5766 | |
| 5767 | // Target is not a function. |
| 5768 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5769 | if (isa<TagDecl>(Target)) { |
| 5770 | // No conflict between a tag and a non-tag. |
| 5771 | if (!Tag) return false; |
| 5772 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5773 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5774 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5775 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 5776 | return true; |
| 5777 | } |
| 5778 | |
| 5779 | // No conflict between a tag and a non-tag. |
| 5780 | if (!NonTag) return false; |
| 5781 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5782 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5783 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5784 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 5785 | return true; |
| 5786 | } |
| 5787 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5788 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5789 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5790 | UsingDecl *UD, |
| 5791 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5792 | |
| 5793 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5794 | NamedDecl *Target = Orig; |
| 5795 | if (isa<UsingShadowDecl>(Target)) { |
| 5796 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 5797 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5798 | } |
| 5799 | |
| 5800 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5801 | = UsingShadowDecl::Create(Context, CurContext, |
| 5802 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5803 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 5804 | |
| 5805 | Shadow->setAccess(UD->getAccess()); |
| 5806 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 5807 | Shadow->setInvalidDecl(); |
| 5808 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5809 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5810 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5811 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5812 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5813 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5814 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5815 | return Shadow; |
| 5816 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5817 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5818 | /// Hides a using shadow declaration. This is required by the current |
| 5819 | /// using-decl implementation when a resolvable using declaration in a |
| 5820 | /// class is followed by a declaration which would hide or override |
| 5821 | /// one or more of the using decl's targets; for example: |
| 5822 | /// |
| 5823 | /// struct Base { void foo(int); }; |
| 5824 | /// struct Derived : Base { |
| 5825 | /// using Base::foo; |
| 5826 | /// void foo(int); |
| 5827 | /// }; |
| 5828 | /// |
| 5829 | /// The governing language is C++03 [namespace.udecl]p12: |
| 5830 | /// |
| 5831 | /// When a using-declaration brings names from a base class into a |
| 5832 | /// derived class scope, member functions in the derived class |
| 5833 | /// override and/or hide member functions with the same name and |
| 5834 | /// parameter types in a base class (rather than conflicting). |
| 5835 | /// |
| 5836 | /// There are two ways to implement this: |
| 5837 | /// (1) optimistically create shadow decls when they're not hidden |
| 5838 | /// by existing declarations, or |
| 5839 | /// (2) don't create any shadow decls (or at least don't make them |
| 5840 | /// visible) until we've fully parsed/instantiated the class. |
| 5841 | /// The problem with (1) is that we might have to retroactively remove |
| 5842 | /// a shadow decl, which requires several O(n) operations because the |
| 5843 | /// decl structures are (very reasonably) not designed for removal. |
| 5844 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 5845 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5846 | if (Shadow->getDeclName().getNameKind() == |
| 5847 | DeclarationName::CXXConversionFunctionName) |
| 5848 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 5849 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5850 | // Remove it from the DeclContext... |
| 5851 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5852 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5853 | // ...and the scope, if applicable... |
| 5854 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5855 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5856 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5857 | } |
| 5858 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5859 | // ...and the using decl. |
| 5860 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 5861 | |
| 5862 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5863 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5866 | /// Builds a using declaration. |
| 5867 | /// |
| 5868 | /// \param IsInstantiation - Whether this call arises from an |
| 5869 | /// instantiation of an unresolved using declaration. We treat |
| 5870 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5871 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 5872 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5873 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5874 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5875 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5876 | bool IsInstantiation, |
| 5877 | bool IsTypeName, |
| 5878 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5879 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5880 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5881 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 5882 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 5883 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5884 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5885 | if (SS.isEmpty()) { |
| 5886 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5887 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5888 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5889 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5890 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5891 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5892 | ForRedeclaration); |
| 5893 | Previous.setHideTags(false); |
| 5894 | if (S) { |
| 5895 | LookupName(Previous, S); |
| 5896 | |
| 5897 | // It is really dumb that we have to do this. |
| 5898 | LookupResult::Filter F = Previous.makeFilter(); |
| 5899 | while (F.hasNext()) { |
| 5900 | NamedDecl *D = F.next(); |
| 5901 | if (!isDeclInScope(D, CurContext, S)) |
| 5902 | F.erase(); |
| 5903 | } |
| 5904 | F.done(); |
| 5905 | } else { |
| 5906 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 5907 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 5908 | LookupQualifiedName(Previous, CurContext); |
| 5909 | } |
| 5910 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5911 | // Check for invalid redeclarations. |
| 5912 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 5913 | return 0; |
| 5914 | |
| 5915 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5916 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 5917 | return 0; |
| 5918 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5919 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5920 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5921 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5922 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5923 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5924 | // FIXME: not all declaration name kinds are legal here |
| 5925 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 5926 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5927 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5928 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5929 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5930 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 5931 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5932 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5933 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5934 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 5935 | NameInfo, IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 5936 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5937 | D->setAccess(AS); |
| 5938 | CurContext->addDecl(D); |
| 5939 | |
| 5940 | if (!LookupContext) return D; |
| 5941 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5942 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5943 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5944 | UD->setInvalidDecl(); |
| 5945 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5948 | // Constructor inheriting using decls get special treatment. |
| 5949 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 5950 | if (CheckInheritedConstructorUsingDecl(UD)) |
| 5951 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5952 | return UD; |
| 5953 | } |
| 5954 | |
| 5955 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5956 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5957 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5958 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5959 | // Unlike most lookups, we don't always want to hide tag |
| 5960 | // declarations: tag names are visible through the using declaration |
| 5961 | // even if hidden by ordinary names, *except* in a dependent context |
| 5962 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5963 | if (!IsInstantiation) |
| 5964 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5965 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5966 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5967 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5968 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5969 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5970 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5971 | UD->setInvalidDecl(); |
| 5972 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5975 | if (R.isAmbiguous()) { |
| 5976 | UD->setInvalidDecl(); |
| 5977 | return UD; |
| 5978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5979 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5980 | if (IsTypeName) { |
| 5981 | // 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] | 5982 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5983 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 5984 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 5985 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 5986 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5987 | UD->setInvalidDecl(); |
| 5988 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5989 | } |
| 5990 | } else { |
| 5991 | // If we asked for a non-typename and we got a type, error out, |
| 5992 | // but only if this is an instantiation of an unresolved using |
| 5993 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5994 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5995 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 5996 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5997 | UD->setInvalidDecl(); |
| 5998 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5999 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6002 | // C++0x N2914 [namespace.udecl]p6: |
| 6003 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6004 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6005 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 6006 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6007 | UD->setInvalidDecl(); |
| 6008 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6009 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6010 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6011 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 6012 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 6013 | BuildUsingShadowDecl(S, UD, *I); |
| 6014 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6015 | |
| 6016 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6017 | } |
| 6018 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6019 | /// Additional checks for a using declaration referring to a constructor name. |
| 6020 | bool Sema::CheckInheritedConstructorUsingDecl(UsingDecl *UD) { |
| 6021 | if (UD->isTypeName()) { |
| 6022 | // FIXME: Cannot specify typename when specifying constructor |
| 6023 | return true; |
| 6024 | } |
| 6025 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6026 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6027 | assert(SourceType && |
| 6028 | "Using decl naming constructor doesn't have type in scope spec."); |
| 6029 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 6030 | |
| 6031 | // Check whether the named type is a direct base class. |
| 6032 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 6033 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 6034 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 6035 | BaseIt != BaseE; ++BaseIt) { |
| 6036 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 6037 | if (CanonicalSourceType == BaseType) |
| 6038 | break; |
| 6039 | } |
| 6040 | |
| 6041 | if (BaseIt == BaseE) { |
| 6042 | // Did not find SourceType in the bases. |
| 6043 | Diag(UD->getUsingLocation(), |
| 6044 | diag::err_using_decl_constructor_not_in_direct_base) |
| 6045 | << UD->getNameInfo().getSourceRange() |
| 6046 | << QualType(SourceType, 0) << TargetClass; |
| 6047 | return true; |
| 6048 | } |
| 6049 | |
| 6050 | BaseIt->setInheritConstructors(); |
| 6051 | |
| 6052 | return false; |
| 6053 | } |
| 6054 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6055 | /// Checks that the given using declaration is not an invalid |
| 6056 | /// redeclaration. Note that this is checking only for the using decl |
| 6057 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 6058 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 6059 | bool isTypeName, |
| 6060 | const CXXScopeSpec &SS, |
| 6061 | SourceLocation NameLoc, |
| 6062 | const LookupResult &Prev) { |
| 6063 | // C++03 [namespace.udecl]p8: |
| 6064 | // C++0x [namespace.udecl]p10: |
| 6065 | // A using-declaration is a declaration and can therefore be used |
| 6066 | // repeatedly where (and only where) multiple declarations are |
| 6067 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 6068 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 6069 | // That's in non-member contexts. |
| 6070 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6071 | return false; |
| 6072 | |
| 6073 | NestedNameSpecifier *Qual |
| 6074 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 6075 | |
| 6076 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 6077 | NamedDecl *D = *I; |
| 6078 | |
| 6079 | bool DTypename; |
| 6080 | NestedNameSpecifier *DQual; |
| 6081 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 6082 | DTypename = UD->isTypeName(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6083 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6084 | } else if (UnresolvedUsingValueDecl *UD |
| 6085 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 6086 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6087 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6088 | } else if (UnresolvedUsingTypenameDecl *UD |
| 6089 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 6090 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6091 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6092 | } else continue; |
| 6093 | |
| 6094 | // using decls differ if one says 'typename' and the other doesn't. |
| 6095 | // FIXME: non-dependent using decls? |
| 6096 | if (isTypeName != DTypename) continue; |
| 6097 | |
| 6098 | // using decls differ if they name different scopes (but note that |
| 6099 | // template instantiation can cause this check to trigger when it |
| 6100 | // didn't before instantiation). |
| 6101 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 6102 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 6103 | continue; |
| 6104 | |
| 6105 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6106 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6107 | return true; |
| 6108 | } |
| 6109 | |
| 6110 | return false; |
| 6111 | } |
| 6112 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6113 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6114 | /// Checks that the given nested-name qualifier used in a using decl |
| 6115 | /// in the current context is appropriately related to the current |
| 6116 | /// scope. If an error is found, diagnoses it and returns true. |
| 6117 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 6118 | const CXXScopeSpec &SS, |
| 6119 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6120 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6121 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6122 | if (!CurContext->isRecord()) { |
| 6123 | // C++03 [namespace.udecl]p3: |
| 6124 | // C++0x [namespace.udecl]p8: |
| 6125 | // A using-declaration for a class member shall be a member-declaration. |
| 6126 | |
| 6127 | // If we weren't able to compute a valid scope, it must be a |
| 6128 | // dependent class scope. |
| 6129 | if (!NamedContext || NamedContext->isRecord()) { |
| 6130 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 6131 | << SS.getRange(); |
| 6132 | return true; |
| 6133 | } |
| 6134 | |
| 6135 | // Otherwise, everything is known to be fine. |
| 6136 | return false; |
| 6137 | } |
| 6138 | |
| 6139 | // The current scope is a record. |
| 6140 | |
| 6141 | // If the named context is dependent, we can't decide much. |
| 6142 | if (!NamedContext) { |
| 6143 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 6144 | // nested-name-specifier does not refer to a base class, which is |
| 6145 | // still possible in some cases. |
| 6146 | |
| 6147 | // Otherwise we have to conservatively report that things might be |
| 6148 | // okay. |
| 6149 | return false; |
| 6150 | } |
| 6151 | |
| 6152 | if (!NamedContext->isRecord()) { |
| 6153 | // Ideally this would point at the last name in the specifier, |
| 6154 | // but we don't have that level of source info. |
| 6155 | Diag(SS.getRange().getBegin(), |
| 6156 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 6157 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 6158 | return true; |
| 6159 | } |
| 6160 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 6161 | if (!NamedContext->isDependentContext() && |
| 6162 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 6163 | return true; |
| 6164 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6165 | if (getLangOptions().CPlusPlus0x) { |
| 6166 | // C++0x [namespace.udecl]p3: |
| 6167 | // In a using-declaration used as a member-declaration, the |
| 6168 | // nested-name-specifier shall name a base class of the class |
| 6169 | // being defined. |
| 6170 | |
| 6171 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 6172 | cast<CXXRecordDecl>(NamedContext))) { |
| 6173 | if (CurContext == NamedContext) { |
| 6174 | Diag(NameLoc, |
| 6175 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 6176 | << SS.getRange(); |
| 6177 | return true; |
| 6178 | } |
| 6179 | |
| 6180 | Diag(SS.getRange().getBegin(), |
| 6181 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 6182 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 6183 | << cast<CXXRecordDecl>(CurContext) |
| 6184 | << SS.getRange(); |
| 6185 | return true; |
| 6186 | } |
| 6187 | |
| 6188 | return false; |
| 6189 | } |
| 6190 | |
| 6191 | // C++03 [namespace.udecl]p4: |
| 6192 | // A using-declaration used as a member-declaration shall refer |
| 6193 | // to a member of a base class of the class being defined [etc.]. |
| 6194 | |
| 6195 | // Salient point: SS doesn't have to name a base class as long as |
| 6196 | // lookup only finds members from base classes. Therefore we can |
| 6197 | // diagnose here only if we can prove that that can't happen, |
| 6198 | // i.e. if the class hierarchies provably don't intersect. |
| 6199 | |
| 6200 | // TODO: it would be nice if "definitely valid" results were cached |
| 6201 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 6202 | // need to be repeated. |
| 6203 | |
| 6204 | struct UserData { |
| 6205 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 6206 | |
| 6207 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 6208 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 6209 | Data->Bases.insert(Base); |
| 6210 | return true; |
| 6211 | } |
| 6212 | |
| 6213 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 6214 | return !Class->forallBases(collect, this); |
| 6215 | } |
| 6216 | |
| 6217 | /// Returns true if the base is dependent or is one of the |
| 6218 | /// accumulated base classes. |
| 6219 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 6220 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 6221 | return !Data->Bases.count(Base); |
| 6222 | } |
| 6223 | |
| 6224 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 6225 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 6226 | } |
| 6227 | }; |
| 6228 | |
| 6229 | UserData Data; |
| 6230 | |
| 6231 | // Returns false if we find a dependent base. |
| 6232 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 6233 | return false; |
| 6234 | |
| 6235 | // Returns false if the class has a dependent base or if it or one |
| 6236 | // of its bases is present in the base set of the current context. |
| 6237 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 6238 | return false; |
| 6239 | |
| 6240 | Diag(SS.getRange().getBegin(), |
| 6241 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 6242 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 6243 | << cast<CXXRecordDecl>(CurContext) |
| 6244 | << SS.getRange(); |
| 6245 | |
| 6246 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6249 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 6250 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6251 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6252 | SourceLocation UsingLoc, |
| 6253 | UnqualifiedId &Name, |
| 6254 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6255 | // Skip up to the relevant declaration scope. |
| 6256 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6257 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6258 | assert((S->getFlags() & Scope::DeclScope) && |
| 6259 | "got alias-declaration outside of declaration scope"); |
| 6260 | |
| 6261 | if (Type.isInvalid()) |
| 6262 | return 0; |
| 6263 | |
| 6264 | bool Invalid = false; |
| 6265 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 6266 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 6267 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6268 | |
| 6269 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 6270 | return 0; |
| 6271 | |
| 6272 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6273 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6274 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6275 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 6276 | TInfo->getTypeLoc().getBeginLoc()); |
| 6277 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6278 | |
| 6279 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 6280 | LookupName(Previous, S); |
| 6281 | |
| 6282 | // Warn about shadowing the name of a template parameter. |
| 6283 | if (Previous.isSingleResult() && |
| 6284 | Previous.getFoundDecl()->isTemplateParameter()) { |
| 6285 | if (DiagnoseTemplateParameterShadow(Name.StartLocation, |
| 6286 | Previous.getFoundDecl())) |
| 6287 | Invalid = true; |
| 6288 | Previous.clear(); |
| 6289 | } |
| 6290 | |
| 6291 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 6292 | "name in alias declaration must be an identifier"); |
| 6293 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 6294 | Name.StartLocation, |
| 6295 | Name.Identifier, TInfo); |
| 6296 | |
| 6297 | NewTD->setAccess(AS); |
| 6298 | |
| 6299 | if (Invalid) |
| 6300 | NewTD->setInvalidDecl(); |
| 6301 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6302 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 6303 | Invalid |= NewTD->isInvalidDecl(); |
| 6304 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6305 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6306 | |
| 6307 | NamedDecl *NewND; |
| 6308 | if (TemplateParamLists.size()) { |
| 6309 | TypeAliasTemplateDecl *OldDecl = 0; |
| 6310 | TemplateParameterList *OldTemplateParams = 0; |
| 6311 | |
| 6312 | if (TemplateParamLists.size() != 1) { |
| 6313 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
| 6314 | << SourceRange(TemplateParamLists.get()[1]->getTemplateLoc(), |
| 6315 | TemplateParamLists.get()[TemplateParamLists.size()-1]->getRAngleLoc()); |
| 6316 | } |
| 6317 | TemplateParameterList *TemplateParams = TemplateParamLists.get()[0]; |
| 6318 | |
| 6319 | // Only consider previous declarations in the same scope. |
| 6320 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 6321 | /*ExplicitInstantiationOrSpecialization*/false); |
| 6322 | if (!Previous.empty()) { |
| 6323 | Redeclaration = true; |
| 6324 | |
| 6325 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 6326 | if (!OldDecl && !Invalid) { |
| 6327 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 6328 | << Name.Identifier; |
| 6329 | |
| 6330 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 6331 | if (OldD->getLocation().isValid()) |
| 6332 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 6333 | |
| 6334 | Invalid = true; |
| 6335 | } |
| 6336 | |
| 6337 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 6338 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 6339 | OldDecl->getTemplateParameters(), |
| 6340 | /*Complain=*/true, |
| 6341 | TPL_TemplateMatch)) |
| 6342 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 6343 | else |
| 6344 | Invalid = true; |
| 6345 | |
| 6346 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 6347 | if (!Invalid && |
| 6348 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 6349 | NewTD->getUnderlyingType())) { |
| 6350 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 6351 | // but we can't reasonably accept it. |
| 6352 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 6353 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 6354 | if (OldTD->getLocation().isValid()) |
| 6355 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 6356 | Invalid = true; |
| 6357 | } |
| 6358 | } |
| 6359 | } |
| 6360 | |
| 6361 | // Merge any previous default template arguments into our parameters, |
| 6362 | // and check the parameter list. |
| 6363 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 6364 | TPC_TypeAliasTemplate)) |
| 6365 | return 0; |
| 6366 | |
| 6367 | TypeAliasTemplateDecl *NewDecl = |
| 6368 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 6369 | Name.Identifier, TemplateParams, |
| 6370 | NewTD); |
| 6371 | |
| 6372 | NewDecl->setAccess(AS); |
| 6373 | |
| 6374 | if (Invalid) |
| 6375 | NewDecl->setInvalidDecl(); |
| 6376 | else if (OldDecl) |
| 6377 | NewDecl->setPreviousDeclaration(OldDecl); |
| 6378 | |
| 6379 | NewND = NewDecl; |
| 6380 | } else { |
| 6381 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 6382 | NewND = NewTD; |
| 6383 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6384 | |
| 6385 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6386 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6387 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6388 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6389 | } |
| 6390 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6391 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 6392 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6393 | SourceLocation AliasLoc, |
| 6394 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6395 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 6396 | SourceLocation IdentLoc, |
| 6397 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6398 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6399 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6400 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 6401 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6402 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6403 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 6404 | NamedDecl *PrevDecl |
| 6405 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 6406 | ForRedeclaration); |
| 6407 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 6408 | PrevDecl = 0; |
| 6409 | |
| 6410 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6411 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6412 | // 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] | 6413 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 6414 | // FIXME: At some point, we'll want to create the (redundant) |
| 6415 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6416 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 6417 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6418 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6419 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6420 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6421 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 6422 | diag::err_redefinition_different_kind; |
| 6423 | Diag(AliasLoc, DiagID) << Alias; |
| 6424 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6425 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6426 | } |
| 6427 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6428 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6429 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6430 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6431 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6432 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 6433 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6434 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 6435 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 6436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6437 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6438 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6439 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 6440 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6441 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6442 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 6443 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6444 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 6445 | } |
| 6446 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6447 | namespace { |
| 6448 | /// \brief Scoped object used to handle the state changes required in Sema |
| 6449 | /// to implicitly define the body of a C++ member function; |
| 6450 | class ImplicitlyDefinedFunctionScope { |
| 6451 | Sema &S; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6452 | Sema::ContextRAII SavedContext; |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6453 | |
| 6454 | public: |
| 6455 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6456 | : S(S), SavedContext(S, Method) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6457 | { |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6458 | S.PushFunctionScope(); |
| 6459 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 6460 | } |
| 6461 | |
| 6462 | ~ImplicitlyDefinedFunctionScope() { |
| 6463 | S.PopExpressionEvaluationContext(); |
| 6464 | S.PopFunctionOrBlockScope(); |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6465 | } |
| 6466 | }; |
| 6467 | } |
| 6468 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 6469 | Sema::ImplicitExceptionSpecification |
| 6470 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6471 | // C++ [except.spec]p14: |
| 6472 | // An implicitly declared special member function (Clause 12) shall have an |
| 6473 | // exception-specification. [...] |
| 6474 | ImplicitExceptionSpecification ExceptSpec(Context); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 6475 | if (ClassDecl->isInvalidDecl()) |
| 6476 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6477 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6478 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6479 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 6480 | BEnd = ClassDecl->bases_end(); |
| 6481 | B != BEnd; ++B) { |
| 6482 | if (B->isVirtual()) // Handled below. |
| 6483 | continue; |
| 6484 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6485 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 6486 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6487 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 6488 | // If this is a deleted function, add it anyway. This might be conformant |
| 6489 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6490 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6491 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6492 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6493 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6494 | |
| 6495 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6496 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 6497 | BEnd = ClassDecl->vbases_end(); |
| 6498 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6499 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 6500 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6501 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 6502 | // If this is a deleted function, add it anyway. This might be conformant |
| 6503 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6504 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6505 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6506 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6507 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6508 | |
| 6509 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6510 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 6511 | FEnd = ClassDecl->field_end(); |
| 6512 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6513 | if (F->hasInClassInitializer()) { |
| 6514 | if (Expr *E = F->getInClassInitializer()) |
| 6515 | ExceptSpec.CalledExpr(E); |
| 6516 | else if (!F->isInvalidDecl()) |
| 6517 | ExceptSpec.SetDelayed(); |
| 6518 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6519 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6520 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 6521 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 6522 | // If this is a deleted function, add it anyway. This might be conformant |
| 6523 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6524 | // In particular, the problem is that this function never gets called. It |
| 6525 | // might just be ill-formed because this function attempts to refer to |
| 6526 | // a deleted function here. |
| 6527 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6528 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6529 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6530 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6531 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 6532 | return ExceptSpec; |
| 6533 | } |
| 6534 | |
| 6535 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 6536 | CXXRecordDecl *ClassDecl) { |
| 6537 | // C++ [class.ctor]p5: |
| 6538 | // A default constructor for a class X is a constructor of class X |
| 6539 | // that can be called without an argument. If there is no |
| 6540 | // user-declared constructor for class X, a default constructor is |
| 6541 | // implicitly declared. An implicitly-declared default constructor |
| 6542 | // is an inline public member of its class. |
| 6543 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 6544 | "Should not build implicit default constructor!"); |
| 6545 | |
| 6546 | ImplicitExceptionSpecification Spec = |
| 6547 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 6548 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 6549 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6550 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6551 | CanQualType ClassType |
| 6552 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6553 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6554 | DeclarationName Name |
| 6555 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6556 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6557 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6558 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6559 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6560 | 0, 0, EPI), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6561 | /*TInfo=*/0, |
| 6562 | /*isExplicit=*/false, |
| 6563 | /*isInline=*/true, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6564 | /*isImplicitlyDeclared=*/true, |
| 6565 | // FIXME: apply the rules for definitions here |
| 6566 | /*isConstexpr=*/false); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6567 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 6568 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6569 | DefaultCon->setImplicit(); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6570 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6571 | |
| 6572 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6573 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 6574 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6575 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6576 | PushOnScopeChains(DefaultCon, S, false); |
| 6577 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 6578 | |
| 6579 | if (ShouldDeleteDefaultConstructor(DefaultCon)) |
| 6580 | DefaultCon->setDeletedAsWritten(); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6581 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6582 | return DefaultCon; |
| 6583 | } |
| 6584 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6585 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 6586 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 6587 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6588 | !Constructor->doesThisDeclarationHaveABody() && |
| 6589 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6590 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6591 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 6592 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6593 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 6594 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6595 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6596 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 6597 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6598 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6599 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 6600 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6601 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6602 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6603 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6604 | |
| 6605 | SourceLocation Loc = Constructor->getLocation(); |
| 6606 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 6607 | |
| 6608 | Constructor->setUsed(); |
| 6609 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 6610 | |
| 6611 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 6612 | L->CompletedImplicitDefinition(Constructor); |
| 6613 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6614 | } |
| 6615 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6616 | /// Get any existing defaulted default constructor for the given class. Do not |
| 6617 | /// implicitly define one if it does not exist. |
| 6618 | static CXXConstructorDecl *getDefaultedDefaultConstructorUnsafe(Sema &Self, |
| 6619 | CXXRecordDecl *D) { |
| 6620 | ASTContext &Context = Self.Context; |
| 6621 | QualType ClassType = Context.getTypeDeclType(D); |
| 6622 | DeclarationName ConstructorName |
| 6623 | = Context.DeclarationNames.getCXXConstructorName( |
| 6624 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 6625 | |
| 6626 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 6627 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 6628 | Con != ConEnd; ++Con) { |
| 6629 | // A function template cannot be defaulted. |
| 6630 | if (isa<FunctionTemplateDecl>(*Con)) |
| 6631 | continue; |
| 6632 | |
| 6633 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 6634 | if (Constructor->isDefaultConstructor()) |
| 6635 | return Constructor->isDefaulted() ? Constructor : 0; |
| 6636 | } |
| 6637 | return 0; |
| 6638 | } |
| 6639 | |
| 6640 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
| 6641 | if (!D) return; |
| 6642 | AdjustDeclIfTemplate(D); |
| 6643 | |
| 6644 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D); |
| 6645 | CXXConstructorDecl *CtorDecl |
| 6646 | = getDefaultedDefaultConstructorUnsafe(*this, ClassDecl); |
| 6647 | |
| 6648 | if (!CtorDecl) return; |
| 6649 | |
| 6650 | // Compute the exception specification for the default constructor. |
| 6651 | const FunctionProtoType *CtorTy = |
| 6652 | CtorDecl->getType()->castAs<FunctionProtoType>(); |
| 6653 | if (CtorTy->getExceptionSpecType() == EST_Delayed) { |
| 6654 | ImplicitExceptionSpecification Spec = |
| 6655 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 6656 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 6657 | assert(EPI.ExceptionSpecType != EST_Delayed); |
| 6658 | |
| 6659 | CtorDecl->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 6660 | } |
| 6661 | |
| 6662 | // If the default constructor is explicitly defaulted, checking the exception |
| 6663 | // specification is deferred until now. |
| 6664 | if (!CtorDecl->isInvalidDecl() && CtorDecl->isExplicitlyDefaulted() && |
| 6665 | !ClassDecl->isDependentType()) |
| 6666 | CheckExplicitlyDefaultedDefaultConstructor(CtorDecl); |
| 6667 | } |
| 6668 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6669 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 6670 | // We start with an initial pass over the base classes to collect those that |
| 6671 | // inherit constructors from. If there are none, we can forgo all further |
| 6672 | // processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6673 | typedef SmallVector<const RecordType *, 4> BasesVector; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6674 | BasesVector BasesToInheritFrom; |
| 6675 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 6676 | BaseE = ClassDecl->bases_end(); |
| 6677 | BaseIt != BaseE; ++BaseIt) { |
| 6678 | if (BaseIt->getInheritConstructors()) { |
| 6679 | QualType Base = BaseIt->getType(); |
| 6680 | if (Base->isDependentType()) { |
| 6681 | // If we inherit constructors from anything that is dependent, just |
| 6682 | // abort processing altogether. We'll get another chance for the |
| 6683 | // instantiations. |
| 6684 | return; |
| 6685 | } |
| 6686 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 6687 | } |
| 6688 | } |
| 6689 | if (BasesToInheritFrom.empty()) |
| 6690 | return; |
| 6691 | |
| 6692 | // Now collect the constructors that we already have in the current class. |
| 6693 | // Those take precedence over inherited constructors. |
| 6694 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 6695 | // unless there is a user-declared constructor with the same signature in |
| 6696 | // the class where the using-declaration appears. |
| 6697 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 6698 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 6699 | CtorE = ClassDecl->ctor_end(); |
| 6700 | CtorIt != CtorE; ++CtorIt) { |
| 6701 | ExistingConstructors.insert( |
| 6702 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 6703 | } |
| 6704 | |
| 6705 | Scope *S = getScopeForContext(ClassDecl); |
| 6706 | DeclarationName CreatedCtorName = |
| 6707 | Context.DeclarationNames.getCXXConstructorName( |
| 6708 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 6709 | |
| 6710 | // Now comes the true work. |
| 6711 | // First, we keep a map from constructor types to the base that introduced |
| 6712 | // them. Needed for finding conflicting constructors. We also keep the |
| 6713 | // actually inserted declarations in there, for pretty diagnostics. |
| 6714 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 6715 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 6716 | ConstructorToSourceMap InheritedConstructors; |
| 6717 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 6718 | BaseE = BasesToInheritFrom.end(); |
| 6719 | BaseIt != BaseE; ++BaseIt) { |
| 6720 | const RecordType *Base = *BaseIt; |
| 6721 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 6722 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 6723 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 6724 | CtorE = BaseDecl->ctor_end(); |
| 6725 | CtorIt != CtorE; ++CtorIt) { |
| 6726 | // Find the using declaration for inheriting this base's constructors. |
| 6727 | DeclarationName Name = |
| 6728 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
| 6729 | UsingDecl *UD = dyn_cast_or_null<UsingDecl>( |
| 6730 | LookupSingleName(S, Name,SourceLocation(), LookupUsingDeclName)); |
| 6731 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 6732 | ClassDecl->getLocation(); |
| 6733 | |
| 6734 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 6735 | // from the class X named in the using-declaration consists of actual |
| 6736 | // constructors and notional constructors that result from the |
| 6737 | // transformation of defaulted parameters as follows: |
| 6738 | // - all non-template default constructors of X, and |
| 6739 | // - for each non-template constructor of X that has at least one |
| 6740 | // parameter with a default argument, the set of constructors that |
| 6741 | // results from omitting any ellipsis parameter specification and |
| 6742 | // successively omitting parameters with a default argument from the |
| 6743 | // end of the parameter-type-list. |
| 6744 | CXXConstructorDecl *BaseCtor = *CtorIt; |
| 6745 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 6746 | const FunctionProtoType *BaseCtorType = |
| 6747 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 6748 | |
| 6749 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 6750 | maxParams = BaseCtor->getNumParams(); |
| 6751 | params <= maxParams; ++params) { |
| 6752 | // Skip default constructors. They're never inherited. |
| 6753 | if (params == 0) |
| 6754 | continue; |
| 6755 | // Skip copy and move constructors for the same reason. |
| 6756 | if (CanBeCopyOrMove && params == 1) |
| 6757 | continue; |
| 6758 | |
| 6759 | // Build up a function type for this particular constructor. |
| 6760 | // FIXME: The working paper does not consider that the exception spec |
| 6761 | // for the inheriting constructor might be larger than that of the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6762 | // source. This code doesn't yet, either. When it does, this code will |
| 6763 | // need to be delayed until after exception specifications and in-class |
| 6764 | // member initializers are attached. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6765 | const Type *NewCtorType; |
| 6766 | if (params == maxParams) |
| 6767 | NewCtorType = BaseCtorType; |
| 6768 | else { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6769 | SmallVector<QualType, 16> Args; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6770 | for (unsigned i = 0; i < params; ++i) { |
| 6771 | Args.push_back(BaseCtorType->getArgType(i)); |
| 6772 | } |
| 6773 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 6774 | BaseCtorType->getExtProtoInfo(); |
| 6775 | ExtInfo.Variadic = false; |
| 6776 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 6777 | Args.data(), params, ExtInfo) |
| 6778 | .getTypePtr(); |
| 6779 | } |
| 6780 | const Type *CanonicalNewCtorType = |
| 6781 | Context.getCanonicalType(NewCtorType); |
| 6782 | |
| 6783 | // Now that we have the type, first check if the class already has a |
| 6784 | // constructor with this signature. |
| 6785 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 6786 | continue; |
| 6787 | |
| 6788 | // Then we check if we have already declared an inherited constructor |
| 6789 | // with this signature. |
| 6790 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 6791 | InheritedConstructors.insert(std::make_pair( |
| 6792 | CanonicalNewCtorType, |
| 6793 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 6794 | if (!result.second) { |
| 6795 | // Already in the map. If it came from a different class, that's an |
| 6796 | // error. Not if it's from the same. |
| 6797 | CanQualType PreviousBase = result.first->second.first; |
| 6798 | if (CanonicalBase != PreviousBase) { |
| 6799 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 6800 | const CXXConstructorDecl *PrevBaseCtor = |
| 6801 | PrevCtor->getInheritedConstructor(); |
| 6802 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 6803 | |
| 6804 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 6805 | Diag(BaseCtor->getLocation(), |
| 6806 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 6807 | Diag(PrevBaseCtor->getLocation(), |
| 6808 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 6809 | Diag(PrevCtor->getLocation(), |
| 6810 | diag::note_using_decl_constructor_conflict_previous_using); |
| 6811 | } |
| 6812 | continue; |
| 6813 | } |
| 6814 | |
| 6815 | // OK, we're there, now add the constructor. |
| 6816 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6817 | // user-written inline constructor [...] |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6818 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 6819 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6820 | Context, ClassDecl, UsingLoc, DNI, QualType(NewCtorType, 0), |
| 6821 | /*TInfo=*/0, BaseCtor->isExplicit(), /*Inline=*/true, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 6822 | /*ImplicitlyDeclared=*/true, |
| 6823 | // FIXME: Due to a defect in the standard, we treat inherited |
| 6824 | // constructors as constexpr even if that makes them ill-formed. |
| 6825 | /*Constexpr=*/BaseCtor->isConstexpr()); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6826 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 6827 | |
| 6828 | // Build up the parameter decls and add them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6829 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6830 | for (unsigned i = 0; i < params; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6831 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, |
| 6832 | UsingLoc, UsingLoc, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6833 | /*IdentifierInfo=*/0, |
| 6834 | BaseCtorType->getArgType(i), |
| 6835 | /*TInfo=*/0, SC_None, |
| 6836 | SC_None, /*DefaultArg=*/0)); |
| 6837 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6838 | NewCtor->setParams(ParamDecls); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6839 | NewCtor->setInheritedConstructor(BaseCtor); |
| 6840 | |
| 6841 | PushOnScopeChains(NewCtor, S, false); |
| 6842 | ClassDecl->addDecl(NewCtor); |
| 6843 | result.first->second.second = NewCtor; |
| 6844 | } |
| 6845 | } |
| 6846 | } |
| 6847 | } |
| 6848 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6849 | Sema::ImplicitExceptionSpecification |
| 6850 | Sema::ComputeDefaultedDtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6851 | // C++ [except.spec]p14: |
| 6852 | // An implicitly declared special member function (Clause 12) shall have |
| 6853 | // an exception-specification. |
| 6854 | ImplicitExceptionSpecification ExceptSpec(Context); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 6855 | if (ClassDecl->isInvalidDecl()) |
| 6856 | return ExceptSpec; |
| 6857 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6858 | // Direct base-class destructors. |
| 6859 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 6860 | BEnd = ClassDecl->bases_end(); |
| 6861 | B != BEnd; ++B) { |
| 6862 | if (B->isVirtual()) // Handled below. |
| 6863 | continue; |
| 6864 | |
| 6865 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 6866 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6867 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6868 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6869 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6870 | // Virtual base-class destructors. |
| 6871 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 6872 | BEnd = ClassDecl->vbases_end(); |
| 6873 | B != BEnd; ++B) { |
| 6874 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 6875 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6876 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6877 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6878 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6879 | // Field destructors. |
| 6880 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 6881 | FEnd = ClassDecl->field_end(); |
| 6882 | F != FEnd; ++F) { |
| 6883 | if (const RecordType *RecordTy |
| 6884 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 6885 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6886 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6887 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6888 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6889 | return ExceptSpec; |
| 6890 | } |
| 6891 | |
| 6892 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 6893 | // C++ [class.dtor]p2: |
| 6894 | // If a class has no user-declared destructor, a destructor is |
| 6895 | // declared implicitly. An implicitly-declared destructor is an |
| 6896 | // inline public member of its class. |
| 6897 | |
| 6898 | ImplicitExceptionSpecification Spec = |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6899 | ComputeDefaultedDtorExceptionSpec(ClassDecl); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6900 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 6901 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6902 | // Create the actual destructor declaration. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6903 | QualType Ty = Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6904 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6905 | CanQualType ClassType |
| 6906 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6907 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6908 | DeclarationName Name |
| 6909 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6910 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6911 | CXXDestructorDecl *Destructor |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6912 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, Ty, 0, |
| 6913 | /*isInline=*/true, |
| 6914 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6915 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6916 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6917 | Destructor->setImplicit(); |
| 6918 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6919 | |
| 6920 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6921 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 6922 | |
| 6923 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6924 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6925 | PushOnScopeChains(Destructor, S, false); |
| 6926 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6927 | |
| 6928 | // This could be uniqued if it ever proves significant. |
| 6929 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6930 | |
| 6931 | if (ShouldDeleteDestructor(Destructor)) |
| 6932 | Destructor->setDeletedAsWritten(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6933 | |
| 6934 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6935 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6936 | return Destructor; |
| 6937 | } |
| 6938 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6939 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 6940 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6941 | assert((Destructor->isDefaulted() && |
| 6942 | !Destructor->doesThisDeclarationHaveABody()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6943 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6944 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6945 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6946 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6947 | if (Destructor->isInvalidDecl()) |
| 6948 | return; |
| 6949 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6950 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6951 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6952 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 6953 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 6954 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6955 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6956 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6957 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6958 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 6959 | |
| 6960 | Destructor->setInvalidDecl(); |
| 6961 | return; |
| 6962 | } |
| 6963 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6964 | SourceLocation Loc = Destructor->getLocation(); |
| 6965 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame^] | 6966 | Destructor->setImplicitlyDefined(true); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6967 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6968 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 6969 | |
| 6970 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 6971 | L->CompletedImplicitDefinition(Destructor); |
| 6972 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6975 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *classDecl, |
| 6976 | CXXDestructorDecl *destructor) { |
| 6977 | // C++11 [class.dtor]p3: |
| 6978 | // A declaration of a destructor that does not have an exception- |
| 6979 | // specification is implicitly considered to have the same exception- |
| 6980 | // specification as an implicit declaration. |
| 6981 | const FunctionProtoType *dtorType = destructor->getType()-> |
| 6982 | getAs<FunctionProtoType>(); |
| 6983 | if (dtorType->hasExceptionSpec()) |
| 6984 | return; |
| 6985 | |
| 6986 | ImplicitExceptionSpecification exceptSpec = |
| 6987 | ComputeDefaultedDtorExceptionSpec(classDecl); |
| 6988 | |
Chandler Carruth | 3f224b2 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 6989 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 6990 | // the only thing of interest in the destructor type is its extended info. |
| 6991 | // The return and arguments are fixed. |
| 6992 | FunctionProtoType::ExtProtoInfo epi = dtorType->getExtProtoInfo(); |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6993 | epi.ExceptionSpecType = exceptSpec.getExceptionSpecType(); |
| 6994 | epi.NumExceptions = exceptSpec.size(); |
| 6995 | epi.Exceptions = exceptSpec.data(); |
| 6996 | QualType ty = Context.getFunctionType(Context.VoidTy, 0, 0, epi); |
| 6997 | |
| 6998 | destructor->setType(ty); |
| 6999 | |
| 7000 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 7001 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 7002 | // change in behavior can break conforming C++03 programs at runtime. |
| 7003 | // However, we don't have a body yet, so it needs to be done somewhere else. |
| 7004 | } |
| 7005 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7006 | /// \brief Builds a statement that copies/moves the given entity from \p From to |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7007 | /// \c To. |
| 7008 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7009 | /// This routine is used to copy/move the members of a class with an |
| 7010 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7011 | /// copied are arrays, this routine builds for loops to copy them. |
| 7012 | /// |
| 7013 | /// \param S The Sema object used for type-checking. |
| 7014 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7015 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7016 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7017 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 7018 | /// must have this type. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7019 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7020 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7021 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7022 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7023 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7024 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7025 | /// Otherwise, it's a non-static member subobject. |
| 7026 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7027 | /// \param Copying Whether we're copying or moving. |
| 7028 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7029 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 7030 | /// |
| 7031 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7032 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7033 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7034 | Expr *To, Expr *From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7035 | bool CopyingBaseSubobject, bool Copying, |
| 7036 | unsigned Depth = 0) { |
| 7037 | // C++0x [class.copy]p28: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7038 | // Each subobject is assigned in the manner appropriate to its type: |
| 7039 | // |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7040 | // - if the subobject is of class type, as if by a call to operator= with |
| 7041 | // the subobject as the object expression and the corresponding |
| 7042 | // subobject of x as a single function argument (as if by explicit |
| 7043 | // qualification; that is, ignoring any possible virtual overriding |
| 7044 | // functions in more derived classes); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7045 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 7046 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7047 | |
| 7048 | // Look for operator=. |
| 7049 | DeclarationName Name |
| 7050 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 7051 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 7052 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 7053 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7054 | // Filter out any result that isn't a copy/move-assignment operator. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7055 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 7056 | while (F.hasNext()) { |
| 7057 | NamedDecl *D = F.next(); |
| 7058 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7059 | if (Copying ? Method->isCopyAssignmentOperator() : |
| 7060 | Method->isMoveAssignmentOperator()) |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7061 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7062 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7063 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 7064 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7065 | F.done(); |
| 7066 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7067 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 7068 | // assignment operators we found. This strange dance is required when |
| 7069 | // we're assigning via a base classes's copy-assignment operator. To |
| 7070 | // ensure that we're getting the right base class subobject (without |
| 7071 | // ambiguities), we need to cast "this" to that subobject type; to |
| 7072 | // ensure that we don't go through the virtual call mechanism, we need |
| 7073 | // to qualify the operator= name with the base class (see below). However, |
| 7074 | // this means that if the base class has a protected copy assignment |
| 7075 | // operator, the protected member access check will fail. So, we |
| 7076 | // rewrite "protected" access to "public" access in this case, since we |
| 7077 | // know by construction that we're calling from a derived class. |
| 7078 | if (CopyingBaseSubobject) { |
| 7079 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 7080 | L != LEnd; ++L) { |
| 7081 | if (L.getAccess() == AS_protected) |
| 7082 | L.setAccess(AS_public); |
| 7083 | } |
| 7084 | } |
| 7085 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7086 | // Create the nested-name-specifier that will be used to qualify the |
| 7087 | // reference to operator=; this is required to suppress the virtual |
| 7088 | // call mechanism. |
| 7089 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 7090 | SS.MakeTrivial(S.Context, |
| 7091 | NestedNameSpecifier::Create(S.Context, 0, false, |
| 7092 | T.getTypePtr()), |
| 7093 | Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7094 | |
| 7095 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7096 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7097 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7098 | /*FirstQualifierInScope=*/0, OpLookup, |
| 7099 | /*TemplateArgs=*/0, |
| 7100 | /*SuppressQualifierCheck=*/true); |
| 7101 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7102 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7103 | |
| 7104 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7105 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7106 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7107 | OpEqualRef.takeAs<Expr>(), |
| 7108 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7109 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7110 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7111 | |
| 7112 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7113 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 7114 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7115 | // - if the subobject is of scalar type, the built-in assignment |
| 7116 | // operator is used. |
| 7117 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 7118 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7119 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7120 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7121 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7122 | |
| 7123 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7124 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7125 | |
| 7126 | // - if the subobject is an array, each element is assigned, in the |
| 7127 | // manner appropriate to the element type; |
| 7128 | |
| 7129 | // Construct a loop over the array bounds, e.g., |
| 7130 | // |
| 7131 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 7132 | // |
| 7133 | // that will copy each of the array elements. |
| 7134 | QualType SizeType = S.Context.getSizeType(); |
| 7135 | |
| 7136 | // Create the iteration variable. |
| 7137 | IdentifierInfo *IterationVarName = 0; |
| 7138 | { |
| 7139 | llvm::SmallString<8> Str; |
| 7140 | llvm::raw_svector_ostream OS(Str); |
| 7141 | OS << "__i" << Depth; |
| 7142 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 7143 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7144 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7145 | IterationVarName, SizeType, |
| 7146 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7147 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7148 | |
| 7149 | // Initialize the iteration variable to zero. |
| 7150 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7151 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7152 | |
| 7153 | // Create a reference to the iteration variable; we'll use this several |
| 7154 | // times throughout. |
| 7155 | Expr *IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7156 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7157 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 7158 | |
| 7159 | // Create the DeclStmt that holds the iteration variable. |
| 7160 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 7161 | |
| 7162 | // Create the comparison against the array bound. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 7163 | llvm::APInt Upper |
| 7164 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7165 | Expr *Comparison |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 7166 | = new (S.Context) BinaryOperator(IterationVarRef, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7167 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 7168 | BO_NE, S.Context.BoolTy, |
| 7169 | VK_RValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7170 | |
| 7171 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7172 | Expr *Increment |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7173 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 7174 | VK_LValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7175 | |
| 7176 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7177 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 7178 | IterationVarRef, Loc)); |
| 7179 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 7180 | IterationVarRef, Loc)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7181 | if (!Copying) // Cast to rvalue |
| 7182 | From = CastForMoving(S, From); |
| 7183 | |
| 7184 | // Build the copy/move for an individual element of the array. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7185 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, ArrayTy->getElementType(), |
| 7186 | To, From, CopyingBaseSubobject, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7187 | Copying, Depth + 1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7188 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7189 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7190 | |
| 7191 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7192 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7193 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7194 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7195 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7198 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 7199 | Sema::ComputeDefaultedCopyAssignmentExceptionSpecAndConst( |
| 7200 | CXXRecordDecl *ClassDecl) { |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7201 | if (ClassDecl->isInvalidDecl()) |
| 7202 | return std::make_pair(ImplicitExceptionSpecification(Context), false); |
| 7203 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7204 | // C++ [class.copy]p10: |
| 7205 | // If the class definition does not explicitly declare a copy |
| 7206 | // assignment operator, one is declared implicitly. |
| 7207 | // The implicitly-defined copy assignment operator for a class X |
| 7208 | // will have the form |
| 7209 | // |
| 7210 | // X& X::operator=(const X&) |
| 7211 | // |
| 7212 | // if |
| 7213 | bool HasConstCopyAssignment = true; |
| 7214 | |
| 7215 | // -- each direct base class B of X has a copy assignment operator |
| 7216 | // whose parameter is of type const B&, const volatile B& or B, |
| 7217 | // and |
| 7218 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7219 | BaseEnd = ClassDecl->bases_end(); |
| 7220 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7221 | // We'll handle this below |
| 7222 | if (LangOpts.CPlusPlus0x && Base->isVirtual()) |
| 7223 | continue; |
| 7224 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7225 | assert(!Base->getType()->isDependentType() && |
| 7226 | "Cannot generate implicit members for class with dependent bases."); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7227 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 7228 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 7229 | &HasConstCopyAssignment); |
| 7230 | } |
| 7231 | |
| 7232 | // In C++0x, the above citation has "or virtual added" |
| 7233 | if (LangOpts.CPlusPlus0x) { |
| 7234 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7235 | BaseEnd = ClassDecl->vbases_end(); |
| 7236 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 7237 | assert(!Base->getType()->isDependentType() && |
| 7238 | "Cannot generate implicit members for class with dependent bases."); |
| 7239 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 7240 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 7241 | &HasConstCopyAssignment); |
| 7242 | } |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
| 7245 | // -- for all the nonstatic data members of X that are of a class |
| 7246 | // type M (or array thereof), each such class type has a copy |
| 7247 | // assignment operator whose parameter is of type const M&, |
| 7248 | // const volatile M& or M. |
| 7249 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7250 | FieldEnd = ClassDecl->field_end(); |
| 7251 | HasConstCopyAssignment && Field != FieldEnd; |
| 7252 | ++Field) { |
| 7253 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7254 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 7255 | LookupCopyingAssignment(FieldClassDecl, Qualifiers::Const, false, 0, |
| 7256 | &HasConstCopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7257 | } |
| 7258 | } |
| 7259 | |
| 7260 | // Otherwise, the implicitly declared copy assignment operator will |
| 7261 | // have the form |
| 7262 | // |
| 7263 | // X& X::operator=(X&) |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7264 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7265 | // C++ [except.spec]p14: |
| 7266 | // An implicitly declared special member function (Clause 12) shall have an |
| 7267 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7268 | |
| 7269 | // It is unspecified whether or not an implicit copy assignment operator |
| 7270 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 7271 | // made. As such, this exception specification is effectively unspecified. |
| 7272 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 7273 | // the side of assuming such calls to be made regardless of whether they |
| 7274 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7275 | ImplicitExceptionSpecification ExceptSpec(Context); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7276 | unsigned ArgQuals = HasConstCopyAssignment ? Qualifiers::Const : 0; |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7277 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7278 | BaseEnd = ClassDecl->bases_end(); |
| 7279 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7280 | if (Base->isVirtual()) |
| 7281 | continue; |
| 7282 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7283 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7284 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7285 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 7286 | ArgQuals, false, 0)) |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7287 | ExceptSpec.CalledDecl(CopyAssign); |
| 7288 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7289 | |
| 7290 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7291 | BaseEnd = ClassDecl->vbases_end(); |
| 7292 | Base != BaseEnd; ++Base) { |
| 7293 | CXXRecordDecl *BaseClassDecl |
| 7294 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 7295 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 7296 | ArgQuals, false, 0)) |
| 7297 | ExceptSpec.CalledDecl(CopyAssign); |
| 7298 | } |
| 7299 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7300 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7301 | FieldEnd = ClassDecl->field_end(); |
| 7302 | Field != FieldEnd; |
| 7303 | ++Field) { |
| 7304 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7305 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 7306 | if (CXXMethodDecl *CopyAssign = |
| 7307 | LookupCopyingAssignment(FieldClassDecl, ArgQuals, false, 0)) |
| 7308 | ExceptSpec.CalledDecl(CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7309 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7310 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7311 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7312 | return std::make_pair(ExceptSpec, HasConstCopyAssignment); |
| 7313 | } |
| 7314 | |
| 7315 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 7316 | // Note: The following rules are largely analoguous to the copy |
| 7317 | // constructor rules. Note that virtual bases are not taken into account |
| 7318 | // for determining the argument type of the operator. Note also that |
| 7319 | // operators taking an object instead of a reference are allowed. |
| 7320 | |
| 7321 | ImplicitExceptionSpecification Spec(Context); |
| 7322 | bool Const; |
| 7323 | llvm::tie(Spec, Const) = |
| 7324 | ComputeDefaultedCopyAssignmentExceptionSpecAndConst(ClassDecl); |
| 7325 | |
| 7326 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 7327 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 7328 | if (Const) |
| 7329 | ArgType = ArgType.withConst(); |
| 7330 | ArgType = Context.getLValueReferenceType(ArgType); |
| 7331 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7332 | // An implicitly-declared copy assignment operator is an inline public |
| 7333 | // member of its class. |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7334 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7335 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7336 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 7337 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7338 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7339 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7340 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7341 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7342 | /*StorageClassAsWritten=*/SC_None, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7343 | /*isInline=*/true, /*isConstexpr=*/false, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 7344 | SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7345 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7346 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7347 | CopyAssignment->setImplicit(); |
| 7348 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7349 | |
| 7350 | // Add the parameter to the operator. |
| 7351 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7352 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7353 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7354 | SC_None, |
| 7355 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7356 | CopyAssignment->setParams(FromParam); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7357 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7358 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7359 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7360 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7361 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7362 | PushOnScopeChains(CopyAssignment, S, false); |
| 7363 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7364 | |
Sean Hunt | 1ccbc54 | 2011-06-22 01:05:13 +0000 | [diff] [blame] | 7365 | // C++0x [class.copy]p18: |
| 7366 | // ... If the class definition declares a move constructor or move |
| 7367 | // assignment operator, the implicitly declared copy assignment operator is |
| 7368 | // defined as deleted; ... |
| 7369 | if (ClassDecl->hasUserDeclaredMoveConstructor() || |
| 7370 | ClassDecl->hasUserDeclaredMoveAssignment() || |
| 7371 | ShouldDeleteCopyAssignmentOperator(CopyAssignment)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7372 | CopyAssignment->setDeletedAsWritten(); |
| 7373 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7374 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 7375 | return CopyAssignment; |
| 7376 | } |
| 7377 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7378 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 7379 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7380 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7381 | CopyAssignOperator->isOverloadedOperator() && |
| 7382 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7383 | !CopyAssignOperator->doesThisDeclarationHaveABody()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7384 | "DefineImplicitCopyAssignment called for wrong function"); |
| 7385 | |
| 7386 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 7387 | |
| 7388 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 7389 | CopyAssignOperator->setInvalidDecl(); |
| 7390 | return; |
| 7391 | } |
| 7392 | |
| 7393 | CopyAssignOperator->setUsed(); |
| 7394 | |
| 7395 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7396 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7397 | |
| 7398 | // C++0x [class.copy]p30: |
| 7399 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 7400 | // for a non-union class X performs memberwise copy assignment of its |
| 7401 | // subobjects. The direct base classes of X are assigned first, in the |
| 7402 | // order of their declaration in the base-specifier-list, and then the |
| 7403 | // immediate non-static data members of X are assigned, in the order in |
| 7404 | // which they were declared in the class definition. |
| 7405 | |
| 7406 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7407 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7408 | |
| 7409 | // The parameter for the "other" object, which we are copying from. |
| 7410 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 7411 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 7412 | QualType OtherRefType = Other->getType(); |
| 7413 | if (const LValueReferenceType *OtherRef |
| 7414 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 7415 | OtherRefType = OtherRef->getPointeeType(); |
| 7416 | OtherQuals = OtherRefType.getQualifiers(); |
| 7417 | } |
| 7418 | |
| 7419 | // Our location for everything implicitly-generated. |
| 7420 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 7421 | |
| 7422 | // Construct a reference to the "other" object. We'll be using this |
| 7423 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7424 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7425 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 7426 | |
| 7427 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 7428 | // ASTs. |
| 7429 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 7430 | assert(This && "Reference to this cannot fail!"); |
| 7431 | |
| 7432 | // Assign base classes. |
| 7433 | bool Invalid = false; |
| 7434 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7435 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 7436 | // Form the assignment: |
| 7437 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 7438 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 7439 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7440 | Invalid = true; |
| 7441 | continue; |
| 7442 | } |
| 7443 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7444 | CXXCastPath BasePath; |
| 7445 | BasePath.push_back(Base); |
| 7446 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7447 | // Construct the "from" expression, which is an implicit cast to the |
| 7448 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 7449 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7450 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 7451 | CK_UncheckedDerivedToBase, |
| 7452 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7453 | |
| 7454 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7455 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7456 | |
| 7457 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7458 | To = ImpCastExprToType(To.take(), |
| 7459 | Context.getCVRQualifiedType(BaseType, |
| 7460 | CopyAssignOperator->getTypeQualifiers()), |
| 7461 | CK_UncheckedDerivedToBase, |
| 7462 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7463 | |
| 7464 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7465 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7466 | To.get(), From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7467 | /*CopyingBaseSubobject=*/true, |
| 7468 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7469 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7470 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7471 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7472 | CopyAssignOperator->setInvalidDecl(); |
| 7473 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
| 7476 | // Success! Record the copy. |
| 7477 | Statements.push_back(Copy.takeAs<Expr>()); |
| 7478 | } |
| 7479 | |
| 7480 | // \brief Reference to the __builtin_memcpy function. |
| 7481 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 7482 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7483 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7484 | |
| 7485 | // Assign non-static members. |
| 7486 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7487 | FieldEnd = ClassDecl->field_end(); |
| 7488 | Field != FieldEnd; ++Field) { |
| 7489 | // Check for members of reference type; we can't copy those. |
| 7490 | if (Field->getType()->isReferenceType()) { |
| 7491 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7492 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 7493 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7494 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7495 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7496 | Invalid = true; |
| 7497 | continue; |
| 7498 | } |
| 7499 | |
| 7500 | // Check for members of const-qualified, non-class type. |
| 7501 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 7502 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 7503 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7504 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 7505 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7506 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7507 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7508 | Invalid = true; |
| 7509 | continue; |
| 7510 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 7511 | |
| 7512 | // Suppress assigning zero-width bitfields. |
| 7513 | if (const Expr *Width = Field->getBitWidth()) |
| 7514 | if (Width->EvaluateAsInt(Context) == 0) |
| 7515 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7516 | |
| 7517 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 7518 | if (FieldType->isIncompleteArrayType()) { |
| 7519 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 7520 | "Incomplete array type is not valid"); |
| 7521 | continue; |
| 7522 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7523 | |
| 7524 | // Build references to the field in the object we're copying from and to. |
| 7525 | CXXScopeSpec SS; // Intentionally empty |
| 7526 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 7527 | LookupMemberName); |
| 7528 | MemberLookup.addDecl(*Field); |
| 7529 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7530 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7531 | Loc, /*IsArrow=*/false, |
| 7532 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7533 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7534 | Loc, /*IsArrow=*/true, |
| 7535 | SS, 0, MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7536 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 7537 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 7538 | |
| 7539 | // If the field should be copied with __builtin_memcpy rather than via |
| 7540 | // explicit assignments, do so. This optimization only applies for arrays |
| 7541 | // of scalars and arrays of class type with trivial copy-assignment |
| 7542 | // operators. |
Fariborz Jahanian | 6b167f4 | 2011-08-09 00:26:11 +0000 | [diff] [blame] | 7543 | if (FieldType->isArrayType() && !FieldType.isVolatileQualified() |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7544 | && BaseType.hasTrivialAssignment(Context, /*Copying=*/true)) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7545 | // Compute the size of the memory buffer to be copied. |
| 7546 | QualType SizeType = Context.getSizeType(); |
| 7547 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 7548 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 7549 | for (const ConstantArrayType *Array |
| 7550 | = Context.getAsConstantArrayType(FieldType); |
| 7551 | Array; |
| 7552 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 7553 | llvm::APInt ArraySize |
| 7554 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7555 | Size *= ArraySize; |
| 7556 | } |
| 7557 | |
| 7558 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7559 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 7560 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7561 | |
| 7562 | bool NeedsCollectableMemCpy = |
| 7563 | (BaseType->isRecordType() && |
| 7564 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 7565 | |
| 7566 | if (NeedsCollectableMemCpy) { |
| 7567 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 7568 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 7569 | LookupResult R(*this, |
| 7570 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7571 | Loc, LookupOrdinaryName); |
| 7572 | LookupName(R, TUScope, true); |
| 7573 | |
| 7574 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 7575 | if (!CollectableMemCpy) { |
| 7576 | // Something went horribly wrong earlier, and we will have |
| 7577 | // complained about it. |
| 7578 | Invalid = true; |
| 7579 | continue; |
| 7580 | } |
| 7581 | |
| 7582 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 7583 | CollectableMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7584 | VK_LValue, Loc, 0).take(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7585 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 7586 | } |
| 7587 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7588 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7589 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7590 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 7591 | LookupOrdinaryName); |
| 7592 | LookupName(R, TUScope, true); |
| 7593 | |
| 7594 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 7595 | if (!BuiltinMemCpy) { |
| 7596 | // Something went horribly wrong earlier, and we will have complained |
| 7597 | // about it. |
| 7598 | Invalid = true; |
| 7599 | continue; |
| 7600 | } |
| 7601 | |
| 7602 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 7603 | BuiltinMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7604 | VK_LValue, Loc, 0).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7605 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 7606 | } |
| 7607 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7608 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7609 | CallArgs.push_back(To.takeAs<Expr>()); |
| 7610 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7611 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7612 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7613 | if (NeedsCollectableMemCpy) |
| 7614 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7615 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7616 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7617 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7618 | else |
| 7619 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7620 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7621 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7622 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7623 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7624 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 7625 | Statements.push_back(Call.takeAs<Expr>()); |
| 7626 | continue; |
| 7627 | } |
| 7628 | |
| 7629 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7630 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7631 | To.get(), From.get(), |
| 7632 | /*CopyingBaseSubobject=*/false, |
| 7633 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7634 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7635 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7636 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7637 | CopyAssignOperator->setInvalidDecl(); |
| 7638 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | // Success! Record the copy. |
| 7642 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 7643 | } |
| 7644 | |
| 7645 | if (!Invalid) { |
| 7646 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7647 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7648 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7649 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7650 | if (Return.isInvalid()) |
| 7651 | Invalid = true; |
| 7652 | else { |
| 7653 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7654 | |
| 7655 | if (Trap.hasErrorOccurred()) { |
| 7656 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7657 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7658 | Invalid = true; |
| 7659 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7660 | } |
| 7661 | } |
| 7662 | |
| 7663 | if (Invalid) { |
| 7664 | CopyAssignOperator->setInvalidDecl(); |
| 7665 | return; |
| 7666 | } |
| 7667 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7668 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7669 | /*isStmtExpr=*/false); |
| 7670 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 7671 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7672 | |
| 7673 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7674 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 7675 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7676 | } |
| 7677 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7678 | Sema::ImplicitExceptionSpecification |
| 7679 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXRecordDecl *ClassDecl) { |
| 7680 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 7681 | |
| 7682 | if (ClassDecl->isInvalidDecl()) |
| 7683 | return ExceptSpec; |
| 7684 | |
| 7685 | // C++0x [except.spec]p14: |
| 7686 | // An implicitly declared special member function (Clause 12) shall have an |
| 7687 | // exception-specification. [...] |
| 7688 | |
| 7689 | // It is unspecified whether or not an implicit move assignment operator |
| 7690 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 7691 | // made. As such, this exception specification is effectively unspecified. |
| 7692 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 7693 | // the side of assuming such calls to be made regardless of whether they |
| 7694 | // actually happen. |
| 7695 | // Note that a move constructor is not implicitly declared when there are |
| 7696 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| 7697 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7698 | BaseEnd = ClassDecl->bases_end(); |
| 7699 | Base != BaseEnd; ++Base) { |
| 7700 | if (Base->isVirtual()) |
| 7701 | continue; |
| 7702 | |
| 7703 | CXXRecordDecl *BaseClassDecl |
| 7704 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 7705 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
| 7706 | false, 0)) |
| 7707 | ExceptSpec.CalledDecl(MoveAssign); |
| 7708 | } |
| 7709 | |
| 7710 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7711 | BaseEnd = ClassDecl->vbases_end(); |
| 7712 | Base != BaseEnd; ++Base) { |
| 7713 | CXXRecordDecl *BaseClassDecl |
| 7714 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 7715 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
| 7716 | false, 0)) |
| 7717 | ExceptSpec.CalledDecl(MoveAssign); |
| 7718 | } |
| 7719 | |
| 7720 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7721 | FieldEnd = ClassDecl->field_end(); |
| 7722 | Field != FieldEnd; |
| 7723 | ++Field) { |
| 7724 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 7725 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 7726 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(FieldClassDecl, |
| 7727 | false, 0)) |
| 7728 | ExceptSpec.CalledDecl(MoveAssign); |
| 7729 | } |
| 7730 | } |
| 7731 | |
| 7732 | return ExceptSpec; |
| 7733 | } |
| 7734 | |
| 7735 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
| 7736 | // Note: The following rules are largely analoguous to the move |
| 7737 | // constructor rules. |
| 7738 | |
| 7739 | ImplicitExceptionSpecification Spec( |
| 7740 | ComputeDefaultedMoveAssignmentExceptionSpec(ClassDecl)); |
| 7741 | |
| 7742 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 7743 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 7744 | ArgType = Context.getRValueReferenceType(ArgType); |
| 7745 | |
| 7746 | // An implicitly-declared move assignment operator is an inline public |
| 7747 | // member of its class. |
| 7748 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 7749 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 7750 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 7751 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 7752 | CXXMethodDecl *MoveAssignment |
| 7753 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 7754 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
| 7755 | /*TInfo=*/0, /*isStatic=*/false, |
| 7756 | /*StorageClassAsWritten=*/SC_None, |
| 7757 | /*isInline=*/true, |
| 7758 | /*isConstexpr=*/false, |
| 7759 | SourceLocation()); |
| 7760 | MoveAssignment->setAccess(AS_public); |
| 7761 | MoveAssignment->setDefaulted(); |
| 7762 | MoveAssignment->setImplicit(); |
| 7763 | MoveAssignment->setTrivial(ClassDecl->hasTrivialMoveAssignment()); |
| 7764 | |
| 7765 | // Add the parameter to the operator. |
| 7766 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 7767 | ClassLoc, ClassLoc, /*Id=*/0, |
| 7768 | ArgType, /*TInfo=*/0, |
| 7769 | SC_None, |
| 7770 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7771 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7772 | |
| 7773 | // Note that we have added this copy-assignment operator. |
| 7774 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 7775 | |
| 7776 | // C++0x [class.copy]p9: |
| 7777 | // If the definition of a class X does not explicitly declare a move |
| 7778 | // assignment operator, one will be implicitly declared as defaulted if and |
| 7779 | // only if: |
| 7780 | // [...] |
| 7781 | // - the move assignment operator would not be implicitly defined as |
| 7782 | // deleted. |
| 7783 | if (ShouldDeleteMoveAssignmentOperator(MoveAssignment)) { |
| 7784 | // Cache this result so that we don't try to generate this over and over |
| 7785 | // on every lookup, leaking memory and wasting time. |
| 7786 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 7787 | return 0; |
| 7788 | } |
| 7789 | |
| 7790 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 7791 | PushOnScopeChains(MoveAssignment, S, false); |
| 7792 | ClassDecl->addDecl(MoveAssignment); |
| 7793 | |
| 7794 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 7795 | return MoveAssignment; |
| 7796 | } |
| 7797 | |
| 7798 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 7799 | CXXMethodDecl *MoveAssignOperator) { |
| 7800 | assert((MoveAssignOperator->isDefaulted() && |
| 7801 | MoveAssignOperator->isOverloadedOperator() && |
| 7802 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
| 7803 | !MoveAssignOperator->doesThisDeclarationHaveABody()) && |
| 7804 | "DefineImplicitMoveAssignment called for wrong function"); |
| 7805 | |
| 7806 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 7807 | |
| 7808 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 7809 | MoveAssignOperator->setInvalidDecl(); |
| 7810 | return; |
| 7811 | } |
| 7812 | |
| 7813 | MoveAssignOperator->setUsed(); |
| 7814 | |
| 7815 | ImplicitlyDefinedFunctionScope Scope(*this, MoveAssignOperator); |
| 7816 | DiagnosticErrorTrap Trap(Diags); |
| 7817 | |
| 7818 | // C++0x [class.copy]p28: |
| 7819 | // The implicitly-defined or move assignment operator for a non-union class |
| 7820 | // X performs memberwise move assignment of its subobjects. The direct base |
| 7821 | // classes of X are assigned first, in the order of their declaration in the |
| 7822 | // base-specifier-list, and then the immediate non-static data members of X |
| 7823 | // are assigned, in the order in which they were declared in the class |
| 7824 | // definition. |
| 7825 | |
| 7826 | // The statements that form the synthesized function body. |
| 7827 | ASTOwningVector<Stmt*> Statements(*this); |
| 7828 | |
| 7829 | // The parameter for the "other" object, which we are move from. |
| 7830 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 7831 | QualType OtherRefType = Other->getType()-> |
| 7832 | getAs<RValueReferenceType>()->getPointeeType(); |
| 7833 | assert(OtherRefType.getQualifiers() == 0 && |
| 7834 | "Bad argument type of defaulted move assignment"); |
| 7835 | |
| 7836 | // Our location for everything implicitly-generated. |
| 7837 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 7838 | |
| 7839 | // Construct a reference to the "other" object. We'll be using this |
| 7840 | // throughout the generated ASTs. |
| 7841 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
| 7842 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 7843 | // Cast to rvalue. |
| 7844 | OtherRef = CastForMoving(*this, OtherRef); |
| 7845 | |
| 7846 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 7847 | // ASTs. |
| 7848 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 7849 | assert(This && "Reference to this cannot fail!"); |
| 7850 | |
| 7851 | // Assign base classes. |
| 7852 | bool Invalid = false; |
| 7853 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7854 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 7855 | // Form the assignment: |
| 7856 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| 7857 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 7858 | if (!BaseType->isRecordType()) { |
| 7859 | Invalid = true; |
| 7860 | continue; |
| 7861 | } |
| 7862 | |
| 7863 | CXXCastPath BasePath; |
| 7864 | BasePath.push_back(Base); |
| 7865 | |
| 7866 | // Construct the "from" expression, which is an implicit cast to the |
| 7867 | // appropriately-qualified base type. |
| 7868 | Expr *From = OtherRef; |
| 7869 | From = ImpCastExprToType(From, BaseType, CK_UncheckedDerivedToBase, |
Douglas Gregor | b2b5658 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 7870 | VK_XValue, &BasePath).take(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7871 | |
| 7872 | // Dereference "this". |
| 7873 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 7874 | |
| 7875 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 7876 | To = ImpCastExprToType(To.take(), |
| 7877 | Context.getCVRQualifiedType(BaseType, |
| 7878 | MoveAssignOperator->getTypeQualifiers()), |
| 7879 | CK_UncheckedDerivedToBase, |
| 7880 | VK_LValue, &BasePath); |
| 7881 | |
| 7882 | // Build the move. |
| 7883 | StmtResult Move = BuildSingleCopyAssign(*this, Loc, BaseType, |
| 7884 | To.get(), From, |
| 7885 | /*CopyingBaseSubobject=*/true, |
| 7886 | /*Copying=*/false); |
| 7887 | if (Move.isInvalid()) { |
| 7888 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7889 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 7890 | MoveAssignOperator->setInvalidDecl(); |
| 7891 | return; |
| 7892 | } |
| 7893 | |
| 7894 | // Success! Record the move. |
| 7895 | Statements.push_back(Move.takeAs<Expr>()); |
| 7896 | } |
| 7897 | |
| 7898 | // \brief Reference to the __builtin_memcpy function. |
| 7899 | Expr *BuiltinMemCpyRef = 0; |
| 7900 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
| 7901 | Expr *CollectableMemCpyRef = 0; |
| 7902 | |
| 7903 | // Assign non-static members. |
| 7904 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7905 | FieldEnd = ClassDecl->field_end(); |
| 7906 | Field != FieldEnd; ++Field) { |
| 7907 | // Check for members of reference type; we can't move those. |
| 7908 | if (Field->getType()->isReferenceType()) { |
| 7909 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7910 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 7911 | Diag(Field->getLocation(), diag::note_declared_at); |
| 7912 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7913 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 7914 | Invalid = true; |
| 7915 | continue; |
| 7916 | } |
| 7917 | |
| 7918 | // Check for members of const-qualified, non-class type. |
| 7919 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 7920 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 7921 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7922 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 7923 | Diag(Field->getLocation(), diag::note_declared_at); |
| 7924 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7925 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 7926 | Invalid = true; |
| 7927 | continue; |
| 7928 | } |
| 7929 | |
| 7930 | // Suppress assigning zero-width bitfields. |
| 7931 | if (const Expr *Width = Field->getBitWidth()) |
| 7932 | if (Width->EvaluateAsInt(Context) == 0) |
| 7933 | continue; |
| 7934 | |
| 7935 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 7936 | if (FieldType->isIncompleteArrayType()) { |
| 7937 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 7938 | "Incomplete array type is not valid"); |
| 7939 | continue; |
| 7940 | } |
| 7941 | |
| 7942 | // Build references to the field in the object we're copying from and to. |
| 7943 | CXXScopeSpec SS; // Intentionally empty |
| 7944 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 7945 | LookupMemberName); |
| 7946 | MemberLookup.addDecl(*Field); |
| 7947 | MemberLookup.resolveKind(); |
| 7948 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
| 7949 | Loc, /*IsArrow=*/false, |
| 7950 | SS, 0, MemberLookup, 0); |
| 7951 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
| 7952 | Loc, /*IsArrow=*/true, |
| 7953 | SS, 0, MemberLookup, 0); |
| 7954 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 7955 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 7956 | |
| 7957 | assert(!From.get()->isLValue() && // could be xvalue or prvalue |
| 7958 | "Member reference with rvalue base must be rvalue except for reference " |
| 7959 | "members, which aren't allowed for move assignment."); |
| 7960 | |
| 7961 | // If the field should be copied with __builtin_memcpy rather than via |
| 7962 | // explicit assignments, do so. This optimization only applies for arrays |
| 7963 | // of scalars and arrays of class type with trivial move-assignment |
| 7964 | // operators. |
| 7965 | if (FieldType->isArrayType() && !FieldType.isVolatileQualified() |
| 7966 | && BaseType.hasTrivialAssignment(Context, /*Copying=*/false)) { |
| 7967 | // Compute the size of the memory buffer to be copied. |
| 7968 | QualType SizeType = Context.getSizeType(); |
| 7969 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 7970 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 7971 | for (const ConstantArrayType *Array |
| 7972 | = Context.getAsConstantArrayType(FieldType); |
| 7973 | Array; |
| 7974 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
| 7975 | llvm::APInt ArraySize |
| 7976 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
| 7977 | Size *= ArraySize; |
| 7978 | } |
| 7979 | |
Douglas Gregor | 45d3d71 | 2011-09-01 02:09:07 +0000 | [diff] [blame] | 7980 | // Take the address of the field references for "from" and "to". We |
| 7981 | // directly construct UnaryOperators here because semantic analysis |
| 7982 | // does not permit us to take the address of an xvalue. |
| 7983 | From = new (Context) UnaryOperator(From.get(), UO_AddrOf, |
| 7984 | Context.getPointerType(From.get()->getType()), |
| 7985 | VK_RValue, OK_Ordinary, Loc); |
| 7986 | To = new (Context) UnaryOperator(To.get(), UO_AddrOf, |
| 7987 | Context.getPointerType(To.get()->getType()), |
| 7988 | VK_RValue, OK_Ordinary, Loc); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7989 | |
| 7990 | bool NeedsCollectableMemCpy = |
| 7991 | (BaseType->isRecordType() && |
| 7992 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 7993 | |
| 7994 | if (NeedsCollectableMemCpy) { |
| 7995 | if (!CollectableMemCpyRef) { |
| 7996 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 7997 | LookupResult R(*this, |
| 7998 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
| 7999 | Loc, LookupOrdinaryName); |
| 8000 | LookupName(R, TUScope, true); |
| 8001 | |
| 8002 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 8003 | if (!CollectableMemCpy) { |
| 8004 | // Something went horribly wrong earlier, and we will have |
| 8005 | // complained about it. |
| 8006 | Invalid = true; |
| 8007 | continue; |
| 8008 | } |
| 8009 | |
| 8010 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 8011 | CollectableMemCpy->getType(), |
| 8012 | VK_LValue, Loc, 0).take(); |
| 8013 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 8014 | } |
| 8015 | } |
| 8016 | // Create a reference to the __builtin_memcpy builtin function. |
| 8017 | else if (!BuiltinMemCpyRef) { |
| 8018 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 8019 | LookupOrdinaryName); |
| 8020 | LookupName(R, TUScope, true); |
| 8021 | |
| 8022 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 8023 | if (!BuiltinMemCpy) { |
| 8024 | // Something went horribly wrong earlier, and we will have complained |
| 8025 | // about it. |
| 8026 | Invalid = true; |
| 8027 | continue; |
| 8028 | } |
| 8029 | |
| 8030 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 8031 | BuiltinMemCpy->getType(), |
| 8032 | VK_LValue, Loc, 0).take(); |
| 8033 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 8034 | } |
| 8035 | |
| 8036 | ASTOwningVector<Expr*> CallArgs(*this); |
| 8037 | CallArgs.push_back(To.takeAs<Expr>()); |
| 8038 | CallArgs.push_back(From.takeAs<Expr>()); |
| 8039 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
| 8040 | ExprResult Call = ExprError(); |
| 8041 | if (NeedsCollectableMemCpy) |
| 8042 | Call = ActOnCallExpr(/*Scope=*/0, |
| 8043 | CollectableMemCpyRef, |
| 8044 | Loc, move_arg(CallArgs), |
| 8045 | Loc); |
| 8046 | else |
| 8047 | Call = ActOnCallExpr(/*Scope=*/0, |
| 8048 | BuiltinMemCpyRef, |
| 8049 | Loc, move_arg(CallArgs), |
| 8050 | Loc); |
| 8051 | |
| 8052 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 8053 | Statements.push_back(Call.takeAs<Expr>()); |
| 8054 | continue; |
| 8055 | } |
| 8056 | |
| 8057 | // Build the move of this field. |
| 8058 | StmtResult Move = BuildSingleCopyAssign(*this, Loc, FieldType, |
| 8059 | To.get(), From.get(), |
| 8060 | /*CopyingBaseSubobject=*/false, |
| 8061 | /*Copying=*/false); |
| 8062 | if (Move.isInvalid()) { |
| 8063 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8064 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8065 | MoveAssignOperator->setInvalidDecl(); |
| 8066 | return; |
| 8067 | } |
| 8068 | |
| 8069 | // Success! Record the copy. |
| 8070 | Statements.push_back(Move.takeAs<Stmt>()); |
| 8071 | } |
| 8072 | |
| 8073 | if (!Invalid) { |
| 8074 | // Add a "return *this;" |
| 8075 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 8076 | |
| 8077 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 8078 | if (Return.isInvalid()) |
| 8079 | Invalid = true; |
| 8080 | else { |
| 8081 | Statements.push_back(Return.takeAs<Stmt>()); |
| 8082 | |
| 8083 | if (Trap.hasErrorOccurred()) { |
| 8084 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8085 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8086 | Invalid = true; |
| 8087 | } |
| 8088 | } |
| 8089 | } |
| 8090 | |
| 8091 | if (Invalid) { |
| 8092 | MoveAssignOperator->setInvalidDecl(); |
| 8093 | return; |
| 8094 | } |
| 8095 | |
| 8096 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
| 8097 | /*isStmtExpr=*/false); |
| 8098 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 8099 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 8100 | |
| 8101 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8102 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 8103 | } |
| 8104 | } |
| 8105 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8106 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 8107 | Sema::ComputeDefaultedCopyCtorExceptionSpecAndConst(CXXRecordDecl *ClassDecl) { |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8108 | if (ClassDecl->isInvalidDecl()) |
| 8109 | return std::make_pair(ImplicitExceptionSpecification(Context), false); |
| 8110 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8111 | // C++ [class.copy]p5: |
| 8112 | // The implicitly-declared copy constructor for a class X will |
| 8113 | // have the form |
| 8114 | // |
| 8115 | // X::X(const X&) |
| 8116 | // |
| 8117 | // if |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8118 | // FIXME: It ought to be possible to store this on the record. |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8119 | bool HasConstCopyConstructor = true; |
| 8120 | |
| 8121 | // -- each direct or virtual base class B of X has a copy |
| 8122 | // constructor whose first parameter is of type const B& or |
| 8123 | // const volatile B&, and |
| 8124 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8125 | BaseEnd = ClassDecl->bases_end(); |
| 8126 | HasConstCopyConstructor && Base != BaseEnd; |
| 8127 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8128 | // Virtual bases are handled below. |
| 8129 | if (Base->isVirtual()) |
| 8130 | continue; |
| 8131 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8132 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8133 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8134 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 8135 | &HasConstCopyConstructor); |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8139 | BaseEnd = ClassDecl->vbases_end(); |
| 8140 | HasConstCopyConstructor && Base != BaseEnd; |
| 8141 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8142 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8143 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8144 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 8145 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8146 | } |
| 8147 | |
| 8148 | // -- for all the nonstatic data members of X that are of a |
| 8149 | // class type M (or array thereof), each such class type |
| 8150 | // has a copy constructor whose first parameter is of type |
| 8151 | // const M& or const volatile M&. |
| 8152 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8153 | FieldEnd = ClassDecl->field_end(); |
| 8154 | HasConstCopyConstructor && Field != FieldEnd; |
| 8155 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8156 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8157 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8158 | LookupCopyingConstructor(FieldClassDecl, Qualifiers::Const, |
| 8159 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8160 | } |
| 8161 | } |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8162 | // Otherwise, the implicitly declared copy constructor will have |
| 8163 | // the form |
| 8164 | // |
| 8165 | // X::X(X&) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8166 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8167 | // C++ [except.spec]p14: |
| 8168 | // An implicitly declared special member function (Clause 12) shall have an |
| 8169 | // exception-specification. [...] |
| 8170 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 8171 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 8172 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8173 | BaseEnd = ClassDecl->bases_end(); |
| 8174 | Base != BaseEnd; |
| 8175 | ++Base) { |
| 8176 | // Virtual bases are handled below. |
| 8177 | if (Base->isVirtual()) |
| 8178 | continue; |
| 8179 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8180 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8181 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8182 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8183 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8184 | ExceptSpec.CalledDecl(CopyConstructor); |
| 8185 | } |
| 8186 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8187 | BaseEnd = ClassDecl->vbases_end(); |
| 8188 | Base != BaseEnd; |
| 8189 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8190 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8191 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8192 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8193 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8194 | ExceptSpec.CalledDecl(CopyConstructor); |
| 8195 | } |
| 8196 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8197 | FieldEnd = ClassDecl->field_end(); |
| 8198 | Field != FieldEnd; |
| 8199 | ++Field) { |
| 8200 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8201 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 8202 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8203 | LookupCopyingConstructor(FieldClassDecl, Quals)) |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8204 | ExceptSpec.CalledDecl(CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8205 | } |
| 8206 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8207 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8208 | return std::make_pair(ExceptSpec, HasConstCopyConstructor); |
| 8209 | } |
| 8210 | |
| 8211 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 8212 | CXXRecordDecl *ClassDecl) { |
| 8213 | // C++ [class.copy]p4: |
| 8214 | // If the class definition does not explicitly declare a copy |
| 8215 | // constructor, one is declared implicitly. |
| 8216 | |
| 8217 | ImplicitExceptionSpecification Spec(Context); |
| 8218 | bool Const; |
| 8219 | llvm::tie(Spec, Const) = |
| 8220 | ComputeDefaultedCopyCtorExceptionSpecAndConst(ClassDecl); |
| 8221 | |
| 8222 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 8223 | QualType ArgType = ClassType; |
| 8224 | if (Const) |
| 8225 | ArgType = ArgType.withConst(); |
| 8226 | ArgType = Context.getLValueReferenceType(ArgType); |
| 8227 | |
| 8228 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 8229 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8230 | DeclarationName Name |
| 8231 | = Context.DeclarationNames.getCXXConstructorName( |
| 8232 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8233 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8234 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8235 | |
| 8236 | // An implicitly-declared copy constructor is an inline public |
| 8237 | // member of its class. |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8238 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8239 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8240 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8241 | &ArgType, 1, EPI), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8242 | /*TInfo=*/0, |
| 8243 | /*isExplicit=*/false, |
| 8244 | /*isInline=*/true, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 8245 | /*isImplicitlyDeclared=*/true, |
| 8246 | // FIXME: apply the rules for definitions here |
| 8247 | /*isConstexpr=*/false); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8248 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8249 | CopyConstructor->setDefaulted(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8250 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 8251 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8252 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8253 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 8254 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8255 | // Add the parameter to the constructor. |
| 8256 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8257 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8258 | /*IdentifierInfo=*/0, |
| 8259 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8260 | SC_None, |
| 8261 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8262 | CopyConstructor->setParams(FromParam); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8263 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8264 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8265 | PushOnScopeChains(CopyConstructor, S, false); |
| 8266 | ClassDecl->addDecl(CopyConstructor); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8267 | |
Sean Hunt | 1ccbc54 | 2011-06-22 01:05:13 +0000 | [diff] [blame] | 8268 | // C++0x [class.copy]p7: |
| 8269 | // ... If the class definition declares a move constructor or move |
| 8270 | // assignment operator, the implicitly declared constructor is defined as |
| 8271 | // deleted; ... |
| 8272 | if (ClassDecl->hasUserDeclaredMoveConstructor() || |
| 8273 | ClassDecl->hasUserDeclaredMoveAssignment() || |
| 8274 | ShouldDeleteCopyConstructor(CopyConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8275 | CopyConstructor->setDeletedAsWritten(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8276 | |
| 8277 | return CopyConstructor; |
| 8278 | } |
| 8279 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8280 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8281 | CXXConstructorDecl *CopyConstructor) { |
| 8282 | assert((CopyConstructor->isDefaulted() && |
| 8283 | CopyConstructor->isCopyConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8284 | !CopyConstructor->doesThisDeclarationHaveABody()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8285 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8286 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 8287 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8288 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8289 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 8290 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8291 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8292 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 8293 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8294 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 8295 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8296 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 8297 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8298 | } else { |
| 8299 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 8300 | CopyConstructor->getLocation(), |
| 8301 | MultiStmtArg(*this, 0, 0), |
| 8302 | /*isStmtExpr=*/false) |
| 8303 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame^] | 8304 | CopyConstructor->setImplicitlyDefined(true); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 8305 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8306 | |
| 8307 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8308 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8309 | L->CompletedImplicitDefinition(CopyConstructor); |
| 8310 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8311 | } |
| 8312 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8313 | Sema::ImplicitExceptionSpecification |
| 8314 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
| 8315 | // C++ [except.spec]p14: |
| 8316 | // An implicitly declared special member function (Clause 12) shall have an |
| 8317 | // exception-specification. [...] |
| 8318 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 8319 | if (ClassDecl->isInvalidDecl()) |
| 8320 | return ExceptSpec; |
| 8321 | |
| 8322 | // Direct base-class constructors. |
| 8323 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8324 | BEnd = ClassDecl->bases_end(); |
| 8325 | B != BEnd; ++B) { |
| 8326 | if (B->isVirtual()) // Handled below. |
| 8327 | continue; |
| 8328 | |
| 8329 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8330 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8331 | CXXConstructorDecl *Constructor = LookupMovingConstructor(BaseClassDecl); |
| 8332 | // If this is a deleted function, add it anyway. This might be conformant |
| 8333 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8334 | if (Constructor) |
| 8335 | ExceptSpec.CalledDecl(Constructor); |
| 8336 | } |
| 8337 | } |
| 8338 | |
| 8339 | // Virtual base-class constructors. |
| 8340 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8341 | BEnd = ClassDecl->vbases_end(); |
| 8342 | B != BEnd; ++B) { |
| 8343 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8344 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8345 | CXXConstructorDecl *Constructor = LookupMovingConstructor(BaseClassDecl); |
| 8346 | // If this is a deleted function, add it anyway. This might be conformant |
| 8347 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8348 | if (Constructor) |
| 8349 | ExceptSpec.CalledDecl(Constructor); |
| 8350 | } |
| 8351 | } |
| 8352 | |
| 8353 | // Field constructors. |
| 8354 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8355 | FEnd = ClassDecl->field_end(); |
| 8356 | F != FEnd; ++F) { |
| 8357 | if (F->hasInClassInitializer()) { |
| 8358 | if (Expr *E = F->getInClassInitializer()) |
| 8359 | ExceptSpec.CalledExpr(E); |
| 8360 | else if (!F->isInvalidDecl()) |
| 8361 | ExceptSpec.SetDelayed(); |
| 8362 | } else if (const RecordType *RecordTy |
| 8363 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 8364 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8365 | CXXConstructorDecl *Constructor = LookupMovingConstructor(FieldRecDecl); |
| 8366 | // If this is a deleted function, add it anyway. This might be conformant |
| 8367 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8368 | // In particular, the problem is that this function never gets called. It |
| 8369 | // might just be ill-formed because this function attempts to refer to |
| 8370 | // a deleted function here. |
| 8371 | if (Constructor) |
| 8372 | ExceptSpec.CalledDecl(Constructor); |
| 8373 | } |
| 8374 | } |
| 8375 | |
| 8376 | return ExceptSpec; |
| 8377 | } |
| 8378 | |
| 8379 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 8380 | CXXRecordDecl *ClassDecl) { |
| 8381 | ImplicitExceptionSpecification Spec( |
| 8382 | ComputeDefaultedMoveCtorExceptionSpec(ClassDecl)); |
| 8383 | |
| 8384 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 8385 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
| 8386 | |
| 8387 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 8388 | |
| 8389 | DeclarationName Name |
| 8390 | = Context.DeclarationNames.getCXXConstructorName( |
| 8391 | Context.getCanonicalType(ClassType)); |
| 8392 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8393 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 8394 | |
| 8395 | // C++0x [class.copy]p11: |
| 8396 | // An implicitly-declared copy/move constructor is an inline public |
| 8397 | // member of its class. |
| 8398 | CXXConstructorDecl *MoveConstructor |
| 8399 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 8400 | Context.getFunctionType(Context.VoidTy, |
| 8401 | &ArgType, 1, EPI), |
| 8402 | /*TInfo=*/0, |
| 8403 | /*isExplicit=*/false, |
| 8404 | /*isInline=*/true, |
| 8405 | /*isImplicitlyDeclared=*/true, |
| 8406 | // FIXME: apply the rules for definitions here |
| 8407 | /*isConstexpr=*/false); |
| 8408 | MoveConstructor->setAccess(AS_public); |
| 8409 | MoveConstructor->setDefaulted(); |
| 8410 | MoveConstructor->setTrivial(ClassDecl->hasTrivialMoveConstructor()); |
| 8411 | |
| 8412 | // Add the parameter to the constructor. |
| 8413 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 8414 | ClassLoc, ClassLoc, |
| 8415 | /*IdentifierInfo=*/0, |
| 8416 | ArgType, /*TInfo=*/0, |
| 8417 | SC_None, |
| 8418 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8419 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8420 | |
| 8421 | // C++0x [class.copy]p9: |
| 8422 | // If the definition of a class X does not explicitly declare a move |
| 8423 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 8424 | // [...] |
| 8425 | // - the move constructor would not be implicitly defined as deleted. |
| 8426 | if (ShouldDeleteMoveConstructor(MoveConstructor)) { |
| 8427 | // Cache this result so that we don't try to generate this over and over |
| 8428 | // on every lookup, leaking memory and wasting time. |
| 8429 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 8430 | return 0; |
| 8431 | } |
| 8432 | |
| 8433 | // Note that we have declared this constructor. |
| 8434 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 8435 | |
| 8436 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8437 | PushOnScopeChains(MoveConstructor, S, false); |
| 8438 | ClassDecl->addDecl(MoveConstructor); |
| 8439 | |
| 8440 | return MoveConstructor; |
| 8441 | } |
| 8442 | |
| 8443 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 8444 | CXXConstructorDecl *MoveConstructor) { |
| 8445 | assert((MoveConstructor->isDefaulted() && |
| 8446 | MoveConstructor->isMoveConstructor() && |
| 8447 | !MoveConstructor->doesThisDeclarationHaveABody()) && |
| 8448 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 8449 | |
| 8450 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 8451 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 8452 | |
| 8453 | ImplicitlyDefinedFunctionScope Scope(*this, MoveConstructor); |
| 8454 | DiagnosticErrorTrap Trap(Diags); |
| 8455 | |
| 8456 | if (SetCtorInitializers(MoveConstructor, 0, 0, /*AnyErrors=*/false) || |
| 8457 | Trap.hasErrorOccurred()) { |
| 8458 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8459 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 8460 | MoveConstructor->setInvalidDecl(); |
| 8461 | } else { |
| 8462 | MoveConstructor->setBody(ActOnCompoundStmt(MoveConstructor->getLocation(), |
| 8463 | MoveConstructor->getLocation(), |
| 8464 | MultiStmtArg(*this, 0, 0), |
| 8465 | /*isStmtExpr=*/false) |
| 8466 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame^] | 8467 | MoveConstructor->setImplicitlyDefined(true); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8468 | } |
| 8469 | |
| 8470 | MoveConstructor->setUsed(); |
| 8471 | |
| 8472 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8473 | L->CompletedImplicitDefinition(MoveConstructor); |
| 8474 | } |
| 8475 | } |
| 8476 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8477 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 8478 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8479 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8480 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8481 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8482 | unsigned ConstructKind, |
| 8483 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 8484 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8485 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 8486 | // C++0x [class.copy]p34: |
| 8487 | // When certain criteria are met, an implementation is allowed to |
| 8488 | // omit the copy/move construction of a class object, even if the |
| 8489 | // copy/move constructor and/or destructor for the object have |
| 8490 | // side effects. [...] |
| 8491 | // - when a temporary class object that has not been bound to a |
| 8492 | // reference (12.2) would be copied/moved to a class object |
| 8493 | // with the same cv-unqualified type, the copy/move operation |
| 8494 | // can be omitted by constructing the temporary object |
| 8495 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 8496 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | 70a21de | 2011-01-27 23:24:55 +0000 | [diff] [blame] | 8497 | Constructor->isCopyOrMoveConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 8498 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 8499 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 8500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8501 | |
| 8502 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8503 | Elidable, move(ExprArgs), RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8504 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 8505 | } |
| 8506 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 8507 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 8508 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8509 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 8510 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 8511 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8512 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8513 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8514 | unsigned ConstructKind, |
| 8515 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 8516 | unsigned NumExprs = ExprArgs.size(); |
| 8517 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8518 | |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 8519 | for (specific_attr_iterator<NonNullAttr> |
| 8520 | i = Constructor->specific_attr_begin<NonNullAttr>(), |
| 8521 | e = Constructor->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 8522 | const NonNullAttr *NonNull = *i; |
| 8523 | CheckNonNullArguments(NonNull, ExprArgs.get(), ConstructLoc); |
| 8524 | } |
| 8525 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 8526 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8527 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8528 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 8529 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8530 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 8531 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 8532 | } |
| 8533 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8534 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 8535 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 8536 | MultiExprArg Exprs) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8537 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8538 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 8539 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8540 | move(Exprs), false, CXXConstructExpr::CK_Complete, |
| 8541 | SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 8542 | if (TempResult.isInvalid()) |
| 8543 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8544 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 8545 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 8546 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 8547 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 8548 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 8549 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8550 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 8551 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 8552 | } |
| 8553 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 8554 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8555 | if (VD->isInvalidDecl()) return; |
| 8556 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 8557 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8558 | if (ClassDecl->isInvalidDecl()) return; |
| 8559 | if (ClassDecl->hasTrivialDestructor()) return; |
| 8560 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 8561 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8562 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 8563 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
| 8564 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 8565 | PDiag(diag::err_access_dtor_var) |
| 8566 | << VD->getDeclName() |
| 8567 | << VD->getType()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 8568 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8569 | if (!VD->hasGlobalStorage()) return; |
| 8570 | |
| 8571 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 8572 | // class-static, function-static). |
| 8573 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 8574 | |
| 8575 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 8576 | if (!VD->isStaticLocal()) |
| 8577 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8578 | } |
| 8579 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8580 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8581 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 8582 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8583 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 8584 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8585 | MultiExprArg Exprs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8586 | SourceLocation RParenLoc, |
| 8587 | bool TypeMayContainAuto) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 8588 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8589 | |
| 8590 | // If there is no declaration, there was an error parsing it. Just ignore |
| 8591 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 8592 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8593 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8594 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8595 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 8596 | if (!VDecl) { |
| 8597 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 8598 | RealDecl->setInvalidDecl(); |
| 8599 | return; |
| 8600 | } |
| 8601 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8602 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 8603 | if (TypeMayContainAuto && VDecl->getType()->getContainedAutoType()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8604 | // FIXME: n3225 doesn't actually seem to indicate this is ill-formed |
| 8605 | if (Exprs.size() > 1) { |
| 8606 | Diag(Exprs.get()[1]->getSourceRange().getBegin(), |
| 8607 | diag::err_auto_var_init_multiple_expressions) |
| 8608 | << VDecl->getDeclName() << VDecl->getType() |
| 8609 | << VDecl->getSourceRange(); |
| 8610 | RealDecl->setInvalidDecl(); |
| 8611 | return; |
| 8612 | } |
| 8613 | |
| 8614 | Expr *Init = Exprs.get()[0]; |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 8615 | TypeSourceInfo *DeducedType = 0; |
| 8616 | if (!DeduceAutoType(VDecl->getTypeSourceInfo(), Init, DeducedType)) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8617 | Diag(VDecl->getLocation(), diag::err_auto_var_deduction_failure) |
| 8618 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 8619 | << Init->getSourceRange(); |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 8620 | if (!DeducedType) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8621 | RealDecl->setInvalidDecl(); |
| 8622 | return; |
| 8623 | } |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 8624 | VDecl->setTypeSourceInfo(DeducedType); |
| 8625 | VDecl->setType(DeducedType->getType()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8626 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8627 | // In ARC, infer lifetime. |
| 8628 | if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
| 8629 | VDecl->setInvalidDecl(); |
| 8630 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 8631 | // If this is a redeclaration, check that the type we just deduced matches |
| 8632 | // the previously declared type. |
| 8633 | if (VarDecl *Old = VDecl->getPreviousDeclaration()) |
| 8634 | MergeVarDeclTypes(VDecl, Old); |
| 8635 | } |
| 8636 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 8637 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 8638 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8639 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 8640 | // |
| 8641 | // Clients that want to distinguish between the two forms, can check for |
| 8642 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 8643 | // A major benefit is that clients that don't particularly care about which |
| 8644 | // exactly form was it (like the CodeGen) can handle both cases without |
| 8645 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 8646 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8647 | // C++ 8.5p11: |
| 8648 | // The form of initialization (using parentheses or '=') is generally |
| 8649 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 8650 | // class type. |
| 8651 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 8652 | if (!VDecl->getType()->isDependentType() && |
| 8653 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 8654 | diag::err_typecheck_decl_incomplete_type)) { |
| 8655 | VDecl->setInvalidDecl(); |
| 8656 | return; |
| 8657 | } |
| 8658 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 8659 | // The variable can not have an abstract class type. |
| 8660 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 8661 | diag::err_abstract_type_in_decl, |
| 8662 | AbstractVariableType)) |
| 8663 | VDecl->setInvalidDecl(); |
| 8664 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 8665 | const VarDecl *Def; |
| 8666 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 8667 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 8668 | << VDecl->getDeclName(); |
| 8669 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 8670 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 8671 | return; |
| 8672 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 8673 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 8674 | // C++ [class.static.data]p4 |
| 8675 | // If a static data member is of const integral or const |
| 8676 | // enumeration type, its declaration in the class definition can |
| 8677 | // specify a constant-initializer which shall be an integral |
| 8678 | // constant expression (5.19). In that case, the member can appear |
| 8679 | // in integral constant expressions. The member shall still be |
| 8680 | // defined in a namespace scope if it is used in the program and the |
| 8681 | // namespace scope definition shall not contain an initializer. |
| 8682 | // |
| 8683 | // We already performed a redefinition check above, but for static |
| 8684 | // data members we also need to check whether there was an in-class |
| 8685 | // declaration with an initializer. |
| 8686 | const VarDecl* PrevInit = 0; |
| 8687 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 8688 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 8689 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 8690 | return; |
| 8691 | } |
| 8692 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8693 | bool IsDependent = false; |
| 8694 | for (unsigned I = 0, N = Exprs.size(); I != N; ++I) { |
| 8695 | if (DiagnoseUnexpandedParameterPack(Exprs.get()[I], UPPC_Expression)) { |
| 8696 | VDecl->setInvalidDecl(); |
| 8697 | return; |
| 8698 | } |
| 8699 | |
| 8700 | if (Exprs.get()[I]->isTypeDependent()) |
| 8701 | IsDependent = true; |
| 8702 | } |
| 8703 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 8704 | // If either the declaration has a dependent type or if any of the |
| 8705 | // expressions is type-dependent, we represent the initialization |
| 8706 | // via a ParenListExpr for later use during template instantiation. |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 8707 | if (VDecl->getType()->isDependentType() || IsDependent) { |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 8708 | // Let clients know that initialization was done with a direct initializer. |
| 8709 | VDecl->setCXXDirectInitializer(true); |
| 8710 | |
| 8711 | // Store the initialization expressions as a ParenListExpr. |
| 8712 | unsigned NumExprs = Exprs.size(); |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 8713 | VDecl->setInit(new (Context) ParenListExpr( |
| 8714 | Context, LParenLoc, (Expr **)Exprs.release(), NumExprs, RParenLoc, |
| 8715 | VDecl->getType().getNonReferenceType())); |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 8716 | return; |
| 8717 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 8718 | |
| 8719 | // Capture the variable that is being initialized and the style of |
| 8720 | // initialization. |
| 8721 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 8722 | |
| 8723 | // FIXME: Poor source location information. |
| 8724 | InitializationKind Kind |
| 8725 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 8726 | LParenLoc, RParenLoc); |
| 8727 | |
| 8728 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8729 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8730 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 8731 | if (Result.isInvalid()) { |
| 8732 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8733 | return; |
| 8734 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 8735 | |
| 8736 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 8737 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 8738 | Result = MaybeCreateExprWithCleanups(Result); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 8739 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8740 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 8741 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 8742 | CheckCompleteVariableDeclaration(VDecl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 8743 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 8744 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8745 | /// \brief Given a constructor and the set of arguments provided for the |
| 8746 | /// constructor, convert the arguments and add any required default arguments |
| 8747 | /// to form a proper call to this constructor. |
| 8748 | /// |
| 8749 | /// \returns true if an error occurred, false otherwise. |
| 8750 | bool |
| 8751 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 8752 | MultiExprArg ArgsPtr, |
| 8753 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 8754 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8755 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 8756 | unsigned NumArgs = ArgsPtr.size(); |
| 8757 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 8758 | |
| 8759 | const FunctionProtoType *Proto |
| 8760 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 8761 | assert(Proto && "Constructor without a prototype?"); |
| 8762 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8763 | |
| 8764 | // 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] | 8765 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8766 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 8767 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 8768 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 8769 | |
| 8770 | VariadicCallType CallType = |
| 8771 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8772 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 8773 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 8774 | Proto, 0, Args, NumArgs, AllArgs, |
| 8775 | CallType); |
| 8776 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 8777 | ConvertedArgs.push_back(AllArgs[i]); |
| 8778 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 8779 | } |
| 8780 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 8781 | static inline bool |
| 8782 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 8783 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8784 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 8785 | if (isa<NamespaceDecl>(DC)) { |
| 8786 | return SemaRef.Diag(FnDecl->getLocation(), |
| 8787 | diag::err_operator_new_delete_declared_in_namespace) |
| 8788 | << FnDecl->getDeclName(); |
| 8789 | } |
| 8790 | |
| 8791 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8792 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 8793 | return SemaRef.Diag(FnDecl->getLocation(), |
| 8794 | diag::err_operator_new_delete_declared_static) |
| 8795 | << FnDecl->getDeclName(); |
| 8796 | } |
| 8797 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 8798 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 8799 | } |
| 8800 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8801 | static inline bool |
| 8802 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 8803 | CanQualType ExpectedResultType, |
| 8804 | CanQualType ExpectedFirstParamType, |
| 8805 | unsigned DependentParamTypeDiag, |
| 8806 | unsigned InvalidParamTypeDiag) { |
| 8807 | QualType ResultType = |
| 8808 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 8809 | |
| 8810 | // Check that the result type is not dependent. |
| 8811 | if (ResultType->isDependentType()) |
| 8812 | return SemaRef.Diag(FnDecl->getLocation(), |
| 8813 | diag::err_operator_new_delete_dependent_result_type) |
| 8814 | << FnDecl->getDeclName() << ExpectedResultType; |
| 8815 | |
| 8816 | // Check that the result type is what we expect. |
| 8817 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 8818 | return SemaRef.Diag(FnDecl->getLocation(), |
| 8819 | diag::err_operator_new_delete_invalid_result_type) |
| 8820 | << FnDecl->getDeclName() << ExpectedResultType; |
| 8821 | |
| 8822 | // A function template must have at least 2 parameters. |
| 8823 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 8824 | return SemaRef.Diag(FnDecl->getLocation(), |
| 8825 | diag::err_operator_new_delete_template_too_few_parameters) |
| 8826 | << FnDecl->getDeclName(); |
| 8827 | |
| 8828 | // The function decl must have at least 1 parameter. |
| 8829 | if (FnDecl->getNumParams() == 0) |
| 8830 | return SemaRef.Diag(FnDecl->getLocation(), |
| 8831 | diag::err_operator_new_delete_too_few_parameters) |
| 8832 | << FnDecl->getDeclName(); |
| 8833 | |
| 8834 | // Check the the first parameter type is not dependent. |
| 8835 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 8836 | if (FirstParamType->isDependentType()) |
| 8837 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 8838 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 8839 | |
| 8840 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 8841 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8842 | ExpectedFirstParamType) |
| 8843 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 8844 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 8845 | |
| 8846 | return false; |
| 8847 | } |
| 8848 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 8849 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8850 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 8851 | // C++ [basic.stc.dynamic.allocation]p1: |
| 8852 | // A program is ill-formed if an allocation function is declared in a |
| 8853 | // namespace scope other than global scope or declared static in global |
| 8854 | // scope. |
| 8855 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 8856 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8857 | |
| 8858 | CanQualType SizeTy = |
| 8859 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 8860 | |
| 8861 | // C++ [basic.stc.dynamic.allocation]p1: |
| 8862 | // The return type shall be void*. The first parameter shall have type |
| 8863 | // std::size_t. |
| 8864 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 8865 | SizeTy, |
| 8866 | diag::err_operator_new_dependent_param_type, |
| 8867 | diag::err_operator_new_param_type)) |
| 8868 | return true; |
| 8869 | |
| 8870 | // C++ [basic.stc.dynamic.allocation]p1: |
| 8871 | // The first parameter shall not have an associated default argument. |
| 8872 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 8873 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8874 | diag::err_operator_new_default_arg) |
| 8875 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 8876 | |
| 8877 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 8878 | } |
| 8879 | |
| 8880 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 8881 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 8882 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 8883 | // A program is ill-formed if deallocation functions are declared in a |
| 8884 | // namespace scope other than global scope or declared static in global |
| 8885 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 8886 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 8887 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 8888 | |
| 8889 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 8890 | // Each deallocation function shall return void and its first parameter |
| 8891 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8892 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 8893 | SemaRef.Context.VoidPtrTy, |
| 8894 | diag::err_operator_delete_dependent_param_type, |
| 8895 | diag::err_operator_delete_param_type)) |
| 8896 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 8897 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 8898 | return false; |
| 8899 | } |
| 8900 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8901 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 8902 | /// of this overloaded operator is well-formed. If so, returns false; |
| 8903 | /// otherwise, emits appropriate diagnostics and returns true. |
| 8904 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 8905 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8906 | "Expected an overloaded operator declaration"); |
| 8907 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8908 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 8909 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8910 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8911 | // The allocation and deallocation functions, operator new, |
| 8912 | // operator new[], operator delete and operator delete[], are |
| 8913 | // described completely in 3.7.3. The attributes and restrictions |
| 8914 | // found in the rest of this subclause do not apply to them unless |
| 8915 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 8916 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 8917 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 8918 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 8919 | if (Op == OO_New || Op == OO_Array_New) |
| 8920 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8921 | |
| 8922 | // C++ [over.oper]p6: |
| 8923 | // An operator function shall either be a non-static member |
| 8924 | // function or be a non-member function and have at least one |
| 8925 | // parameter whose type is a class, a reference to a class, an |
| 8926 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 8927 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 8928 | if (MethodDecl->isStatic()) |
| 8929 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 8930 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8931 | } else { |
| 8932 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 8933 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 8934 | ParamEnd = FnDecl->param_end(); |
| 8935 | Param != ParamEnd; ++Param) { |
| 8936 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 8937 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 8938 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8939 | ClassOrEnumParam = true; |
| 8940 | break; |
| 8941 | } |
| 8942 | } |
| 8943 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 8944 | if (!ClassOrEnumParam) |
| 8945 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 8946 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 8947 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
| 8950 | // C++ [over.oper]p8: |
| 8951 | // An operator function cannot have default arguments (8.3.6), |
| 8952 | // except where explicitly stated below. |
| 8953 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8954 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8955 | // (C++ [over.call]p1). |
| 8956 | if (Op != OO_Call) { |
| 8957 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 8958 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8959 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8960 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 8961 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 8962 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8963 | } |
| 8964 | } |
| 8965 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 8966 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 8967 | { false, false, false } |
| 8968 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 8969 | , { Unary, Binary, MemberOnly } |
| 8970 | #include "clang/Basic/OperatorKinds.def" |
| 8971 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8972 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 8973 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 8974 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 8975 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8976 | |
| 8977 | // C++ [over.oper]p8: |
| 8978 | // [...] Operator functions cannot have more or fewer parameters |
| 8979 | // than the number required for the corresponding operator, as |
| 8980 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8981 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 8982 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8983 | if (Op != OO_Call && |
| 8984 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 8985 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 8986 | (NumParams < 1) || (NumParams > 2))) { |
| 8987 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 8988 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 8989 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 8990 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 8991 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 8992 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 8993 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 8994 | assert(CanBeBinaryOperator && |
| 8995 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 8996 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 8997 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 8998 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 8999 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9000 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9001 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 9002 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9003 | // Overloaded operators other than operator() cannot be variadic. |
| 9004 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9005 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9006 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9007 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9008 | } |
| 9009 | |
| 9010 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9011 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 9012 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9013 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9014 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9015 | } |
| 9016 | |
| 9017 | // C++ [over.inc]p1: |
| 9018 | // The user-defined function called operator++ implements the |
| 9019 | // prefix and postfix ++ operator. If this function is a member |
| 9020 | // function with no parameters, or a non-member function with one |
| 9021 | // parameter of class or enumeration type, it defines the prefix |
| 9022 | // increment operator ++ for objects of that type. If the function |
| 9023 | // is a member function with one parameter (which shall be of type |
| 9024 | // int) or a non-member function with two parameters (the second |
| 9025 | // of which shall be of type int), it defines the postfix |
| 9026 | // increment operator ++ for objects of that type. |
| 9027 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 9028 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 9029 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9030 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9031 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 9032 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 9033 | if (!ParamIsInt) |
| 9034 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9035 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 9036 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9037 | } |
| 9038 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9039 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9040 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 9041 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9042 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 9043 | /// of this literal operator function is well-formed. If so, returns |
| 9044 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 9045 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 9046 | DeclContext *DC = FnDecl->getDeclContext(); |
| 9047 | Decl::Kind Kind = DC->getDeclKind(); |
| 9048 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 9049 | Kind != Decl::LinkageSpec) { |
| 9050 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 9051 | << FnDecl->getDeclName(); |
| 9052 | return true; |
| 9053 | } |
| 9054 | |
| 9055 | bool Valid = false; |
| 9056 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9057 | // template <char...> type operator "" name() is the only valid template |
| 9058 | // signature, and the only valid signature with no parameters. |
| 9059 | if (FnDecl->param_size() == 0) { |
| 9060 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 9061 | // Must have only one template parameter |
| 9062 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 9063 | if (Params->size() == 1) { |
| 9064 | NonTypeTemplateParmDecl *PmDecl = |
| 9065 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9066 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9067 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9068 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 9069 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 9070 | Valid = true; |
| 9071 | } |
| 9072 | } |
| 9073 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9074 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9075 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 9076 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9077 | QualType T = (*Param)->getType(); |
| 9078 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9079 | // unsigned long long int, long double, and any character type are allowed |
| 9080 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9081 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 9082 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 9083 | Context.hasSameType(T, Context.CharTy) || |
| 9084 | Context.hasSameType(T, Context.WCharTy) || |
| 9085 | Context.hasSameType(T, Context.Char16Ty) || |
| 9086 | Context.hasSameType(T, Context.Char32Ty)) { |
| 9087 | if (++Param == FnDecl->param_end()) |
| 9088 | Valid = true; |
| 9089 | goto FinishedParams; |
| 9090 | } |
| 9091 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9092 | // 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] | 9093 | const PointerType *PT = T->getAs<PointerType>(); |
| 9094 | if (!PT) |
| 9095 | goto FinishedParams; |
| 9096 | T = PT->getPointeeType(); |
| 9097 | if (!T.isConstQualified()) |
| 9098 | goto FinishedParams; |
| 9099 | T = T.getUnqualifiedType(); |
| 9100 | |
| 9101 | // Move on to the second parameter; |
| 9102 | ++Param; |
| 9103 | |
| 9104 | // If there is no second parameter, the first must be a const char * |
| 9105 | if (Param == FnDecl->param_end()) { |
| 9106 | if (Context.hasSameType(T, Context.CharTy)) |
| 9107 | Valid = true; |
| 9108 | goto FinishedParams; |
| 9109 | } |
| 9110 | |
| 9111 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 9112 | // are allowed as the first parameter to a two-parameter function |
| 9113 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 9114 | Context.hasSameType(T, Context.WCharTy) || |
| 9115 | Context.hasSameType(T, Context.Char16Ty) || |
| 9116 | Context.hasSameType(T, Context.Char32Ty))) |
| 9117 | goto FinishedParams; |
| 9118 | |
| 9119 | // The second and final parameter must be an std::size_t |
| 9120 | T = (*Param)->getType().getUnqualifiedType(); |
| 9121 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 9122 | ++Param == FnDecl->param_end()) |
| 9123 | Valid = true; |
| 9124 | } |
| 9125 | |
| 9126 | // FIXME: This diagnostic is absolutely terrible. |
| 9127 | FinishedParams: |
| 9128 | if (!Valid) { |
| 9129 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 9130 | << FnDecl->getDeclName(); |
| 9131 | return true; |
| 9132 | } |
| 9133 | |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 9134 | StringRef LiteralName |
| 9135 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 9136 | if (LiteralName[0] != '_') { |
| 9137 | // C++0x [usrlit.suffix]p1: |
| 9138 | // Literal suffix identifiers that do not start with an underscore are |
| 9139 | // reserved for future standardization. |
| 9140 | bool IsHexFloat = true; |
| 9141 | if (LiteralName.size() > 1 && |
| 9142 | (LiteralName[0] == 'P' || LiteralName[0] == 'p')) { |
| 9143 | for (unsigned I = 1, N = LiteralName.size(); I < N; ++I) { |
| 9144 | if (!isdigit(LiteralName[I])) { |
| 9145 | IsHexFloat = false; |
| 9146 | break; |
| 9147 | } |
| 9148 | } |
| 9149 | } |
| 9150 | |
| 9151 | if (IsHexFloat) |
| 9152 | Diag(FnDecl->getLocation(), diag::warn_user_literal_hexfloat) |
| 9153 | << LiteralName; |
| 9154 | else |
| 9155 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved); |
| 9156 | } |
| 9157 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9158 | return false; |
| 9159 | } |
| 9160 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9161 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 9162 | /// linkage specification, including the language and (if present) |
| 9163 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 9164 | /// the location of the language string literal, which is provided |
| 9165 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 9166 | /// the '{' brace. Otherwise, this linkage specification does not |
| 9167 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 9168 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 9169 | SourceLocation LangLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9170 | StringRef Lang, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 9171 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9172 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 9173 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9174 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 9175 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9176 | Language = LinkageSpecDecl::lang_cxx; |
| 9177 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9178 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9179 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9181 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9182 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9183 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9184 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9185 | ExternLoc, LangLoc, Language); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9186 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9187 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9188 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9189 | } |
| 9190 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 9191 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9192 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 9193 | /// valid, it's the position of the closing '}' brace in a linkage |
| 9194 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9195 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 9196 | Decl *LinkageSpec, |
| 9197 | SourceLocation RBraceLoc) { |
| 9198 | if (LinkageSpec) { |
| 9199 | if (RBraceLoc.isValid()) { |
| 9200 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 9201 | LSDecl->setRBraceLoc(RBraceLoc); |
| 9202 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9203 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 9204 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9205 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 9206 | } |
| 9207 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9208 | /// \brief Perform semantic analysis for the variable declaration that |
| 9209 | /// occurs within a C++ catch clause, returning the newly-created |
| 9210 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9211 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9212 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9213 | SourceLocation StartLoc, |
| 9214 | SourceLocation Loc, |
| 9215 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9216 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9217 | QualType ExDeclType = TInfo->getType(); |
| 9218 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9219 | // Arrays and functions decay. |
| 9220 | if (ExDeclType->isArrayType()) |
| 9221 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 9222 | else if (ExDeclType->isFunctionType()) |
| 9223 | ExDeclType = Context.getPointerType(ExDeclType); |
| 9224 | |
| 9225 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 9226 | // The exception-declaration shall not denote a pointer or reference to an |
| 9227 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9228 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9229 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9230 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9231 | Invalid = true; |
| 9232 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9233 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 9234 | // GCC allows catching pointers and references to incomplete types |
| 9235 | // as an extension; so do we, but we warn by default. |
| 9236 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9237 | QualType BaseType = ExDeclType; |
| 9238 | 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] | 9239 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 9240 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 9241 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9242 | BaseType = Ptr->getPointeeType(); |
| 9243 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 9244 | DK = diag::ext_catch_incomplete_ptr; |
| 9245 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9246 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9247 | // 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] | 9248 | BaseType = Ref->getPointeeType(); |
| 9249 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 9250 | DK = diag::ext_catch_incomplete_ref; |
| 9251 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9252 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9253 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 9254 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 9255 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9256 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9257 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9258 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9259 | RequireNonAbstractType(Loc, ExDeclType, |
| 9260 | diag::err_abstract_type_in_decl, |
| 9261 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 9262 | Invalid = true; |
| 9263 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 9264 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 9265 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 9266 | if (!Invalid && getLangOptions().ObjC1) { |
| 9267 | QualType T = ExDeclType; |
| 9268 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 9269 | T = RT->getPointeeType(); |
| 9270 | |
| 9271 | if (T->isObjCObjectType()) { |
| 9272 | Diag(Loc, diag::err_objc_object_catch); |
| 9273 | Invalid = true; |
| 9274 | } else if (T->isObjCObjectPointerType()) { |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 9275 | if (!getLangOptions().ObjCNonFragileABI) |
| 9276 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 9277 | } |
| 9278 | } |
| 9279 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9280 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 9281 | ExDeclType, TInfo, SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 9282 | ExDecl->setExceptionVariable(true); |
| 9283 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 9284 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9285 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9286 | // C++ [except.handle]p16: |
| 9287 | // The object declared in an exception-declaration or, if the |
| 9288 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 9289 | // copy-initialized (8.5) from the exception object. [...] |
| 9290 | // The object is destroyed when the handler exits, after the destruction |
| 9291 | // of any automatic objects initialized within the handler. |
| 9292 | // |
| 9293 | // We just pretend to initialize the object with itself, then make sure |
| 9294 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9295 | QualType initType = ExDeclType; |
| 9296 | |
| 9297 | InitializedEntity entity = |
| 9298 | InitializedEntity::InitializeVariable(ExDecl); |
| 9299 | InitializationKind initKind = |
| 9300 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 9301 | |
| 9302 | Expr *opaqueValue = |
| 9303 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 9304 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 9305 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 9306 | MultiExprArg(&opaqueValue, 1)); |
| 9307 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9308 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9309 | else { |
| 9310 | // If the constructor used was non-trivial, set this as the |
| 9311 | // "initializer". |
| 9312 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 9313 | if (!construct->getConstructor()->isTrivial()) { |
| 9314 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 9315 | ExDecl->setInit(init); |
| 9316 | } |
| 9317 | |
| 9318 | // And make sure it's destructable. |
| 9319 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 9320 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9321 | } |
| 9322 | } |
| 9323 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9324 | if (Invalid) |
| 9325 | ExDecl->setInvalidDecl(); |
| 9326 | |
| 9327 | return ExDecl; |
| 9328 | } |
| 9329 | |
| 9330 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 9331 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9332 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9333 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 9334 | bool Invalid = D.isInvalidType(); |
| 9335 | |
| 9336 | // Check for unexpanded parameter packs. |
| 9337 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 9338 | UPPC_ExceptionType)) { |
| 9339 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 9340 | D.getIdentifierLoc()); |
| 9341 | Invalid = true; |
| 9342 | } |
| 9343 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9344 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 9345 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 9346 | LookupOrdinaryName, |
| 9347 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9348 | // The scope should be freshly made just for us. There is just no way |
| 9349 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9350 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9351 | if (PrevDecl->isTemplateParameter()) { |
| 9352 | // Maybe we will complain about the shadowed template parameter. |
| 9353 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9354 | } |
| 9355 | } |
| 9356 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9357 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9358 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 9359 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9360 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9363 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9364 | D.getSourceRange().getBegin(), |
| 9365 | D.getIdentifierLoc(), |
| 9366 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9367 | if (Invalid) |
| 9368 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9369 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9370 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9371 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9372 | PushOnScopeChains(ExDecl, S); |
| 9373 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9374 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9375 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9376 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9377 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9378 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9379 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9380 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9381 | Expr *AssertExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9382 | Expr *AssertMessageExpr_, |
| 9383 | SourceLocation RParenLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9384 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9385 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9386 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 9387 | llvm::APSInt Value(32); |
| 9388 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9389 | Diag(StaticAssertLoc, |
| 9390 | diag::err_static_assert_expression_is_not_constant) << |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9391 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9392 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9393 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9394 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9395 | if (Value == 0) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9396 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 9397 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9398 | } |
| 9399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9400 | |
Douglas Gregor | 399ad97 | 2010-12-15 23:55:21 +0000 | [diff] [blame] | 9401 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 9402 | return 0; |
| 9403 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9404 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
| 9405 | AssertExpr, AssertMessage, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9406 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9407 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9408 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9409 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 9410 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9411 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 9412 | /// |
| 9413 | /// \returns A friend declaration that. |
| 9414 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 9415 | TypeSourceInfo *TSInfo) { |
| 9416 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 9417 | |
| 9418 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9419 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9420 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 9421 | if (!getLangOptions().CPlusPlus0x) { |
| 9422 | // C++03 [class.friend]p2: |
| 9423 | // An elaborated-type-specifier shall be used in a friend declaration |
| 9424 | // for a class.* |
| 9425 | // |
| 9426 | // * The class-key of the elaborated-type-specifier is required. |
| 9427 | if (!ActiveTemplateInstantiations.empty()) { |
| 9428 | // Do not complain about the form of friend template types during |
| 9429 | // template instantiation; we will already have complained when the |
| 9430 | // template was declared. |
| 9431 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 9432 | // If we evaluated the type to a record type, suggest putting |
| 9433 | // a tag in front. |
| 9434 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 9435 | RecordDecl *RD = RT->getDecl(); |
| 9436 | |
| 9437 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 9438 | |
| 9439 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 9440 | << (unsigned) RD->getTagKind() |
| 9441 | << T |
| 9442 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 9443 | InsertionText); |
| 9444 | } else { |
| 9445 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 9446 | << T |
| 9447 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 9448 | } |
| 9449 | } else if (T->getAs<EnumType>()) { |
| 9450 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9451 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9452 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9453 | } |
| 9454 | } |
| 9455 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 9456 | // C++0x [class.friend]p3: |
| 9457 | // If the type specifier in a friend declaration designates a (possibly |
| 9458 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 9459 | // the friend declaration is ignored. |
| 9460 | |
| 9461 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 9462 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9463 | |
| 9464 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 9465 | } |
| 9466 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9467 | /// Handle a friend tag declaration where the scope specifier was |
| 9468 | /// templated. |
| 9469 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 9470 | unsigned TagSpec, SourceLocation TagLoc, |
| 9471 | CXXScopeSpec &SS, |
| 9472 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 9473 | AttributeList *Attr, |
| 9474 | MultiTemplateParamsArg TempParamLists) { |
| 9475 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 9476 | |
| 9477 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9478 | bool Invalid = false; |
| 9479 | |
| 9480 | if (TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 9481 | = MatchTemplateParametersToScopeSpecifier(TagLoc, NameLoc, SS, |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9482 | TempParamLists.get(), |
| 9483 | TempParamLists.size(), |
| 9484 | /*friend*/ true, |
| 9485 | isExplicitSpecialization, |
| 9486 | Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9487 | if (TemplateParams->size() > 0) { |
| 9488 | // This is a declaration of a class template. |
| 9489 | if (Invalid) |
| 9490 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 9491 | |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 9492 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 9493 | SS, Name, NameLoc, Attr, |
| 9494 | TemplateParams, AS_public, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 9495 | /*ModulePrivateLoc=*/SourceLocation(), |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 9496 | TempParamLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 9497 | (TemplateParameterList**) TempParamLists.release()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9498 | } else { |
| 9499 | // The "template<>" header is extraneous. |
| 9500 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 9501 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 9502 | isExplicitSpecialization = true; |
| 9503 | } |
| 9504 | } |
| 9505 | |
| 9506 | if (Invalid) return 0; |
| 9507 | |
| 9508 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 9509 | |
| 9510 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 9511 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9512 | if (TempParamLists.get()[I]->size()) { |
| 9513 | isAllExplicitSpecializations = false; |
| 9514 | break; |
| 9515 | } |
| 9516 | } |
| 9517 | |
| 9518 | // FIXME: don't ignore attributes. |
| 9519 | |
| 9520 | // If it's explicit specializations all the way down, just forget |
| 9521 | // about the template header and build an appropriate non-templated |
| 9522 | // friend. TODO: for source fidelity, remember the headers. |
| 9523 | if (isAllExplicitSpecializations) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9524 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9525 | ElaboratedTypeKeyword Keyword |
| 9526 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9527 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9528 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9529 | if (T.isNull()) |
| 9530 | return 0; |
| 9531 | |
| 9532 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9533 | if (isa<DependentNameType>(T)) { |
| 9534 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 9535 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9536 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9537 | TL.setNameLoc(NameLoc); |
| 9538 | } else { |
| 9539 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
| 9540 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9541 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9542 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 9543 | } |
| 9544 | |
| 9545 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 9546 | TSI, FriendLoc); |
| 9547 | Friend->setAccess(AS_public); |
| 9548 | CurContext->addDecl(Friend); |
| 9549 | return Friend; |
| 9550 | } |
| 9551 | |
| 9552 | // Handle the case of a templated-scope friend class. e.g. |
| 9553 | // template <class T> class A<T>::B; |
| 9554 | // FIXME: we don't support these right now. |
| 9555 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 9556 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 9557 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9558 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 9559 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9560 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9561 | TL.setNameLoc(NameLoc); |
| 9562 | |
| 9563 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 9564 | TSI, FriendLoc); |
| 9565 | Friend->setAccess(AS_public); |
| 9566 | Friend->setUnsupportedFriend(true); |
| 9567 | CurContext->addDecl(Friend); |
| 9568 | return Friend; |
| 9569 | } |
| 9570 | |
| 9571 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9572 | /// Handle a friend type declaration. This works in tandem with |
| 9573 | /// ActOnTag. |
| 9574 | /// |
| 9575 | /// Notes on friend class templates: |
| 9576 | /// |
| 9577 | /// We generally treat friend class declarations as if they were |
| 9578 | /// declaring a class. So, for example, the elaborated type specifier |
| 9579 | /// in a friend declaration is required to obey the restrictions of a |
| 9580 | /// class-head (i.e. no typedefs in the scope chain), template |
| 9581 | /// parameters are required to match up with simple template-ids, &c. |
| 9582 | /// However, unlike when declaring a template specialization, it's |
| 9583 | /// okay to refer to a template specialization without an empty |
| 9584 | /// template parameter declaration, e.g. |
| 9585 | /// friend class A<T>::B<unsigned>; |
| 9586 | /// We permit this as a special case; if there are any template |
| 9587 | /// parameters present at all, require proper matching, i.e. |
| 9588 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9589 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 9590 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9591 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9592 | |
| 9593 | assert(DS.isFriendSpecified()); |
| 9594 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 9595 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9596 | // Try to convert the decl specifier to a type. This works for |
| 9597 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 9598 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 9599 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9600 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 9601 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 9602 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9603 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9604 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 9605 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 9606 | return 0; |
| 9607 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9608 | // This is definitely an error in C++98. It's probably meant to |
| 9609 | // be forbidden in C++0x, too, but the specification is just |
| 9610 | // poorly written. |
| 9611 | // |
| 9612 | // The problem is with declarations like the following: |
| 9613 | // template <T> friend A<T>::foo; |
| 9614 | // where deciding whether a class C is a friend or not now hinges |
| 9615 | // on whether there exists an instantiation of A that causes |
| 9616 | // 'foo' to equal C. There are restrictions on class-heads |
| 9617 | // (which we declare (by fiat) elaborated friend declarations to |
| 9618 | // be) that makes this tractable. |
| 9619 | // |
| 9620 | // FIXME: handle "template <> friend class A<T>;", which |
| 9621 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 9622 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9623 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 9624 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9625 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9626 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9627 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9628 | // C++98 [class.friend]p1: A friend of a class is a function |
| 9629 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 9630 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 9631 | // deadline. It's also a very silly restriction that seriously |
| 9632 | // affects inner classes and which nobody else seems to implement; |
| 9633 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 9634 | // |
| 9635 | // But note that we could warn about it: it's always useless to |
| 9636 | // friend one of your own members (it's not, however, worthless to |
| 9637 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9638 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9639 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9640 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9641 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9642 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 9643 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 9644 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9645 | DS.getFriendSpecLoc()); |
| 9646 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9647 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 9648 | |
| 9649 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9650 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9651 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9652 | D->setAccess(AS_public); |
| 9653 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9654 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9655 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9656 | } |
| 9657 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9658 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 9659 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9660 | const DeclSpec &DS = D.getDeclSpec(); |
| 9661 | |
| 9662 | assert(DS.isFriendSpecified()); |
| 9663 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 9664 | |
| 9665 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9666 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 9667 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9668 | |
| 9669 | // C++ [class.friend]p1 |
| 9670 | // A friend of a class is a function or class.... |
| 9671 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9672 | // It *doesn't* see through dependent types, which is correct |
| 9673 | // according to [temp.arg.type]p3: |
| 9674 | // If a declaration acquires a function type through a |
| 9675 | // type dependent on a template-parameter and this causes |
| 9676 | // a declaration that does not use the syntactic form of a |
| 9677 | // function declarator to have a function type, the program |
| 9678 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9679 | if (!T->isFunctionType()) { |
| 9680 | Diag(Loc, diag::err_unexpected_friend); |
| 9681 | |
| 9682 | // It might be worthwhile to try to recover by creating an |
| 9683 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9684 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9685 | } |
| 9686 | |
| 9687 | // C++ [namespace.memdef]p3 |
| 9688 | // - If a friend declaration in a non-local class first declares a |
| 9689 | // class or function, the friend class or function is a member |
| 9690 | // of the innermost enclosing namespace. |
| 9691 | // - The name of the friend is not found by simple name lookup |
| 9692 | // until a matching declaration is provided in that namespace |
| 9693 | // scope (either before or after the class declaration granting |
| 9694 | // friendship). |
| 9695 | // - If a friend function is called, its name may be found by the |
| 9696 | // name lookup that considers functions from namespaces and |
| 9697 | // classes associated with the types of the function arguments. |
| 9698 | // - When looking for a prior declaration of a class or a function |
| 9699 | // declared as a friend, scopes outside the innermost enclosing |
| 9700 | // namespace scope are not considered. |
| 9701 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9702 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9703 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 9704 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9705 | assert(Name); |
| 9706 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 9707 | // Check for unexpanded parameter packs. |
| 9708 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 9709 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 9710 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 9711 | return 0; |
| 9712 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9713 | // The context we found the declaration in, or in which we should |
| 9714 | // create the declaration. |
| 9715 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 9716 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9717 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 9718 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9719 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9720 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9721 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9722 | // There are four cases here. |
| 9723 | // - 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] | 9724 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9725 | // there as appropriate. |
| 9726 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 9727 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 9728 | // C++0x [namespace.memdef]p3: |
| 9729 | // If the name in a friend declaration is neither qualified nor |
| 9730 | // a template-id and the declaration is a function or an |
| 9731 | // elaborated-type-specifier, the lookup to determine whether |
| 9732 | // the entity has been previously declared shall not consider |
| 9733 | // any scopes outside the innermost enclosing namespace. |
| 9734 | // C++0x [class.friend]p11: |
| 9735 | // If a friend declaration appears in a local class and the name |
| 9736 | // specified is an unqualified name, a prior declaration is |
| 9737 | // looked up without considering scopes that are outside the |
| 9738 | // innermost enclosing non-class scope. For a friend function |
| 9739 | // declaration, if there is no prior declaration, the program is |
| 9740 | // ill-formed. |
| 9741 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 9742 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9743 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 9744 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9745 | DC = CurContext; |
| 9746 | while (true) { |
| 9747 | // Skip class contexts. If someone can cite chapter and verse |
| 9748 | // for this behavior, that would be nice --- it's what GCC and |
| 9749 | // EDG do, and it seems like a reasonable intent, but the spec |
| 9750 | // really only says that checks for unqualified existing |
| 9751 | // declarations should stop at the nearest enclosing namespace, |
| 9752 | // not that they should only consider the nearest enclosing |
| 9753 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 9754 | while (DC->isRecord()) |
| 9755 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9756 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 9757 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9758 | |
| 9759 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 9760 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9761 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 9762 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 9763 | if (isTemplateId) { |
| 9764 | if (isa<TranslationUnitDecl>(DC)) break; |
| 9765 | } else { |
| 9766 | if (DC->isFileContext()) break; |
| 9767 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9768 | DC = DC->getParent(); |
| 9769 | } |
| 9770 | |
| 9771 | // C++ [class.friend]p1: A friend of a class is a function or |
| 9772 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 9773 | // C++0x changes this for both friend types and functions. |
| 9774 | // Most C++ 98 compilers do seem to give an error here, so |
| 9775 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 9776 | if (!Previous.empty() && DC->Equals(CurContext) |
| 9777 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9778 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9779 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 9780 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 9781 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9782 | // - There's a non-dependent scope specifier, in which case we |
| 9783 | // compute it and do a previous lookup there for a function |
| 9784 | // or function template. |
| 9785 | } else if (!SS.getScopeRep()->isDependent()) { |
| 9786 | DC = computeDeclContext(SS); |
| 9787 | if (!DC) return 0; |
| 9788 | |
| 9789 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 9790 | |
| 9791 | LookupQualifiedName(Previous, DC); |
| 9792 | |
| 9793 | // Ignore things found implicitly in the wrong scope. |
| 9794 | // TODO: better diagnostics for this case. Suggesting the right |
| 9795 | // qualified scope would be nice... |
| 9796 | LookupResult::Filter F = Previous.makeFilter(); |
| 9797 | while (F.hasNext()) { |
| 9798 | NamedDecl *D = F.next(); |
| 9799 | if (!DC->InEnclosingNamespaceSetOf( |
| 9800 | D->getDeclContext()->getRedeclContext())) |
| 9801 | F.erase(); |
| 9802 | } |
| 9803 | F.done(); |
| 9804 | |
| 9805 | if (Previous.empty()) { |
| 9806 | D.setInvalidType(); |
| 9807 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 9808 | return 0; |
| 9809 | } |
| 9810 | |
| 9811 | // C++ [class.friend]p1: A friend of a class is a function or |
| 9812 | // class that is not a member of the class . . . |
| 9813 | if (DC->Equals(CurContext)) |
| 9814 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 9815 | |
| 9816 | // - There's a scope specifier that does not match any template |
| 9817 | // parameter lists, in which case we use some arbitrary context, |
| 9818 | // create a method or method template, and wait for instantiation. |
| 9819 | // - There's a scope specifier that does match some template |
| 9820 | // parameter lists, which we don't handle right now. |
| 9821 | } else { |
| 9822 | DC = CurContext; |
| 9823 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9824 | } |
| 9825 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 9826 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9827 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 9828 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 9829 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 9830 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9831 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 9832 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 9833 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9834 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9835 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9836 | } |
| 9837 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 9838 | bool Redeclaration = false; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 9839 | bool AddToScope = true; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 9840 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 9841 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 9842 | IsDefinition, |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 9843 | Redeclaration, AddToScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9844 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 9845 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 9846 | assert(ND->getDeclContext() == DC); |
| 9847 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 9848 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 9849 | // Add the function declaration to the appropriate lookup tables, |
| 9850 | // adjusting the redeclarations list as necessary. We don't |
| 9851 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9852 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 9853 | // Also update the scope-based lookup if the target context's |
| 9854 | // lookup context is in lexical scope. |
| 9855 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 9856 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 9857 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 9858 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 9859 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 9860 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9861 | |
| 9862 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 9863 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9864 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 9865 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9866 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9867 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9868 | if (ND->isInvalidDecl()) |
| 9869 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 9870 | else { |
| 9871 | FunctionDecl *FD; |
| 9872 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 9873 | FD = FTD->getTemplatedDecl(); |
| 9874 | else |
| 9875 | FD = cast<FunctionDecl>(ND); |
| 9876 | |
| 9877 | // Mark templated-scope function declarations as unsupported. |
| 9878 | if (FD->getNumTemplateParameterLists()) |
| 9879 | FrD->setUnsupportedFriend(true); |
| 9880 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9881 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9882 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9885 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 9886 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9887 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 9888 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 9889 | if (!Fn) { |
| 9890 | Diag(DelLoc, diag::err_deleted_non_function); |
| 9891 | return; |
| 9892 | } |
| 9893 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 9894 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 9895 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 9896 | // If the declaration wasn't the first, we delete the function anyway for |
| 9897 | // recovery. |
| 9898 | } |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 9899 | Fn->setDeletedAsWritten(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 9900 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 9901 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 9902 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
| 9903 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Dcl); |
| 9904 | |
| 9905 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 9906 | if (MD->getParent()->isDependentType()) { |
| 9907 | MD->setDefaulted(); |
| 9908 | MD->setExplicitlyDefaulted(); |
| 9909 | return; |
| 9910 | } |
| 9911 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 9912 | CXXSpecialMember Member = getSpecialMember(MD); |
| 9913 | if (Member == CXXInvalid) { |
| 9914 | Diag(DefaultLoc, diag::err_default_special_members); |
| 9915 | return; |
| 9916 | } |
| 9917 | |
| 9918 | MD->setDefaulted(); |
| 9919 | MD->setExplicitlyDefaulted(); |
| 9920 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 9921 | // If this definition appears within the record, do the checking when |
| 9922 | // the record is complete. |
| 9923 | const FunctionDecl *Primary = MD; |
| 9924 | if (MD->getTemplatedKind() != FunctionDecl::TK_NonTemplate) |
| 9925 | // Find the uninstantiated declaration that actually had the '= default' |
| 9926 | // on it. |
| 9927 | MD->getTemplateInstantiationPattern()->isDefined(Primary); |
| 9928 | |
| 9929 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 9930 | return; |
| 9931 | |
| 9932 | switch (Member) { |
| 9933 | case CXXDefaultConstructor: { |
| 9934 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 9935 | CheckExplicitlyDefaultedDefaultConstructor(CD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9936 | if (!CD->isInvalidDecl()) |
| 9937 | DefineImplicitDefaultConstructor(DefaultLoc, CD); |
| 9938 | break; |
| 9939 | } |
| 9940 | |
| 9941 | case CXXCopyConstructor: { |
| 9942 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 9943 | CheckExplicitlyDefaultedCopyConstructor(CD); |
| 9944 | if (!CD->isInvalidDecl()) |
| 9945 | DefineImplicitCopyConstructor(DefaultLoc, CD); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 9946 | break; |
| 9947 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9948 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 9949 | case CXXCopyAssignment: { |
| 9950 | CheckExplicitlyDefaultedCopyAssignment(MD); |
| 9951 | if (!MD->isInvalidDecl()) |
| 9952 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
| 9953 | break; |
| 9954 | } |
| 9955 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9956 | case CXXDestructor: { |
| 9957 | CXXDestructorDecl *DD = cast<CXXDestructorDecl>(MD); |
| 9958 | CheckExplicitlyDefaultedDestructor(DD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9959 | if (!DD->isInvalidDecl()) |
| 9960 | DefineImplicitDestructor(DefaultLoc, DD); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9961 | break; |
| 9962 | } |
| 9963 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9964 | case CXXMoveConstructor: { |
| 9965 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 9966 | CheckExplicitlyDefaultedMoveConstructor(CD); |
| 9967 | if (!CD->isInvalidDecl()) |
| 9968 | DefineImplicitMoveConstructor(DefaultLoc, CD); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 9969 | break; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9970 | } |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 9971 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9972 | case CXXMoveAssignment: { |
| 9973 | CheckExplicitlyDefaultedMoveAssignment(MD); |
| 9974 | if (!MD->isInvalidDecl()) |
| 9975 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
| 9976 | break; |
| 9977 | } |
| 9978 | |
| 9979 | case CXXInvalid: |
| 9980 | assert(false && "Invalid special member."); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 9981 | break; |
| 9982 | } |
| 9983 | } else { |
| 9984 | Diag(DefaultLoc, diag::err_default_special_members); |
| 9985 | } |
| 9986 | } |
| 9987 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 9988 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 9989 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 9990 | Stmt *SubStmt = *CI; |
| 9991 | if (!SubStmt) |
| 9992 | continue; |
| 9993 | if (isa<ReturnStmt>(SubStmt)) |
| 9994 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 9995 | diag::err_return_in_constructor_handler); |
| 9996 | if (!isa<Expr>(SubStmt)) |
| 9997 | SearchForReturnInStmt(Self, SubStmt); |
| 9998 | } |
| 9999 | } |
| 10000 | |
| 10001 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 10002 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 10003 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 10004 | SearchForReturnInStmt(*this, Handler); |
| 10005 | } |
| 10006 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10007 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10008 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10009 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10010 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 10011 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10012 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 10013 | if (Context.hasSameType(NewTy, OldTy) || |
| 10014 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10015 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10016 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10017 | // Check if the return types are covariant |
| 10018 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10019 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10020 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10021 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 10022 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10023 | NewClassTy = NewPT->getPointeeType(); |
| 10024 | OldClassTy = OldPT->getPointeeType(); |
| 10025 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10026 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 10027 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 10028 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 10029 | NewClassTy = NewRT->getPointeeType(); |
| 10030 | OldClassTy = OldRT->getPointeeType(); |
| 10031 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10032 | } |
| 10033 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10034 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10035 | // The return types aren't either both pointers or references to a class type. |
| 10036 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10037 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10038 | diag::err_different_return_type_for_overriding_virtual_function) |
| 10039 | << New->getDeclName() << NewTy << OldTy; |
| 10040 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10041 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10042 | return true; |
| 10043 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10044 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10045 | // C++ [class.virtual]p6: |
| 10046 | // If the return type of D::f differs from the return type of B::f, the |
| 10047 | // class type in the return type of D::f shall be complete at the point of |
| 10048 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 10049 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 10050 | if (!RT->isBeingDefined() && |
| 10051 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 10052 | PDiag(diag::err_covariant_return_incomplete) |
| 10053 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10054 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 10055 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10056 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 10057 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10058 | // Check if the new class derives from the old class. |
| 10059 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 10060 | Diag(New->getLocation(), |
| 10061 | diag::err_covariant_return_not_derived) |
| 10062 | << New->getDeclName() << NewTy << OldTy; |
| 10063 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10064 | return true; |
| 10065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10066 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10067 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 10068 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 10069 | diag::err_covariant_return_inaccessible_base, |
| 10070 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 10071 | // FIXME: Should this point to the return type? |
| 10072 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 10073 | // FIXME: this note won't trigger for delayed access control |
| 10074 | // diagnostics, and it's impossible to get an undelayed error |
| 10075 | // here from access control during the original parse because |
| 10076 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10077 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10078 | return true; |
| 10079 | } |
| 10080 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10081 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10082 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10083 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10084 | Diag(New->getLocation(), |
| 10085 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10086 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10087 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10088 | return true; |
| 10089 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10090 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10091 | |
| 10092 | // The new class type must have the same or less qualifiers as the old type. |
| 10093 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 10094 | Diag(New->getLocation(), |
| 10095 | diag::err_covariant_return_type_class_type_more_qualified) |
| 10096 | << New->getDeclName() << NewTy << OldTy; |
| 10097 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10098 | return true; |
| 10099 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10100 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10101 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10102 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10103 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10104 | /// \brief Mark the given method pure. |
| 10105 | /// |
| 10106 | /// \param Method the method to be marked pure. |
| 10107 | /// |
| 10108 | /// \param InitRange the source range that covers the "0" initializer. |
| 10109 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 10110 | SourceLocation EndLoc = InitRange.getEnd(); |
| 10111 | if (EndLoc.isValid()) |
| 10112 | Method->setRangeEnd(EndLoc); |
| 10113 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10114 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 10115 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10116 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 10117 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10118 | |
| 10119 | if (!Method->isInvalidDecl()) |
| 10120 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 10121 | << Method->getDeclName() << InitRange; |
| 10122 | return true; |
| 10123 | } |
| 10124 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10125 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 10126 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 10127 | /// is a fresh scope pushed for just this purpose. |
| 10128 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10129 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 10130 | /// static data member of class X, names should be looked up in the scope of |
| 10131 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10132 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10133 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 10134 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10135 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10136 | // We should only get called for declarations with scope specifiers, like: |
| 10137 | // int foo::bar; |
| 10138 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 10139 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10140 | } |
| 10141 | |
| 10142 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10143 | /// initializer for the out-of-line declaration 'D'. |
| 10144 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10145 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 10146 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10147 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10148 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 10149 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10150 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10151 | |
| 10152 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 10153 | /// C++ if/switch/while/for statement. |
| 10154 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10155 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10156 | // C++ 6.4p2: |
| 10157 | // The declarator shall not specify a function or an array. |
| 10158 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 10159 | // new class or enumeration. |
| 10160 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 10161 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 10162 | |
| 10163 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 10164 | if (!Dcl) |
| 10165 | return true; |
| 10166 | |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 10167 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 10168 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10169 | << D.getSourceRange(); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 10170 | return true; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10171 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10172 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10173 | return Dcl; |
| 10174 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10175 | |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10176 | void Sema::LoadExternalVTableUses() { |
| 10177 | if (!ExternalSource) |
| 10178 | return; |
| 10179 | |
| 10180 | SmallVector<ExternalVTableUse, 4> VTables; |
| 10181 | ExternalSource->ReadUsedVTables(VTables); |
| 10182 | SmallVector<VTableUse, 4> NewUses; |
| 10183 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 10184 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 10185 | = VTablesUsed.find(VTables[I].Record); |
| 10186 | // Even if a definition wasn't required before, it may be required now. |
| 10187 | if (Pos != VTablesUsed.end()) { |
| 10188 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 10189 | Pos->second = true; |
| 10190 | continue; |
| 10191 | } |
| 10192 | |
| 10193 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 10194 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 10195 | } |
| 10196 | |
| 10197 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 10198 | } |
| 10199 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10200 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 10201 | bool DefinitionRequired) { |
| 10202 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 10203 | // not have a vtable. |
| 10204 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 10205 | CurContext->isDependentContext() || |
| 10206 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 10207 | return; |
| 10208 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10209 | // Try to insert this class into the map. |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10210 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10211 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 10212 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 10213 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 10214 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 10215 | // If we already had an entry, check to see if we are promoting this vtable |
| 10216 | // to required a definition. If so, we need to reappend to the VTableUses |
| 10217 | // list, since we may have already processed the first entry. |
| 10218 | if (DefinitionRequired && !Pos.first->second) { |
| 10219 | Pos.first->second = true; |
| 10220 | } else { |
| 10221 | // Otherwise, we can early exit. |
| 10222 | return; |
| 10223 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10224 | } |
| 10225 | |
| 10226 | // Local classes need to have their virtual members marked |
| 10227 | // immediately. For all other classes, we mark their virtual members |
| 10228 | // at the end of the translation unit. |
| 10229 | if (Class->isLocalClass()) |
| 10230 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 10231 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10232 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 10233 | } |
| 10234 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10235 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10236 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10237 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10238 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 10239 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10240 | // Note: The VTableUses vector could grow as a result of marking |
| 10241 | // the members of a class as "used", so we check the size each |
| 10242 | // time through the loop and prefer indices (with are stable) to |
| 10243 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10244 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10245 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 10246 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10247 | if (!Class) |
| 10248 | continue; |
| 10249 | |
| 10250 | SourceLocation Loc = VTableUses[I].second; |
| 10251 | |
| 10252 | // If this class has a key function, but that key function is |
| 10253 | // defined in another translation unit, we don't need to emit the |
| 10254 | // vtable even though we're using it. |
| 10255 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 10256 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10257 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 10258 | case TSK_Undeclared: |
| 10259 | case TSK_ExplicitSpecialization: |
| 10260 | case TSK_ExplicitInstantiationDeclaration: |
| 10261 | // The key function is in another translation unit. |
| 10262 | continue; |
| 10263 | |
| 10264 | case TSK_ExplicitInstantiationDefinition: |
| 10265 | case TSK_ImplicitInstantiation: |
| 10266 | // We will be instantiating the key function. |
| 10267 | break; |
| 10268 | } |
| 10269 | } else if (!KeyFunction) { |
| 10270 | // If we have a class with no key function that is the subject |
| 10271 | // of an explicit instantiation declaration, suppress the |
| 10272 | // vtable; it will live with the explicit instantiation |
| 10273 | // definition. |
| 10274 | bool IsExplicitInstantiationDeclaration |
| 10275 | = Class->getTemplateSpecializationKind() |
| 10276 | == TSK_ExplicitInstantiationDeclaration; |
| 10277 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 10278 | REnd = Class->redecls_end(); |
| 10279 | R != REnd; ++R) { |
| 10280 | TemplateSpecializationKind TSK |
| 10281 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 10282 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 10283 | IsExplicitInstantiationDeclaration = true; |
| 10284 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 10285 | IsExplicitInstantiationDeclaration = false; |
| 10286 | break; |
| 10287 | } |
| 10288 | } |
| 10289 | |
| 10290 | if (IsExplicitInstantiationDeclaration) |
| 10291 | continue; |
| 10292 | } |
| 10293 | |
| 10294 | // Mark all of the virtual members of this class as referenced, so |
| 10295 | // that we can build a vtable. Then, tell the AST consumer that a |
| 10296 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10297 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10298 | MarkVirtualMembersReferenced(Loc, Class); |
| 10299 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 10300 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 10301 | |
| 10302 | // Optionally warn if we're emitting a weak vtable. |
| 10303 | if (Class->getLinkage() == ExternalLinkage && |
| 10304 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 10305 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10306 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 10307 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10308 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10309 | VTableUses.clear(); |
| 10310 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10311 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10312 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10313 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10314 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 10315 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10316 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 10317 | e = RD->method_end(); i != e; ++i) { |
| 10318 | CXXMethodDecl *MD = *i; |
| 10319 | |
| 10320 | // C++ [basic.def.odr]p2: |
| 10321 | // [...] A virtual member function is used if it is not pure. [...] |
| 10322 | if (MD->isVirtual() && !MD->isPure()) |
| 10323 | MarkDeclarationReferenced(Loc, MD); |
| 10324 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10325 | |
| 10326 | // Only classes that have virtual bases need a VTT. |
| 10327 | if (RD->getNumVBases() == 0) |
| 10328 | return; |
| 10329 | |
| 10330 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 10331 | e = RD->bases_end(); i != e; ++i) { |
| 10332 | const CXXRecordDecl *Base = |
| 10333 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10334 | if (Base->getNumVBases() == 0) |
| 10335 | continue; |
| 10336 | MarkVirtualMembersReferenced(Loc, Base); |
| 10337 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10338 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10339 | |
| 10340 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 10341 | /// Objective-C implementation whose ivars need be initialized. |
| 10342 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 10343 | if (!getLangOptions().CPlusPlus) |
| 10344 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 10345 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10346 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10347 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 10348 | if (ivars.empty()) |
| 10349 | return; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10350 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10351 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 10352 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10353 | if (Field->isInvalidDecl()) |
| 10354 | continue; |
| 10355 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 10356 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10357 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 10358 | InitializationKind InitKind = |
| 10359 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 10360 | |
| 10361 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10362 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10363 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 10364 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10365 | // Note, MemberInit could actually come back empty if no initialization |
| 10366 | // is required (e.g., because it would call a trivial default constructor) |
| 10367 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 10368 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 10369 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10370 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 10371 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 10372 | SourceLocation(), |
| 10373 | MemberInit.takeAs<Expr>(), |
| 10374 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10375 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10376 | |
| 10377 | // Be sure that the destructor is accessible and is marked as referenced. |
| 10378 | if (const RecordType *RecordTy |
| 10379 | = Context.getBaseElementType(Field->getType()) |
| 10380 | ->getAs<RecordType>()) { |
| 10381 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 10382 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10383 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 10384 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 10385 | PDiag(diag::err_access_dtor_ivar) |
| 10386 | << Context.getBaseElementType(Field->getType())); |
| 10387 | } |
| 10388 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10389 | } |
| 10390 | ObjCImplementation->setIvarInitializers(Context, |
| 10391 | AllToInit.data(), AllToInit.size()); |
| 10392 | } |
| 10393 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10394 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10395 | static |
| 10396 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 10397 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 10398 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 10399 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 10400 | Sema &S) { |
| 10401 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 10402 | CE = Current.end(); |
| 10403 | if (Ctor->isInvalidDecl()) |
| 10404 | return; |
| 10405 | |
| 10406 | const FunctionDecl *FNTarget = 0; |
| 10407 | CXXConstructorDecl *Target; |
| 10408 | |
| 10409 | // We ignore the result here since if we don't have a body, Target will be |
| 10410 | // null below. |
| 10411 | (void)Ctor->getTargetConstructor()->hasBody(FNTarget); |
| 10412 | Target |
| 10413 | = const_cast<CXXConstructorDecl*>(cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 10414 | |
| 10415 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 10416 | // Avoid dereferencing a null pointer here. |
| 10417 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 10418 | |
| 10419 | if (!Current.insert(Canonical)) |
| 10420 | return; |
| 10421 | |
| 10422 | // We know that beyond here, we aren't chaining into a cycle. |
| 10423 | if (!Target || !Target->isDelegatingConstructor() || |
| 10424 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| 10425 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 10426 | Valid.insert(*CI); |
| 10427 | Current.clear(); |
| 10428 | // We've hit a cycle. |
| 10429 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 10430 | Current.count(TCanonical)) { |
| 10431 | // If we haven't diagnosed this cycle yet, do so now. |
| 10432 | if (!Invalid.count(TCanonical)) { |
| 10433 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 10434 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10435 | << Ctor; |
| 10436 | |
| 10437 | // Don't add a note for a function delegating directo to itself. |
| 10438 | if (TCanonical != Canonical) |
| 10439 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 10440 | |
| 10441 | CXXConstructorDecl *C = Target; |
| 10442 | while (C->getCanonicalDecl() != Canonical) { |
| 10443 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 10444 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 10445 | |
| 10446 | C |
| 10447 | = const_cast<CXXConstructorDecl*>(cast<CXXConstructorDecl>(FNTarget)); |
| 10448 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 10449 | } |
| 10450 | } |
| 10451 | |
| 10452 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 10453 | Invalid.insert(*CI); |
| 10454 | Current.clear(); |
| 10455 | } else { |
| 10456 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 10457 | } |
| 10458 | } |
| 10459 | |
| 10460 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10461 | void Sema::CheckDelegatingCtorCycles() { |
| 10462 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 10463 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10464 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 10465 | CE = Current.end(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10466 | |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 10467 | for (DelegatingCtorDeclsType::iterator |
| 10468 | I = DelegatingCtorDecls.begin(ExternalSource), |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10469 | E = DelegatingCtorDecls.end(); |
| 10470 | I != E; ++I) { |
| 10471 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10472 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10473 | |
| 10474 | for (CI = Invalid.begin(), CE = Invalid.end(); CI != CE; ++CI) |
| 10475 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10476 | } |