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" |
Eli Friedman | 7badd24 | 2012-02-09 20:13:14 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 23 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 24 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 27 | #include "clang/AST/RecordLayout.h" |
| 28 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 29 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 30 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 31 | #include "clang/Sema/DeclSpec.h" |
| 32 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 33 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 34 | #include "clang/Lex/Preprocessor.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 37 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 38 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace clang; |
| 41 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // CheckDefaultArgumentVisitor |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 48 | /// the default argument of a parameter to determine whether it |
| 49 | /// contains any ill-formed subexpressions. For example, this will |
| 50 | /// diagnose the use of local variables or parameters within the |
| 51 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 52 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 53 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | Expr *DefaultArg; |
| 55 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 57 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | bool VisitExpr(Expr *Node); |
| 62 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 63 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 64 | bool VisitLambdaExpr(LambdaExpr *Lambda); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 65 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 67 | /// VisitExpr - Visit all of the children of this expression. |
| 68 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 69 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 70 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 71 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 72 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 75 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 76 | /// determine whether this declaration can be used in the default |
| 77 | /// argument expression. |
| 78 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 79 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 80 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 81 | // C++ [dcl.fct.default]p9 |
| 82 | // Default arguments are evaluated each time the function is |
| 83 | // called. The order of evaluation of function arguments is |
| 84 | // unspecified. Consequently, parameters of a function shall not |
| 85 | // be used in default argument expressions, even if they are not |
| 86 | // evaluated. Parameters of a function declared before a default |
| 87 | // argument expression are in scope and can hide namespace and |
| 88 | // class member names. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 89 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 90 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 91 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 92 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 93 | // C++ [dcl.fct.default]p7 |
| 94 | // Local variables shall not be used in default argument |
| 95 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 96 | if (VDecl->isLocalVarDecl()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 97 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 98 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 99 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 104 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 105 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 106 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 107 | // C++ [dcl.fct.default]p8: |
| 108 | // The keyword this shall not be used in a default argument of a |
| 109 | // member function. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 110 | return S->Diag(ThisE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 111 | diag::err_param_default_argument_references_this) |
| 112 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 113 | } |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 114 | |
| 115 | bool CheckDefaultArgumentVisitor::VisitLambdaExpr(LambdaExpr *Lambda) { |
| 116 | // C++11 [expr.lambda.prim]p13: |
| 117 | // A lambda-expression appearing in a default argument shall not |
| 118 | // implicitly or explicitly capture any entity. |
| 119 | if (Lambda->capture_begin() == Lambda->capture_end()) |
| 120 | return false; |
| 121 | |
| 122 | return S->Diag(Lambda->getLocStart(), |
| 123 | diag::err_lambda_capture_default_arg); |
| 124 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 127 | void Sema::ImplicitExceptionSpecification::CalledDecl(CXXMethodDecl *Method) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 128 | assert(Context && "ImplicitExceptionSpecification without an ASTContext"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 129 | // If we have an MSAny or unknown spec already, don't bother. |
| 130 | if (!Method || ComputedEST == EST_MSAny || ComputedEST == EST_Delayed) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 131 | return; |
| 132 | |
| 133 | const FunctionProtoType *Proto |
| 134 | = Method->getType()->getAs<FunctionProtoType>(); |
| 135 | |
| 136 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 137 | |
| 138 | // If this function can throw any exceptions, make a note of that. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 139 | if (EST == EST_Delayed || EST == EST_MSAny || EST == EST_None) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 140 | ClearExceptions(); |
| 141 | ComputedEST = EST; |
| 142 | return; |
| 143 | } |
| 144 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 145 | // FIXME: If the call to this decl is using any of its default arguments, we |
| 146 | // need to search them for potentially-throwing calls. |
| 147 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 148 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 149 | if (EST == EST_BasicNoexcept) |
| 150 | return; |
| 151 | |
| 152 | // If we have a throw-all spec at this point, ignore the function. |
| 153 | if (ComputedEST == EST_None) |
| 154 | return; |
| 155 | |
| 156 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 157 | // change to that specification. |
| 158 | if (EST == EST_DynamicNone) { |
| 159 | if (ComputedEST == EST_BasicNoexcept) |
| 160 | ComputedEST = EST_DynamicNone; |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | // Check out noexcept specs. |
| 165 | if (EST == EST_ComputedNoexcept) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 166 | FunctionProtoType::NoexceptResult NR = Proto->getNoexceptSpec(*Context); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 167 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 168 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 169 | assert(NR != FunctionProtoType::NR_Dependent && |
| 170 | "Should not generate implicit declarations for dependent cases, " |
| 171 | "and don't know how to handle them anyway."); |
| 172 | |
| 173 | // noexcept(false) -> no spec on the new function |
| 174 | if (NR == FunctionProtoType::NR_Throw) { |
| 175 | ClearExceptions(); |
| 176 | ComputedEST = EST_None; |
| 177 | } |
| 178 | // noexcept(true) won't change anything either. |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 183 | assert(ComputedEST != EST_None && |
| 184 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 185 | ComputedEST = EST_Dynamic; |
| 186 | // Record the exceptions in this function's exception specification. |
| 187 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 188 | EEnd = Proto->exception_end(); |
| 189 | E != EEnd; ++E) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 190 | if (ExceptionsSeen.insert(Context->getCanonicalType(*E))) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 191 | Exceptions.push_back(*E); |
| 192 | } |
| 193 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 194 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
| 195 | if (!E || ComputedEST == EST_MSAny || ComputedEST == EST_Delayed) |
| 196 | return; |
| 197 | |
| 198 | // FIXME: |
| 199 | // |
| 200 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 4857947 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 201 | // [An] implicit exception-specification specifies the type-id T if and |
| 202 | // only if T is allowed by the exception-specification of a function directly |
| 203 | // 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] | 204 | // function it directly invokes allows all exceptions, and f shall allow no |
| 205 | // exceptions if every function it directly invokes allows no exceptions. |
| 206 | // |
| 207 | // Note in particular that if an implicit exception-specification is generated |
| 208 | // for a function containing a throw-expression, that specification can still |
| 209 | // be noexcept(true). |
| 210 | // |
| 211 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 212 | // is no indication that we should only consider potentially-evaluated calls. |
| 213 | // |
| 214 | // Ultimately we should implement the intent of the standard: the exception |
| 215 | // specification should be the set of exceptions which can be thrown by the |
| 216 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 217 | // throw any exception. |
| 218 | |
| 219 | if (E->CanThrow(*Context)) |
| 220 | ComputedEST = EST_None; |
| 221 | } |
| 222 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 223 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 224 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 226 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 227 | diag::err_typecheck_decl_incomplete_type)) { |
| 228 | Param->setInvalidDecl(); |
| 229 | return true; |
| 230 | } |
| 231 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 232 | // C++ [dcl.fct.default]p5 |
| 233 | // A default argument expression is implicitly converted (clause |
| 234 | // 4) to the parameter type. The default argument expression has |
| 235 | // the same semantic constraints as the initializer expression in |
| 236 | // a declaration of a variable of the parameter type, using the |
| 237 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 238 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 239 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 240 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 241 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 242 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 243 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 244 | MultiExprArg(*this, &Arg, 1)); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 245 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 246 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 247 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 248 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 249 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 250 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 252 | // Okay: add the default argument to the parameter |
| 253 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 255 | // We have already instantiated this parameter; provide each of the |
| 256 | // instantiations with the uninstantiated default argument. |
| 257 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 258 | = UnparsedDefaultArgInstantiations.find(Param); |
| 259 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 260 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 261 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 262 | |
| 263 | // We're done tracking this parameter's instantiations. |
| 264 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 265 | } |
| 266 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 267 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 270 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 271 | /// provided for a function parameter is well-formed. If so, attach it |
| 272 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 273 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 274 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 275 | Expr *DefaultArg) { |
| 276 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 277 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 279 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 280 | UnparsedDefaultArgLocs.erase(Param); |
| 281 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 282 | // Default arguments are only permitted in C++ |
| 283 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 284 | Diag(EqualLoc, diag::err_param_default_argument) |
| 285 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 286 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 287 | return; |
| 288 | } |
| 289 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 290 | // Check for unexpanded parameter packs. |
| 291 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 292 | Param->setInvalidDecl(); |
| 293 | return; |
| 294 | } |
| 295 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 296 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 297 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 298 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 299 | Param->setInvalidDecl(); |
| 300 | return; |
| 301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 303 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 306 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 307 | /// argument for a function parameter, but we can't parse it yet |
| 308 | /// because we're inside a class definition. Note that this default |
| 309 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 310 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 311 | SourceLocation EqualLoc, |
| 312 | SourceLocation ArgLoc) { |
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); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 317 | if (Param) |
| 318 | Param->setUnparsedDefaultArg(); |
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[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 323 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 324 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 325 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 326 | if (!param) |
| 327 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 329 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 331 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 333 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 336 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 337 | /// arguments in the declarator, which is not a function declaration |
| 338 | /// or definition and therefore is not permitted to have default |
| 339 | /// arguments. This routine should be invoked for every declarator |
| 340 | /// that is not a function declaration or definition. |
| 341 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 342 | // C++ [dcl.fct.default]p3 |
| 343 | // A default argument expression shall be specified only in the |
| 344 | // parameter-declaration-clause of a function declaration or in a |
| 345 | // template-parameter (14.1). It shall not be specified for a |
| 346 | // parameter pack. If it is specified in a |
| 347 | // parameter-declaration-clause, it shall not occur within a |
| 348 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 349 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 350 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 351 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 352 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 353 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 354 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 355 | if (Param->hasUnparsedDefaultArg()) { |
| 356 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 357 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 358 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 359 | delete Toks; |
| 360 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 361 | } else if (Param->getDefaultArg()) { |
| 362 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 363 | << Param->getDefaultArg()->getSourceRange(); |
| 364 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 371 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 372 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 373 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 374 | // error, false otherwise. |
| 375 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 376 | bool Invalid = false; |
| 377 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 378 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 379 | // For non-template functions, default arguments can be added in |
| 380 | // later declarations of a function in the same |
| 381 | // scope. Declarations in different scopes have completely |
| 382 | // distinct sets of default arguments. That is, declarations in |
| 383 | // inner scopes do not acquire default arguments from |
| 384 | // declarations in outer scopes, and vice versa. In a given |
| 385 | // function declaration, all parameters subsequent to a |
| 386 | // parameter with a default argument shall have default |
| 387 | // arguments supplied in this or previous declarations. A |
| 388 | // default argument shall not be redefined by a later |
| 389 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 390 | // |
| 391 | // C++ [dcl.fct.default]p6: |
| 392 | // Except for member functions of class templates, the default arguments |
| 393 | // in a member function definition that appears outside of the class |
| 394 | // definition are added to the set of default arguments provided by the |
| 395 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 396 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 397 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 398 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 399 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 400 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 401 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 402 | unsigned DiagDefaultParamID = |
| 403 | diag::err_param_default_argument_redefinition; |
| 404 | |
| 405 | // MSVC accepts that default parameters be redefined for member functions |
| 406 | // of template class. The new default parameter's value is ignored. |
| 407 | Invalid = true; |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 408 | if (getLangOptions().MicrosoftExt) { |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 409 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 410 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 411 | // Merge the old default argument into the new parameter. |
| 412 | NewParam->setHasInheritedDefaultArg(); |
| 413 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 414 | NewParam->setUninstantiatedDefaultArg( |
| 415 | OldParam->getUninstantiatedDefaultArg()); |
| 416 | else |
| 417 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 418 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 419 | Invalid = false; |
| 420 | } |
| 421 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 422 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 423 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 424 | // hint here. Alternatively, we could walk the type-source information |
| 425 | // for NewParam to find the last source location in the type... but it |
| 426 | // isn't worth the effort right now. This is the kind of test case that |
| 427 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 428 | // int f(int); |
| 429 | // void g(int (*fp)(int) = f); |
| 430 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 431 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 432 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 433 | |
| 434 | // Look for the function declaration where the default argument was |
| 435 | // actually written, which may be a declaration prior to Old. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 436 | for (FunctionDecl *Older = Old->getPreviousDecl(); |
| 437 | Older; Older = Older->getPreviousDecl()) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 438 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 439 | break; |
| 440 | |
| 441 | OldParam = Older->getParamDecl(p); |
| 442 | } |
| 443 | |
| 444 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 445 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 446 | } else if (OldParam->hasDefaultArg()) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 447 | // Merge the old default argument into the new parameter. |
| 448 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 449 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 450 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 451 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 452 | NewParam->setUninstantiatedDefaultArg( |
| 453 | OldParam->getUninstantiatedDefaultArg()); |
| 454 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 455 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 456 | } else if (NewParam->hasDefaultArg()) { |
| 457 | if (New->getDescribedFunctionTemplate()) { |
| 458 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 459 | Diag(NewParam->getLocation(), |
| 460 | diag::err_param_default_argument_template_redecl) |
| 461 | << NewParam->getDefaultArgRange(); |
| 462 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 463 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 464 | } else if (New->getTemplateSpecializationKind() |
| 465 | != TSK_ImplicitInstantiation && |
| 466 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 467 | // C++ [temp.expr.spec]p21: |
| 468 | // Default function arguments shall not be specified in a declaration |
| 469 | // or a definition for one of the following explicit specializations: |
| 470 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 471 | // - the explicit specialization of a member function template; |
| 472 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 473 | // template where the class template specialization to which the |
| 474 | // member function specialization belongs is implicitly |
| 475 | // instantiated. |
| 476 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 477 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 478 | << New->getDeclName() |
| 479 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 480 | } else if (New->getDeclContext()->isDependentContext()) { |
| 481 | // C++ [dcl.fct.default]p6 (DR217): |
| 482 | // Default arguments for a member function of a class template shall |
| 483 | // be specified on the initial declaration of the member function |
| 484 | // within the class template. |
| 485 | // |
| 486 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 487 | // leads me to the conclusion that one cannot add default function |
| 488 | // arguments for an out-of-line definition of a member function of a |
| 489 | // dependent type. |
| 490 | int WhichKind = 2; |
| 491 | if (CXXRecordDecl *Record |
| 492 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 493 | if (Record->getDescribedClassTemplate()) |
| 494 | WhichKind = 0; |
| 495 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 496 | WhichKind = 1; |
| 497 | else |
| 498 | WhichKind = 2; |
| 499 | } |
| 500 | |
| 501 | Diag(NewParam->getLocation(), |
| 502 | diag::err_param_default_argument_member_template_redecl) |
| 503 | << WhichKind |
| 504 | << NewParam->getDefaultArgRange(); |
Sean Hunt | 9ae60d5 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 505 | } else if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(New)) { |
| 506 | CXXSpecialMember NewSM = getSpecialMember(Ctor), |
| 507 | OldSM = getSpecialMember(cast<CXXConstructorDecl>(Old)); |
| 508 | if (NewSM != OldSM) { |
| 509 | Diag(NewParam->getLocation(),diag::warn_default_arg_makes_ctor_special) |
| 510 | << NewParam->getDefaultArgRange() << NewSM; |
| 511 | Diag(Old->getLocation(), diag::note_previous_declaration_special) |
| 512 | << OldSM; |
| 513 | } |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 518 | // C++11 [dcl.constexpr]p1: If any declaration of a function or function |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 519 | // template has a constexpr specifier then all its declarations shall |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 520 | // contain the constexpr specifier. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 521 | if (New->isConstexpr() != Old->isConstexpr()) { |
| 522 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 523 | << New << New->isConstexpr(); |
| 524 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 525 | Invalid = true; |
| 526 | } |
| 527 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 528 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 529 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 530 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 531 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 534 | /// \brief Merge the exception specifications of two variable declarations. |
| 535 | /// |
| 536 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 537 | /// checks if the redeclaration might have an exception specification and |
| 538 | /// validates compatibility and merges the specs if necessary. |
| 539 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 540 | // Shortcut if exceptions are disabled. |
| 541 | if (!getLangOptions().CXXExceptions) |
| 542 | return; |
| 543 | |
| 544 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 545 | "Should only be called if types are otherwise the same."); |
| 546 | |
| 547 | QualType NewType = New->getType(); |
| 548 | QualType OldType = Old->getType(); |
| 549 | |
| 550 | // We're only interested in pointers and references to functions, as well |
| 551 | // as pointers to member functions. |
| 552 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 553 | NewType = R->getPointeeType(); |
| 554 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 555 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 556 | NewType = P->getPointeeType(); |
| 557 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 558 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 559 | NewType = M->getPointeeType(); |
| 560 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 561 | } |
| 562 | |
| 563 | if (!NewType->isFunctionProtoType()) |
| 564 | return; |
| 565 | |
| 566 | // There's lots of special cases for functions. For function pointers, system |
| 567 | // libraries are hopefully not as broken so that we don't need these |
| 568 | // workarounds. |
| 569 | if (CheckEquivalentExceptionSpec( |
| 570 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 571 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 572 | New->setInvalidDecl(); |
| 573 | } |
| 574 | } |
| 575 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 576 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 577 | /// function declaration are well-formed according to C++ |
| 578 | /// [dcl.fct.default]. |
| 579 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 580 | unsigned NumParams = FD->getNumParams(); |
| 581 | unsigned p; |
| 582 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 583 | bool IsLambda = FD->getOverloadedOperator() == OO_Call && |
| 584 | isa<CXXMethodDecl>(FD) && |
| 585 | cast<CXXMethodDecl>(FD)->getParent()->isLambda(); |
| 586 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 587 | // Find first parameter with a default argument |
| 588 | for (p = 0; p < NumParams; ++p) { |
| 589 | ParmVarDecl *Param = FD->getParamDecl(p); |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 590 | if (Param->hasDefaultArg()) { |
| 591 | // C++11 [expr.prim.lambda]p5: |
| 592 | // [...] Default arguments (8.3.6) shall not be specified in the |
| 593 | // parameter-declaration-clause of a lambda-declarator. |
| 594 | // |
| 595 | // FIXME: Core issue 974 strikes this sentence, we only provide an |
| 596 | // extension warning. |
| 597 | if (IsLambda) |
| 598 | Diag(Param->getLocation(), diag::ext_lambda_default_arguments) |
| 599 | << Param->getDefaultArgRange(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 600 | break; |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 601 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // C++ [dcl.fct.default]p4: |
| 605 | // In a given function declaration, all parameters |
| 606 | // subsequent to a parameter with a default argument shall |
| 607 | // have default arguments supplied in this or previous |
| 608 | // declarations. A default argument shall not be redefined |
| 609 | // by a later declaration (not even to the same value). |
| 610 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 612 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 613 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 614 | if (Param->isInvalidDecl()) |
| 615 | /* We already complained about this parameter. */; |
| 616 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 618 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 619 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 620 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 622 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 624 | LastMissingDefaultArg = p; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | if (LastMissingDefaultArg > 0) { |
| 629 | // Some default arguments were missing. Clear out all of the |
| 630 | // default arguments up to (and including) the last missing |
| 631 | // default argument, so that we leave the function parameters |
| 632 | // in a semantically valid state. |
| 633 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 634 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 635 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 636 | Param->setDefaultArg(0); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 641 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 642 | // CheckConstexprParameterTypes - Check whether a function's parameter types |
| 643 | // are all literal types. If so, return true. If not, produce a suitable |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 644 | // diagnostic and return false. |
| 645 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 646 | const FunctionDecl *FD) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 647 | unsigned ArgIndex = 0; |
| 648 | const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); |
| 649 | for (FunctionProtoType::arg_type_iterator i = FT->arg_type_begin(), |
| 650 | e = FT->arg_type_end(); i != e; ++i, ++ArgIndex) { |
| 651 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 652 | SourceLocation ParamLoc = PD->getLocation(); |
| 653 | if (!(*i)->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 654 | SemaRef.RequireLiteralType(ParamLoc, *i, |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 655 | SemaRef.PDiag(diag::err_constexpr_non_literal_param) |
| 656 | << ArgIndex+1 << PD->getSourceRange() |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 657 | << isa<CXXConstructorDecl>(FD))) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 658 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | // CheckConstexprFunctionDecl - Check whether a function declaration satisfies |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 664 | // the requirements of a constexpr function definition or a constexpr |
| 665 | // constructor definition. If so, return true. If not, produce appropriate |
| 666 | // diagnostics and return false. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 667 | // |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 668 | // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. |
| 669 | bool Sema::CheckConstexprFunctionDecl(const FunctionDecl *NewFD) { |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 670 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 671 | if (MD && MD->isInstance()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 672 | // C++11 [dcl.constexpr]p4: |
| 673 | // The definition of a constexpr constructor shall satisfy the following |
| 674 | // constraints: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 675 | // - the class shall not have any virtual base classes; |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 676 | const CXXRecordDecl *RD = MD->getParent(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 677 | if (RD->getNumVBases()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 678 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 679 | << isa<CXXConstructorDecl>(NewFD) << RD->isStruct() |
| 680 | << RD->getNumVBases(); |
| 681 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 682 | E = RD->vbases_end(); I != E; ++I) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 683 | Diag(I->getLocStart(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 684 | diag::note_constexpr_virtual_base_here) << I->getSourceRange(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 685 | return false; |
| 686 | } |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 690 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 691 | // The definition of a constexpr function shall satisfy the following |
| 692 | // constraints: |
| 693 | // - it shall not be virtual; |
| 694 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 695 | if (Method && Method->isVirtual()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 696 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 697 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 698 | // If it's not obvious why this function is virtual, find an overridden |
| 699 | // function which uses the 'virtual' keyword. |
| 700 | const CXXMethodDecl *WrittenVirtual = Method; |
| 701 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 702 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 703 | if (WrittenVirtual != Method) |
| 704 | Diag(WrittenVirtual->getLocation(), |
| 705 | diag::note_overridden_virtual_function); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | |
| 709 | // - its return type shall be a literal type; |
| 710 | QualType RT = NewFD->getResultType(); |
| 711 | if (!RT->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 712 | RequireLiteralType(NewFD->getLocation(), RT, |
| 713 | PDiag(diag::err_constexpr_non_literal_return))) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 714 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 717 | // - each of its parameter types shall be a literal type; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 718 | if (!CheckConstexprParameterTypes(*this, NewFD)) |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 719 | return false; |
| 720 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 721 | return true; |
| 722 | } |
| 723 | |
| 724 | /// Check the given declaration statement is legal within a constexpr function |
| 725 | /// body. C++0x [dcl.constexpr]p3,p4. |
| 726 | /// |
| 727 | /// \return true if the body is OK, false if we have diagnosed a problem. |
| 728 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
| 729 | DeclStmt *DS) { |
| 730 | // C++0x [dcl.constexpr]p3 and p4: |
| 731 | // The definition of a constexpr function(p3) or constructor(p4) [...] shall |
| 732 | // contain only |
| 733 | for (DeclStmt::decl_iterator DclIt = DS->decl_begin(), |
| 734 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) { |
| 735 | switch ((*DclIt)->getKind()) { |
| 736 | case Decl::StaticAssert: |
| 737 | case Decl::Using: |
| 738 | case Decl::UsingShadow: |
| 739 | case Decl::UsingDirective: |
| 740 | case Decl::UnresolvedUsingTypename: |
| 741 | // - static_assert-declarations |
| 742 | // - using-declarations, |
| 743 | // - using-directives, |
| 744 | continue; |
| 745 | |
| 746 | case Decl::Typedef: |
| 747 | case Decl::TypeAlias: { |
| 748 | // - typedef declarations and alias-declarations that do not define |
| 749 | // classes or enumerations, |
| 750 | TypedefNameDecl *TN = cast<TypedefNameDecl>(*DclIt); |
| 751 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 752 | // Don't allow variably-modified types in constexpr functions. |
| 753 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 754 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 755 | << TL.getSourceRange() << TL.getType() |
| 756 | << isa<CXXConstructorDecl>(Dcl); |
| 757 | return false; |
| 758 | } |
| 759 | continue; |
| 760 | } |
| 761 | |
| 762 | case Decl::Enum: |
| 763 | case Decl::CXXRecord: |
| 764 | // As an extension, we allow the declaration (but not the definition) of |
| 765 | // classes and enumerations in all declarations, not just in typedef and |
| 766 | // alias declarations. |
| 767 | if (cast<TagDecl>(*DclIt)->isThisDeclarationADefinition()) { |
| 768 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_type_definition) |
| 769 | << isa<CXXConstructorDecl>(Dcl); |
| 770 | return false; |
| 771 | } |
| 772 | continue; |
| 773 | |
| 774 | case Decl::Var: |
| 775 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_var_declaration) |
| 776 | << isa<CXXConstructorDecl>(Dcl); |
| 777 | return false; |
| 778 | |
| 779 | default: |
| 780 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 781 | << isa<CXXConstructorDecl>(Dcl); |
| 782 | return false; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | /// Check that the given field is initialized within a constexpr constructor. |
| 790 | /// |
| 791 | /// \param Dcl The constexpr constructor being checked. |
| 792 | /// \param Field The field being checked. This may be a member of an anonymous |
| 793 | /// struct or union nested within the class being checked. |
| 794 | /// \param Inits All declarations, including anonymous struct/union members and |
| 795 | /// indirect members, for which any initialization was provided. |
| 796 | /// \param Diagnosed Set to true if an error is produced. |
| 797 | static void CheckConstexprCtorInitializer(Sema &SemaRef, |
| 798 | const FunctionDecl *Dcl, |
| 799 | FieldDecl *Field, |
| 800 | llvm::SmallSet<Decl*, 16> &Inits, |
| 801 | bool &Diagnosed) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 802 | if (Field->isUnnamedBitfield()) |
| 803 | return; |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 804 | |
| 805 | if (Field->isAnonymousStructOrUnion() && |
| 806 | Field->getType()->getAsCXXRecordDecl()->isEmpty()) |
| 807 | return; |
| 808 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 809 | if (!Inits.count(Field)) { |
| 810 | if (!Diagnosed) { |
| 811 | SemaRef.Diag(Dcl->getLocation(), diag::err_constexpr_ctor_missing_init); |
| 812 | Diagnosed = true; |
| 813 | } |
| 814 | SemaRef.Diag(Field->getLocation(), diag::note_constexpr_ctor_missing_init); |
| 815 | } else if (Field->isAnonymousStructOrUnion()) { |
| 816 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 817 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 818 | I != E; ++I) |
| 819 | // If an anonymous union contains an anonymous struct of which any member |
| 820 | // is initialized, all members must be initialized. |
| 821 | if (!RD->isUnion() || Inits.count(*I)) |
| 822 | CheckConstexprCtorInitializer(SemaRef, Dcl, *I, Inits, Diagnosed); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /// Check the body for the given constexpr function declaration only contains |
| 827 | /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4. |
| 828 | /// |
| 829 | /// \return true if the body is OK, false if we have diagnosed a problem. |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 830 | bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 831 | if (isa<CXXTryStmt>(Body)) { |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 832 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 833 | // The definition of a constexpr function shall satisfy the following |
| 834 | // constraints: [...] |
| 835 | // - its function-body shall be = delete, = default, or a |
| 836 | // compound-statement |
| 837 | // |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 838 | // C++11 [dcl.constexpr]p4: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 839 | // In the definition of a constexpr constructor, [...] |
| 840 | // - its function-body shall not be a function-try-block; |
| 841 | Diag(Body->getLocStart(), diag::err_constexpr_function_try_block) |
| 842 | << isa<CXXConstructorDecl>(Dcl); |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | // - its function-body shall be [...] a compound-statement that contains only |
| 847 | CompoundStmt *CompBody = cast<CompoundStmt>(Body); |
| 848 | |
| 849 | llvm::SmallVector<SourceLocation, 4> ReturnStmts; |
| 850 | for (CompoundStmt::body_iterator BodyIt = CompBody->body_begin(), |
| 851 | BodyEnd = CompBody->body_end(); BodyIt != BodyEnd; ++BodyIt) { |
| 852 | switch ((*BodyIt)->getStmtClass()) { |
| 853 | case Stmt::NullStmtClass: |
| 854 | // - null statements, |
| 855 | continue; |
| 856 | |
| 857 | case Stmt::DeclStmtClass: |
| 858 | // - static_assert-declarations |
| 859 | // - using-declarations, |
| 860 | // - using-directives, |
| 861 | // - typedef declarations and alias-declarations that do not define |
| 862 | // classes or enumerations, |
| 863 | if (!CheckConstexprDeclStmt(*this, Dcl, cast<DeclStmt>(*BodyIt))) |
| 864 | return false; |
| 865 | continue; |
| 866 | |
| 867 | case Stmt::ReturnStmtClass: |
| 868 | // - and exactly one return statement; |
| 869 | if (isa<CXXConstructorDecl>(Dcl)) |
| 870 | break; |
| 871 | |
| 872 | ReturnStmts.push_back((*BodyIt)->getLocStart()); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 873 | continue; |
| 874 | |
| 875 | default: |
| 876 | break; |
| 877 | } |
| 878 | |
| 879 | Diag((*BodyIt)->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 880 | << isa<CXXConstructorDecl>(Dcl); |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | if (const CXXConstructorDecl *Constructor |
| 885 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 886 | const CXXRecordDecl *RD = Constructor->getParent(); |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 887 | // DR1359: |
| 888 | // - every non-variant non-static data member and base class sub-object |
| 889 | // shall be initialized; |
| 890 | // - if the class is a non-empty union, or for each non-empty anonymous |
| 891 | // union member of a non-union class, exactly one non-static data member |
| 892 | // shall be initialized; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 893 | if (RD->isUnion()) { |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 894 | if (Constructor->getNumCtorInitializers() == 0 && !RD->isEmpty()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 895 | Diag(Dcl->getLocation(), diag::err_constexpr_union_ctor_no_init); |
| 896 | return false; |
| 897 | } |
Richard Smith | 6e43375 | 2011-10-10 16:38:04 +0000 | [diff] [blame] | 898 | } else if (!Constructor->isDependentContext() && |
| 899 | !Constructor->isDelegatingConstructor()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 900 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 901 | |
| 902 | // Skip detailed checking if we have enough initializers, and we would |
| 903 | // allow at most one initializer per member. |
| 904 | bool AnyAnonStructUnionMembers = false; |
| 905 | unsigned Fields = 0; |
| 906 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 907 | E = RD->field_end(); I != E; ++I, ++Fields) { |
| 908 | if ((*I)->isAnonymousStructOrUnion()) { |
| 909 | AnyAnonStructUnionMembers = true; |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | if (AnyAnonStructUnionMembers || |
| 914 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 915 | // Check initialization of non-static data members. Base classes are |
| 916 | // always initialized so do not need to be checked. Dependent bases |
| 917 | // might not have initializers in the member initializer list. |
| 918 | llvm::SmallSet<Decl*, 16> Inits; |
| 919 | for (CXXConstructorDecl::init_const_iterator |
| 920 | I = Constructor->init_begin(), E = Constructor->init_end(); |
| 921 | I != E; ++I) { |
| 922 | if (FieldDecl *FD = (*I)->getMember()) |
| 923 | Inits.insert(FD); |
| 924 | else if (IndirectFieldDecl *ID = (*I)->getIndirectMember()) |
| 925 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 926 | } |
| 927 | |
| 928 | bool Diagnosed = false; |
| 929 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 930 | E = RD->field_end(); I != E; ++I) |
| 931 | CheckConstexprCtorInitializer(*this, Dcl, *I, Inits, Diagnosed); |
| 932 | if (Diagnosed) |
| 933 | return false; |
| 934 | } |
| 935 | } |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 936 | } else { |
| 937 | if (ReturnStmts.empty()) { |
| 938 | Diag(Dcl->getLocation(), diag::err_constexpr_body_no_return); |
| 939 | return false; |
| 940 | } |
| 941 | if (ReturnStmts.size() > 1) { |
| 942 | Diag(ReturnStmts.back(), diag::err_constexpr_body_multiple_return); |
| 943 | for (unsigned I = 0; I < ReturnStmts.size() - 1; ++I) |
| 944 | Diag(ReturnStmts[I], diag::note_constexpr_body_previous_return); |
| 945 | return false; |
| 946 | } |
| 947 | } |
| 948 | |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 949 | // C++11 [dcl.constexpr]p5: |
| 950 | // if no function argument values exist such that the function invocation |
| 951 | // substitution would produce a constant expression, the program is |
| 952 | // ill-formed; no diagnostic required. |
| 953 | // C++11 [dcl.constexpr]p3: |
| 954 | // - every constructor call and implicit conversion used in initializing the |
| 955 | // return value shall be one of those allowed in a constant expression. |
| 956 | // C++11 [dcl.constexpr]p4: |
| 957 | // - every constructor involved in initializing non-static data members and |
| 958 | // base class sub-objects shall be a constexpr constructor. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 959 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 960 | if (!Expr::isPotentialConstantExpr(Dcl, Diags)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 961 | Diag(Dcl->getLocation(), diag::err_constexpr_function_never_constant_expr) |
| 962 | << isa<CXXConstructorDecl>(Dcl); |
| 963 | for (size_t I = 0, N = Diags.size(); I != N; ++I) |
| 964 | Diag(Diags[I].first, Diags[I].second); |
| 965 | return false; |
| 966 | } |
| 967 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 968 | return true; |
| 969 | } |
| 970 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 971 | /// isCurrentClassName - Determine whether the identifier II is the |
| 972 | /// name of the class type currently being defined. In the case of |
| 973 | /// nested classes, this will only return true if II is the name of |
| 974 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 975 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 976 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 977 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 978 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 979 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 980 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 981 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 982 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 983 | } else |
| 984 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 985 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 986 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 987 | return &II == CurDecl->getIdentifier(); |
| 988 | else |
| 989 | return false; |
| 990 | } |
| 991 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 993 | /// |
| 994 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 995 | /// and returns NULL otherwise. |
| 996 | CXXBaseSpecifier * |
| 997 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 998 | SourceRange SpecifierRange, |
| 999 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1000 | TypeSourceInfo *TInfo, |
| 1001 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1002 | QualType BaseType = TInfo->getType(); |
| 1003 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1004 | // C++ [class.union]p1: |
| 1005 | // A union shall not have base classes. |
| 1006 | if (Class->isUnion()) { |
| 1007 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1008 | << SpecifierRange; |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1012 | if (EllipsisLoc.isValid() && |
| 1013 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1014 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1015 | << TInfo->getTypeLoc().getSourceRange(); |
| 1016 | EllipsisLoc = SourceLocation(); |
| 1017 | } |
| 1018 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1019 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1021 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1022 | Access, TInfo, EllipsisLoc); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1023 | |
| 1024 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1025 | |
| 1026 | // Base specifiers must be record types. |
| 1027 | if (!BaseType->isRecordType()) { |
| 1028 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | // C++ [class.union]p1: |
| 1033 | // A union shall not be used as a base class. |
| 1034 | if (BaseType->isUnionType()) { |
| 1035 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | // C++ [class.derived]p2: |
| 1040 | // The class-name in a base-specifier shall not be an incompletely |
| 1041 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1043 | PDiag(diag::err_incomplete_base_class) |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1044 | << SpecifierRange)) { |
| 1045 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1046 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1047 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1048 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1049 | // 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] | 1050 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1051 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1052 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1053 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1054 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 1055 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1056 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1057 | // C++ [class]p3: |
| 1058 | // If a class is marked final and it appears as a base-type-specifier in |
| 1059 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1060 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1061 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 1062 | << CXXBaseDecl->getDeclName(); |
| 1063 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 1064 | << CXXBaseDecl->getDeclName(); |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1068 | if (BaseDecl->isInvalidDecl()) |
| 1069 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1070 | |
| 1071 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1072 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1073 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1074 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1077 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1078 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | /// example: |
| 1080 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1081 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1082 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1083 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1084 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1085 | ParsedType basetype, SourceLocation BaseLoc, |
| 1086 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1087 | if (!classdecl) |
| 1088 | return true; |
| 1089 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1090 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1091 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1092 | if (!Class) |
| 1093 | return true; |
| 1094 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1095 | TypeSourceInfo *TInfo = 0; |
| 1096 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1098 | if (EllipsisLoc.isInvalid() && |
| 1099 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1100 | UPPC_BaseType)) |
| 1101 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1103 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1104 | Virtual, Access, TInfo, |
| 1105 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1106 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1108 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1109 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1111 | /// \brief Performs the actual work of attaching the given base class |
| 1112 | /// specifiers to a C++ class. |
| 1113 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1114 | unsigned NumBases) { |
| 1115 | if (NumBases == 0) |
| 1116 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1117 | |
| 1118 | // Used to keep track of which base types we have already seen, so |
| 1119 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1120 | // that the key is always the unqualified canonical type of the base |
| 1121 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1122 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1123 | |
| 1124 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1125 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1126 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1127 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1129 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1130 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1131 | |
| 1132 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1133 | if (KnownBase) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1134 | // C++ [class.mi]p3: |
| 1135 | // A class shall not be specified as a direct base class of a |
| 1136 | // derived class more than once. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1137 | Diag(Bases[idx]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1138 | diag::err_duplicate_base_class) |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1139 | << KnownBase->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1140 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Delete the duplicate base class specifier; we're going to |
| 1143 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1144 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1145 | |
| 1146 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1147 | } else { |
| 1148 | // Okay, add this new base class. |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1149 | KnownBase = Bases[idx]; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1150 | Bases[NumGoodBases++] = Bases[idx]; |
Fariborz Jahanian | 9158902 | 2011-10-24 17:30:45 +0000 | [diff] [blame] | 1151 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) |
Fariborz Jahanian | 13c7fcc | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 1152 | if (const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl())) |
| 1153 | if (RD->hasAttr<WeakAttr>()) |
| 1154 | Class->addAttr(::new (Context) WeakAttr(SourceRange(), Context)); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1159 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Delete the remaining (good) base class specifiers, since their |
| 1162 | // data has been copied into the CXXRecordDecl. |
| 1163 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1164 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1165 | |
| 1166 | return Invalid; |
| 1167 | } |
| 1168 | |
| 1169 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1170 | /// class, after checking whether there are any duplicate base |
| 1171 | /// classes. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1172 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1173 | unsigned NumBases) { |
| 1174 | if (!ClassDecl || !Bases || !NumBases) |
| 1175 | return; |
| 1176 | |
| 1177 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1178 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1179 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1180 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1181 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1182 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 1183 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 1184 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 1185 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 1186 | return ICT->getDecl(); |
| 1187 | else |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1191 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1192 | /// derived from the type \p Base. |
| 1193 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 1194 | if (!getLangOptions().CPlusPlus) |
| 1195 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1196 | |
| 1197 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 1198 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1199 | return false; |
| 1200 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1201 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 1202 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1203 | return false; |
| 1204 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1205 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1206 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1210 | /// derived from the type \p Base. |
| 1211 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 1212 | if (!getLangOptions().CPlusPlus) |
| 1213 | return false; |
| 1214 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1215 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 1216 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1217 | return false; |
| 1218 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1219 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 1220 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1221 | return false; |
| 1222 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1223 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1224 | } |
| 1225 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1226 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1227 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1228 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1229 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1230 | |
| 1231 | const CXXBasePath &Path = Paths.front(); |
| 1232 | |
| 1233 | // We first go backward and check if we have a virtual base. |
| 1234 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1235 | // the nearest virtual base. |
| 1236 | unsigned Start = 0; |
| 1237 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1238 | if (Path[I - 1].Base->isVirtual()) { |
| 1239 | Start = I - 1; |
| 1240 | break; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | // Now add all bases. |
| 1245 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1246 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1249 | /// \brief Determine whether the given base path includes a virtual |
| 1250 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1251 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 1252 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 1253 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1254 | B != BEnd; ++B) |
| 1255 | if ((*B)->isVirtual()) |
| 1256 | return true; |
| 1257 | |
| 1258 | return false; |
| 1259 | } |
| 1260 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1261 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1262 | /// conversion (where Derived and Base are class types) is |
| 1263 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1264 | /// that all of the base classes are accessible). Returns true |
| 1265 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1266 | /// otherwise. Loc is the location where this routine should point to |
| 1267 | /// if there is an error, and Range is the source range to highlight |
| 1268 | /// if there is an error. |
| 1269 | bool |
| 1270 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1271 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1272 | unsigned AmbigiousBaseConvID, |
| 1273 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1274 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1275 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1276 | // First, determine whether the path from Derived to Base is |
| 1277 | // ambiguous. This is slightly more expensive than checking whether |
| 1278 | // the Derived to Base conversion exists, because here we need to |
| 1279 | // explore multiple paths to determine if there is an ambiguity. |
| 1280 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1281 | /*DetectVirtual=*/false); |
| 1282 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1283 | assert(DerivationOkay && |
| 1284 | "Can only be used with a derived-to-base conversion"); |
| 1285 | (void)DerivationOkay; |
| 1286 | |
| 1287 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1288 | if (InaccessibleBaseID) { |
| 1289 | // Check that the base class can be accessed. |
| 1290 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1291 | InaccessibleBaseID)) { |
| 1292 | case AR_inaccessible: |
| 1293 | return true; |
| 1294 | case AR_accessible: |
| 1295 | case AR_dependent: |
| 1296 | case AR_delayed: |
| 1297 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1298 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1299 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1300 | |
| 1301 | // Build a base path if necessary. |
| 1302 | if (BasePath) |
| 1303 | BuildBasePathArray(Paths, *BasePath); |
| 1304 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | // We know that the derived-to-base conversion is ambiguous, and |
| 1308 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1309 | // search just one more time to compute all of the possible paths so |
| 1310 | // that we can print them out. This is more expensive than any of |
| 1311 | // the previous derived-to-base checks we've done, but at this point |
| 1312 | // performance isn't as much of an issue. |
| 1313 | Paths.clear(); |
| 1314 | Paths.setRecordingPaths(true); |
| 1315 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1316 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1317 | (void)StillOkay; |
| 1318 | |
| 1319 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1320 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1321 | // conversions in the diagnostic. We only print one of the paths |
| 1322 | // to each base class subobject. |
| 1323 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1324 | |
| 1325 | Diag(Loc, AmbigiousBaseConvID) |
| 1326 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1327 | return true; |
| 1328 | } |
| 1329 | |
| 1330 | bool |
| 1331 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1332 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1333 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1334 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1335 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1336 | IgnoreAccess ? 0 |
| 1337 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1338 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1339 | Loc, Range, DeclarationName(), |
| 1340 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | /// @brief Builds a string representing ambiguous paths from a |
| 1345 | /// specific derived class to different subobjects of the same base |
| 1346 | /// class. |
| 1347 | /// |
| 1348 | /// This function builds a string that can be used in error messages |
| 1349 | /// to show the different paths that one can take through the |
| 1350 | /// inheritance hierarchy to go from the derived class to different |
| 1351 | /// subobjects of a base class. The result looks something like this: |
| 1352 | /// @code |
| 1353 | /// struct D -> struct B -> struct A |
| 1354 | /// struct D -> struct C -> struct A |
| 1355 | /// @endcode |
| 1356 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1357 | std::string PathDisplayStr; |
| 1358 | std::set<unsigned> DisplayedPaths; |
| 1359 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1360 | Path != Paths.end(); ++Path) { |
| 1361 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1362 | // We haven't displayed a path to this particular base |
| 1363 | // class subobject yet. |
| 1364 | PathDisplayStr += "\n "; |
| 1365 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1366 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1367 | Element != Path->end(); ++Element) |
| 1368 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | return PathDisplayStr; |
| 1373 | } |
| 1374 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1375 | //===----------------------------------------------------------------------===// |
| 1376 | // C++ class member Handling |
| 1377 | //===----------------------------------------------------------------------===// |
| 1378 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1379 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1380 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1381 | SourceLocation ASLoc, |
| 1382 | SourceLocation ColonLoc, |
| 1383 | AttributeList *Attrs) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1384 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1385 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1386 | ASLoc, ColonLoc); |
| 1387 | CurContext->addHiddenDecl(ASDecl); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1388 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1391 | /// CheckOverrideControl - Check C++0x override control semantics. |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1392 | void Sema::CheckOverrideControl(const Decl *D) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1393 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1394 | if (!MD || !MD->isVirtual()) |
| 1395 | return; |
| 1396 | |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1397 | if (MD->isDependentContext()) |
| 1398 | return; |
| 1399 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1400 | // C++0x [class.virtual]p3: |
| 1401 | // If a virtual function is marked with the virt-specifier override and does |
| 1402 | // not override a member function of a base class, |
| 1403 | // the program is ill-formed. |
| 1404 | bool HasOverriddenMethods = |
| 1405 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1406 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) { |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1407 | Diag(MD->getLocation(), |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1408 | diag::err_function_marked_override_not_overriding) |
| 1409 | << MD->getDeclName(); |
| 1410 | return; |
| 1411 | } |
| 1412 | } |
| 1413 | |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1414 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
| 1415 | /// function overrides a virtual member function marked 'final', according to |
| 1416 | /// C++0x [class.virtual]p3. |
| 1417 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1418 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1419 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1420 | return false; |
| 1421 | |
| 1422 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 1423 | << New->getDeclName(); |
| 1424 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1425 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1428 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1429 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1430 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
| 1431 | /// one has been parsed, and 'HasDeferredInit' is true if an initializer is |
| 1432 | /// present but parsing it has been deferred. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1433 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1434 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1435 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1436 | Expr *BW, const VirtSpecifiers &VS, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1437 | bool HasDeferredInit) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1438 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1439 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1440 | DeclarationName Name = NameInfo.getName(); |
| 1441 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1442 | |
| 1443 | // For anonymous bitfields, the location should point to the type. |
| 1444 | if (Loc.isInvalid()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1445 | Loc = D.getLocStart(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1446 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1447 | Expr *BitWidth = static_cast<Expr*>(BW); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1448 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1449 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1450 | assert(!DS.isFriendSpecified()); |
| 1451 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1452 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1453 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1454 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1455 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1456 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1457 | // data members and cannot be applied to names declared const or static, |
| 1458 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1459 | switch (DS.getStorageClassSpec()) { |
| 1460 | case DeclSpec::SCS_unspecified: |
| 1461 | case DeclSpec::SCS_typedef: |
| 1462 | case DeclSpec::SCS_static: |
| 1463 | // FALL THROUGH. |
| 1464 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1465 | case DeclSpec::SCS_mutable: |
| 1466 | if (isFunc) { |
| 1467 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1468 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1469 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1470 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1472 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1473 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1474 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1475 | } |
| 1476 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1477 | default: |
| 1478 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1479 | Diag(DS.getStorageClassSpecLoc(), |
| 1480 | diag::err_storageclass_invalid_for_member); |
| 1481 | else |
| 1482 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 1483 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1484 | } |
| 1485 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1486 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1487 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1488 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1489 | |
| 1490 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1491 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1492 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1493 | |
| 1494 | // Data members must have identifiers for names. |
| 1495 | if (Name.getNameKind() != DeclarationName::Identifier) { |
| 1496 | Diag(Loc, diag::err_bad_variable_name) |
| 1497 | << Name; |
| 1498 | return 0; |
| 1499 | } |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1500 | |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1501 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1502 | |
| 1503 | // Member field could not be with "template" keyword. |
| 1504 | // So TemplateParameterLists should be empty in this case. |
| 1505 | if (TemplateParameterLists.size()) { |
| 1506 | TemplateParameterList* TemplateParams = TemplateParameterLists.get()[0]; |
| 1507 | if (TemplateParams->size()) { |
| 1508 | // There is no such thing as a member field template. |
| 1509 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 1510 | << II |
| 1511 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1512 | TemplateParams->getRAngleLoc()); |
| 1513 | } else { |
| 1514 | // There is an extraneous 'template<>' for this member. |
| 1515 | Diag(TemplateParams->getTemplateLoc(), |
| 1516 | diag::err_template_member_noparams) |
| 1517 | << II |
| 1518 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1519 | TemplateParams->getRAngleLoc()); |
| 1520 | } |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1524 | if (SS.isSet() && !SS.isInvalid()) { |
| 1525 | // The user provided a superfluous scope specifier inside a class |
| 1526 | // definition: |
| 1527 | // |
| 1528 | // class X { |
| 1529 | // int X::member; |
| 1530 | // }; |
| 1531 | DeclContext *DC = 0; |
| 1532 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 1533 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
Douglas Gregor | 5d8419c | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 1534 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1535 | else |
| 1536 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1537 | << Name << SS.getRange(); |
Douglas Gregor | 5d8419c | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 1538 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1539 | SS.clear(); |
| 1540 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1541 | |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1542 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1543 | HasDeferredInit, AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1544 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1545 | } else { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1546 | assert(!HasDeferredInit); |
| 1547 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1548 | Member = HandleDeclarator(S, D, move(TemplateParameterLists)); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1549 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1550 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1551 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1552 | |
| 1553 | // Non-instance-fields can't have a bitfield. |
| 1554 | if (BitWidth) { |
| 1555 | if (Member->isInvalidDecl()) { |
| 1556 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1557 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1558 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1559 | // "static member 'A' cannot be a bit-field" |
| 1560 | Diag(Loc, diag::err_static_not_bitfield) |
| 1561 | << Name << BitWidth->getSourceRange(); |
| 1562 | } else if (isa<TypedefDecl>(Member)) { |
| 1563 | // "typedef member 'x' cannot be a bit-field" |
| 1564 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1565 | << Name << BitWidth->getSourceRange(); |
| 1566 | } else { |
| 1567 | // A function typedef ("typedef int f(); f a;"). |
| 1568 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1569 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1571 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1574 | BitWidth = 0; |
| 1575 | Member->setInvalidDecl(); |
| 1576 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1577 | |
| 1578 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1580 | // If we have declared a member function template, set the access of the |
| 1581 | // templated declaration as well. |
| 1582 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1583 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1584 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1585 | |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1586 | if (VS.isOverrideSpecified()) { |
| 1587 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1588 | if (!MD || !MD->isVirtual()) { |
| 1589 | Diag(Member->getLocStart(), |
| 1590 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1591 | << "override" << FixItHint::CreateRemoval(VS.getOverrideLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1592 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1593 | MD->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1594 | } |
| 1595 | if (VS.isFinalSpecified()) { |
| 1596 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1597 | if (!MD || !MD->isVirtual()) { |
| 1598 | Diag(Member->getLocStart(), |
| 1599 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1600 | << "final" << FixItHint::CreateRemoval(VS.getFinalLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1601 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1602 | MD->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1603 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1605 | if (VS.getLastLocation().isValid()) { |
| 1606 | // Update the end location of a method that has a virt-specifiers. |
| 1607 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 1608 | MD->setRangeEnd(VS.getLastLocation()); |
| 1609 | } |
| 1610 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1611 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1612 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1613 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1614 | |
John McCall | b25b295 | 2011-02-15 07:12:36 +0000 | [diff] [blame] | 1615 | if (isInstField) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1616 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1617 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1620 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
Richard Smith | 0ff6f8f | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 1621 | /// in-class initializer for a non-static C++ class member, and after |
| 1622 | /// instantiating an in-class initializer in a class template. Such actions |
| 1623 | /// are deferred until the class is complete. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1624 | void |
| 1625 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation EqualLoc, |
| 1626 | Expr *InitExpr) { |
| 1627 | FieldDecl *FD = cast<FieldDecl>(D); |
| 1628 | |
| 1629 | if (!InitExpr) { |
| 1630 | FD->setInvalidDecl(); |
| 1631 | FD->removeInClassInitializer(); |
| 1632 | return; |
| 1633 | } |
| 1634 | |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 1635 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 1636 | FD->setInvalidDecl(); |
| 1637 | FD->removeInClassInitializer(); |
| 1638 | return; |
| 1639 | } |
| 1640 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1641 | ExprResult Init = InitExpr; |
| 1642 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1643 | if (isa<InitListExpr>(InitExpr) && isStdInitializerList(FD->getType(), 0)) { |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1644 | Diag(FD->getLocation(), diag::warn_dangling_std_initializer_list) |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1645 | << /*at end of ctor*/1 << InitExpr->getSourceRange(); |
| 1646 | } |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1647 | Expr **Inits = &InitExpr; |
| 1648 | unsigned NumInits = 1; |
| 1649 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
| 1650 | InitializationKind Kind = EqualLoc.isInvalid() |
| 1651 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
| 1652 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), EqualLoc); |
| 1653 | InitializationSequence Seq(*this, Entity, Kind, Inits, NumInits); |
| 1654 | Init = Seq.Perform(*this, Entity, Kind, MultiExprArg(Inits, NumInits)); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1655 | if (Init.isInvalid()) { |
| 1656 | FD->setInvalidDecl(); |
| 1657 | return; |
| 1658 | } |
| 1659 | |
| 1660 | CheckImplicitConversions(Init.get(), EqualLoc); |
| 1661 | } |
| 1662 | |
| 1663 | // C++0x [class.base.init]p7: |
| 1664 | // The initialization of each base and member constitutes a |
| 1665 | // full-expression. |
| 1666 | Init = MaybeCreateExprWithCleanups(Init); |
| 1667 | if (Init.isInvalid()) { |
| 1668 | FD->setInvalidDecl(); |
| 1669 | return; |
| 1670 | } |
| 1671 | |
| 1672 | InitExpr = Init.release(); |
| 1673 | |
| 1674 | FD->setInClassInitializer(InitExpr); |
| 1675 | } |
| 1676 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1677 | /// \brief Find the direct and/or virtual base specifiers that |
| 1678 | /// correspond to the given base type, for use in base initialization |
| 1679 | /// within a constructor. |
| 1680 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1681 | CXXRecordDecl *ClassDecl, |
| 1682 | QualType BaseType, |
| 1683 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1684 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1685 | // First, check for a direct base class. |
| 1686 | DirectBaseSpec = 0; |
| 1687 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1688 | = ClassDecl->bases_begin(); |
| 1689 | Base != ClassDecl->bases_end(); ++Base) { |
| 1690 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1691 | // We found a direct base of this type. That's what we're |
| 1692 | // initializing. |
| 1693 | DirectBaseSpec = &*Base; |
| 1694 | break; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | // Check for a virtual base class. |
| 1699 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1700 | // there are no virtual bases. |
| 1701 | VirtualBaseSpec = 0; |
| 1702 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1703 | // We haven't found a base yet; search the class hierarchy for a |
| 1704 | // virtual base class. |
| 1705 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1706 | /*DetectVirtual=*/false); |
| 1707 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1708 | BaseType, Paths)) { |
| 1709 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1710 | Path != Paths.end(); ++Path) { |
| 1711 | if (Path->back().Base->isVirtual()) { |
| 1712 | VirtualBaseSpec = Path->back().Base; |
| 1713 | break; |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | return DirectBaseSpec || VirtualBaseSpec; |
| 1720 | } |
| 1721 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1722 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 1723 | MemInitResult |
| 1724 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 1725 | Scope *S, |
| 1726 | CXXScopeSpec &SS, |
| 1727 | IdentifierInfo *MemberOrBase, |
| 1728 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 1729 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1730 | SourceLocation IdLoc, |
| 1731 | Expr *InitList, |
| 1732 | SourceLocation EllipsisLoc) { |
| 1733 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1734 | DS, IdLoc, InitList, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 1735 | EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | /// \brief Handle a C++ member initializer using parentheses syntax. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1739 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1740 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1741 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1742 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1743 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1744 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 1745 | const DeclSpec &DS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1746 | SourceLocation IdLoc, |
| 1747 | SourceLocation LParenLoc, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1748 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1749 | SourceLocation RParenLoc, |
| 1750 | SourceLocation EllipsisLoc) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1751 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1752 | RParenLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1753 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1754 | DS, IdLoc, List, EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 1757 | namespace { |
| 1758 | |
Kaelyn Uhrain | dc98cd0 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 1759 | // Callback to only accept typo corrections that can be a valid C++ member |
| 1760 | // intializer: either a non-static field member or a base class. |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 1761 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1762 | public: |
| 1763 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 1764 | : ClassDecl(ClassDecl) {} |
| 1765 | |
| 1766 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1767 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 1768 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 1769 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
| 1770 | else |
| 1771 | return isa<TypeDecl>(ND); |
| 1772 | } |
| 1773 | return false; |
| 1774 | } |
| 1775 | |
| 1776 | private: |
| 1777 | CXXRecordDecl *ClassDecl; |
| 1778 | }; |
| 1779 | |
| 1780 | } |
| 1781 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1782 | /// \brief Handle a C++ member initializer. |
| 1783 | MemInitResult |
| 1784 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 1785 | Scope *S, |
| 1786 | CXXScopeSpec &SS, |
| 1787 | IdentifierInfo *MemberOrBase, |
| 1788 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 1789 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1790 | SourceLocation IdLoc, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1791 | Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1792 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1793 | if (!ConstructorD) |
| 1794 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1796 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | |
| 1798 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1799 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1800 | if (!Constructor) { |
| 1801 | // The user wrote a constructor initializer on a function that is |
| 1802 | // not a C++ constructor. Ignore the error for now, because we may |
| 1803 | // have more member initializers coming; we'll diagnose it just |
| 1804 | // once in ActOnMemInitializers. |
| 1805 | return true; |
| 1806 | } |
| 1807 | |
| 1808 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1809 | |
| 1810 | // C++ [class.base.init]p2: |
| 1811 | // 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] | 1812 | // constructor's class and, if not found in that scope, are looked |
| 1813 | // up in the scope containing the constructor's definition. |
| 1814 | // [Note: if the constructor's class contains a member with the |
| 1815 | // same name as a direct or virtual base class of the class, a |
| 1816 | // mem-initializer-id naming the member or base class and composed |
| 1817 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1818 | // mem-initializer-id for the hidden base class may be specified |
| 1819 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1820 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1821 | // Look for a member, first. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1823 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1824 | if (Result.first != Result.second) { |
Peter Collingbourne | dc69be2 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 1825 | ValueDecl *Member; |
| 1826 | if ((Member = dyn_cast<FieldDecl>(*Result.first)) || |
| 1827 | (Member = dyn_cast<IndirectFieldDecl>(*Result.first))) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1828 | if (EllipsisLoc.isValid()) |
| 1829 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1830 | << MemberOrBase |
| 1831 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 1832 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1833 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1834 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1835 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1836 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1837 | // 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] | 1838 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1839 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1840 | |
| 1841 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1842 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 1843 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 1844 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1845 | } else { |
| 1846 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1847 | LookupParsedName(R, S, &SS); |
| 1848 | |
| 1849 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1850 | if (!TyD) { |
| 1851 | if (R.isAmbiguous()) return true; |
| 1852 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1853 | // We don't want access-control diagnostics here. |
| 1854 | R.suppressDiagnostics(); |
| 1855 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1856 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1857 | bool NotUnknownSpecialization = false; |
| 1858 | DeclContext *DC = computeDeclContext(SS, false); |
| 1859 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1860 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1861 | |
| 1862 | if (!NotUnknownSpecialization) { |
| 1863 | // When the scope specifier can refer to a member of an unknown |
| 1864 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 1865 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 1866 | SS.getWithLocInContext(Context), |
| 1867 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1868 | if (BaseType.isNull()) |
| 1869 | return true; |
| 1870 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1871 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1872 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1876 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1877 | TypoCorrection Corr; |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 1878 | MemInitializerValidatorCCC Validator(ClassDecl); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1879 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1880 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1881 | Validator, ClassDecl))) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1882 | std::string CorrectedStr(Corr.getAsString(getLangOptions())); |
| 1883 | std::string CorrectedQuotedStr(Corr.getQuoted(getLangOptions())); |
| 1884 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 1885 | // We have found a non-static data member with a similar |
| 1886 | // name to what was typed; complain and initialize that |
| 1887 | // member. |
| 1888 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1889 | << MemberOrBase << true << CorrectedQuotedStr |
| 1890 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1891 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1892 | << CorrectedQuotedStr; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1893 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1894 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1895 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1896 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1897 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1898 | if (FindBaseInitializer(*this, ClassDecl, |
| 1899 | Context.getTypeDeclType(Type), |
| 1900 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1901 | // We have found a direct or virtual base class with a |
| 1902 | // similar name to what was typed; complain and initialize |
| 1903 | // that base class. |
| 1904 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1905 | << MemberOrBase << false << CorrectedQuotedStr |
| 1906 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1907 | |
| 1908 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1909 | : VirtualBaseSpec; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1910 | Diag(BaseSpec->getLocStart(), |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1911 | diag::note_base_class_specified_here) |
| 1912 | << BaseSpec->getType() |
| 1913 | << BaseSpec->getSourceRange(); |
| 1914 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1915 | TyD = Type; |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1920 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1921 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1922 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1923 | return true; |
| 1924 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1927 | if (BaseType.isNull()) { |
| 1928 | BaseType = Context.getTypeDeclType(TyD); |
| 1929 | if (SS.isSet()) { |
| 1930 | NestedNameSpecifier *Qualifier = |
| 1931 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1932 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1933 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1934 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1935 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1936 | } |
| 1937 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1939 | if (!TInfo) |
| 1940 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1941 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1942 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1945 | /// Checks a member initializer expression for cases where reference (or |
| 1946 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1947 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 1948 | Expr *Init, |
| 1949 | SourceLocation IdLoc) { |
| 1950 | QualType MemberTy = Member->getType(); |
| 1951 | |
| 1952 | // We only handle pointers and references currently. |
| 1953 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 1954 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 1955 | return; |
| 1956 | |
| 1957 | const bool IsPointer = MemberTy->isPointerType(); |
| 1958 | if (IsPointer) { |
| 1959 | if (const UnaryOperator *Op |
| 1960 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 1961 | // The only case we're worried about with pointers requires taking the |
| 1962 | // address. |
| 1963 | if (Op->getOpcode() != UO_AddrOf) |
| 1964 | return; |
| 1965 | |
| 1966 | Init = Op->getSubExpr(); |
| 1967 | } else { |
| 1968 | // We only handle address-of expression initializers for pointers. |
| 1969 | return; |
| 1970 | } |
| 1971 | } |
| 1972 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1973 | if (isa<MaterializeTemporaryExpr>(Init->IgnoreParens())) { |
| 1974 | // Taking the address of a temporary will be diagnosed as a hard error. |
| 1975 | if (IsPointer) |
| 1976 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1977 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1978 | S.Diag(Init->getExprLoc(), diag::warn_bind_ref_member_to_temporary) |
| 1979 | << Member << Init->getSourceRange(); |
| 1980 | } else if (const DeclRefExpr *DRE |
| 1981 | = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
| 1982 | // We only warn when referring to a non-reference parameter declaration. |
| 1983 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 1984 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1985 | return; |
| 1986 | |
| 1987 | S.Diag(Init->getExprLoc(), |
| 1988 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 1989 | : diag::warn_bind_ref_member_to_parameter) |
| 1990 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1991 | } else { |
| 1992 | // Other initializers are fine. |
| 1993 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1994 | } |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 1995 | |
| 1996 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 1997 | << (unsigned)IsPointer; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2000 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 2001 | /// containing the field that is being initialized. Returns true if there is an |
| 2002 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 2003 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 2004 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2005 | const ValueDecl *LhsField, |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 2006 | SourceLocation *L) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2007 | assert(isa<FieldDecl>(LhsField) || isa<IndirectFieldDecl>(LhsField)); |
| 2008 | |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 2009 | if (isa<CallExpr>(S)) { |
| 2010 | // Do not descend into function calls or constructors, as the use |
| 2011 | // of an uninitialized field may be valid. One would have to inspect |
| 2012 | // the contents of the function/ctor to determine if it is safe or not. |
| 2013 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 2014 | // may be safe, depending on what the function/ctor does. |
| 2015 | return false; |
| 2016 | } |
| 2017 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 2018 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 2019 | |
| 2020 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 2021 | // The member expression points to a static data member. |
| 2022 | assert(VD->isStaticDataMember() && |
| 2023 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 2024 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 2025 | return false; |
| 2026 | } |
| 2027 | |
| 2028 | if (isa<EnumConstantDecl>(RhsField)) { |
| 2029 | // The member expression points to an enum. |
| 2030 | return false; |
| 2031 | } |
| 2032 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2033 | if (RhsField == LhsField) { |
| 2034 | // Initializing a field with itself. Throw a warning. |
| 2035 | // But wait; there are exceptions! |
| 2036 | // Exception #1: The field may not belong to this record. |
| 2037 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 2038 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2039 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 2040 | // Even though the field matches, it does not belong to this record. |
| 2041 | return false; |
| 2042 | } |
| 2043 | // None of the exceptions triggered; return true to indicate an |
| 2044 | // uninitialized field was used. |
| 2045 | *L = ME->getMemberLoc(); |
| 2046 | return true; |
| 2047 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2048 | } else if (isa<UnaryExprOrTypeTraitExpr>(S)) { |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 2049 | // sizeof/alignof doesn't reference contents, do not warn. |
| 2050 | return false; |
| 2051 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 2052 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 2053 | // in the same expression but it would be rare; and weird). |
| 2054 | if (UOE->getOpcode() == UO_AddrOf) |
| 2055 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2056 | } |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 2057 | for (Stmt::const_child_range it = S->children(); it; ++it) { |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 2058 | if (!*it) { |
| 2059 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2060 | continue; |
| 2061 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 2062 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 2063 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2064 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 2065 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2068 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2069 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2070 | SourceLocation IdLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2071 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 2072 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 2073 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2074 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 2075 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2076 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2077 | return true; |
| 2078 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 2079 | if (Member->isInvalidDecl()) |
| 2080 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2081 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2082 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2083 | // foo(foo) |
| 2084 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 2085 | // TODO: implement -Wuninitialized and fold this into that framework. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2086 | Expr **Args; |
| 2087 | unsigned NumArgs; |
| 2088 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2089 | Args = ParenList->getExprs(); |
| 2090 | NumArgs = ParenList->getNumExprs(); |
| 2091 | } else { |
| 2092 | InitListExpr *InitList = cast<InitListExpr>(Init); |
| 2093 | Args = InitList->getInits(); |
| 2094 | NumArgs = InitList->getNumInits(); |
| 2095 | } |
| 2096 | for (unsigned i = 0; i < NumArgs; ++i) { |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2097 | SourceLocation L; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2098 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2099 | // FIXME: Return true in the case when other fields are used before being |
| 2100 | // uninitialized. For example, let this field be the i'th field. When |
| 2101 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 2102 | // fields are used, as they are not yet initialized. |
| 2103 | // Right now we are only handling the case where the i'th field uses |
| 2104 | // itself in its initializer. |
| 2105 | Diag(L, diag::warn_field_is_uninit); |
| 2106 | } |
| 2107 | } |
| 2108 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2109 | SourceRange InitRange = Init->getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2110 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2111 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2112 | // Can't check initialization for a member of dependent type or when |
| 2113 | // any of the arguments are type-dependent expressions. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2114 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2115 | } else { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2116 | bool InitList = false; |
| 2117 | if (isa<InitListExpr>(Init)) { |
| 2118 | InitList = true; |
| 2119 | Args = &Init; |
| 2120 | NumArgs = 1; |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 2121 | |
| 2122 | if (isStdInitializerList(Member->getType(), 0)) { |
| 2123 | Diag(IdLoc, diag::warn_dangling_std_initializer_list) |
| 2124 | << /*at end of ctor*/1 << InitRange; |
| 2125 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2128 | // Initialize the member. |
| 2129 | InitializedEntity MemberEntity = |
| 2130 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 2131 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 2132 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2133 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 2134 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 2135 | InitRange.getEnd()); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2136 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2137 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 2138 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, |
| 2139 | MultiExprArg(*this, Args, NumArgs), |
| 2140 | 0); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2141 | if (MemberInit.isInvalid()) |
| 2142 | return true; |
| 2143 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2144 | CheckImplicitConversions(MemberInit.get(), |
| 2145 | InitRange.getBegin()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2146 | |
| 2147 | // C++0x [class.base.init]p7: |
| 2148 | // The initialization of each base and member constitutes a |
| 2149 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2150 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2151 | if (MemberInit.isInvalid()) |
| 2152 | return true; |
| 2153 | |
| 2154 | // If we are in a dependent context, template instantiation will |
| 2155 | // perform this type-checking again. Just save the arguments that we |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2156 | // received. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2157 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2158 | // of the information that we have about the member |
| 2159 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2160 | // 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] | 2161 | if (CurContext->isDependentContext()) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2162 | // The existing Init will do fine. |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2163 | } else { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2164 | Init = MemberInit.get(); |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2165 | CheckForDanglingReferenceOrPointer(*this, Member, Init, IdLoc); |
| 2166 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2169 | if (DirectMember) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2170 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 2171 | InitRange.getBegin(), Init, |
| 2172 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2173 | } else { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2174 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 2175 | InitRange.getBegin(), Init, |
| 2176 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2177 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2180 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2181 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2182 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2183 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2184 | if (!LangOpts.CPlusPlus0x) |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2185 | return Diag(NameLoc, diag::err_delegating_ctor) |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2186 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2187 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 2188 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2189 | bool InitList = true; |
| 2190 | Expr **Args = &Init; |
| 2191 | unsigned NumArgs = 1; |
| 2192 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2193 | InitList = false; |
| 2194 | Args = ParenList->getExprs(); |
| 2195 | NumArgs = ParenList->getNumExprs(); |
| 2196 | } |
| 2197 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2198 | SourceRange InitRange = Init->getSourceRange(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2199 | // Initialize the object. |
| 2200 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 2201 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 2202 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2203 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 2204 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 2205 | InitRange.getEnd()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2206 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args, NumArgs); |
| 2207 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
| 2208 | MultiExprArg(*this, Args,NumArgs), |
| 2209 | 0); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2210 | if (DelegationInit.isInvalid()) |
| 2211 | return true; |
| 2212 | |
Matt Beaumont-Gay | 2eb0ce3 | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 2213 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 2214 | "Delegating constructor with no target?"); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2215 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2216 | CheckImplicitConversions(DelegationInit.get(), InitRange.getBegin()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2217 | |
| 2218 | // C++0x [class.base.init]p7: |
| 2219 | // The initialization of each base and member constitutes a |
| 2220 | // full-expression. |
| 2221 | DelegationInit = MaybeCreateExprWithCleanups(DelegationInit); |
| 2222 | if (DelegationInit.isInvalid()) |
| 2223 | return true; |
| 2224 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2225 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2226 | DelegationInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2227 | InitRange.getEnd()); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2231 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2232 | Expr *Init, CXXRecordDecl *ClassDecl, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2233 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2234 | SourceLocation BaseLoc |
| 2235 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2237 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 2238 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 2239 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2240 | |
| 2241 | // C++ [class.base.init]p2: |
| 2242 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2243 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2244 | // of that class, the mem-initializer is ill-formed. A |
| 2245 | // mem-initializer-list can initialize a base class using any |
| 2246 | // name that denotes that base class type. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2247 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2248 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2249 | SourceRange InitRange = Init->getSourceRange(); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2250 | if (EllipsisLoc.isValid()) { |
| 2251 | // This is a pack expansion. |
| 2252 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 2253 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2254 | << SourceRange(BaseLoc, InitRange.getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2256 | EllipsisLoc = SourceLocation(); |
| 2257 | } |
| 2258 | } else { |
| 2259 | // Check for any unexpanded parameter packs. |
| 2260 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 2261 | return true; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2262 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2263 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2264 | return true; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2265 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2266 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2267 | // Check for direct and virtual base classes. |
| 2268 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 2269 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 2270 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2271 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 2272 | BaseType)) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2273 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2274 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2275 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 2276 | VirtualBaseSpec); |
| 2277 | |
| 2278 | // C++ [base.class.init]p2: |
| 2279 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 2280 | // constructor's class or a direct or virtual base of that class, the |
| 2281 | // mem-initializer is ill-formed. |
| 2282 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 2283 | // If the class has any dependent bases, then it's possible that |
| 2284 | // one of those types will resolve to the same type as |
| 2285 | // BaseType. Therefore, just treat this as a dependent base |
| 2286 | // class initialization. FIXME: Should we try to check the |
| 2287 | // initialization anyway? It seems odd. |
| 2288 | if (ClassDecl->hasAnyDependentBases()) |
| 2289 | Dependent = true; |
| 2290 | else |
| 2291 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 2292 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 2293 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | if (Dependent) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2298 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2300 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 2301 | /*IsVirtual=*/false, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2302 | InitRange.getBegin(), Init, |
| 2303 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2304 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2305 | |
| 2306 | // C++ [base.class.init]p2: |
| 2307 | // If a mem-initializer-id is ambiguous because it designates both |
| 2308 | // a direct non-virtual base class and an inherited virtual base |
| 2309 | // class, the mem-initializer is ill-formed. |
| 2310 | if (DirectBaseSpec && VirtualBaseSpec) |
| 2311 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2312 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2313 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2314 | CXXBaseSpecifier *BaseSpec = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2315 | if (!BaseSpec) |
| 2316 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 2317 | |
| 2318 | // Initialize the base. |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2319 | bool InitList = true; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2320 | Expr **Args = &Init; |
| 2321 | unsigned NumArgs = 1; |
| 2322 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2323 | InitList = false; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2324 | Args = ParenList->getExprs(); |
| 2325 | NumArgs = ParenList->getNumExprs(); |
| 2326 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2327 | |
| 2328 | InitializedEntity BaseEntity = |
| 2329 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 2330 | InitializationKind Kind = |
| 2331 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 2332 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 2333 | InitRange.getEnd()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2334 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 2335 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, |
| 2336 | MultiExprArg(*this, Args, NumArgs), |
| 2337 | 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2338 | if (BaseInit.isInvalid()) |
| 2339 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2340 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2341 | CheckImplicitConversions(BaseInit.get(), InitRange.getBegin()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2342 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2343 | // C++0x [class.base.init]p7: |
| 2344 | // The initialization of each base and member constitutes a |
| 2345 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2346 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2347 | if (BaseInit.isInvalid()) |
| 2348 | return true; |
| 2349 | |
| 2350 | // If we are in a dependent context, template instantiation will |
| 2351 | // perform this type-checking again. Just save the arguments that we |
| 2352 | // received in a ParenListExpr. |
| 2353 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2354 | // of the information that we have about the base |
| 2355 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2356 | // and this approach is far more likely to get the corner cases right. |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2357 | if (CurContext->isDependentContext()) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2358 | BaseInit = Owned(Init); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2359 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2360 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2361 | BaseSpec->isVirtual(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2362 | InitRange.getBegin(), |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2363 | BaseInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2364 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2367 | // Create a static_cast\<T&&>(expr). |
| 2368 | static Expr *CastForMoving(Sema &SemaRef, Expr *E) { |
| 2369 | QualType ExprType = E->getType(); |
| 2370 | QualType TargetType = SemaRef.Context.getRValueReferenceType(ExprType); |
| 2371 | SourceLocation ExprLoc = E->getLocStart(); |
| 2372 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 2373 | TargetType, ExprLoc); |
| 2374 | |
| 2375 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 2376 | SourceRange(ExprLoc, ExprLoc), |
| 2377 | E->getSourceRange()).take(); |
| 2378 | } |
| 2379 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2380 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 2381 | /// initialize its base or member. |
| 2382 | enum ImplicitInitializerKind { |
| 2383 | IIK_Default, |
| 2384 | IIK_Copy, |
| 2385 | IIK_Move |
| 2386 | }; |
| 2387 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2388 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2389 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2390 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2391 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2392 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2393 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2394 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2395 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 2396 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2397 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2398 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2399 | |
| 2400 | switch (ImplicitInitKind) { |
| 2401 | case IIK_Default: { |
| 2402 | InitializationKind InitKind |
| 2403 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 2404 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 2405 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2406 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2407 | break; |
| 2408 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2409 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2410 | case IIK_Move: |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2411 | case IIK_Copy: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2412 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2413 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2414 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | cf7c14c | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2415 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2416 | Expr *CopyCtorArg = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2417 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2418 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2419 | Constructor->getLocation(), ParamType, |
| 2420 | VK_LValue, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2421 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2422 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 2423 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2424 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 2425 | QualType ArgTy = |
| 2426 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 2427 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2428 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2429 | if (Moving) { |
| 2430 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 2431 | } |
| 2432 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2433 | CXXCastPath BasePath; |
| 2434 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2435 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 2436 | CK_UncheckedDerivedToBase, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2437 | Moving ? VK_XValue : VK_LValue, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2438 | &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2439 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2440 | InitializationKind InitKind |
| 2441 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 2442 | SourceLocation(), SourceLocation()); |
| 2443 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 2444 | &CopyCtorArg, 1); |
| 2445 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2446 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2447 | break; |
| 2448 | } |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2449 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2450 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2451 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2452 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2453 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2454 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2455 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2456 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2457 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 2458 | SourceLocation()), |
| 2459 | BaseSpec->isVirtual(), |
| 2460 | SourceLocation(), |
| 2461 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2462 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2463 | SourceLocation()); |
| 2464 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2465 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2468 | static bool RefersToRValueRef(Expr *MemRef) { |
| 2469 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 2470 | return Referenced->getType()->isRValueReferenceType(); |
| 2471 | } |
| 2472 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2473 | static bool |
| 2474 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2475 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2476 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2477 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2478 | if (Field->isInvalidDecl()) |
| 2479 | return true; |
| 2480 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2481 | SourceLocation Loc = Constructor->getLocation(); |
| 2482 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2483 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 2484 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2485 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2486 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2487 | |
| 2488 | // Suppress copying zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2489 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 2490 | return false; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2491 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2492 | Expr *MemberExprBase = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2493 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2494 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2495 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2496 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2497 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 2498 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2499 | if (Moving) { |
| 2500 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 2501 | } |
| 2502 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2503 | // Build a reference to this field within the parameter. |
| 2504 | CXXScopeSpec SS; |
| 2505 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 2506 | Sema::LookupMemberName); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2507 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 2508 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2509 | MemberLookup.resolveKind(); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2510 | ExprResult CtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2511 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2512 | ParamType, Loc, |
| 2513 | /*IsArrow=*/false, |
| 2514 | SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2515 | /*TemplateKWLoc=*/SourceLocation(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2516 | /*FirstQualifierInScope=*/0, |
| 2517 | MemberLookup, |
| 2518 | /*TemplateArgs=*/0); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2519 | if (CtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2520 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2521 | |
| 2522 | // C++11 [class.copy]p15: |
| 2523 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 2524 | // with static_cast<T&&>(x.m); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2525 | if (RefersToRValueRef(CtorArg.get())) { |
| 2526 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2529 | // When the field we are copying is an array, create index variables for |
| 2530 | // each dimension of the array. We use these index variables to subscript |
| 2531 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 2532 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2533 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2534 | QualType BaseType = Field->getType(); |
| 2535 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2536 | bool InitializingArray = false; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2537 | while (const ConstantArrayType *Array |
| 2538 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2539 | InitializingArray = true; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2540 | // Create the iteration variable for this array index. |
| 2541 | IdentifierInfo *IterationVarName = 0; |
| 2542 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2543 | SmallString<8> Str; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2544 | llvm::raw_svector_ostream OS(Str); |
| 2545 | OS << "__i" << IndexVariables.size(); |
| 2546 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 2547 | } |
| 2548 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2549 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2550 | IterationVarName, SizeType, |
| 2551 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2552 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2553 | IndexVariables.push_back(IterationVar); |
| 2554 | |
| 2555 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2556 | ExprResult IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2557 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2558 | assert(!IterationVarRef.isInvalid() && |
| 2559 | "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2560 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.take()); |
| 2561 | assert(!IterationVarRef.isInvalid() && |
| 2562 | "Conversion of invented variable cannot fail!"); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2563 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2564 | // Subscript the array with this iteration variable. |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2565 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2566 | IterationVarRef.take(), |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2567 | Loc); |
| 2568 | if (CtorArg.isInvalid()) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2569 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2570 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2571 | BaseType = Array->getElementType(); |
| 2572 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2573 | |
| 2574 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 2575 | if (Moving && InitializingArray) |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2576 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2577 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2578 | // Construct the entity that we will be initializing. For an array, this |
| 2579 | // will be first element in the array, which may require several levels |
| 2580 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2581 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2582 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2583 | if (Indirect) |
| 2584 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 2585 | else |
| 2586 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2587 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 2588 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 2589 | 0, |
| 2590 | Entities.back())); |
| 2591 | |
| 2592 | // Direct-initialize to use the copy constructor. |
| 2593 | InitializationKind InitKind = |
| 2594 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 2595 | |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2596 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2597 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2598 | &CtorArgE, 1); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2599 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2600 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2601 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2602 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2603 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2604 | if (MemberInit.isInvalid()) |
| 2605 | return true; |
| 2606 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2607 | if (Indirect) { |
| 2608 | assert(IndexVariables.size() == 0 && |
| 2609 | "Indirect field improperly initialized"); |
| 2610 | CXXMemberInit |
| 2611 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2612 | Loc, Loc, |
| 2613 | MemberInit.takeAs<Expr>(), |
| 2614 | Loc); |
| 2615 | } else |
| 2616 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 2617 | Loc, MemberInit.takeAs<Expr>(), |
| 2618 | Loc, |
| 2619 | IndexVariables.data(), |
| 2620 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2621 | return false; |
| 2622 | } |
| 2623 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2624 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 2625 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2626 | QualType FieldBaseElementType = |
| 2627 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 2628 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2629 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2630 | InitializedEntity InitEntity |
| 2631 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 2632 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2633 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2634 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2635 | |
| 2636 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2637 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2638 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2639 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2640 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2641 | if (MemberInit.isInvalid()) |
| 2642 | return true; |
| 2643 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2644 | if (Indirect) |
| 2645 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2646 | Indirect, Loc, |
| 2647 | Loc, |
| 2648 | MemberInit.get(), |
| 2649 | Loc); |
| 2650 | else |
| 2651 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2652 | Field, Loc, Loc, |
| 2653 | MemberInit.get(), |
| 2654 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2655 | return false; |
| 2656 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2657 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2658 | if (!Field->getParent()->isUnion()) { |
| 2659 | if (FieldBaseElementType->isReferenceType()) { |
| 2660 | SemaRef.Diag(Constructor->getLocation(), |
| 2661 | diag::err_uninitialized_member_in_ctor) |
| 2662 | << (int)Constructor->isImplicit() |
| 2663 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2664 | << 0 << Field->getDeclName(); |
| 2665 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2666 | return true; |
| 2667 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2668 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2669 | if (FieldBaseElementType.isConstQualified()) { |
| 2670 | SemaRef.Diag(Constructor->getLocation(), |
| 2671 | diag::err_uninitialized_member_in_ctor) |
| 2672 | << (int)Constructor->isImplicit() |
| 2673 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2674 | << 1 << Field->getDeclName(); |
| 2675 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2676 | return true; |
| 2677 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2678 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2679 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2680 | if (SemaRef.getLangOptions().ObjCAutoRefCount && |
| 2681 | FieldBaseElementType->isObjCRetainableType() && |
| 2682 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 2683 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
| 2684 | // Instant objects: |
| 2685 | // Default-initialize Objective-C pointers to NULL. |
| 2686 | CXXMemberInit |
| 2687 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2688 | Loc, Loc, |
| 2689 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 2690 | Loc); |
| 2691 | return false; |
| 2692 | } |
| 2693 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2694 | // Nothing to initialize. |
| 2695 | CXXMemberInit = 0; |
| 2696 | return false; |
| 2697 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2698 | |
| 2699 | namespace { |
| 2700 | struct BaseAndFieldInfo { |
| 2701 | Sema &S; |
| 2702 | CXXConstructorDecl *Ctor; |
| 2703 | bool AnyErrorsInInits; |
| 2704 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2705 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2706 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2707 | |
| 2708 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 2709 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2710 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 2711 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2712 | IIK = IIK_Copy; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2713 | else if (Generated && Ctor->isMoveConstructor()) |
| 2714 | IIK = IIK_Move; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2715 | else |
| 2716 | IIK = IIK_Default; |
| 2717 | } |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 2718 | |
| 2719 | bool isImplicitCopyOrMove() const { |
| 2720 | switch (IIK) { |
| 2721 | case IIK_Copy: |
| 2722 | case IIK_Move: |
| 2723 | return true; |
| 2724 | |
| 2725 | case IIK_Default: |
| 2726 | return false; |
| 2727 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2728 | |
| 2729 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 2730 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2731 | }; |
| 2732 | } |
| 2733 | |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 2734 | /// \brief Determine whether the given indirect field declaration is somewhere |
| 2735 | /// within an anonymous union. |
| 2736 | static bool isWithinAnonymousUnion(IndirectFieldDecl *F) { |
| 2737 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 2738 | CEnd = F->chain_end(); |
| 2739 | C != CEnd; ++C) |
| 2740 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>((*C)->getDeclContext())) |
| 2741 | if (Record->isUnion()) |
| 2742 | return true; |
| 2743 | |
| 2744 | return false; |
| 2745 | } |
| 2746 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2747 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 2748 | /// array type. |
| 2749 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 2750 | if (T->isIncompleteArrayType()) |
| 2751 | return true; |
| 2752 | |
| 2753 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 2754 | if (!ArrayT->getSize()) |
| 2755 | return true; |
| 2756 | |
| 2757 | T = ArrayT->getElementType(); |
| 2758 | } |
| 2759 | |
| 2760 | return false; |
| 2761 | } |
| 2762 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2763 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2764 | FieldDecl *Field, |
| 2765 | IndirectFieldDecl *Indirect = 0) { |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2766 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2767 | // Overwhelmingly common case: we have a direct initializer for this field. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2768 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2769 | Info.AllToInit.push_back(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2770 | return false; |
| 2771 | } |
| 2772 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2773 | // C++0x [class.base.init]p8: if the entity is a non-static data member that |
| 2774 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 2775 | // in [dcl.init]. |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 2776 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2777 | CXXCtorInitializer *Init; |
| 2778 | if (Indirect) |
| 2779 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2780 | SourceLocation(), |
| 2781 | SourceLocation(), 0, |
| 2782 | SourceLocation()); |
| 2783 | else |
| 2784 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2785 | SourceLocation(), |
| 2786 | SourceLocation(), 0, |
| 2787 | SourceLocation()); |
| 2788 | Info.AllToInit.push_back(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2789 | return false; |
| 2790 | } |
| 2791 | |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 2792 | // Don't build an implicit initializer for union members if none was |
| 2793 | // explicitly specified. |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 2794 | if (Field->getParent()->isUnion() || |
| 2795 | (Indirect && isWithinAnonymousUnion(Indirect))) |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 2796 | return false; |
| 2797 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2798 | // Don't initialize incomplete or zero-length arrays. |
| 2799 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 2800 | return false; |
| 2801 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2802 | // Don't try to build an implicit initializer if there were semantic |
| 2803 | // errors in any of the initializers (and therefore we might be |
| 2804 | // missing some that the user actually wrote). |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2805 | if (Info.AnyErrorsInInits || Field->isInvalidDecl()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2806 | return false; |
| 2807 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2808 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2809 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 2810 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2811 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2812 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2813 | if (Init) |
| 2814 | Info.AllToInit.push_back(Init); |
| 2815 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2816 | return false; |
| 2817 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2818 | |
| 2819 | bool |
| 2820 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 2821 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2822 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 2823 | Constructor->setNumCtorInitializers(1); |
| 2824 | CXXCtorInitializer **initializer = |
| 2825 | new (Context) CXXCtorInitializer*[1]; |
| 2826 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 2827 | Constructor->setCtorInitializers(initializer); |
| 2828 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2829 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2830 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2831 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 2832 | } |
| 2833 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 2834 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2835 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2836 | return false; |
| 2837 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2838 | |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2839 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 2840 | CXXCtorInitializer **Initializers, |
| 2841 | unsigned NumInitializers, |
| 2842 | bool AnyErrors) { |
Douglas Gregor | d836c0d | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 2843 | if (Constructor->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2844 | // Just store the initializers as written, they will be checked during |
| 2845 | // instantiation. |
| 2846 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2847 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2848 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2849 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2850 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2851 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2852 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | return false; |
| 2856 | } |
| 2857 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2858 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2859 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2860 | // We need to build the initializer AST according to order of construction |
| 2861 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2862 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 2863 | if (!ClassDecl) |
| 2864 | return true; |
| 2865 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2866 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2868 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2869 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2870 | |
| 2871 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2872 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2873 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2874 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2877 | // Keep track of the direct virtual bases. |
| 2878 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 2879 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 2880 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 2881 | if (I->isVirtual()) |
| 2882 | DirectVBases.insert(I); |
| 2883 | } |
| 2884 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2885 | // Push virtual bases before others. |
| 2886 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2887 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 2888 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2889 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2890 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 2891 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2892 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2893 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2894 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2895 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2896 | VBase, IsInheritedVirtualBase, |
| 2897 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2898 | HadError = true; |
| 2899 | continue; |
| 2900 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2901 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2902 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2906 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2907 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2908 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 2909 | // Virtuals are in the virtual base list and already constructed. |
| 2910 | if (Base->isVirtual()) |
| 2911 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2913 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2914 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 2915 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2916 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2917 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2918 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2919 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2920 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2921 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2922 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2923 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 2924 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2925 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2926 | } |
| 2927 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2928 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2929 | // Fields. |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2930 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 2931 | MemEnd = ClassDecl->decls_end(); |
| 2932 | Mem != MemEnd; ++Mem) { |
| 2933 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 2934 | // C++ [class.bit]p2: |
| 2935 | // A declaration for a bit-field that omits the identifier declares an |
| 2936 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 2937 | // initialized. |
| 2938 | if (F->isUnnamedBitfield()) |
| 2939 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2940 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2941 | // 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] | 2942 | // handle anonymous struct/union fields based on their individual |
| 2943 | // indirect fields. |
| 2944 | if (F->isAnonymousStructOrUnion() && Info.IIK == IIK_Default) |
| 2945 | continue; |
| 2946 | |
| 2947 | if (CollectFieldInitializer(*this, Info, F)) |
| 2948 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2949 | continue; |
| 2950 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2951 | |
| 2952 | // Beyond this point, we only consider default initialization. |
| 2953 | if (Info.IIK != IIK_Default) |
| 2954 | continue; |
| 2955 | |
| 2956 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 2957 | if (F->getType()->isIncompleteArrayType()) { |
| 2958 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2959 | "Incomplete array type is not valid"); |
| 2960 | continue; |
| 2961 | } |
| 2962 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2963 | // Initialize each field of an anonymous struct individually. |
| 2964 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 2965 | HadError = true; |
| 2966 | |
| 2967 | continue; |
| 2968 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2969 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2971 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2972 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2973 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2974 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2975 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2976 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2977 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2978 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 2979 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2980 | // Constructors implicitly reference the base and member |
| 2981 | // destructors. |
| 2982 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 2983 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2984 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2985 | |
| 2986 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2989 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 2990 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2991 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2992 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 2993 | return static_cast<void *>(RT->getDecl()); |
| 2994 | } |
| 2995 | return static_cast<void *>(Field); |
| 2996 | } |
| 2997 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2998 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2999 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3002 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3003 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3004 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3005 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3006 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3007 | // For fields injected into the class via declaration of an anonymous union, |
| 3008 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3009 | FieldDecl *Field = Member->getAnyMember(); |
| 3010 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3011 | // If the field is a member of an anonymous struct or union, our key |
| 3012 | // 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] | 3013 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3014 | if (RD->isAnonymousStructOrUnion()) { |
| 3015 | while (true) { |
| 3016 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 3017 | if (Parent->isAnonymousStructOrUnion()) |
| 3018 | RD = Parent; |
| 3019 | else |
| 3020 | break; |
| 3021 | } |
| 3022 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 3023 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3025 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3026 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3029 | static void |
| 3030 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3031 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3032 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3033 | unsigned NumInits) { |
| 3034 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 3035 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3037 | // Don't check initializers order unless the warning is enabled at the |
| 3038 | // location of at least one initializer. |
| 3039 | bool ShouldCheckOrder = false; |
| 3040 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3041 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3042 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 3043 | Init->getSourceLocation()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3044 | != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3045 | ShouldCheckOrder = true; |
| 3046 | break; |
| 3047 | } |
| 3048 | } |
| 3049 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3050 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3051 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3052 | // Build the list of bases and members in the order that they'll |
| 3053 | // actually be initialized. The explicit initializers should be in |
| 3054 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3055 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3057 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3058 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3059 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3060 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3061 | ClassDecl->vbases_begin(), |
| 3062 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3063 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3065 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3066 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3067 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3068 | if (Base->isVirtual()) |
| 3069 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3070 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3073 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3074 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3075 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 3076 | if (Field->isUnnamedBitfield()) |
| 3077 | continue; |
| 3078 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3079 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3082 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 3083 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3084 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3085 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3086 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3087 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3088 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3089 | |
| 3090 | // Scan forward to try to find this initializer in the idealized |
| 3091 | // initializers list. |
| 3092 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3093 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3094 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3095 | |
| 3096 | // If we didn't find this initializer, it must be because we |
| 3097 | // scanned past it on a previous iteration. That can only |
| 3098 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 3099 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3100 | Sema::SemaDiagnosticBuilder D = |
| 3101 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 3102 | diag::warn_initializer_out_of_order); |
| 3103 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3104 | if (PrevInit->isAnyMemberInitializer()) |
| 3105 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3106 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3107 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3108 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3109 | if (Init->isAnyMemberInitializer()) |
| 3110 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3111 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3112 | D << 1 << Init->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3113 | |
| 3114 | // Move back to the initializer's location in the ideal list. |
| 3115 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3116 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3117 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3118 | |
| 3119 | assert(IdealIndex != NumIdealInits && |
| 3120 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3121 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3122 | |
| 3123 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3124 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3127 | namespace { |
| 3128 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3129 | CXXCtorInitializer *Init, |
| 3130 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3131 | if (!PrevInit) { |
| 3132 | PrevInit = Init; |
| 3133 | return false; |
| 3134 | } |
| 3135 | |
| 3136 | if (FieldDecl *Field = Init->getMember()) |
| 3137 | S.Diag(Init->getSourceLocation(), |
| 3138 | diag::err_multiple_mem_initialization) |
| 3139 | << Field->getDeclName() |
| 3140 | << Init->getSourceRange(); |
| 3141 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3142 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3143 | assert(BaseClass && "neither field nor base"); |
| 3144 | S.Diag(Init->getSourceLocation(), |
| 3145 | diag::err_multiple_base_initialization) |
| 3146 | << QualType(BaseClass, 0) |
| 3147 | << Init->getSourceRange(); |
| 3148 | } |
| 3149 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 3150 | << 0 << PrevInit->getSourceRange(); |
| 3151 | |
| 3152 | return true; |
| 3153 | } |
| 3154 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3155 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3156 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 3157 | |
| 3158 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3159 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3160 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3161 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3162 | RecordDecl *Parent = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3163 | NamedDecl *Child = Field; |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3164 | |
| 3165 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3166 | if (Parent->isUnion()) { |
| 3167 | UnionEntry &En = Unions[Parent]; |
| 3168 | if (En.first && En.first != Child) { |
| 3169 | S.Diag(Init->getSourceLocation(), |
| 3170 | diag::err_multiple_mem_union_initialization) |
| 3171 | << Field->getDeclName() |
| 3172 | << Init->getSourceRange(); |
| 3173 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 3174 | << 0 << En.second->getSourceRange(); |
| 3175 | return true; |
David Blaikie | 5bbe816 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 3176 | } |
| 3177 | if (!En.first) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3178 | En.first = Child; |
| 3179 | En.second = Init; |
| 3180 | } |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3181 | if (!Parent->isAnonymousStructOrUnion()) |
| 3182 | return false; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | Child = Parent; |
| 3186 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3187 | } |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3188 | |
| 3189 | return false; |
| 3190 | } |
| 3191 | } |
| 3192 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3193 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3194 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3195 | SourceLocation ColonLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 3196 | CXXCtorInitializer **meminits, |
| 3197 | unsigned NumMemInits, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3198 | bool AnyErrors) { |
| 3199 | if (!ConstructorDecl) |
| 3200 | return; |
| 3201 | |
| 3202 | AdjustDeclIfTemplate(ConstructorDecl); |
| 3203 | |
| 3204 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3205 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3206 | |
| 3207 | if (!Constructor) { |
| 3208 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 3209 | return; |
| 3210 | } |
| 3211 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3212 | CXXCtorInitializer **MemInits = |
| 3213 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3214 | |
| 3215 | // Mapping for the duplicate initializers check. |
| 3216 | // For member initializers, this is keyed with a FieldDecl*. |
| 3217 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3218 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3219 | |
| 3220 | // Mapping for the inconsistent anonymous-union initializers check. |
| 3221 | RedundantUnionMap MemberUnions; |
| 3222 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3223 | bool HadError = false; |
| 3224 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3225 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3226 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 3227 | // Set the source order index. |
| 3228 | Init->setSourceOrder(i); |
| 3229 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3230 | if (Init->isAnyMemberInitializer()) { |
| 3231 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3232 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 3233 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 3234 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3235 | } else if (Init->isBaseInitializer()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3236 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 3237 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 3238 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3239 | } else { |
| 3240 | assert(Init->isDelegatingInitializer()); |
| 3241 | // This must be the only initializer |
| 3242 | if (i != 0 || NumMemInits > 1) { |
| 3243 | Diag(MemInits[0]->getSourceLocation(), |
| 3244 | diag::err_delegating_initializer_alone) |
| 3245 | << MemInits[0]->getSourceRange(); |
| 3246 | HadError = true; |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3247 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3248 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3249 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3250 | // Return immediately as the initializer is set. |
| 3251 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3252 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3255 | if (HadError) |
| 3256 | return; |
| 3257 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3258 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 3259 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3260 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3263 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3264 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 3265 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 3266 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 3267 | // have destructors implicitly called. |
| 3268 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3269 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3270 | |
| 3271 | // FIXME: all the access-control diagnostics are positioned on the |
| 3272 | // field/base declaration. That's probably good; that said, the |
| 3273 | // user might reasonably want to know why the destructor is being |
| 3274 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3275 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3276 | // Non-static data members. |
| 3277 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 3278 | E = ClassDecl->field_end(); I != E; ++I) { |
| 3279 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 3280 | if (Field->isInvalidDecl()) |
| 3281 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3282 | |
| 3283 | // Don't destroy incomplete or zero-length arrays. |
| 3284 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 3285 | continue; |
| 3286 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3287 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 3288 | |
| 3289 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 3290 | if (!RT) |
| 3291 | continue; |
| 3292 | |
| 3293 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3294 | if (FieldClassDecl->isInvalidDecl()) |
| 3295 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3296 | if (FieldClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3297 | continue; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 3298 | // The destructor for an implicit anonymous union member is never invoked. |
| 3299 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 3300 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3301 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3302 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3303 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3304 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3305 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3306 | << Field->getDeclName() |
| 3307 | << FieldType); |
| 3308 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3309 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3310 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3313 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 3314 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3315 | // Bases. |
| 3316 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3317 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3318 | // Bases are always records in a well-formed non-dependent class. |
| 3319 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 3320 | |
| 3321 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3322 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3323 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3324 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3325 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3326 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3327 | if (BaseClassDecl->isInvalidDecl()) |
| 3328 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3329 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3330 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3332 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3333 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3334 | |
| 3335 | // FIXME: caret should be on the start of the class name |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3336 | CheckDestructorAccess(Base->getLocStart(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3337 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3338 | << Base->getType() |
| 3339 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3340 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3341 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3342 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3346 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3347 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3348 | |
| 3349 | // Bases are always records in a well-formed non-dependent class. |
| 3350 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 3351 | |
| 3352 | // Ignore direct virtual bases. |
| 3353 | if (DirectVirtualBases.count(RT)) |
| 3354 | continue; |
| 3355 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3356 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3357 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3358 | if (BaseClassDecl->isInvalidDecl()) |
| 3359 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3360 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3361 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3363 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3364 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3365 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3366 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3367 | << VBase->getType()); |
| 3368 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3369 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3370 | DiagnoseUseOfDecl(Dtor, Location); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3371 | } |
| 3372 | } |
| 3373 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3374 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 3375 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3376 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3378 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3379 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3380 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3384 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3385 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3386 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3387 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3388 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3391 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3392 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3393 | if (!getLangOptions().CPlusPlus) |
| 3394 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3395 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 3396 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3397 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3398 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3399 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3400 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3401 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3402 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3404 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3405 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3407 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3408 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3409 | if (!RT) |
| 3410 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3412 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3413 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3414 | // We can't answer whether something is abstract until it has a |
| 3415 | // definition. If it's currently being defined, we'll walk back |
| 3416 | // over all the declarations when we have a full definition. |
| 3417 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 3418 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3419 | return false; |
| 3420 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3421 | if (!RD->isAbstract()) |
| 3422 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3424 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3425 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3427 | return true; |
| 3428 | } |
| 3429 | |
| 3430 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 3431 | // Check if we've already emitted the list of pure virtual functions |
| 3432 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3433 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3434 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3436 | CXXFinalOverriderMap FinalOverriders; |
| 3437 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3439 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 3440 | // more than once. |
| 3441 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 3442 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3443 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 3444 | MEnd = FinalOverriders.end(); |
| 3445 | M != MEnd; |
| 3446 | ++M) { |
| 3447 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 3448 | SOEnd = M->second.end(); |
| 3449 | SO != SOEnd; ++SO) { |
| 3450 | // C++ [class.abstract]p4: |
| 3451 | // A class is abstract if it contains or inherits at least one |
| 3452 | // pure virtual function for which the final overrider is pure |
| 3453 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3455 | // |
| 3456 | if (SO->second.size() != 1) |
| 3457 | continue; |
| 3458 | |
| 3459 | if (!SO->second.front().Method->isPure()) |
| 3460 | continue; |
| 3461 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3462 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 3463 | continue; |
| 3464 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3465 | Diag(SO->second.front().Method->getLocation(), |
| 3466 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 3467 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3468 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | if (!PureVirtualClassDiagSet) |
| 3472 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 3473 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3476 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3477 | struct AbstractUsageInfo { |
| 3478 | Sema &S; |
| 3479 | CXXRecordDecl *Record; |
| 3480 | CanQualType AbstractType; |
| 3481 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3482 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3483 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 3484 | : S(S), Record(Record), |
| 3485 | AbstractType(S.Context.getCanonicalType( |
| 3486 | S.Context.getTypeDeclType(Record))), |
| 3487 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3488 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3489 | void DiagnoseAbstractType() { |
| 3490 | if (Invalid) return; |
| 3491 | S.DiagnoseAbstractType(Record); |
| 3492 | Invalid = true; |
| 3493 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3494 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3495 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 3496 | }; |
| 3497 | |
| 3498 | struct CheckAbstractUsage { |
| 3499 | AbstractUsageInfo &Info; |
| 3500 | const NamedDecl *Ctx; |
| 3501 | |
| 3502 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 3503 | : Info(Info), Ctx(Ctx) {} |
| 3504 | |
| 3505 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3506 | switch (TL.getTypeLocClass()) { |
| 3507 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 3508 | #define TYPELOC(CLASS, PARENT) \ |
| 3509 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 3510 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3511 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3513 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3514 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3515 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 3516 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 3517 | if (!TL.getArg(I)) |
| 3518 | continue; |
| 3519 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3520 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 3521 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3522 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3523 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3524 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3525 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3526 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 3527 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3529 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3530 | // Visit the type parameters from a permissive context. |
| 3531 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 3532 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 3533 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 3534 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 3535 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 3536 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3537 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3540 | // Visit pointee types from a permissive context. |
| 3541 | #define CheckPolymorphic(Type) \ |
| 3542 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 3543 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 3544 | } |
| 3545 | CheckPolymorphic(PointerTypeLoc) |
| 3546 | CheckPolymorphic(ReferenceTypeLoc) |
| 3547 | CheckPolymorphic(MemberPointerTypeLoc) |
| 3548 | CheckPolymorphic(BlockPointerTypeLoc) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3549 | CheckPolymorphic(AtomicTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3550 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3551 | /// Handle all the types we haven't given a more specific |
| 3552 | /// implementation for above. |
| 3553 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3554 | // Every other kind of type that we haven't called out already |
| 3555 | // that has an inner type is either (1) sugar or (2) contains that |
| 3556 | // inner type in some way as a subobject. |
| 3557 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 3558 | return Visit(Next, Sel); |
| 3559 | |
| 3560 | // If there's no inner type and we're in a permissive context, |
| 3561 | // don't diagnose. |
| 3562 | if (Sel == Sema::AbstractNone) return; |
| 3563 | |
| 3564 | // Check whether the type matches the abstract type. |
| 3565 | QualType T = TL.getType(); |
| 3566 | if (T->isArrayType()) { |
| 3567 | Sel = Sema::AbstractArrayType; |
| 3568 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3569 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3570 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 3571 | if (CT != Info.AbstractType) return; |
| 3572 | |
| 3573 | // It matched; do some magic. |
| 3574 | if (Sel == Sema::AbstractArrayType) { |
| 3575 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 3576 | << T << TL.getSourceRange(); |
| 3577 | } else { |
| 3578 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 3579 | << Sel << T << TL.getSourceRange(); |
| 3580 | } |
| 3581 | Info.DiagnoseAbstractType(); |
| 3582 | } |
| 3583 | }; |
| 3584 | |
| 3585 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 3586 | Sema::AbstractDiagSelID Sel) { |
| 3587 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 3588 | } |
| 3589 | |
| 3590 | } |
| 3591 | |
| 3592 | /// Check for invalid uses of an abstract type in a method declaration. |
| 3593 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3594 | CXXMethodDecl *MD) { |
| 3595 | // No need to do the check on definitions, which require that |
| 3596 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3597 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3598 | return; |
| 3599 | |
| 3600 | // For safety's sake, just ignore it if we don't have type source |
| 3601 | // information. This should never happen for non-implicit methods, |
| 3602 | // but... |
| 3603 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 3604 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 3605 | } |
| 3606 | |
| 3607 | /// Check for invalid uses of an abstract type within a class definition. |
| 3608 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3609 | CXXRecordDecl *RD) { |
| 3610 | for (CXXRecordDecl::decl_iterator |
| 3611 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 3612 | Decl *D = *I; |
| 3613 | if (D->isImplicit()) continue; |
| 3614 | |
| 3615 | // Methods and method templates. |
| 3616 | if (isa<CXXMethodDecl>(D)) { |
| 3617 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 3618 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 3619 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 3620 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 3621 | |
| 3622 | // Fields and static variables. |
| 3623 | } else if (isa<FieldDecl>(D)) { |
| 3624 | FieldDecl *FD = cast<FieldDecl>(D); |
| 3625 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 3626 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 3627 | } else if (isa<VarDecl>(D)) { |
| 3628 | VarDecl *VD = cast<VarDecl>(D); |
| 3629 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 3630 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 3631 | |
| 3632 | // Nested classes and class templates. |
| 3633 | } else if (isa<CXXRecordDecl>(D)) { |
| 3634 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 3635 | } else if (isa<ClassTemplateDecl>(D)) { |
| 3636 | CheckAbstractClassUsage(Info, |
| 3637 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 3638 | } |
| 3639 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3642 | /// \brief Perform semantic checks on a class definition that has been |
| 3643 | /// completing, introducing implicitly-declared members, checking for |
| 3644 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3645 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 3646 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3647 | return; |
| 3648 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3649 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 3650 | AbstractUsageInfo Info(*this, Record); |
| 3651 | CheckAbstractClassUsage(Info, Record); |
| 3652 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3653 | |
| 3654 | // If this is not an aggregate type and has no user-declared constructor, |
| 3655 | // complain about any non-static data members of reference or const scalar |
| 3656 | // type, since they will never get initializers. |
| 3657 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
Douglas Gregor | 5e058eb | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 3658 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 3659 | !Record->isLambda()) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3660 | bool Complained = false; |
| 3661 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 3662 | FEnd = Record->field_end(); |
| 3663 | F != FEnd; ++F) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3664 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3665 | continue; |
| 3666 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3667 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 3668 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3669 | if (!Complained) { |
| 3670 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 3671 | << Record->getTagKind() << Record; |
| 3672 | Complained = true; |
| 3673 | } |
| 3674 | |
| 3675 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 3676 | << F->getType()->isReferenceType() |
| 3677 | << F->getDeclName(); |
| 3678 | } |
| 3679 | } |
| 3680 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3681 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 3682 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3683 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3684 | |
| 3685 | if (Record->getIdentifier()) { |
| 3686 | // C++ [class.mem]p13: |
| 3687 | // If T is the name of a class, then each of the following shall have a |
| 3688 | // name different from T: |
| 3689 | // - every member of every anonymous union that is a member of class T. |
| 3690 | // |
| 3691 | // C++ [class.mem]p14: |
| 3692 | // In addition, if class T has a user-declared constructor (12.1), every |
| 3693 | // non-static data member of class T shall have a name different from T. |
| 3694 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3695 | R.first != R.second; ++R.first) { |
| 3696 | NamedDecl *D = *R.first; |
| 3697 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 3698 | isa<IndirectFieldDecl>(D)) { |
| 3699 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 3700 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3703 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3704 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3705 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3706 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 3707 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3708 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3709 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3710 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 3711 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 3712 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3713 | |
| 3714 | // See if a method overloads virtual methods in a base |
| 3715 | /// class without overriding any. |
| 3716 | if (!Record->isDependentType()) { |
| 3717 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 3718 | MEnd = Record->method_end(); |
| 3719 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 0266aa3 | 2011-03-03 22:58:57 +0000 | [diff] [blame] | 3720 | if (!(*M)->isStatic()) |
| 3721 | DiagnoseHiddenVirtualMethods(Record, *M); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3722 | } |
| 3723 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 3724 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 3725 | // C++0x [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 3726 | // function that is not a constructor declares that member function to be |
| 3727 | // const. [...] The class of which that function is a member shall be |
| 3728 | // a literal type. |
| 3729 | // |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 3730 | // If the class has virtual bases, any constexpr members will already have |
| 3731 | // been diagnosed by the checks performed on the member declaration, so |
| 3732 | // suppress this (less useful) diagnostic. |
| 3733 | if (LangOpts.CPlusPlus0x && !Record->isDependentType() && |
| 3734 | !Record->isLiteral() && !Record->getNumVBases()) { |
| 3735 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 3736 | MEnd = Record->method_end(); |
| 3737 | M != MEnd; ++M) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3738 | if (M->isConstexpr() && M->isInstance() && !isa<CXXConstructorDecl>(*M)) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 3739 | switch (Record->getTemplateSpecializationKind()) { |
| 3740 | case TSK_ImplicitInstantiation: |
| 3741 | case TSK_ExplicitInstantiationDeclaration: |
| 3742 | case TSK_ExplicitInstantiationDefinition: |
| 3743 | // If a template instantiates to a non-literal type, but its members |
| 3744 | // instantiate to constexpr functions, the template is technically |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3745 | // ill-formed, but we allow it for sanity. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 3746 | continue; |
| 3747 | |
| 3748 | case TSK_Undeclared: |
| 3749 | case TSK_ExplicitSpecialization: |
| 3750 | RequireLiteralType((*M)->getLocation(), Context.getRecordType(Record), |
| 3751 | PDiag(diag::err_constexpr_method_non_literal)); |
| 3752 | break; |
| 3753 | } |
| 3754 | |
| 3755 | // Only produce one error per class. |
| 3756 | break; |
| 3757 | } |
| 3758 | } |
| 3759 | } |
| 3760 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 3761 | // Declare inherited constructors. We do this eagerly here because: |
| 3762 | // - The standard requires an eager diagnostic for conflicting inherited |
| 3763 | // constructors from different classes. |
| 3764 | // - The lazy declaration of the other implicit constructors is so as to not |
| 3765 | // waste space and performance on classes that are not meant to be |
| 3766 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 3767 | // have inherited constructors. |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 3768 | DeclareInheritedConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3769 | |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 3770 | if (!Record->isDependentType()) |
| 3771 | CheckExplicitlyDefaultedMethods(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | void Sema::CheckExplicitlyDefaultedMethods(CXXRecordDecl *Record) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3775 | for (CXXRecordDecl::method_iterator MI = Record->method_begin(), |
| 3776 | ME = Record->method_end(); |
| 3777 | MI != ME; ++MI) { |
| 3778 | if (!MI->isInvalidDecl() && MI->isExplicitlyDefaulted()) { |
| 3779 | switch (getSpecialMember(*MI)) { |
| 3780 | case CXXDefaultConstructor: |
| 3781 | CheckExplicitlyDefaultedDefaultConstructor( |
| 3782 | cast<CXXConstructorDecl>(*MI)); |
| 3783 | break; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3784 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3785 | case CXXDestructor: |
| 3786 | CheckExplicitlyDefaultedDestructor(cast<CXXDestructorDecl>(*MI)); |
| 3787 | break; |
| 3788 | |
| 3789 | case CXXCopyConstructor: |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3790 | CheckExplicitlyDefaultedCopyConstructor(cast<CXXConstructorDecl>(*MI)); |
| 3791 | break; |
| 3792 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3793 | case CXXCopyAssignment: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3794 | CheckExplicitlyDefaultedCopyAssignment(*MI); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3795 | break; |
| 3796 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3797 | case CXXMoveConstructor: |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3798 | CheckExplicitlyDefaultedMoveConstructor(cast<CXXConstructorDecl>(*MI)); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3801 | case CXXMoveAssignment: |
| 3802 | CheckExplicitlyDefaultedMoveAssignment(*MI); |
| 3803 | break; |
| 3804 | |
| 3805 | case CXXInvalid: |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3806 | llvm_unreachable("non-special member explicitly defaulted!"); |
| 3807 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | void Sema::CheckExplicitlyDefaultedDefaultConstructor(CXXConstructorDecl *CD) { |
| 3814 | assert(CD->isExplicitlyDefaulted() && CD->isDefaultConstructor()); |
| 3815 | |
| 3816 | // Whether this was the first-declared instance of the constructor. |
| 3817 | // This affects whether we implicitly add an exception spec (and, eventually, |
| 3818 | // constexpr). It is also ill-formed to explicitly default a constructor such |
| 3819 | // that it would be deleted. (C++0x [decl.fct.def.default]) |
| 3820 | bool First = CD == CD->getCanonicalDecl(); |
| 3821 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3822 | bool HadError = false; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3823 | if (CD->getNumParams() != 0) { |
| 3824 | Diag(CD->getLocation(), diag::err_defaulted_default_ctor_params) |
| 3825 | << CD->getSourceRange(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3826 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | ImplicitExceptionSpecification Spec |
| 3830 | = ComputeDefaultedDefaultCtorExceptionSpec(CD->getParent()); |
| 3831 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3832 | if (EPI.ExceptionSpecType == EST_Delayed) { |
| 3833 | // Exception specification depends on some deferred part of the class. We'll |
| 3834 | // try again when the class's definition has been fully processed. |
| 3835 | return; |
| 3836 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3837 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3838 | *ExceptionType = Context.getFunctionType( |
| 3839 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3840 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3841 | // C++11 [dcl.fct.def.default]p2: |
| 3842 | // An explicitly-defaulted function may be declared constexpr only if it |
| 3843 | // would have been implicitly declared as constexpr, |
Richard Smith | eb273b7 | 2012-02-14 02:33:50 +0000 | [diff] [blame] | 3844 | // Do not apply this rule to templates, since core issue 1358 makes such |
| 3845 | // functions always instantiate to constexpr functions. |
| 3846 | if (CD->isConstexpr() && |
| 3847 | CD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3848 | if (!CD->getParent()->defaultedDefaultConstructorIsConstexpr()) { |
| 3849 | Diag(CD->getLocStart(), diag::err_incorrect_defaulted_constexpr) |
| 3850 | << CXXDefaultConstructor; |
| 3851 | HadError = true; |
| 3852 | } |
| 3853 | } |
| 3854 | // and may have an explicit exception-specification only if it is compatible |
| 3855 | // with the exception-specification on the implicit declaration. |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3856 | if (CtorType->hasExceptionSpec()) { |
| 3857 | if (CheckEquivalentExceptionSpec( |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3858 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3859 | << CXXDefaultConstructor, |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3860 | PDiag(), |
| 3861 | ExceptionType, SourceLocation(), |
| 3862 | CtorType, CD->getLocation())) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3863 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3864 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | // If a function is explicitly defaulted on its first declaration, |
| 3868 | if (First) { |
| 3869 | // -- it is implicitly considered to be constexpr if the implicit |
| 3870 | // definition would be, |
| 3871 | CD->setConstexpr(CD->getParent()->defaultedDefaultConstructorIsConstexpr()); |
| 3872 | |
| 3873 | // -- it is implicitly considered to have the same |
| 3874 | // exception-specification as if it had been implicitly declared |
| 3875 | // |
| 3876 | // FIXME: a compatible, but different, explicit exception specification |
| 3877 | // will be silently overridden. We should issue a warning if this happens. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3878 | EPI.ExtInfo = CtorType->getExtInfo(); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 3879 | |
| 3880 | // Such a function is also trivial if the implicitly-declared function |
| 3881 | // would have been. |
| 3882 | CD->setTrivial(CD->getParent()->hasTrivialDefaultConstructor()); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3883 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3884 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3885 | if (HadError) { |
| 3886 | CD->setInvalidDecl(); |
| 3887 | return; |
| 3888 | } |
| 3889 | |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 3890 | if (ShouldDeleteSpecialMember(CD, CXXDefaultConstructor)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3891 | if (First) { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3892 | CD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3893 | } else { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3894 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3895 | << CXXDefaultConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3896 | CD->setInvalidDecl(); |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | void Sema::CheckExplicitlyDefaultedCopyConstructor(CXXConstructorDecl *CD) { |
| 3902 | assert(CD->isExplicitlyDefaulted() && CD->isCopyConstructor()); |
| 3903 | |
| 3904 | // Whether this was the first-declared instance of the constructor. |
| 3905 | bool First = CD == CD->getCanonicalDecl(); |
| 3906 | |
| 3907 | bool HadError = false; |
| 3908 | if (CD->getNumParams() != 1) { |
| 3909 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_params) |
| 3910 | << CD->getSourceRange(); |
| 3911 | HadError = true; |
| 3912 | } |
| 3913 | |
| 3914 | ImplicitExceptionSpecification Spec(Context); |
| 3915 | bool Const; |
| 3916 | llvm::tie(Spec, Const) = |
| 3917 | ComputeDefaultedCopyCtorExceptionSpecAndConst(CD->getParent()); |
| 3918 | |
| 3919 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3920 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3921 | *ExceptionType = Context.getFunctionType( |
| 3922 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3923 | |
| 3924 | // Check for parameter type matching. |
| 3925 | // This is a copy ctor so we know it's a cv-qualified reference to T. |
| 3926 | QualType ArgType = CtorType->getArgType(0); |
| 3927 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3928 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_volatile_param); |
| 3929 | HadError = true; |
| 3930 | } |
| 3931 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 3932 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_const_param); |
| 3933 | HadError = true; |
| 3934 | } |
| 3935 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3936 | // C++11 [dcl.fct.def.default]p2: |
| 3937 | // An explicitly-defaulted function may be declared constexpr only if it |
| 3938 | // would have been implicitly declared as constexpr, |
Richard Smith | eb273b7 | 2012-02-14 02:33:50 +0000 | [diff] [blame] | 3939 | // Do not apply this rule to templates, since core issue 1358 makes such |
| 3940 | // functions always instantiate to constexpr functions. |
| 3941 | if (CD->isConstexpr() && |
| 3942 | CD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3943 | if (!CD->getParent()->defaultedCopyConstructorIsConstexpr()) { |
| 3944 | Diag(CD->getLocStart(), diag::err_incorrect_defaulted_constexpr) |
| 3945 | << CXXCopyConstructor; |
| 3946 | HadError = true; |
| 3947 | } |
| 3948 | } |
| 3949 | // and may have an explicit exception-specification only if it is compatible |
| 3950 | // with the exception-specification on the implicit declaration. |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3951 | if (CtorType->hasExceptionSpec()) { |
| 3952 | if (CheckEquivalentExceptionSpec( |
| 3953 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3954 | << CXXCopyConstructor, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3955 | PDiag(), |
| 3956 | ExceptionType, SourceLocation(), |
| 3957 | CtorType, CD->getLocation())) { |
| 3958 | HadError = true; |
| 3959 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | // If a function is explicitly defaulted on its first declaration, |
| 3963 | if (First) { |
| 3964 | // -- it is implicitly considered to be constexpr if the implicit |
| 3965 | // definition would be, |
| 3966 | CD->setConstexpr(CD->getParent()->defaultedCopyConstructorIsConstexpr()); |
| 3967 | |
| 3968 | // -- it is implicitly considered to have the same |
| 3969 | // exception-specification as if it had been implicitly declared, and |
| 3970 | // |
| 3971 | // FIXME: a compatible, but different, explicit exception specification |
| 3972 | // will be silently overridden. We should issue a warning if this happens. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3973 | EPI.ExtInfo = CtorType->getExtInfo(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3974 | |
| 3975 | // -- [...] it shall have the same parameter type as if it had been |
| 3976 | // implicitly declared. |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3977 | CD->setType(Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 3978 | |
| 3979 | // Such a function is also trivial if the implicitly-declared function |
| 3980 | // would have been. |
| 3981 | CD->setTrivial(CD->getParent()->hasTrivialCopyConstructor()); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | if (HadError) { |
| 3985 | CD->setInvalidDecl(); |
| 3986 | return; |
| 3987 | } |
| 3988 | |
Sean Hunt | c32d684 | 2011-10-11 04:55:36 +0000 | [diff] [blame] | 3989 | if (ShouldDeleteSpecialMember(CD, CXXCopyConstructor)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3990 | if (First) { |
| 3991 | CD->setDeletedAsWritten(); |
| 3992 | } else { |
| 3993 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3994 | << CXXCopyConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3995 | CD->setInvalidDecl(); |
| 3996 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3997 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3998 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3999 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4000 | void Sema::CheckExplicitlyDefaultedCopyAssignment(CXXMethodDecl *MD) { |
| 4001 | assert(MD->isExplicitlyDefaulted()); |
| 4002 | |
| 4003 | // Whether this was the first-declared instance of the operator |
| 4004 | bool First = MD == MD->getCanonicalDecl(); |
| 4005 | |
| 4006 | bool HadError = false; |
| 4007 | if (MD->getNumParams() != 1) { |
| 4008 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_params) |
| 4009 | << MD->getSourceRange(); |
| 4010 | HadError = true; |
| 4011 | } |
| 4012 | |
| 4013 | QualType ReturnType = |
| 4014 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 4015 | if (!ReturnType->isLValueReferenceType() || |
| 4016 | !Context.hasSameType( |
| 4017 | Context.getCanonicalType(ReturnType->getPointeeType()), |
| 4018 | Context.getCanonicalType(Context.getTypeDeclType(MD->getParent())))) { |
| 4019 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_return_type); |
| 4020 | HadError = true; |
| 4021 | } |
| 4022 | |
| 4023 | ImplicitExceptionSpecification Spec(Context); |
| 4024 | bool Const; |
| 4025 | llvm::tie(Spec, Const) = |
| 4026 | ComputeDefaultedCopyCtorExceptionSpecAndConst(MD->getParent()); |
| 4027 | |
| 4028 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 4029 | const FunctionProtoType *OperType = MD->getType()->getAs<FunctionProtoType>(), |
| 4030 | *ExceptionType = Context.getFunctionType( |
| 4031 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 4032 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4033 | QualType ArgType = OperType->getArgType(0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4034 | if (!ArgType->isLValueReferenceType()) { |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4035 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4036 | HadError = true; |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4037 | } else { |
| 4038 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 4039 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_volatile_param); |
| 4040 | HadError = true; |
| 4041 | } |
| 4042 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 4043 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_const_param); |
| 4044 | HadError = true; |
| 4045 | } |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4046 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4047 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4048 | if (OperType->getTypeQuals()) { |
| 4049 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_quals); |
| 4050 | HadError = true; |
| 4051 | } |
| 4052 | |
| 4053 | if (OperType->hasExceptionSpec()) { |
| 4054 | if (CheckEquivalentExceptionSpec( |
| 4055 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 4056 | << CXXCopyAssignment, |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4057 | PDiag(), |
| 4058 | ExceptionType, SourceLocation(), |
| 4059 | OperType, MD->getLocation())) { |
| 4060 | HadError = true; |
| 4061 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4062 | } |
| 4063 | if (First) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4064 | // We set the declaration to have the computed exception spec here. |
| 4065 | // We duplicate the one parameter type. |
| 4066 | EPI.RefQualifier = OperType->getRefQualifier(); |
| 4067 | EPI.ExtInfo = OperType->getExtInfo(); |
| 4068 | MD->setType(Context.getFunctionType(ReturnType, &ArgType, 1, EPI)); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 4069 | |
| 4070 | // Such a function is also trivial if the implicitly-declared function |
| 4071 | // would have been. |
| 4072 | MD->setTrivial(MD->getParent()->hasTrivialCopyAssignment()); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | if (HadError) { |
| 4076 | MD->setInvalidDecl(); |
| 4077 | return; |
| 4078 | } |
| 4079 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4080 | if (ShouldDeleteSpecialMember(MD, CXXCopyAssignment)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4081 | if (First) { |
| 4082 | MD->setDeletedAsWritten(); |
| 4083 | } else { |
| 4084 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 4085 | << CXXCopyAssignment; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4086 | MD->setInvalidDecl(); |
| 4087 | } |
| 4088 | } |
| 4089 | } |
| 4090 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4091 | void Sema::CheckExplicitlyDefaultedMoveConstructor(CXXConstructorDecl *CD) { |
| 4092 | assert(CD->isExplicitlyDefaulted() && CD->isMoveConstructor()); |
| 4093 | |
| 4094 | // Whether this was the first-declared instance of the constructor. |
| 4095 | bool First = CD == CD->getCanonicalDecl(); |
| 4096 | |
| 4097 | bool HadError = false; |
| 4098 | if (CD->getNumParams() != 1) { |
| 4099 | Diag(CD->getLocation(), diag::err_defaulted_move_ctor_params) |
| 4100 | << CD->getSourceRange(); |
| 4101 | HadError = true; |
| 4102 | } |
| 4103 | |
| 4104 | ImplicitExceptionSpecification Spec( |
| 4105 | ComputeDefaultedMoveCtorExceptionSpec(CD->getParent())); |
| 4106 | |
| 4107 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 4108 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 4109 | *ExceptionType = Context.getFunctionType( |
| 4110 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 4111 | |
| 4112 | // Check for parameter type matching. |
| 4113 | // This is a move ctor so we know it's a cv-qualified rvalue reference to T. |
| 4114 | QualType ArgType = CtorType->getArgType(0); |
| 4115 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 4116 | Diag(CD->getLocation(), diag::err_defaulted_move_ctor_volatile_param); |
| 4117 | HadError = true; |
| 4118 | } |
| 4119 | if (ArgType->getPointeeType().isConstQualified()) { |
| 4120 | Diag(CD->getLocation(), diag::err_defaulted_move_ctor_const_param); |
| 4121 | HadError = true; |
| 4122 | } |
| 4123 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4124 | // C++11 [dcl.fct.def.default]p2: |
| 4125 | // An explicitly-defaulted function may be declared constexpr only if it |
| 4126 | // would have been implicitly declared as constexpr, |
Richard Smith | eb273b7 | 2012-02-14 02:33:50 +0000 | [diff] [blame] | 4127 | // Do not apply this rule to templates, since core issue 1358 makes such |
| 4128 | // functions always instantiate to constexpr functions. |
| 4129 | if (CD->isConstexpr() && |
| 4130 | CD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4131 | if (!CD->getParent()->defaultedMoveConstructorIsConstexpr()) { |
| 4132 | Diag(CD->getLocStart(), diag::err_incorrect_defaulted_constexpr) |
| 4133 | << CXXMoveConstructor; |
| 4134 | HadError = true; |
| 4135 | } |
| 4136 | } |
| 4137 | // and may have an explicit exception-specification only if it is compatible |
| 4138 | // with the exception-specification on the implicit declaration. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4139 | if (CtorType->hasExceptionSpec()) { |
| 4140 | if (CheckEquivalentExceptionSpec( |
| 4141 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 4142 | << CXXMoveConstructor, |
| 4143 | PDiag(), |
| 4144 | ExceptionType, SourceLocation(), |
| 4145 | CtorType, CD->getLocation())) { |
| 4146 | HadError = true; |
| 4147 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | // If a function is explicitly defaulted on its first declaration, |
| 4151 | if (First) { |
| 4152 | // -- it is implicitly considered to be constexpr if the implicit |
| 4153 | // definition would be, |
| 4154 | CD->setConstexpr(CD->getParent()->defaultedMoveConstructorIsConstexpr()); |
| 4155 | |
| 4156 | // -- it is implicitly considered to have the same |
| 4157 | // exception-specification as if it had been implicitly declared, and |
| 4158 | // |
| 4159 | // FIXME: a compatible, but different, explicit exception specification |
| 4160 | // will be silently overridden. We should issue a warning if this happens. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4161 | EPI.ExtInfo = CtorType->getExtInfo(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4162 | |
| 4163 | // -- [...] it shall have the same parameter type as if it had been |
| 4164 | // implicitly declared. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4165 | CD->setType(Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 4166 | |
| 4167 | // Such a function is also trivial if the implicitly-declared function |
| 4168 | // would have been. |
| 4169 | CD->setTrivial(CD->getParent()->hasTrivialMoveConstructor()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | if (HadError) { |
| 4173 | CD->setInvalidDecl(); |
| 4174 | return; |
| 4175 | } |
| 4176 | |
Sean Hunt | 769bb2d | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 4177 | if (ShouldDeleteSpecialMember(CD, CXXMoveConstructor)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4178 | if (First) { |
| 4179 | CD->setDeletedAsWritten(); |
| 4180 | } else { |
| 4181 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
| 4182 | << CXXMoveConstructor; |
| 4183 | CD->setInvalidDecl(); |
| 4184 | } |
| 4185 | } |
| 4186 | } |
| 4187 | |
| 4188 | void Sema::CheckExplicitlyDefaultedMoveAssignment(CXXMethodDecl *MD) { |
| 4189 | assert(MD->isExplicitlyDefaulted()); |
| 4190 | |
| 4191 | // Whether this was the first-declared instance of the operator |
| 4192 | bool First = MD == MD->getCanonicalDecl(); |
| 4193 | |
| 4194 | bool HadError = false; |
| 4195 | if (MD->getNumParams() != 1) { |
| 4196 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_params) |
| 4197 | << MD->getSourceRange(); |
| 4198 | HadError = true; |
| 4199 | } |
| 4200 | |
| 4201 | QualType ReturnType = |
| 4202 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 4203 | if (!ReturnType->isLValueReferenceType() || |
| 4204 | !Context.hasSameType( |
| 4205 | Context.getCanonicalType(ReturnType->getPointeeType()), |
| 4206 | Context.getCanonicalType(Context.getTypeDeclType(MD->getParent())))) { |
| 4207 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_return_type); |
| 4208 | HadError = true; |
| 4209 | } |
| 4210 | |
| 4211 | ImplicitExceptionSpecification Spec( |
| 4212 | ComputeDefaultedMoveCtorExceptionSpec(MD->getParent())); |
| 4213 | |
| 4214 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 4215 | const FunctionProtoType *OperType = MD->getType()->getAs<FunctionProtoType>(), |
| 4216 | *ExceptionType = Context.getFunctionType( |
| 4217 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 4218 | |
| 4219 | QualType ArgType = OperType->getArgType(0); |
| 4220 | if (!ArgType->isRValueReferenceType()) { |
| 4221 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_not_ref); |
| 4222 | HadError = true; |
| 4223 | } else { |
| 4224 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 4225 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_volatile_param); |
| 4226 | HadError = true; |
| 4227 | } |
| 4228 | if (ArgType->getPointeeType().isConstQualified()) { |
| 4229 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_const_param); |
| 4230 | HadError = true; |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | if (OperType->getTypeQuals()) { |
| 4235 | Diag(MD->getLocation(), diag::err_defaulted_move_assign_quals); |
| 4236 | HadError = true; |
| 4237 | } |
| 4238 | |
| 4239 | if (OperType->hasExceptionSpec()) { |
| 4240 | if (CheckEquivalentExceptionSpec( |
| 4241 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 4242 | << CXXMoveAssignment, |
| 4243 | PDiag(), |
| 4244 | ExceptionType, SourceLocation(), |
| 4245 | OperType, MD->getLocation())) { |
| 4246 | HadError = true; |
| 4247 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4248 | } |
| 4249 | if (First) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4250 | // We set the declaration to have the computed exception spec here. |
| 4251 | // We duplicate the one parameter type. |
| 4252 | EPI.RefQualifier = OperType->getRefQualifier(); |
| 4253 | EPI.ExtInfo = OperType->getExtInfo(); |
| 4254 | MD->setType(Context.getFunctionType(ReturnType, &ArgType, 1, EPI)); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 4255 | |
| 4256 | // Such a function is also trivial if the implicitly-declared function |
| 4257 | // would have been. |
| 4258 | MD->setTrivial(MD->getParent()->hasTrivialMoveAssignment()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | if (HadError) { |
| 4262 | MD->setInvalidDecl(); |
| 4263 | return; |
| 4264 | } |
| 4265 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4266 | if (ShouldDeleteSpecialMember(MD, CXXMoveAssignment)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4267 | if (First) { |
| 4268 | MD->setDeletedAsWritten(); |
| 4269 | } else { |
| 4270 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) |
| 4271 | << CXXMoveAssignment; |
| 4272 | MD->setInvalidDecl(); |
| 4273 | } |
| 4274 | } |
| 4275 | } |
| 4276 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4277 | void Sema::CheckExplicitlyDefaultedDestructor(CXXDestructorDecl *DD) { |
| 4278 | assert(DD->isExplicitlyDefaulted()); |
| 4279 | |
| 4280 | // Whether this was the first-declared instance of the destructor. |
| 4281 | bool First = DD == DD->getCanonicalDecl(); |
| 4282 | |
| 4283 | ImplicitExceptionSpecification Spec |
| 4284 | = ComputeDefaultedDtorExceptionSpec(DD->getParent()); |
| 4285 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 4286 | const FunctionProtoType *DtorType = DD->getType()->getAs<FunctionProtoType>(), |
| 4287 | *ExceptionType = Context.getFunctionType( |
| 4288 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 4289 | |
| 4290 | if (DtorType->hasExceptionSpec()) { |
| 4291 | if (CheckEquivalentExceptionSpec( |
| 4292 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 4293 | << CXXDestructor, |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4294 | PDiag(), |
| 4295 | ExceptionType, SourceLocation(), |
| 4296 | DtorType, DD->getLocation())) { |
| 4297 | DD->setInvalidDecl(); |
| 4298 | return; |
| 4299 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4300 | } |
| 4301 | if (First) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4302 | // We set the declaration to have the computed exception spec here. |
| 4303 | // There are no parameters. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4304 | EPI.ExtInfo = DtorType->getExtInfo(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4305 | DD->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 4306 | |
| 4307 | // Such a function is also trivial if the implicitly-declared function |
| 4308 | // would have been. |
| 4309 | DD->setTrivial(DD->getParent()->hasTrivialDestructor()); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4312 | if (ShouldDeleteSpecialMember(DD, CXXDestructor)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4313 | if (First) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4314 | DD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4315 | } else { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4316 | Diag(DD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 4317 | << CXXDestructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4318 | DD->setInvalidDecl(); |
| 4319 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4320 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4323 | namespace { |
| 4324 | struct SpecialMemberDeletionInfo { |
| 4325 | Sema &S; |
| 4326 | CXXMethodDecl *MD; |
| 4327 | Sema::CXXSpecialMember CSM; |
| 4328 | |
| 4329 | // Properties of the special member, computed for convenience. |
| 4330 | bool IsConstructor, IsAssignment, IsMove, ConstArg, VolatileArg; |
| 4331 | SourceLocation Loc; |
| 4332 | |
| 4333 | bool AllFieldsAreConst; |
| 4334 | |
| 4335 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
| 4336 | Sema::CXXSpecialMember CSM) |
| 4337 | : S(S), MD(MD), CSM(CSM), |
| 4338 | IsConstructor(false), IsAssignment(false), IsMove(false), |
| 4339 | ConstArg(false), VolatileArg(false), Loc(MD->getLocation()), |
| 4340 | AllFieldsAreConst(true) { |
| 4341 | switch (CSM) { |
| 4342 | case Sema::CXXDefaultConstructor: |
| 4343 | case Sema::CXXCopyConstructor: |
| 4344 | IsConstructor = true; |
| 4345 | break; |
| 4346 | case Sema::CXXMoveConstructor: |
| 4347 | IsConstructor = true; |
| 4348 | IsMove = true; |
| 4349 | break; |
| 4350 | case Sema::CXXCopyAssignment: |
| 4351 | IsAssignment = true; |
| 4352 | break; |
| 4353 | case Sema::CXXMoveAssignment: |
| 4354 | IsAssignment = true; |
| 4355 | IsMove = true; |
| 4356 | break; |
| 4357 | case Sema::CXXDestructor: |
| 4358 | break; |
| 4359 | case Sema::CXXInvalid: |
| 4360 | llvm_unreachable("invalid special member kind"); |
| 4361 | } |
| 4362 | |
| 4363 | if (MD->getNumParams()) { |
| 4364 | ConstArg = MD->getParamDecl(0)->getType().isConstQualified(); |
| 4365 | VolatileArg = MD->getParamDecl(0)->getType().isVolatileQualified(); |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 4370 | |
| 4371 | /// Look up the corresponding special member in the given class. |
| 4372 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class) { |
| 4373 | unsigned TQ = MD->getTypeQualifiers(); |
| 4374 | return S.LookupSpecialMember(Class, CSM, ConstArg, VolatileArg, |
| 4375 | MD->getRefQualifier() == RQ_RValue, |
| 4376 | TQ & Qualifiers::Const, |
| 4377 | TQ & Qualifiers::Volatile); |
| 4378 | } |
| 4379 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4380 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, FieldDecl *Field); |
| 4381 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4382 | bool shouldDeleteForBase(CXXRecordDecl *BaseDecl, bool IsVirtualBase); |
| 4383 | bool shouldDeleteForField(FieldDecl *FD); |
| 4384 | bool shouldDeleteForAllConstMembers(); |
| 4385 | }; |
| 4386 | } |
| 4387 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4388 | /// Check whether we should delete a special member function due to having a |
| 4389 | /// direct or virtual base class or static data member of class type M. |
| 4390 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
| 4391 | CXXRecordDecl *Class, FieldDecl *Field) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4392 | // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5: |
| 4393 | // -- any direct or virtual base class [...] has a type with a destructor |
| 4394 | // that is deleted or inaccessible |
| 4395 | if (!IsAssignment) { |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4396 | CXXDestructorDecl *Dtor = S.LookupDestructor(Class); |
| 4397 | if (Dtor->isDeleted()) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4398 | return true; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4399 | if (S.CheckDestructorAccess(Loc, Dtor, S.PDiag()) != Sema::AR_accessible) |
| 4400 | return true; |
| 4401 | |
| 4402 | // C++11 [class.dtor]p5: |
| 4403 | // -- X is a union-like class that has a variant member with a non-trivial |
| 4404 | // destructor |
| 4405 | if (CSM == Sema::CXXDestructor && Field && Field->getParent()->isUnion() && |
| 4406 | !Dtor->isTrivial()) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4407 | return true; |
| 4408 | } |
| 4409 | |
| 4410 | // C++11 [class.ctor]p5: |
| 4411 | // -- any direct or virtual base class [...] has class type M [...] and |
| 4412 | // either M has no default constructor or overload resolution as applied |
| 4413 | // to M's default constructor results in an ambiguity or in a function |
| 4414 | // that is deleted or inaccessible |
| 4415 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 4416 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 4417 | // overload resolution, as applied to B's corresponding special member, |
| 4418 | // results in an ambiguity or a function that is deleted or inaccessible |
| 4419 | // from the defaulted special member |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4420 | // FIXME: in-class initializers should be handled here |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4421 | if (CSM != Sema::CXXDestructor) { |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4422 | Sema::SpecialMemberOverloadResult *SMOR = lookupIn(Class); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4423 | if (!SMOR->hasSuccess()) |
| 4424 | return true; |
| 4425 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4426 | CXXMethodDecl *Member = SMOR->getMethod(); |
| 4427 | // A member of a union must have a trivial corresponding special member. |
| 4428 | if (Field && Field->getParent()->isUnion() && !Member->isTrivial()) |
| 4429 | return true; |
| 4430 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4431 | if (IsConstructor) { |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4432 | CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(Member); |
| 4433 | if (S.CheckConstructorAccess(Loc, Ctor, Ctor->getAccess(), S.PDiag()) |
| 4434 | != Sema::AR_accessible) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4435 | return true; |
| 4436 | |
| 4437 | // -- for the move constructor, a [...] direct or virtual base class with |
| 4438 | // a type that does not have a move constructor and is not trivially |
| 4439 | // copyable. |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4440 | if (IsMove && !Ctor->isMoveConstructor() && !Class->isTriviallyCopyable()) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4441 | return true; |
| 4442 | } else { |
| 4443 | assert(IsAssignment && "unexpected kind of special member"); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4444 | if (S.CheckDirectMemberAccess(Loc, Member, S.PDiag()) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4445 | != Sema::AR_accessible) |
| 4446 | return true; |
| 4447 | |
| 4448 | // -- for the move assignment operator, a direct base class with a type |
| 4449 | // that does not have a move assignment operator and is not trivially |
| 4450 | // copyable. |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4451 | if (IsMove && !Member->isMoveAssignmentOperator() && |
| 4452 | !Class->isTriviallyCopyable()) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4453 | return true; |
| 4454 | } |
| 4455 | } |
| 4456 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4457 | return false; |
| 4458 | } |
| 4459 | |
| 4460 | /// Check whether we should delete a special member function due to the class |
| 4461 | /// having a particular direct or virtual base class. |
| 4462 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXRecordDecl *BaseDecl, |
| 4463 | bool IsVirtualBase) { |
| 4464 | // C++11 [class.copy]p23: |
| 4465 | // -- for the move assignment operator, any direct or indirect virtual |
| 4466 | // base class. |
| 4467 | if (CSM == Sema::CXXMoveAssignment && IsVirtualBase) |
| 4468 | return true; |
| 4469 | |
| 4470 | if (shouldDeleteForClassSubobject(BaseDecl, 0)) |
| 4471 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4472 | |
| 4473 | return false; |
| 4474 | } |
| 4475 | |
| 4476 | /// Check whether we should delete a special member function due to the class |
| 4477 | /// having a particular non-static data member. |
| 4478 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 4479 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 4480 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4481 | |
| 4482 | if (CSM == Sema::CXXDefaultConstructor) { |
| 4483 | // For a default constructor, all references must be initialized in-class |
| 4484 | // and, if a union, it must have a non-const member. |
| 4485 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) |
| 4486 | return true; |
| 4487 | |
| 4488 | if (inUnion() && !FieldType.isConstQualified()) |
| 4489 | AllFieldsAreConst = false; |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4490 | |
| 4491 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 4492 | // const-qualified type (or array thereof) with no |
| 4493 | // brace-or-equal-initializer does not have a user-provided default |
| 4494 | // constructor. |
| 4495 | if (!inUnion() && FieldType.isConstQualified() && |
| 4496 | !FD->hasInClassInitializer() && |
| 4497 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) |
| 4498 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4499 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 4500 | // For a copy constructor, data members must not be of rvalue reference |
| 4501 | // type. |
| 4502 | if (FieldType->isRValueReferenceType()) |
| 4503 | return true; |
| 4504 | } else if (IsAssignment) { |
| 4505 | // For an assignment operator, data members must not be of reference type. |
| 4506 | if (FieldType->isReferenceType()) |
| 4507 | return true; |
| 4508 | } |
| 4509 | |
| 4510 | if (FieldRecord) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4511 | // Some additional restrictions exist on the variant members. |
| 4512 | if (!inUnion() && FieldRecord->isUnion() && |
| 4513 | FieldRecord->isAnonymousStructOrUnion()) { |
| 4514 | bool AllVariantFieldsAreConst = true; |
| 4515 | |
| 4516 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4517 | UE = FieldRecord->field_end(); |
| 4518 | UI != UE; ++UI) { |
| 4519 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4520 | |
| 4521 | if (!UnionFieldType.isConstQualified()) |
| 4522 | AllVariantFieldsAreConst = false; |
| 4523 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4524 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 4525 | if (UnionFieldRecord && |
| 4526 | shouldDeleteForClassSubobject(UnionFieldRecord, *UI)) |
| 4527 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | // At least one member in each anonymous union must be non-const |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4531 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
| 4532 | FieldRecord->field_begin() != FieldRecord->field_end()) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4533 | return true; |
| 4534 | |
| 4535 | // Don't try to initialize the anonymous union |
| 4536 | // This is technically non-conformant, but sanity demands it. |
| 4537 | return false; |
| 4538 | } |
| 4539 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4540 | // When checking a constructor, the field's destructor must be accessible |
| 4541 | // and not deleted. |
| 4542 | if (IsConstructor) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4543 | CXXDestructorDecl *FieldDtor = S.LookupDestructor(FieldRecord); |
| 4544 | if (FieldDtor->isDeleted()) |
| 4545 | return true; |
| 4546 | if (S.CheckDestructorAccess(Loc, FieldDtor, S.PDiag()) != |
| 4547 | Sema::AR_accessible) |
| 4548 | return true; |
| 4549 | } |
| 4550 | |
| 4551 | // Check that the corresponding member of the field is accessible, |
| 4552 | // unique, and non-deleted. We don't do this if it has an explicit |
| 4553 | // initialization when default-constructing. |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4554 | if (!(CSM == Sema::CXXDefaultConstructor && FD->hasInClassInitializer())) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4555 | Sema::SpecialMemberOverloadResult *SMOR = lookupIn(FieldRecord); |
| 4556 | if (!SMOR->hasSuccess()) |
| 4557 | return true; |
| 4558 | |
| 4559 | CXXMethodDecl *FieldMember = SMOR->getMethod(); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4560 | |
| 4561 | // We need the corresponding member of a union to be trivial so that |
| 4562 | // we can safely process all members simultaneously. |
| 4563 | if (inUnion() && !FieldMember->isTrivial()) |
| 4564 | return true; |
| 4565 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4566 | if (IsConstructor) { |
| 4567 | CXXConstructorDecl *FieldCtor = cast<CXXConstructorDecl>(FieldMember); |
| 4568 | if (S.CheckConstructorAccess(Loc, FieldCtor, FieldCtor->getAccess(), |
| 4569 | S.PDiag()) != Sema::AR_accessible) |
| 4570 | return true; |
| 4571 | |
| 4572 | // For a move operation, the corresponding operation must actually |
| 4573 | // be a move operation (and not a copy selected by overload |
| 4574 | // resolution) unless we are working on a trivially copyable class. |
| 4575 | if (IsMove && !FieldCtor->isMoveConstructor() && |
| 4576 | !FieldRecord->isTriviallyCopyable()) |
| 4577 | return true; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4578 | } else if (CSM == Sema::CXXDestructor) { |
| 4579 | CXXDestructorDecl *FieldDtor = S.LookupDestructor(FieldRecord); |
| 4580 | if (FieldDtor->isDeleted()) |
| 4581 | return true; |
| 4582 | if (S.CheckDestructorAccess(Loc, FieldDtor, S.PDiag()) != |
| 4583 | Sema::AR_accessible) |
| 4584 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4585 | } else { |
| 4586 | assert(IsAssignment && "unexpected kind of special member"); |
| 4587 | if (S.CheckDirectMemberAccess(Loc, FieldMember, S.PDiag()) |
| 4588 | != Sema::AR_accessible) |
| 4589 | return true; |
| 4590 | |
| 4591 | // -- for the move assignment operator, a non-static data member with a |
| 4592 | // type that does not have a move assignment operator and is not |
| 4593 | // trivially copyable. |
| 4594 | if (IsMove && !FieldMember->isMoveAssignmentOperator() && |
| 4595 | !FieldRecord->isTriviallyCopyable()) |
| 4596 | return true; |
| 4597 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4598 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4599 | } else if (IsAssignment && FieldType.isConstQualified()) { |
| 4600 | // C++11 [class.copy]p23: |
| 4601 | // -- a non-static data member of const non-class type (or array thereof) |
| 4602 | return true; |
| 4603 | } |
| 4604 | |
| 4605 | return false; |
| 4606 | } |
| 4607 | |
| 4608 | /// C++11 [class.ctor] p5: |
| 4609 | /// A defaulted default constructor for a class X is defined as deleted if |
| 4610 | /// X is a union and all of its variant members are of const-qualified type. |
| 4611 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4612 | // This is a silly definition, because it gives an empty union a deleted |
| 4613 | // default constructor. Don't do that. |
| 4614 | return CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
| 4615 | (MD->getParent()->field_begin() != MD->getParent()->field_end()); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | /// Determine whether a defaulted special member function should be defined as |
| 4619 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 4620 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4621 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM) { |
| 4622 | assert(!MD->isInvalidDecl()); |
| 4623 | CXXRecordDecl *RD = MD->getParent(); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4624 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 4625 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4626 | return false; |
| 4627 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4628 | // FIXME: Provide the ability to diagnose why a special member was deleted. |
| 4629 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4630 | // C++11 [expr.lambda.prim]p19: |
| 4631 | // The closure type associated with a lambda-expression has a |
| 4632 | // deleted (8.4.3) default constructor and a deleted copy |
| 4633 | // assignment operator. |
| 4634 | if (RD->isLambda() && |
| 4635 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) |
| 4636 | return true; |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4637 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4638 | // C++11 [class.dtor]p5: |
| 4639 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 4640 | // results in an ambiguity or in a function that is deleted or inaccessible |
| 4641 | if (CSM == Sema::CXXDestructor && MD->isVirtual()) { |
| 4642 | FunctionDecl *OperatorDelete = 0; |
| 4643 | DeclarationName Name = |
| 4644 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 4645 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
| 4646 | OperatorDelete, false)) |
| 4647 | return true; |
| 4648 | } |
| 4649 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4650 | // For an anonymous struct or union, the copy and assignment special members |
| 4651 | // will never be used, so skip the check. For an anonymous union declared at |
| 4652 | // namespace scope, the constructor and destructor are used. |
| 4653 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 4654 | RD->isAnonymousStructOrUnion()) |
| 4655 | return false; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 4656 | |
Sean Hunt | c32d684 | 2011-10-11 04:55:36 +0000 | [diff] [blame] | 4657 | // Do access control from the special member function |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4658 | ContextRAII MethodContext(*this, MD); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4659 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4660 | SpecialMemberDeletionInfo SMI(*this, MD, CSM); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4661 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4662 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4663 | BE = RD->bases_end(); BI != BE; ++BI) |
| 4664 | if (!BI->isVirtual() && |
| 4665 | SMI.shouldDeleteForBase(BI->getType()->getAsCXXRecordDecl(), false)) |
| 4666 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4667 | |
| 4668 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4669 | BE = RD->vbases_end(); BI != BE; ++BI) |
| 4670 | if (SMI.shouldDeleteForBase(BI->getType()->getAsCXXRecordDecl(), true)) |
| 4671 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4672 | |
| 4673 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4674 | FE = RD->field_end(); FI != FE; ++FI) |
| 4675 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
| 4676 | SMI.shouldDeleteForField(*FI)) |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 4677 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4678 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4679 | if (SMI.shouldDeleteForAllConstMembers()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4680 | return true; |
| 4681 | |
| 4682 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4686 | namespace { |
| 4687 | struct FindHiddenVirtualMethodData { |
| 4688 | Sema *S; |
| 4689 | CXXMethodDecl *Method; |
| 4690 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4691 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4692 | }; |
| 4693 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4694 | |
| 4695 | /// \brief Member lookup function that determines whether a given C++ |
| 4696 | /// method overloads virtual methods in a base class without overriding any, |
| 4697 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 4698 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 4699 | CXXBasePath &Path, |
| 4700 | void *UserData) { |
| 4701 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 4702 | |
| 4703 | FindHiddenVirtualMethodData &Data |
| 4704 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 4705 | |
| 4706 | DeclarationName Name = Data.Method->getDeclName(); |
| 4707 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 4708 | |
| 4709 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4710 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4711 | for (Path.Decls = BaseRecord->lookup(Name); |
| 4712 | Path.Decls.first != Path.Decls.second; |
| 4713 | ++Path.Decls.first) { |
| 4714 | NamedDecl *D = *Path.Decls.first; |
| 4715 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4716 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4717 | foundSameNameMethod = true; |
| 4718 | // Interested only in hidden virtual methods. |
| 4719 | if (!MD->isVirtual()) |
| 4720 | continue; |
| 4721 | // If the method we are checking overrides a method from its base |
| 4722 | // don't warn about the other overloaded methods. |
| 4723 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 4724 | return true; |
| 4725 | // Collect the overload only if its hidden. |
| 4726 | if (!Data.OverridenAndUsingBaseMethods.count(MD)) |
| 4727 | overloadedMethods.push_back(MD); |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | if (foundSameNameMethod) |
| 4732 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 4733 | overloadedMethods.end()); |
| 4734 | return foundSameNameMethod; |
| 4735 | } |
| 4736 | |
| 4737 | /// \brief See if a method overloads virtual methods in a base class without |
| 4738 | /// overriding any. |
| 4739 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 4740 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 4741 | MD->getLocation()) == DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4742 | return; |
| 4743 | if (MD->getDeclName().getNameKind() != DeclarationName::Identifier) |
| 4744 | return; |
| 4745 | |
| 4746 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 4747 | /*bool RecordPaths=*/false, |
| 4748 | /*bool DetectVirtual=*/false); |
| 4749 | FindHiddenVirtualMethodData Data; |
| 4750 | Data.Method = MD; |
| 4751 | Data.S = this; |
| 4752 | |
| 4753 | // Keep the base methods that were overriden or introduced in the subclass |
| 4754 | // by 'using' in a set. A base method not in this set is hidden. |
| 4755 | for (DeclContext::lookup_result res = DC->lookup(MD->getDeclName()); |
| 4756 | res.first != res.second; ++res.first) { |
| 4757 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*res.first)) |
| 4758 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 4759 | E = MD->end_overridden_methods(); |
| 4760 | I != E; ++I) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4761 | Data.OverridenAndUsingBaseMethods.insert((*I)->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4762 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*res.first)) |
| 4763 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(shad->getTargetDecl())) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4764 | Data.OverridenAndUsingBaseMethods.insert(MD->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 4768 | !Data.OverloadedMethods.empty()) { |
| 4769 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 4770 | << MD << (Data.OverloadedMethods.size() > 1); |
| 4771 | |
| 4772 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 4773 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 4774 | Diag(overloadedMD->getLocation(), |
| 4775 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 4776 | } |
| 4777 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4780 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4781 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4782 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 4783 | SourceLocation RBrac, |
| 4784 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4785 | if (!TagDecl) |
| 4786 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4788 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4789 | |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 4790 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4791 | // strict aliasing violation! |
| 4792 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 4793 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4794 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4795 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4796 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4799 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 4800 | /// special functions, such as the default constructor, copy |
| 4801 | /// constructor, or destructor, to the given C++ class (C++ |
| 4802 | /// [special]p1). This routine can only be executed just before the |
| 4803 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4804 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4805 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4806 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 4808 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 4809 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4810 | |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 4811 | if (getLangOptions().CPlusPlus0x && ClassDecl->needsImplicitMoveConstructor()) |
| 4812 | ++ASTContext::NumImplicitMoveConstructors; |
| 4813 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4814 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 4815 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 4816 | |
| 4817 | // If we have a dynamic class, then the copy assignment operator may be |
| 4818 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 4819 | // it shows up in the right place in the vtable and that we diagnose |
| 4820 | // problems with the implicit exception specification. |
| 4821 | if (ClassDecl->isDynamicClass()) |
| 4822 | DeclareImplicitCopyAssignment(ClassDecl); |
| 4823 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4824 | |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 4825 | if (getLangOptions().CPlusPlus0x && ClassDecl->needsImplicitMoveAssignment()){ |
| 4826 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 4827 | |
| 4828 | // Likewise for the move assignment operator. |
| 4829 | if (ClassDecl->isDynamicClass()) |
| 4830 | DeclareImplicitMoveAssignment(ClassDecl); |
| 4831 | } |
| 4832 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4833 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 4834 | ++ASTContext::NumImplicitDestructors; |
| 4835 | |
| 4836 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 4837 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 4838 | // shows up in the right place in the vtable and that we diagnose problems |
| 4839 | // with the implicit exception specification. |
| 4840 | if (ClassDecl->isDynamicClass()) |
| 4841 | DeclareImplicitDestructor(ClassDecl); |
| 4842 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4845 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 4846 | if (!D) |
| 4847 | return; |
| 4848 | |
| 4849 | int NumParamList = D->getNumTemplateParameterLists(); |
| 4850 | for (int i = 0; i < NumParamList; i++) { |
| 4851 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 4852 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4853 | ParamEnd = Params->end(); |
| 4854 | Param != ParamEnd; ++Param) { |
| 4855 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4856 | if (Named->getDeclName()) { |
| 4857 | S->AddDecl(Named); |
| 4858 | IdResolver.AddDecl(Named); |
| 4859 | } |
| 4860 | } |
| 4861 | } |
| 4862 | } |
| 4863 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4864 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 4865 | if (!D) |
| 4866 | return; |
| 4867 | |
| 4868 | TemplateParameterList *Params = 0; |
| 4869 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 4870 | Params = Template->getTemplateParameters(); |
| 4871 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4872 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 4873 | Params = PartialSpec->getTemplateParameters(); |
| 4874 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4875 | return; |
| 4876 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4877 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4878 | ParamEnd = Params->end(); |
| 4879 | Param != ParamEnd; ++Param) { |
| 4880 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4881 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4882 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4883 | IdResolver.AddDecl(Named); |
| 4884 | } |
| 4885 | } |
| 4886 | } |
| 4887 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4888 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4889 | if (!RecordD) return; |
| 4890 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4891 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4892 | PushDeclContext(S, Record); |
| 4893 | } |
| 4894 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4895 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4896 | if (!RecordD) return; |
| 4897 | PopDeclContext(); |
| 4898 | } |
| 4899 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4900 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 4901 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 4902 | /// parsing those parts of the given Method declaration that could |
| 4903 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 4904 | /// arguments. This action should enter the scope of the given |
| 4905 | /// Method declaration as if we had just parsed the qualified method |
| 4906 | /// name. However, it should not bring the parameters into scope; |
| 4907 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4908 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 4912 | /// C++ method declaration. We're (re-)introducing the given |
| 4913 | /// function parameter into scope for use in parsing later parts of |
| 4914 | /// the method declaration. For example, we could see an |
| 4915 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4916 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4917 | if (!ParamD) |
| 4918 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4919 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4920 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4921 | |
| 4922 | // If this parameter has an unparsed default argument, clear it out |
| 4923 | // to make way for the parsed default argument. |
| 4924 | if (Param->hasUnparsedDefaultArg()) |
| 4925 | Param->setDefaultArg(0); |
| 4926 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4927 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4928 | if (Param->getDeclName()) |
| 4929 | IdResolver.AddDecl(Param); |
| 4930 | } |
| 4931 | |
| 4932 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 4933 | /// processing the delayed method declaration for Method. The method |
| 4934 | /// declaration is now considered finished. There may be a separate |
| 4935 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 4936 | /// immediately!) for this method, if it was also defined inside the |
| 4937 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4938 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4939 | if (!MethodD) |
| 4940 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4942 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4944 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4945 | |
| 4946 | // Now that we have our default arguments, check the constructor |
| 4947 | // again. It could produce additional diagnostics or affect whether |
| 4948 | // the class has implicitly-declared destructors, among other |
| 4949 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4950 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 4951 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4952 | |
| 4953 | // Check the default arguments, which we may have added. |
| 4954 | if (!Method->isInvalidDecl()) |
| 4955 | CheckCXXDefaultArguments(Method); |
| 4956 | } |
| 4957 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4958 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4959 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4960 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4961 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 4962 | /// will be updated to reflect a well-formed type for the constructor and |
| 4963 | /// returned. |
| 4964 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4965 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4966 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4967 | |
| 4968 | // C++ [class.ctor]p3: |
| 4969 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 4970 | // constructor can be invoked for a const, volatile or const |
| 4971 | // volatile object. A constructor shall not be declared const, |
| 4972 | // volatile, or const volatile (9.3.2). |
| 4973 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4974 | if (!D.isInvalidType()) |
| 4975 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4976 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 4977 | << SourceRange(D.getIdentifierLoc()); |
| 4978 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4979 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4980 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4981 | if (!D.isInvalidType()) |
| 4982 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4983 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 4984 | << SourceRange(D.getIdentifierLoc()); |
| 4985 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4986 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4987 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4988 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4989 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4990 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4991 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4992 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4993 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4994 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4995 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4996 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4997 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4998 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4999 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5000 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5002 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5003 | // C++0x [class.ctor]p4: |
| 5004 | // A constructor shall not be declared with a ref-qualifier. |
| 5005 | if (FTI.hasRefQualifier()) { |
| 5006 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 5007 | << FTI.RefQualifierIsLValueRef |
| 5008 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5009 | D.setInvalidType(); |
| 5010 | } |
| 5011 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5012 | // Rebuild the function type "R" without any type qualifiers (in |
| 5013 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5014 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5015 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5016 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 5017 | return R; |
| 5018 | |
| 5019 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5020 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5021 | EPI.RefQualifier = RQ_None; |
| 5022 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5023 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5024 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5027 | /// CheckConstructor - Checks a fully-formed constructor for |
| 5028 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 5029 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5030 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5031 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5032 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 5033 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5034 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5035 | |
| 5036 | // C++ [class.copy]p3: |
| 5037 | // A declaration of a constructor for a class X is ill-formed if |
| 5038 | // its first parameter is of type (optionally cv-qualified) X and |
| 5039 | // either there are no other parameters or else all other |
| 5040 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5041 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5042 | ((Constructor->getNumParams() == 1) || |
| 5043 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5044 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 5045 | Constructor->getTemplateSpecializationKind() |
| 5046 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5047 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 5048 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 5049 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5050 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5051 | const char *ConstRef |
| 5052 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 5053 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5054 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5055 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5056 | |
| 5057 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 5058 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5059 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5060 | } |
| 5061 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5062 | } |
| 5063 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 5064 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 5065 | /// well-formedness, issuing any diagnostics required. Returns true |
| 5066 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5067 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5068 | CXXRecordDecl *RD = Destructor->getParent(); |
| 5069 | |
| 5070 | if (Destructor->isVirtual()) { |
| 5071 | SourceLocation Loc; |
| 5072 | |
| 5073 | if (!Destructor->isImplicit()) |
| 5074 | Loc = Destructor->getLocation(); |
| 5075 | else |
| 5076 | Loc = RD->getLocation(); |
| 5077 | |
| 5078 | // If we have a virtual destructor, look up the deallocation function |
| 5079 | FunctionDecl *OperatorDelete = 0; |
| 5080 | DeclarationName Name = |
| 5081 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5082 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5083 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 5084 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5085 | MarkFunctionReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5086 | |
| 5087 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5088 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5089 | |
| 5090 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5093 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5094 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 5095 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 5096 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5097 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5100 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 5101 | /// the well-formednes of the destructor declarator @p D with type @p |
| 5102 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5103 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 5104 | /// will be updated to reflect a well-formed type for the destructor and |
| 5105 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5106 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5107 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5108 | // C++ [class.dtor]p1: |
| 5109 | // [...] A typedef-name that names a class is a class-name |
| 5110 | // (7.1.3); however, a typedef-name that names a class shall not |
| 5111 | // be used as the identifier in the declarator for a destructor |
| 5112 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5113 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5114 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5115 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5116 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5117 | else if (const TemplateSpecializationType *TST = |
| 5118 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 5119 | if (TST->isTypeAlias()) |
| 5120 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 5121 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5122 | |
| 5123 | // C++ [class.dtor]p2: |
| 5124 | // A destructor is used to destroy objects of its class type. A |
| 5125 | // destructor takes no parameters, and no return type can be |
| 5126 | // specified for it (not even void). The address of a destructor |
| 5127 | // shall not be taken. A destructor shall not be static. A |
| 5128 | // destructor can be invoked for a const, volatile or const |
| 5129 | // volatile object. A destructor shall not be declared const, |
| 5130 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5131 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5132 | if (!D.isInvalidType()) |
| 5133 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 5134 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5135 | << SourceRange(D.getIdentifierLoc()) |
| 5136 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5137 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5138 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5139 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5140 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5141 | // Destructors don't have return types, but the parser will |
| 5142 | // happily parse something like: |
| 5143 | // |
| 5144 | // class X { |
| 5145 | // float ~X(); |
| 5146 | // }; |
| 5147 | // |
| 5148 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5149 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 5150 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5151 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5153 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5154 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5155 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5156 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5157 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5158 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5159 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5160 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5161 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5162 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5163 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5164 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5165 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5168 | // C++0x [class.dtor]p2: |
| 5169 | // A destructor shall not be declared with a ref-qualifier. |
| 5170 | if (FTI.hasRefQualifier()) { |
| 5171 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 5172 | << FTI.RefQualifierIsLValueRef |
| 5173 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5174 | D.setInvalidType(); |
| 5175 | } |
| 5176 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5177 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5178 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5179 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 5180 | |
| 5181 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5182 | FTI.freeArgs(); |
| 5183 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5186 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5187 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5188 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5189 | D.setInvalidType(); |
| 5190 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5191 | |
| 5192 | // Rebuild the function type "R" without any type qualifiers or |
| 5193 | // parameters (in case any of the errors above fired) and with |
| 5194 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5195 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5196 | if (!D.isInvalidType()) |
| 5197 | return R; |
| 5198 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5199 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5200 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5201 | EPI.Variadic = false; |
| 5202 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5203 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5204 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5207 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 5208 | /// well-formednes of the conversion function declarator @p D with |
| 5209 | /// type @p R. If there are any errors in the declarator, this routine |
| 5210 | /// will emit diagnostics and return true. Otherwise, it will return |
| 5211 | /// false. Either way, the type @p R will be updated to reflect a |
| 5212 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5213 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5214 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5215 | // C++ [class.conv.fct]p1: |
| 5216 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5217 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5219 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5220 | if (!D.isInvalidType()) |
| 5221 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 5222 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5223 | << SourceRange(D.getIdentifierLoc()); |
| 5224 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5225 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5226 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5227 | |
| 5228 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 5229 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5230 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5231 | // Conversion functions don't have return types, but the parser will |
| 5232 | // happily parse something like: |
| 5233 | // |
| 5234 | // class X { |
| 5235 | // float operator bool(); |
| 5236 | // }; |
| 5237 | // |
| 5238 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5239 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 5240 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5241 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5242 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5245 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 5246 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5247 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5248 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5249 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 5250 | |
| 5251 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5252 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5253 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5254 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5255 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5256 | D.setInvalidType(); |
| 5257 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5258 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5259 | // Diagnose "&operator bool()" and other such nonsense. This |
| 5260 | // is actually a gcc extension which we don't support. |
| 5261 | if (Proto->getResultType() != ConvType) { |
| 5262 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 5263 | << Proto->getResultType(); |
| 5264 | D.setInvalidType(); |
| 5265 | ConvType = Proto->getResultType(); |
| 5266 | } |
| 5267 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5268 | // C++ [class.conv.fct]p4: |
| 5269 | // The conversion-type-id shall not represent a function type nor |
| 5270 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5271 | if (ConvType->isArrayType()) { |
| 5272 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 5273 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5274 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5275 | } else if (ConvType->isFunctionType()) { |
| 5276 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 5277 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5278 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | // Rebuild the function type "R" without any parameters (in case any |
| 5282 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5283 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5284 | if (D.isInvalidType()) |
| 5285 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5286 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5287 | // C++0x explicit conversion operators. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5288 | if (D.getDeclSpec().isExplicitSpecified()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5289 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5290 | getLangOptions().CPlusPlus0x ? |
| 5291 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 5292 | diag::ext_explicit_conversion_functions) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5293 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5296 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 5297 | /// the declaration of the given C++ conversion function. This routine |
| 5298 | /// is responsible for recording the conversion function in the C++ |
| 5299 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5300 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5301 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 5302 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 5303 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5304 | |
| 5305 | // Make sure we aren't redeclaring the conversion function. |
| 5306 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5307 | |
| 5308 | // C++ [class.conv.fct]p1: |
| 5309 | // [...] A conversion function is never used to convert a |
| 5310 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 5311 | // same object type (or a reference to it), to a (possibly |
| 5312 | // cv-qualified) base class of that type (or a reference to it), |
| 5313 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5314 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 5315 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5316 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5317 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5318 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5319 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5320 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 5321 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 5322 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5323 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5324 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 5325 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5326 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5327 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5328 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5329 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5330 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5331 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5332 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5333 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 5336 | if (FunctionTemplateDecl *ConversionTemplate |
| 5337 | = Conversion->getDescribedFunctionTemplate()) |
| 5338 | return ConversionTemplate; |
| 5339 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5340 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5343 | //===----------------------------------------------------------------------===// |
| 5344 | // Namespace Handling |
| 5345 | //===----------------------------------------------------------------------===// |
| 5346 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5347 | |
| 5348 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5349 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 5350 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5351 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 5352 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5353 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5354 | SourceLocation IdentLoc, |
| 5355 | IdentifierInfo *II, |
| 5356 | SourceLocation LBrace, |
| 5357 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5358 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 5359 | // For anonymous namespace, take the location of the left brace. |
| 5360 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5361 | bool IsInline = InlineLoc.isValid(); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5362 | bool IsInvalid = false; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5363 | bool IsStd = false; |
| 5364 | bool AddToKnown = false; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5365 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 5366 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5367 | NamespaceDecl *PrevNS = 0; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5368 | if (II) { |
| 5369 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 5370 | // The identifier in an original-namespace-definition shall not |
| 5371 | // have been previously defined in the declarative region in |
| 5372 | // which the original-namespace-definition appears. The |
| 5373 | // identifier in an original-namespace-definition is the name of |
| 5374 | // the namespace. Subsequently in that declarative region, it is |
| 5375 | // treated as an original-namespace-name. |
| 5376 | // |
| 5377 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 5378 | // look through using directives, just look for any ordinary names. |
| 5379 | |
| 5380 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5381 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 5382 | Decl::IDNS_Namespace; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 5383 | NamedDecl *PrevDecl = 0; |
| 5384 | for (DeclContext::lookup_result R |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5385 | = CurContext->getRedeclContext()->lookup(II); |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 5386 | R.first != R.second; ++R.first) { |
| 5387 | if ((*R.first)->getIdentifierNamespace() & IDNS) { |
| 5388 | PrevDecl = *R.first; |
| 5389 | break; |
| 5390 | } |
| 5391 | } |
| 5392 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5393 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 5394 | |
| 5395 | if (PrevNS) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5396 | // This is an extended namespace definition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5397 | if (IsInline != PrevNS->isInline()) { |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5398 | // inline-ness must match |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5399 | if (PrevNS->isInline()) { |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 5400 | // The user probably just forgot the 'inline', so suggest that it |
| 5401 | // be added back. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5402 | Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 5403 | << FixItHint::CreateInsertion(NamespaceLoc, "inline "); |
| 5404 | } else { |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5405 | Diag(Loc, diag::err_inline_namespace_mismatch) |
| 5406 | << IsInline; |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 5407 | } |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5408 | Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 5409 | |
| 5410 | IsInline = PrevNS->isInline(); |
| 5411 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5412 | } else if (PrevDecl) { |
| 5413 | // This is an invalid name redefinition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5414 | Diag(Loc, diag::err_redefinition_different_kind) |
| 5415 | << II; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5416 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5417 | IsInvalid = true; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5418 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5419 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5420 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 5421 | // This is the first "real" definition of the namespace "std", so update |
| 5422 | // our cache of the "std" namespace to point at this definition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5423 | PrevNS = getStdNamespace(); |
| 5424 | IsStd = true; |
| 5425 | AddToKnown = !IsInline; |
| 5426 | } else { |
| 5427 | // We've seen this namespace for the first time. |
| 5428 | AddToKnown = !IsInline; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5429 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5430 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5431 | // Anonymous namespaces. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5432 | |
| 5433 | // Determine whether the parent already has an anonymous namespace. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5434 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5435 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5436 | PrevNS = TU->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5437 | } else { |
| 5438 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5439 | PrevNS = ND->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5442 | if (PrevNS && IsInline != PrevNS->isInline()) { |
| 5443 | // inline-ness must match |
| 5444 | Diag(Loc, diag::err_inline_namespace_mismatch) |
| 5445 | << IsInline; |
| 5446 | Diag(PrevNS->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5447 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5448 | // Recover by ignoring the new namespace's inline status. |
| 5449 | IsInline = PrevNS->isInline(); |
| 5450 | } |
| 5451 | } |
| 5452 | |
| 5453 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 5454 | StartLoc, Loc, II, PrevNS); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5455 | if (IsInvalid) |
| 5456 | Namespc->setInvalidDecl(); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5457 | |
| 5458 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5459 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5460 | // FIXME: Should we be merging attributes? |
| 5461 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 5462 | PushNamespaceVisibilityAttr(Attr, Loc); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5463 | |
| 5464 | if (IsStd) |
| 5465 | StdNamespace = Namespc; |
| 5466 | if (AddToKnown) |
| 5467 | KnownNamespaces[Namespc] = false; |
| 5468 | |
| 5469 | if (II) { |
| 5470 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 5471 | } else { |
| 5472 | // Link the anonymous namespace into its parent. |
| 5473 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 5474 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 5475 | TU->setAnonymousNamespace(Namespc); |
| 5476 | } else { |
| 5477 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5478 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5479 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 5480 | CurContext->addDecl(Namespc); |
| 5481 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5482 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 5483 | // behaves as if it were replaced by |
| 5484 | // namespace unique { /* empty body */ } |
| 5485 | // using namespace unique; |
| 5486 | // namespace unique { namespace-body } |
| 5487 | // where all occurrences of 'unique' in a translation unit are |
| 5488 | // replaced by the same identifier and this identifier differs |
| 5489 | // from all other identifiers in the entire program. |
| 5490 | |
| 5491 | // We just create the namespace with an empty name and then add an |
| 5492 | // implicit using declaration, just like the standard suggests. |
| 5493 | // |
| 5494 | // CodeGen enforces the "universally unique" aspect by giving all |
| 5495 | // declarations semantically contained within an anonymous |
| 5496 | // namespace internal linkage. |
| 5497 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5498 | if (!PrevNS) { |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5499 | UsingDirectiveDecl* UD |
| 5500 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 5501 | /* 'using' */ LBrace, |
| 5502 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 5503 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5504 | /* identifier */ SourceLocation(), |
| 5505 | Namespc, |
| 5506 | /* Ancestor */ CurContext); |
| 5507 | UD->setImplicit(); |
| 5508 | CurContext->addDecl(UD); |
| 5509 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 5513 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5514 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 5515 | // for the namespace has the declarations that showed up in that particular |
| 5516 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5517 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5518 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5521 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 5522 | /// is a namespace alias, returns the namespace it points to. |
| 5523 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 5524 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 5525 | return AD->getNamespace(); |
| 5526 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 5527 | } |
| 5528 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5529 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 5530 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5531 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5532 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 5533 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5534 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5535 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 5536 | if (Namespc->hasAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 5537 | PopPragmaVisibility(true, RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5538 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5539 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 5540 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 5541 | return cast_or_null<CXXRecordDecl>( |
| 5542 | StdBadAlloc.get(Context.getExternalSource())); |
| 5543 | } |
| 5544 | |
| 5545 | NamespaceDecl *Sema::getStdNamespace() const { |
| 5546 | return cast_or_null<NamespaceDecl>( |
| 5547 | StdNamespace.get(Context.getExternalSource())); |
| 5548 | } |
| 5549 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5550 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 5551 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 5552 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5553 | if (!StdNamespace) { |
| 5554 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 5555 | StdNamespace = NamespaceDecl::Create(Context, |
| 5556 | Context.getTranslationUnitDecl(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5557 | /*Inline=*/false, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5558 | SourceLocation(), SourceLocation(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5559 | &PP.getIdentifierTable().get("std"), |
| 5560 | /*PrevDecl=*/0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5561 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5564 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5567 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
| 5568 | assert(getLangOptions().CPlusPlus && |
| 5569 | "Looking for std::initializer_list outside of C++."); |
| 5570 | |
| 5571 | // We're looking for implicit instantiations of |
| 5572 | // template <typename E> class std::initializer_list. |
| 5573 | |
| 5574 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 5575 | return false; |
| 5576 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5577 | ClassTemplateDecl *Template = 0; |
| 5578 | const TemplateArgument *Arguments = 0; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5579 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5580 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5581 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5582 | ClassTemplateSpecializationDecl *Specialization = |
| 5583 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 5584 | if (!Specialization) |
| 5585 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5586 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5587 | Template = Specialization->getSpecializedTemplate(); |
| 5588 | Arguments = Specialization->getTemplateArgs().data(); |
| 5589 | } else if (const TemplateSpecializationType *TST = |
| 5590 | Ty->getAs<TemplateSpecializationType>()) { |
| 5591 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 5592 | TST->getTemplateName().getAsTemplateDecl()); |
| 5593 | Arguments = TST->getArgs(); |
| 5594 | } |
| 5595 | if (!Template) |
| 5596 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5597 | |
| 5598 | if (!StdInitializerList) { |
| 5599 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 5600 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 5601 | if (TemplateClass->getIdentifier() != |
| 5602 | &PP.getIdentifierTable().get("initializer_list") || |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 5603 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 5604 | TemplateClass->getDeclContext())) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5605 | return false; |
| 5606 | // This is a template called std::initializer_list, but is it the right |
| 5607 | // template? |
| 5608 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 5609 | if (Params->getMinRequiredArguments() != 1) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5610 | return false; |
| 5611 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 5612 | return false; |
| 5613 | |
| 5614 | // It's the right template. |
| 5615 | StdInitializerList = Template; |
| 5616 | } |
| 5617 | |
| 5618 | if (Template != StdInitializerList) |
| 5619 | return false; |
| 5620 | |
| 5621 | // This is an instance of std::initializer_list. Find the argument type. |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5622 | if (Element) |
| 5623 | *Element = Arguments[0].getAsType(); |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5624 | return true; |
| 5625 | } |
| 5626 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 5627 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 5628 | NamespaceDecl *Std = S.getStdNamespace(); |
| 5629 | if (!Std) { |
| 5630 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 5631 | return 0; |
| 5632 | } |
| 5633 | |
| 5634 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 5635 | Loc, Sema::LookupOrdinaryName); |
| 5636 | if (!S.LookupQualifiedName(Result, Std)) { |
| 5637 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 5638 | return 0; |
| 5639 | } |
| 5640 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 5641 | if (!Template) { |
| 5642 | Result.suppressDiagnostics(); |
| 5643 | // We found something weird. Complain about the first thing we found. |
| 5644 | NamedDecl *Found = *Result.begin(); |
| 5645 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 5646 | return 0; |
| 5647 | } |
| 5648 | |
| 5649 | // We found some template called std::initializer_list. Now verify that it's |
| 5650 | // correct. |
| 5651 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 5652 | if (Params->getMinRequiredArguments() != 1 || |
| 5653 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 5654 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 5655 | return 0; |
| 5656 | } |
| 5657 | |
| 5658 | return Template; |
| 5659 | } |
| 5660 | |
| 5661 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 5662 | if (!StdInitializerList) { |
| 5663 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 5664 | if (!StdInitializerList) |
| 5665 | return QualType(); |
| 5666 | } |
| 5667 | |
| 5668 | TemplateArgumentListInfo Args(Loc, Loc); |
| 5669 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 5670 | Context.getTrivialTypeSourceInfo(Element, |
| 5671 | Loc))); |
| 5672 | return Context.getCanonicalType( |
| 5673 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 5674 | } |
| 5675 | |
Sebastian Redl | 98d3606 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 5676 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 5677 | // C++ [dcl.init.list]p2: |
| 5678 | // A constructor is an initializer-list constructor if its first parameter |
| 5679 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 5680 | // std::initializer_list<E> for some type E, and either there are no other |
| 5681 | // parameters or else all other parameters have default arguments. |
| 5682 | if (Ctor->getNumParams() < 1 || |
| 5683 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 5684 | return false; |
| 5685 | |
| 5686 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 5687 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 5688 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 5689 | |
| 5690 | return isStdInitializerList(ArgType, 0); |
| 5691 | } |
| 5692 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 5693 | /// \brief Determine whether a using statement is in a context where it will be |
| 5694 | /// apply in all contexts. |
| 5695 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 5696 | switch (CurContext->getDeclKind()) { |
| 5697 | case Decl::TranslationUnit: |
| 5698 | return true; |
| 5699 | case Decl::LinkageSpec: |
| 5700 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 5701 | default: |
| 5702 | return false; |
| 5703 | } |
| 5704 | } |
| 5705 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5706 | namespace { |
| 5707 | |
| 5708 | // Callback to only accept typo corrections that are namespaces. |
| 5709 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
| 5710 | public: |
| 5711 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 5712 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 5713 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 5714 | } |
| 5715 | return false; |
| 5716 | } |
| 5717 | }; |
| 5718 | |
| 5719 | } |
| 5720 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5721 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 5722 | CXXScopeSpec &SS, |
| 5723 | SourceLocation IdentLoc, |
| 5724 | IdentifierInfo *Ident) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5725 | NamespaceValidatorCCC Validator; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5726 | R.clear(); |
| 5727 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5728 | R.getLookupKind(), Sc, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 5729 | Validator)) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5730 | std::string CorrectedStr(Corrected.getAsString(S.getLangOptions())); |
| 5731 | std::string CorrectedQuotedStr(Corrected.getQuoted(S.getLangOptions())); |
| 5732 | if (DeclContext *DC = S.computeDeclContext(SS, false)) |
| 5733 | S.Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 5734 | << Ident << DC << CorrectedQuotedStr << SS.getRange() |
| 5735 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
| 5736 | else |
| 5737 | S.Diag(IdentLoc, diag::err_using_directive_suggest) |
| 5738 | << Ident << CorrectedQuotedStr |
| 5739 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5740 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5741 | S.Diag(Corrected.getCorrectionDecl()->getLocation(), |
| 5742 | diag::note_namespace_defined_here) << CorrectedQuotedStr; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5743 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5744 | Ident = Corrected.getCorrectionAsIdentifierInfo(); |
| 5745 | R.addDecl(Corrected.getCorrectionDecl()); |
| 5746 | return true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5747 | } |
| 5748 | return false; |
| 5749 | } |
| 5750 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5751 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5752 | SourceLocation UsingLoc, |
| 5753 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5754 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5755 | SourceLocation IdentLoc, |
| 5756 | IdentifierInfo *NamespcName, |
| 5757 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5758 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 5759 | assert(NamespcName && "Invalid NamespcName."); |
| 5760 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 5761 | |
| 5762 | // This can only happen along a recovery path. |
| 5763 | while (S->getFlags() & Scope::TemplateParamScope) |
| 5764 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5765 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5766 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5767 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5768 | NestedNameSpecifier *Qualifier = 0; |
| 5769 | if (SS.isSet()) |
| 5770 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5771 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 5772 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5773 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 5774 | LookupParsedName(R, S, &SS); |
| 5775 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5776 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5777 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5778 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5779 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5780 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 5781 | // "std" hasn't been defined yet, for GCC compatibility. |
| 5782 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 5783 | NamespcName->isStr("std")) { |
| 5784 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 5785 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5786 | R.resolveKind(); |
| 5787 | } |
| 5788 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5789 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5792 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5793 | NamedDecl *Named = R.getFoundDecl(); |
| 5794 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 5795 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5796 | // C++ [namespace.udir]p1: |
| 5797 | // A using-directive specifies that the names in the nominated |
| 5798 | // namespace can be used in the scope in which the |
| 5799 | // using-directive appears after the using-directive. During |
| 5800 | // unqualified name lookup (3.4.1), the names appear as if they |
| 5801 | // were declared in the nearest enclosing namespace which |
| 5802 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5803 | // namespace. [Note: in this context, "contains" means "contains |
| 5804 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5805 | |
| 5806 | // Find enclosing context containing both using-directive and |
| 5807 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5808 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5809 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 5810 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 5811 | CommonAncestor = CommonAncestor->getParent(); |
| 5812 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5813 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 5814 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5815 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 5816 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 5817 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5818 | !SourceMgr.isFromMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 5819 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 5820 | } |
| 5821 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5822 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5823 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 5824 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5825 | } |
| 5826 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5827 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5828 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 5832 | // If scope has associated entity, then using directive is at namespace |
| 5833 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 5834 | // it's lookup structure. |
| 5835 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5836 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5837 | else |
| 5838 | // Otherwise it is block-sope. using-directives will affect lookup |
| 5839 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5840 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5841 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5842 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5843 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5844 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 5845 | AccessSpecifier AS, |
| 5846 | bool HasUsingKeyword, |
| 5847 | SourceLocation UsingLoc, |
| 5848 | CXXScopeSpec &SS, |
| 5849 | UnqualifiedId &Name, |
| 5850 | AttributeList *AttrList, |
| 5851 | bool IsTypeName, |
| 5852 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5853 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5854 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5855 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 5856 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5857 | case UnqualifiedId::IK_Identifier: |
| 5858 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 5859 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5860 | case UnqualifiedId::IK_ConversionFunctionId: |
| 5861 | break; |
| 5862 | |
| 5863 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5864 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5865 | // C++0x inherited constructors. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5866 | Diag(Name.getLocStart(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5867 | getLangOptions().CPlusPlus0x ? |
| 5868 | diag::warn_cxx98_compat_using_decl_constructor : |
| 5869 | diag::err_using_decl_constructor) |
| 5870 | << SS.getRange(); |
| 5871 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5872 | if (getLangOptions().CPlusPlus0x) break; |
| 5873 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5874 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5875 | |
| 5876 | case UnqualifiedId::IK_DestructorName: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5877 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5878 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5879 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5880 | |
| 5881 | case UnqualifiedId::IK_TemplateId: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5882 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5883 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5884 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5885 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5886 | |
| 5887 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 5888 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5889 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5890 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5891 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5892 | // Warn about using declarations. |
| 5893 | // TODO: store that the declaration was written without 'using' and |
| 5894 | // talk about access decls instead of using decls in the |
| 5895 | // diagnostics. |
| 5896 | if (!HasUsingKeyword) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5897 | UsingLoc = Name.getLocStart(); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5898 | |
| 5899 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5900 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5901 | } |
| 5902 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 5903 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 5904 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 5905 | return 0; |
| 5906 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5907 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5908 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5909 | /* IsInstantiation */ false, |
| 5910 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5911 | if (UD) |
| 5912 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5913 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5914 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5917 | /// \brief Determine whether a using declaration considers the given |
| 5918 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 5919 | /// the same entity or are both typedefs of the same type. |
| 5920 | static bool |
| 5921 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 5922 | bool &SuppressRedeclaration) { |
| 5923 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 5924 | SuppressRedeclaration = false; |
| 5925 | return true; |
| 5926 | } |
| 5927 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5928 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 5929 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5930 | SuppressRedeclaration = true; |
| 5931 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 5932 | TD2->getUnderlyingType()); |
| 5933 | } |
| 5934 | |
| 5935 | return false; |
| 5936 | } |
| 5937 | |
| 5938 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5939 | /// Determines whether to create a using shadow decl for a particular |
| 5940 | /// decl, given the set of decls existing prior to this using lookup. |
| 5941 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 5942 | const LookupResult &Previous) { |
| 5943 | // Diagnose finding a decl which is not from a base class of the |
| 5944 | // current class. We do this now because there are cases where this |
| 5945 | // function will silently decide not to build a shadow decl, which |
| 5946 | // will pre-empt further diagnostics. |
| 5947 | // |
| 5948 | // We don't need to do this in C++0x because we do the check once on |
| 5949 | // the qualifier. |
| 5950 | // |
| 5951 | // FIXME: diagnose the following if we care enough: |
| 5952 | // struct A { int foo; }; |
| 5953 | // struct B : A { using A::foo; }; |
| 5954 | // template <class T> struct C : A {}; |
| 5955 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 5956 | // This is invalid (during instantiation) in C++03 because B::foo |
| 5957 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 5958 | // We can't diagnose it immediately because C<T> is an unknown |
| 5959 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 5960 | // to A::foo, which will look well-formed when we instantiate. |
| 5961 | // The right solution is to not collapse the shadow-decl chain. |
| 5962 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 5963 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 5964 | |
| 5965 | // Handle enums and anonymous structs. |
| 5966 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 5967 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 5968 | while (OrigRec->isAnonymousStructOrUnion()) |
| 5969 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 5970 | |
| 5971 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 5972 | if (OrigDC == CurContext) { |
| 5973 | Diag(Using->getLocation(), |
| 5974 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5975 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5976 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5977 | return true; |
| 5978 | } |
| 5979 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5980 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5981 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5982 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5983 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5984 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5985 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5986 | return true; |
| 5987 | } |
| 5988 | } |
| 5989 | |
| 5990 | if (Previous.empty()) return false; |
| 5991 | |
| 5992 | NamedDecl *Target = Orig; |
| 5993 | if (isa<UsingShadowDecl>(Target)) |
| 5994 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 5995 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 5996 | // If the target happens to be one of the previous declarations, we |
| 5997 | // don't have a conflict. |
| 5998 | // |
| 5999 | // FIXME: but we might be increasing its access, in which case we |
| 6000 | // should redeclare it. |
| 6001 | NamedDecl *NonTag = 0, *Tag = 0; |
| 6002 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6003 | I != E; ++I) { |
| 6004 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6005 | bool Result; |
| 6006 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 6007 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6008 | |
| 6009 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 6010 | } |
| 6011 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6012 | if (Target->isFunctionOrFunctionTemplate()) { |
| 6013 | FunctionDecl *FD; |
| 6014 | if (isa<FunctionTemplateDecl>(Target)) |
| 6015 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 6016 | else |
| 6017 | FD = cast<FunctionDecl>(Target); |
| 6018 | |
| 6019 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 6020 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6021 | case Ovl_Overload: |
| 6022 | return false; |
| 6023 | |
| 6024 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6025 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6026 | break; |
| 6027 | |
| 6028 | // We found a decl with the exact signature. |
| 6029 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6030 | // If we're in a record, we want to hide the target, so we |
| 6031 | // return true (without a diagnostic) to tell the caller not to |
| 6032 | // build a shadow decl. |
| 6033 | if (CurContext->isRecord()) |
| 6034 | return true; |
| 6035 | |
| 6036 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6037 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6038 | break; |
| 6039 | } |
| 6040 | |
| 6041 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6042 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 6043 | return true; |
| 6044 | } |
| 6045 | |
| 6046 | // Target is not a function. |
| 6047 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6048 | if (isa<TagDecl>(Target)) { |
| 6049 | // No conflict between a tag and a non-tag. |
| 6050 | if (!Tag) return false; |
| 6051 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6052 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6053 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6054 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 6055 | return true; |
| 6056 | } |
| 6057 | |
| 6058 | // No conflict between a tag and a non-tag. |
| 6059 | if (!NonTag) return false; |
| 6060 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6061 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6062 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6063 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 6064 | return true; |
| 6065 | } |
| 6066 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6067 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6068 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6069 | UsingDecl *UD, |
| 6070 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6071 | |
| 6072 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6073 | NamedDecl *Target = Orig; |
| 6074 | if (isa<UsingShadowDecl>(Target)) { |
| 6075 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6076 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6077 | } |
| 6078 | |
| 6079 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6080 | = UsingShadowDecl::Create(Context, CurContext, |
| 6081 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6082 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6083 | |
| 6084 | Shadow->setAccess(UD->getAccess()); |
| 6085 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 6086 | Shadow->setInvalidDecl(); |
| 6087 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6088 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6089 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6090 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6091 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6092 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6093 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6094 | return Shadow; |
| 6095 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6096 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6097 | /// Hides a using shadow declaration. This is required by the current |
| 6098 | /// using-decl implementation when a resolvable using declaration in a |
| 6099 | /// class is followed by a declaration which would hide or override |
| 6100 | /// one or more of the using decl's targets; for example: |
| 6101 | /// |
| 6102 | /// struct Base { void foo(int); }; |
| 6103 | /// struct Derived : Base { |
| 6104 | /// using Base::foo; |
| 6105 | /// void foo(int); |
| 6106 | /// }; |
| 6107 | /// |
| 6108 | /// The governing language is C++03 [namespace.udecl]p12: |
| 6109 | /// |
| 6110 | /// When a using-declaration brings names from a base class into a |
| 6111 | /// derived class scope, member functions in the derived class |
| 6112 | /// override and/or hide member functions with the same name and |
| 6113 | /// parameter types in a base class (rather than conflicting). |
| 6114 | /// |
| 6115 | /// There are two ways to implement this: |
| 6116 | /// (1) optimistically create shadow decls when they're not hidden |
| 6117 | /// by existing declarations, or |
| 6118 | /// (2) don't create any shadow decls (or at least don't make them |
| 6119 | /// visible) until we've fully parsed/instantiated the class. |
| 6120 | /// The problem with (1) is that we might have to retroactively remove |
| 6121 | /// a shadow decl, which requires several O(n) operations because the |
| 6122 | /// decl structures are (very reasonably) not designed for removal. |
| 6123 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 6124 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6125 | if (Shadow->getDeclName().getNameKind() == |
| 6126 | DeclarationName::CXXConversionFunctionName) |
| 6127 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 6128 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6129 | // Remove it from the DeclContext... |
| 6130 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6131 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6132 | // ...and the scope, if applicable... |
| 6133 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6134 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6135 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6138 | // ...and the using decl. |
| 6139 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 6140 | |
| 6141 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6142 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6145 | /// Builds a using declaration. |
| 6146 | /// |
| 6147 | /// \param IsInstantiation - Whether this call arises from an |
| 6148 | /// instantiation of an unresolved using declaration. We treat |
| 6149 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6150 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 6151 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6152 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6153 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6154 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6155 | bool IsInstantiation, |
| 6156 | bool IsTypeName, |
| 6157 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6158 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6159 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6160 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 6161 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 6162 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6163 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6164 | if (SS.isEmpty()) { |
| 6165 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6166 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6168 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6169 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6170 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6171 | ForRedeclaration); |
| 6172 | Previous.setHideTags(false); |
| 6173 | if (S) { |
| 6174 | LookupName(Previous, S); |
| 6175 | |
| 6176 | // It is really dumb that we have to do this. |
| 6177 | LookupResult::Filter F = Previous.makeFilter(); |
| 6178 | while (F.hasNext()) { |
| 6179 | NamedDecl *D = F.next(); |
| 6180 | if (!isDeclInScope(D, CurContext, S)) |
| 6181 | F.erase(); |
| 6182 | } |
| 6183 | F.done(); |
| 6184 | } else { |
| 6185 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 6186 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 6187 | LookupQualifiedName(Previous, CurContext); |
| 6188 | } |
| 6189 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6190 | // Check for invalid redeclarations. |
| 6191 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 6192 | return 0; |
| 6193 | |
| 6194 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6195 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 6196 | return 0; |
| 6197 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 6198 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6199 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6200 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 6201 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6202 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6203 | // FIXME: not all declaration name kinds are legal here |
| 6204 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 6205 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6206 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6207 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6208 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6209 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 6210 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6211 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6212 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6213 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 6214 | NameInfo, IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 6215 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6216 | D->setAccess(AS); |
| 6217 | CurContext->addDecl(D); |
| 6218 | |
| 6219 | if (!LookupContext) return D; |
| 6220 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6221 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6222 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6223 | UD->setInvalidDecl(); |
| 6224 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6227 | // Constructor inheriting using decls get special treatment. |
| 6228 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 6229 | if (CheckInheritedConstructorUsingDecl(UD)) |
| 6230 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6231 | return UD; |
| 6232 | } |
| 6233 | |
| 6234 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6235 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6236 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6237 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6238 | // Unlike most lookups, we don't always want to hide tag |
| 6239 | // declarations: tag names are visible through the using declaration |
| 6240 | // even if hidden by ordinary names, *except* in a dependent context |
| 6241 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6242 | if (!IsInstantiation) |
| 6243 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6244 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6245 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6246 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6247 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6248 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6249 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6250 | UD->setInvalidDecl(); |
| 6251 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6254 | if (R.isAmbiguous()) { |
| 6255 | UD->setInvalidDecl(); |
| 6256 | return UD; |
| 6257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6258 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6259 | if (IsTypeName) { |
| 6260 | // 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] | 6261 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6262 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 6263 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 6264 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 6265 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6266 | UD->setInvalidDecl(); |
| 6267 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6268 | } |
| 6269 | } else { |
| 6270 | // If we asked for a non-typename and we got a type, error out, |
| 6271 | // but only if this is an instantiation of an unresolved using |
| 6272 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6273 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6274 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 6275 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6276 | UD->setInvalidDecl(); |
| 6277 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6278 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6281 | // C++0x N2914 [namespace.udecl]p6: |
| 6282 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6283 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6284 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 6285 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6286 | UD->setInvalidDecl(); |
| 6287 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6289 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6290 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 6291 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 6292 | BuildUsingShadowDecl(S, UD, *I); |
| 6293 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6294 | |
| 6295 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6296 | } |
| 6297 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6298 | /// Additional checks for a using declaration referring to a constructor name. |
| 6299 | bool Sema::CheckInheritedConstructorUsingDecl(UsingDecl *UD) { |
| 6300 | if (UD->isTypeName()) { |
| 6301 | // FIXME: Cannot specify typename when specifying constructor |
| 6302 | return true; |
| 6303 | } |
| 6304 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6305 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6306 | assert(SourceType && |
| 6307 | "Using decl naming constructor doesn't have type in scope spec."); |
| 6308 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 6309 | |
| 6310 | // Check whether the named type is a direct base class. |
| 6311 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 6312 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 6313 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 6314 | BaseIt != BaseE; ++BaseIt) { |
| 6315 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 6316 | if (CanonicalSourceType == BaseType) |
| 6317 | break; |
| 6318 | } |
| 6319 | |
| 6320 | if (BaseIt == BaseE) { |
| 6321 | // Did not find SourceType in the bases. |
| 6322 | Diag(UD->getUsingLocation(), |
| 6323 | diag::err_using_decl_constructor_not_in_direct_base) |
| 6324 | << UD->getNameInfo().getSourceRange() |
| 6325 | << QualType(SourceType, 0) << TargetClass; |
| 6326 | return true; |
| 6327 | } |
| 6328 | |
| 6329 | BaseIt->setInheritConstructors(); |
| 6330 | |
| 6331 | return false; |
| 6332 | } |
| 6333 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6334 | /// Checks that the given using declaration is not an invalid |
| 6335 | /// redeclaration. Note that this is checking only for the using decl |
| 6336 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 6337 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 6338 | bool isTypeName, |
| 6339 | const CXXScopeSpec &SS, |
| 6340 | SourceLocation NameLoc, |
| 6341 | const LookupResult &Prev) { |
| 6342 | // C++03 [namespace.udecl]p8: |
| 6343 | // C++0x [namespace.udecl]p10: |
| 6344 | // A using-declaration is a declaration and can therefore be used |
| 6345 | // repeatedly where (and only where) multiple declarations are |
| 6346 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 6347 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 6348 | // That's in non-member contexts. |
| 6349 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6350 | return false; |
| 6351 | |
| 6352 | NestedNameSpecifier *Qual |
| 6353 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 6354 | |
| 6355 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 6356 | NamedDecl *D = *I; |
| 6357 | |
| 6358 | bool DTypename; |
| 6359 | NestedNameSpecifier *DQual; |
| 6360 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 6361 | DTypename = UD->isTypeName(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6362 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6363 | } else if (UnresolvedUsingValueDecl *UD |
| 6364 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 6365 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6366 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6367 | } else if (UnresolvedUsingTypenameDecl *UD |
| 6368 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 6369 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6370 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6371 | } else continue; |
| 6372 | |
| 6373 | // using decls differ if one says 'typename' and the other doesn't. |
| 6374 | // FIXME: non-dependent using decls? |
| 6375 | if (isTypeName != DTypename) continue; |
| 6376 | |
| 6377 | // using decls differ if they name different scopes (but note that |
| 6378 | // template instantiation can cause this check to trigger when it |
| 6379 | // didn't before instantiation). |
| 6380 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 6381 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 6382 | continue; |
| 6383 | |
| 6384 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6385 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6386 | return true; |
| 6387 | } |
| 6388 | |
| 6389 | return false; |
| 6390 | } |
| 6391 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6392 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6393 | /// Checks that the given nested-name qualifier used in a using decl |
| 6394 | /// in the current context is appropriately related to the current |
| 6395 | /// scope. If an error is found, diagnoses it and returns true. |
| 6396 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 6397 | const CXXScopeSpec &SS, |
| 6398 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6399 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6400 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6401 | if (!CurContext->isRecord()) { |
| 6402 | // C++03 [namespace.udecl]p3: |
| 6403 | // C++0x [namespace.udecl]p8: |
| 6404 | // A using-declaration for a class member shall be a member-declaration. |
| 6405 | |
| 6406 | // If we weren't able to compute a valid scope, it must be a |
| 6407 | // dependent class scope. |
| 6408 | if (!NamedContext || NamedContext->isRecord()) { |
| 6409 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 6410 | << SS.getRange(); |
| 6411 | return true; |
| 6412 | } |
| 6413 | |
| 6414 | // Otherwise, everything is known to be fine. |
| 6415 | return false; |
| 6416 | } |
| 6417 | |
| 6418 | // The current scope is a record. |
| 6419 | |
| 6420 | // If the named context is dependent, we can't decide much. |
| 6421 | if (!NamedContext) { |
| 6422 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 6423 | // nested-name-specifier does not refer to a base class, which is |
| 6424 | // still possible in some cases. |
| 6425 | |
| 6426 | // Otherwise we have to conservatively report that things might be |
| 6427 | // okay. |
| 6428 | return false; |
| 6429 | } |
| 6430 | |
| 6431 | if (!NamedContext->isRecord()) { |
| 6432 | // Ideally this would point at the last name in the specifier, |
| 6433 | // but we don't have that level of source info. |
| 6434 | Diag(SS.getRange().getBegin(), |
| 6435 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 6436 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 6437 | return true; |
| 6438 | } |
| 6439 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 6440 | if (!NamedContext->isDependentContext() && |
| 6441 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 6442 | return true; |
| 6443 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6444 | if (getLangOptions().CPlusPlus0x) { |
| 6445 | // C++0x [namespace.udecl]p3: |
| 6446 | // In a using-declaration used as a member-declaration, the |
| 6447 | // nested-name-specifier shall name a base class of the class |
| 6448 | // being defined. |
| 6449 | |
| 6450 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 6451 | cast<CXXRecordDecl>(NamedContext))) { |
| 6452 | if (CurContext == NamedContext) { |
| 6453 | Diag(NameLoc, |
| 6454 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 6455 | << SS.getRange(); |
| 6456 | return true; |
| 6457 | } |
| 6458 | |
| 6459 | Diag(SS.getRange().getBegin(), |
| 6460 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 6461 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 6462 | << cast<CXXRecordDecl>(CurContext) |
| 6463 | << SS.getRange(); |
| 6464 | return true; |
| 6465 | } |
| 6466 | |
| 6467 | return false; |
| 6468 | } |
| 6469 | |
| 6470 | // C++03 [namespace.udecl]p4: |
| 6471 | // A using-declaration used as a member-declaration shall refer |
| 6472 | // to a member of a base class of the class being defined [etc.]. |
| 6473 | |
| 6474 | // Salient point: SS doesn't have to name a base class as long as |
| 6475 | // lookup only finds members from base classes. Therefore we can |
| 6476 | // diagnose here only if we can prove that that can't happen, |
| 6477 | // i.e. if the class hierarchies provably don't intersect. |
| 6478 | |
| 6479 | // TODO: it would be nice if "definitely valid" results were cached |
| 6480 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 6481 | // need to be repeated. |
| 6482 | |
| 6483 | struct UserData { |
Benjamin Kramer | 8c43dcc | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 6484 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6485 | |
| 6486 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 6487 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 6488 | Data->Bases.insert(Base); |
| 6489 | return true; |
| 6490 | } |
| 6491 | |
| 6492 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 6493 | return !Class->forallBases(collect, this); |
| 6494 | } |
| 6495 | |
| 6496 | /// Returns true if the base is dependent or is one of the |
| 6497 | /// accumulated base classes. |
| 6498 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 6499 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 6500 | return !Data->Bases.count(Base); |
| 6501 | } |
| 6502 | |
| 6503 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 6504 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 6505 | } |
| 6506 | }; |
| 6507 | |
| 6508 | UserData Data; |
| 6509 | |
| 6510 | // Returns false if we find a dependent base. |
| 6511 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 6512 | return false; |
| 6513 | |
| 6514 | // Returns false if the class has a dependent base or if it or one |
| 6515 | // of its bases is present in the base set of the current context. |
| 6516 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 6517 | return false; |
| 6518 | |
| 6519 | Diag(SS.getRange().getBegin(), |
| 6520 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 6521 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 6522 | << cast<CXXRecordDecl>(CurContext) |
| 6523 | << SS.getRange(); |
| 6524 | |
| 6525 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6528 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 6529 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6530 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6531 | SourceLocation UsingLoc, |
| 6532 | UnqualifiedId &Name, |
| 6533 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6534 | // Skip up to the relevant declaration scope. |
| 6535 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6536 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6537 | assert((S->getFlags() & Scope::DeclScope) && |
| 6538 | "got alias-declaration outside of declaration scope"); |
| 6539 | |
| 6540 | if (Type.isInvalid()) |
| 6541 | return 0; |
| 6542 | |
| 6543 | bool Invalid = false; |
| 6544 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 6545 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 6546 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6547 | |
| 6548 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 6549 | return 0; |
| 6550 | |
| 6551 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6552 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6553 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6554 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 6555 | TInfo->getTypeLoc().getBeginLoc()); |
| 6556 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6557 | |
| 6558 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 6559 | LookupName(Previous, S); |
| 6560 | |
| 6561 | // Warn about shadowing the name of a template parameter. |
| 6562 | if (Previous.isSingleResult() && |
| 6563 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 6564 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6565 | Previous.clear(); |
| 6566 | } |
| 6567 | |
| 6568 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 6569 | "name in alias declaration must be an identifier"); |
| 6570 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 6571 | Name.StartLocation, |
| 6572 | Name.Identifier, TInfo); |
| 6573 | |
| 6574 | NewTD->setAccess(AS); |
| 6575 | |
| 6576 | if (Invalid) |
| 6577 | NewTD->setInvalidDecl(); |
| 6578 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6579 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 6580 | Invalid |= NewTD->isInvalidDecl(); |
| 6581 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6582 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6583 | |
| 6584 | NamedDecl *NewND; |
| 6585 | if (TemplateParamLists.size()) { |
| 6586 | TypeAliasTemplateDecl *OldDecl = 0; |
| 6587 | TemplateParameterList *OldTemplateParams = 0; |
| 6588 | |
| 6589 | if (TemplateParamLists.size() != 1) { |
| 6590 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
| 6591 | << SourceRange(TemplateParamLists.get()[1]->getTemplateLoc(), |
| 6592 | TemplateParamLists.get()[TemplateParamLists.size()-1]->getRAngleLoc()); |
| 6593 | } |
| 6594 | TemplateParameterList *TemplateParams = TemplateParamLists.get()[0]; |
| 6595 | |
| 6596 | // Only consider previous declarations in the same scope. |
| 6597 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 6598 | /*ExplicitInstantiationOrSpecialization*/false); |
| 6599 | if (!Previous.empty()) { |
| 6600 | Redeclaration = true; |
| 6601 | |
| 6602 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 6603 | if (!OldDecl && !Invalid) { |
| 6604 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 6605 | << Name.Identifier; |
| 6606 | |
| 6607 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 6608 | if (OldD->getLocation().isValid()) |
| 6609 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 6610 | |
| 6611 | Invalid = true; |
| 6612 | } |
| 6613 | |
| 6614 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 6615 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 6616 | OldDecl->getTemplateParameters(), |
| 6617 | /*Complain=*/true, |
| 6618 | TPL_TemplateMatch)) |
| 6619 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 6620 | else |
| 6621 | Invalid = true; |
| 6622 | |
| 6623 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 6624 | if (!Invalid && |
| 6625 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 6626 | NewTD->getUnderlyingType())) { |
| 6627 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 6628 | // but we can't reasonably accept it. |
| 6629 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 6630 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 6631 | if (OldTD->getLocation().isValid()) |
| 6632 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 6633 | Invalid = true; |
| 6634 | } |
| 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | // Merge any previous default template arguments into our parameters, |
| 6639 | // and check the parameter list. |
| 6640 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 6641 | TPC_TypeAliasTemplate)) |
| 6642 | return 0; |
| 6643 | |
| 6644 | TypeAliasTemplateDecl *NewDecl = |
| 6645 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 6646 | Name.Identifier, TemplateParams, |
| 6647 | NewTD); |
| 6648 | |
| 6649 | NewDecl->setAccess(AS); |
| 6650 | |
| 6651 | if (Invalid) |
| 6652 | NewDecl->setInvalidDecl(); |
| 6653 | else if (OldDecl) |
| 6654 | NewDecl->setPreviousDeclaration(OldDecl); |
| 6655 | |
| 6656 | NewND = NewDecl; |
| 6657 | } else { |
| 6658 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 6659 | NewND = NewTD; |
| 6660 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6661 | |
| 6662 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6663 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6664 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6665 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6668 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 6669 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6670 | SourceLocation AliasLoc, |
| 6671 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6672 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 6673 | SourceLocation IdentLoc, |
| 6674 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6675 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6676 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6677 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 6678 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6679 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6680 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 6681 | NamedDecl *PrevDecl |
| 6682 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 6683 | ForRedeclaration); |
| 6684 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 6685 | PrevDecl = 0; |
| 6686 | |
| 6687 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6688 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6689 | // 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] | 6690 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 6691 | // FIXME: At some point, we'll want to create the (redundant) |
| 6692 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6693 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 6694 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6695 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6696 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6697 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6698 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 6699 | diag::err_redefinition_different_kind; |
| 6700 | Diag(AliasLoc, DiagID) << Alias; |
| 6701 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6702 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6703 | } |
| 6704 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6705 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6706 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6707 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6708 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6709 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 6710 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6711 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 6712 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 6713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6714 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6715 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6716 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 6717 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6718 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6719 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 6720 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6721 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 6722 | } |
| 6723 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6724 | namespace { |
| 6725 | /// \brief Scoped object used to handle the state changes required in Sema |
| 6726 | /// to implicitly define the body of a C++ member function; |
| 6727 | class ImplicitlyDefinedFunctionScope { |
| 6728 | Sema &S; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6729 | Sema::ContextRAII SavedContext; |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6730 | |
| 6731 | public: |
| 6732 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6733 | : S(S), SavedContext(S, Method) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6734 | { |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6735 | S.PushFunctionScope(); |
| 6736 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 6737 | } |
| 6738 | |
| 6739 | ~ImplicitlyDefinedFunctionScope() { |
| 6740 | S.PopExpressionEvaluationContext(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 6741 | S.PopFunctionScopeInfo(); |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6742 | } |
| 6743 | }; |
| 6744 | } |
| 6745 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 6746 | Sema::ImplicitExceptionSpecification |
| 6747 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6748 | // C++ [except.spec]p14: |
| 6749 | // An implicitly declared special member function (Clause 12) shall have an |
| 6750 | // exception-specification. [...] |
| 6751 | ImplicitExceptionSpecification ExceptSpec(Context); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 6752 | if (ClassDecl->isInvalidDecl()) |
| 6753 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6754 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6755 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6756 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 6757 | BEnd = ClassDecl->bases_end(); |
| 6758 | B != BEnd; ++B) { |
| 6759 | if (B->isVirtual()) // Handled below. |
| 6760 | continue; |
| 6761 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6762 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 6763 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6764 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 6765 | // If this is a deleted function, add it anyway. This might be conformant |
| 6766 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6767 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6768 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6769 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6770 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6771 | |
| 6772 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6773 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 6774 | BEnd = ClassDecl->vbases_end(); |
| 6775 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6776 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 6777 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6778 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 6779 | // If this is a deleted function, add it anyway. This might be conformant |
| 6780 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6781 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6782 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6783 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6784 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6785 | |
| 6786 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6787 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 6788 | FEnd = ClassDecl->field_end(); |
| 6789 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6790 | if (F->hasInClassInitializer()) { |
| 6791 | if (Expr *E = F->getInClassInitializer()) |
| 6792 | ExceptSpec.CalledExpr(E); |
| 6793 | else if (!F->isInvalidDecl()) |
| 6794 | ExceptSpec.SetDelayed(); |
| 6795 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6796 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6797 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 6798 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 6799 | // If this is a deleted function, add it anyway. This might be conformant |
| 6800 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6801 | // In particular, the problem is that this function never gets called. It |
| 6802 | // might just be ill-formed because this function attempts to refer to |
| 6803 | // a deleted function here. |
| 6804 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6805 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6806 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6807 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6808 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 6809 | return ExceptSpec; |
| 6810 | } |
| 6811 | |
| 6812 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 6813 | CXXRecordDecl *ClassDecl) { |
| 6814 | // C++ [class.ctor]p5: |
| 6815 | // A default constructor for a class X is a constructor of class X |
| 6816 | // that can be called without an argument. If there is no |
| 6817 | // user-declared constructor for class X, a default constructor is |
| 6818 | // implicitly declared. An implicitly-declared default constructor |
| 6819 | // is an inline public member of its class. |
| 6820 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 6821 | "Should not build implicit default constructor!"); |
| 6822 | |
| 6823 | ImplicitExceptionSpecification Spec = |
| 6824 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 6825 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 6826 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6827 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6828 | CanQualType ClassType |
| 6829 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6830 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6831 | DeclarationName Name |
| 6832 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6833 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 6834 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
| 6835 | Context, ClassDecl, ClassLoc, NameInfo, |
| 6836 | Context.getFunctionType(Context.VoidTy, 0, 0, EPI), /*TInfo=*/0, |
| 6837 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
| 6838 | /*isConstexpr=*/ClassDecl->defaultedDefaultConstructorIsConstexpr() && |
| 6839 | getLangOptions().CPlusPlus0x); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6840 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 6841 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6842 | DefaultCon->setImplicit(); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6843 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6844 | |
| 6845 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6846 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 6847 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6848 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6849 | PushOnScopeChains(DefaultCon, S, false); |
| 6850 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 6851 | |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 6852 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 6853 | DefaultCon->setDeletedAsWritten(); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6854 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6855 | return DefaultCon; |
| 6856 | } |
| 6857 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6858 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 6859 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 6860 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6861 | !Constructor->doesThisDeclarationHaveABody() && |
| 6862 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6863 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6864 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 6865 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6866 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 6867 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6868 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6869 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 6870 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6871 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6872 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 6873 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6874 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6875 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6876 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6877 | |
| 6878 | SourceLocation Loc = Constructor->getLocation(); |
| 6879 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 6880 | |
| 6881 | Constructor->setUsed(); |
| 6882 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 6883 | |
| 6884 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 6885 | L->CompletedImplicitDefinition(Constructor); |
| 6886 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6887 | } |
| 6888 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6889 | /// Get any existing defaulted default constructor for the given class. Do not |
| 6890 | /// implicitly define one if it does not exist. |
| 6891 | static CXXConstructorDecl *getDefaultedDefaultConstructorUnsafe(Sema &Self, |
| 6892 | CXXRecordDecl *D) { |
| 6893 | ASTContext &Context = Self.Context; |
| 6894 | QualType ClassType = Context.getTypeDeclType(D); |
| 6895 | DeclarationName ConstructorName |
| 6896 | = Context.DeclarationNames.getCXXConstructorName( |
| 6897 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 6898 | |
| 6899 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 6900 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 6901 | Con != ConEnd; ++Con) { |
| 6902 | // A function template cannot be defaulted. |
| 6903 | if (isa<FunctionTemplateDecl>(*Con)) |
| 6904 | continue; |
| 6905 | |
| 6906 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 6907 | if (Constructor->isDefaultConstructor()) |
| 6908 | return Constructor->isDefaulted() ? Constructor : 0; |
| 6909 | } |
| 6910 | return 0; |
| 6911 | } |
| 6912 | |
| 6913 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
| 6914 | if (!D) return; |
| 6915 | AdjustDeclIfTemplate(D); |
| 6916 | |
| 6917 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D); |
| 6918 | CXXConstructorDecl *CtorDecl |
| 6919 | = getDefaultedDefaultConstructorUnsafe(*this, ClassDecl); |
| 6920 | |
| 6921 | if (!CtorDecl) return; |
| 6922 | |
| 6923 | // Compute the exception specification for the default constructor. |
| 6924 | const FunctionProtoType *CtorTy = |
| 6925 | CtorDecl->getType()->castAs<FunctionProtoType>(); |
| 6926 | if (CtorTy->getExceptionSpecType() == EST_Delayed) { |
| 6927 | ImplicitExceptionSpecification Spec = |
| 6928 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 6929 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 6930 | assert(EPI.ExceptionSpecType != EST_Delayed); |
| 6931 | |
| 6932 | CtorDecl->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 6933 | } |
| 6934 | |
| 6935 | // If the default constructor is explicitly defaulted, checking the exception |
| 6936 | // specification is deferred until now. |
| 6937 | if (!CtorDecl->isInvalidDecl() && CtorDecl->isExplicitlyDefaulted() && |
| 6938 | !ClassDecl->isDependentType()) |
| 6939 | CheckExplicitlyDefaultedDefaultConstructor(CtorDecl); |
| 6940 | } |
| 6941 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6942 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 6943 | // We start with an initial pass over the base classes to collect those that |
| 6944 | // inherit constructors from. If there are none, we can forgo all further |
| 6945 | // processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6946 | typedef SmallVector<const RecordType *, 4> BasesVector; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6947 | BasesVector BasesToInheritFrom; |
| 6948 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 6949 | BaseE = ClassDecl->bases_end(); |
| 6950 | BaseIt != BaseE; ++BaseIt) { |
| 6951 | if (BaseIt->getInheritConstructors()) { |
| 6952 | QualType Base = BaseIt->getType(); |
| 6953 | if (Base->isDependentType()) { |
| 6954 | // If we inherit constructors from anything that is dependent, just |
| 6955 | // abort processing altogether. We'll get another chance for the |
| 6956 | // instantiations. |
| 6957 | return; |
| 6958 | } |
| 6959 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 6960 | } |
| 6961 | } |
| 6962 | if (BasesToInheritFrom.empty()) |
| 6963 | return; |
| 6964 | |
| 6965 | // Now collect the constructors that we already have in the current class. |
| 6966 | // Those take precedence over inherited constructors. |
| 6967 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 6968 | // unless there is a user-declared constructor with the same signature in |
| 6969 | // the class where the using-declaration appears. |
| 6970 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 6971 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 6972 | CtorE = ClassDecl->ctor_end(); |
| 6973 | CtorIt != CtorE; ++CtorIt) { |
| 6974 | ExistingConstructors.insert( |
| 6975 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 6976 | } |
| 6977 | |
| 6978 | Scope *S = getScopeForContext(ClassDecl); |
| 6979 | DeclarationName CreatedCtorName = |
| 6980 | Context.DeclarationNames.getCXXConstructorName( |
| 6981 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 6982 | |
| 6983 | // Now comes the true work. |
| 6984 | // First, we keep a map from constructor types to the base that introduced |
| 6985 | // them. Needed for finding conflicting constructors. We also keep the |
| 6986 | // actually inserted declarations in there, for pretty diagnostics. |
| 6987 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 6988 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 6989 | ConstructorToSourceMap InheritedConstructors; |
| 6990 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 6991 | BaseE = BasesToInheritFrom.end(); |
| 6992 | BaseIt != BaseE; ++BaseIt) { |
| 6993 | const RecordType *Base = *BaseIt; |
| 6994 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 6995 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 6996 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 6997 | CtorE = BaseDecl->ctor_end(); |
| 6998 | CtorIt != CtorE; ++CtorIt) { |
| 6999 | // Find the using declaration for inheriting this base's constructors. |
| 7000 | DeclarationName Name = |
| 7001 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
| 7002 | UsingDecl *UD = dyn_cast_or_null<UsingDecl>( |
| 7003 | LookupSingleName(S, Name,SourceLocation(), LookupUsingDeclName)); |
| 7004 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 7005 | ClassDecl->getLocation(); |
| 7006 | |
| 7007 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 7008 | // from the class X named in the using-declaration consists of actual |
| 7009 | // constructors and notional constructors that result from the |
| 7010 | // transformation of defaulted parameters as follows: |
| 7011 | // - all non-template default constructors of X, and |
| 7012 | // - for each non-template constructor of X that has at least one |
| 7013 | // parameter with a default argument, the set of constructors that |
| 7014 | // results from omitting any ellipsis parameter specification and |
| 7015 | // successively omitting parameters with a default argument from the |
| 7016 | // end of the parameter-type-list. |
| 7017 | CXXConstructorDecl *BaseCtor = *CtorIt; |
| 7018 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 7019 | const FunctionProtoType *BaseCtorType = |
| 7020 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 7021 | |
| 7022 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 7023 | maxParams = BaseCtor->getNumParams(); |
| 7024 | params <= maxParams; ++params) { |
| 7025 | // Skip default constructors. They're never inherited. |
| 7026 | if (params == 0) |
| 7027 | continue; |
| 7028 | // Skip copy and move constructors for the same reason. |
| 7029 | if (CanBeCopyOrMove && params == 1) |
| 7030 | continue; |
| 7031 | |
| 7032 | // Build up a function type for this particular constructor. |
| 7033 | // FIXME: The working paper does not consider that the exception spec |
| 7034 | // for the inheriting constructor might be larger than that of the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7035 | // source. This code doesn't yet, either. When it does, this code will |
| 7036 | // need to be delayed until after exception specifications and in-class |
| 7037 | // member initializers are attached. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7038 | const Type *NewCtorType; |
| 7039 | if (params == maxParams) |
| 7040 | NewCtorType = BaseCtorType; |
| 7041 | else { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7042 | SmallVector<QualType, 16> Args; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7043 | for (unsigned i = 0; i < params; ++i) { |
| 7044 | Args.push_back(BaseCtorType->getArgType(i)); |
| 7045 | } |
| 7046 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 7047 | BaseCtorType->getExtProtoInfo(); |
| 7048 | ExtInfo.Variadic = false; |
| 7049 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 7050 | Args.data(), params, ExtInfo) |
| 7051 | .getTypePtr(); |
| 7052 | } |
| 7053 | const Type *CanonicalNewCtorType = |
| 7054 | Context.getCanonicalType(NewCtorType); |
| 7055 | |
| 7056 | // Now that we have the type, first check if the class already has a |
| 7057 | // constructor with this signature. |
| 7058 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 7059 | continue; |
| 7060 | |
| 7061 | // Then we check if we have already declared an inherited constructor |
| 7062 | // with this signature. |
| 7063 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 7064 | InheritedConstructors.insert(std::make_pair( |
| 7065 | CanonicalNewCtorType, |
| 7066 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 7067 | if (!result.second) { |
| 7068 | // Already in the map. If it came from a different class, that's an |
| 7069 | // error. Not if it's from the same. |
| 7070 | CanQualType PreviousBase = result.first->second.first; |
| 7071 | if (CanonicalBase != PreviousBase) { |
| 7072 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 7073 | const CXXConstructorDecl *PrevBaseCtor = |
| 7074 | PrevCtor->getInheritedConstructor(); |
| 7075 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 7076 | |
| 7077 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 7078 | Diag(BaseCtor->getLocation(), |
| 7079 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 7080 | Diag(PrevBaseCtor->getLocation(), |
| 7081 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 7082 | Diag(PrevCtor->getLocation(), |
| 7083 | diag::note_using_decl_constructor_conflict_previous_using); |
| 7084 | } |
| 7085 | continue; |
| 7086 | } |
| 7087 | |
| 7088 | // OK, we're there, now add the constructor. |
| 7089 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7090 | // user-written inline constructor [...] |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7091 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 7092 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7093 | Context, ClassDecl, UsingLoc, DNI, QualType(NewCtorType, 0), |
| 7094 | /*TInfo=*/0, BaseCtor->isExplicit(), /*Inline=*/true, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7095 | /*ImplicitlyDeclared=*/true, |
| 7096 | // FIXME: Due to a defect in the standard, we treat inherited |
| 7097 | // constructors as constexpr even if that makes them ill-formed. |
| 7098 | /*Constexpr=*/BaseCtor->isConstexpr()); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7099 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 7100 | |
| 7101 | // Build up the parameter decls and add them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7102 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7103 | for (unsigned i = 0; i < params; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7104 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, |
| 7105 | UsingLoc, UsingLoc, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7106 | /*IdentifierInfo=*/0, |
| 7107 | BaseCtorType->getArgType(i), |
| 7108 | /*TInfo=*/0, SC_None, |
| 7109 | SC_None, /*DefaultArg=*/0)); |
| 7110 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7111 | NewCtor->setParams(ParamDecls); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7112 | NewCtor->setInheritedConstructor(BaseCtor); |
| 7113 | |
| 7114 | PushOnScopeChains(NewCtor, S, false); |
| 7115 | ClassDecl->addDecl(NewCtor); |
| 7116 | result.first->second.second = NewCtor; |
| 7117 | } |
| 7118 | } |
| 7119 | } |
| 7120 | } |
| 7121 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7122 | Sema::ImplicitExceptionSpecification |
| 7123 | Sema::ComputeDefaultedDtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7124 | // C++ [except.spec]p14: |
| 7125 | // An implicitly declared special member function (Clause 12) shall have |
| 7126 | // an exception-specification. |
| 7127 | ImplicitExceptionSpecification ExceptSpec(Context); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7128 | if (ClassDecl->isInvalidDecl()) |
| 7129 | return ExceptSpec; |
| 7130 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7131 | // Direct base-class destructors. |
| 7132 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7133 | BEnd = ClassDecl->bases_end(); |
| 7134 | B != BEnd; ++B) { |
| 7135 | if (B->isVirtual()) // Handled below. |
| 7136 | continue; |
| 7137 | |
| 7138 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 7139 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7140 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7141 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7142 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7143 | // Virtual base-class destructors. |
| 7144 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7145 | BEnd = ClassDecl->vbases_end(); |
| 7146 | B != BEnd; ++B) { |
| 7147 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 7148 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7149 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7150 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7151 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7152 | // Field destructors. |
| 7153 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7154 | FEnd = ClassDecl->field_end(); |
| 7155 | F != FEnd; ++F) { |
| 7156 | if (const RecordType *RecordTy |
| 7157 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 7158 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7159 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7160 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7161 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7162 | return ExceptSpec; |
| 7163 | } |
| 7164 | |
| 7165 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 7166 | // C++ [class.dtor]p2: |
| 7167 | // If a class has no user-declared destructor, a destructor is |
| 7168 | // declared implicitly. An implicitly-declared destructor is an |
| 7169 | // inline public member of its class. |
| 7170 | |
| 7171 | ImplicitExceptionSpecification Spec = |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7172 | ComputeDefaultedDtorExceptionSpec(ClassDecl); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7173 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 7174 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7175 | // Create the actual destructor declaration. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7176 | QualType Ty = Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7177 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7178 | CanQualType ClassType |
| 7179 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7180 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7181 | DeclarationName Name |
| 7182 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7183 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7184 | CXXDestructorDecl *Destructor |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7185 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, Ty, 0, |
| 7186 | /*isInline=*/true, |
| 7187 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7188 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7189 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7190 | Destructor->setImplicit(); |
| 7191 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7192 | |
| 7193 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7194 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 7195 | |
| 7196 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7197 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7198 | PushOnScopeChains(Destructor, S, false); |
| 7199 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7200 | |
| 7201 | // This could be uniqued if it ever proves significant. |
| 7202 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7203 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 7204 | AddOverriddenMethods(ClassDecl, Destructor); |
| 7205 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 7206 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7207 | Destructor->setDeletedAsWritten(); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 7208 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7209 | return Destructor; |
| 7210 | } |
| 7211 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7212 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 7213 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7214 | assert((Destructor->isDefaulted() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 7215 | !Destructor->doesThisDeclarationHaveABody() && |
| 7216 | !Destructor->isDeleted()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7217 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7218 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7219 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7220 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7221 | if (Destructor->isInvalidDecl()) |
| 7222 | return; |
| 7223 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 7224 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7225 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7226 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 7227 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 7228 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7229 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7230 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 7231 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7232 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 7233 | |
| 7234 | Destructor->setInvalidDecl(); |
| 7235 | return; |
| 7236 | } |
| 7237 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7238 | SourceLocation Loc = Destructor->getLocation(); |
| 7239 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 7240 | Destructor->setImplicitlyDefined(true); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7241 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7242 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7243 | |
| 7244 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7245 | L->CompletedImplicitDefinition(Destructor); |
| 7246 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7247 | } |
| 7248 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7249 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *classDecl, |
| 7250 | CXXDestructorDecl *destructor) { |
| 7251 | // C++11 [class.dtor]p3: |
| 7252 | // A declaration of a destructor that does not have an exception- |
| 7253 | // specification is implicitly considered to have the same exception- |
| 7254 | // specification as an implicit declaration. |
| 7255 | const FunctionProtoType *dtorType = destructor->getType()-> |
| 7256 | getAs<FunctionProtoType>(); |
| 7257 | if (dtorType->hasExceptionSpec()) |
| 7258 | return; |
| 7259 | |
| 7260 | ImplicitExceptionSpecification exceptSpec = |
| 7261 | ComputeDefaultedDtorExceptionSpec(classDecl); |
| 7262 | |
Chandler Carruth | 3f224b2 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 7263 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 7264 | // the only thing of interest in the destructor type is its extended info. |
| 7265 | // The return and arguments are fixed. |
| 7266 | FunctionProtoType::ExtProtoInfo epi = dtorType->getExtProtoInfo(); |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7267 | epi.ExceptionSpecType = exceptSpec.getExceptionSpecType(); |
| 7268 | epi.NumExceptions = exceptSpec.size(); |
| 7269 | epi.Exceptions = exceptSpec.data(); |
| 7270 | QualType ty = Context.getFunctionType(Context.VoidTy, 0, 0, epi); |
| 7271 | |
| 7272 | destructor->setType(ty); |
| 7273 | |
| 7274 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 7275 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 7276 | // change in behavior can break conforming C++03 programs at runtime. |
| 7277 | // However, we don't have a body yet, so it needs to be done somewhere else. |
| 7278 | } |
| 7279 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7280 | /// \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] | 7281 | /// \c To. |
| 7282 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7283 | /// This routine is used to copy/move the members of a class with an |
| 7284 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7285 | /// copied are arrays, this routine builds for loops to copy them. |
| 7286 | /// |
| 7287 | /// \param S The Sema object used for type-checking. |
| 7288 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7289 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7290 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7291 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 7292 | /// must have this type. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7293 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7294 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7295 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7296 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7297 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7298 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7299 | /// Otherwise, it's a non-static member subobject. |
| 7300 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7301 | /// \param Copying Whether we're copying or moving. |
| 7302 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7303 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 7304 | /// |
| 7305 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7306 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7307 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7308 | Expr *To, Expr *From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7309 | bool CopyingBaseSubobject, bool Copying, |
| 7310 | unsigned Depth = 0) { |
| 7311 | // C++0x [class.copy]p28: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7312 | // Each subobject is assigned in the manner appropriate to its type: |
| 7313 | // |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7314 | // - if the subobject is of class type, as if by a call to operator= with |
| 7315 | // the subobject as the object expression and the corresponding |
| 7316 | // subobject of x as a single function argument (as if by explicit |
| 7317 | // qualification; that is, ignoring any possible virtual overriding |
| 7318 | // functions in more derived classes); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7319 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 7320 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7321 | |
| 7322 | // Look for operator=. |
| 7323 | DeclarationName Name |
| 7324 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 7325 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 7326 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 7327 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7328 | // Filter out any result that isn't a copy/move-assignment operator. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7329 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 7330 | while (F.hasNext()) { |
| 7331 | NamedDecl *D = F.next(); |
| 7332 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7333 | if (Copying ? Method->isCopyAssignmentOperator() : |
| 7334 | Method->isMoveAssignmentOperator()) |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7335 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7336 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7337 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 7338 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7339 | F.done(); |
| 7340 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7341 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 7342 | // assignment operators we found. This strange dance is required when |
| 7343 | // we're assigning via a base classes's copy-assignment operator. To |
| 7344 | // ensure that we're getting the right base class subobject (without |
| 7345 | // ambiguities), we need to cast "this" to that subobject type; to |
| 7346 | // ensure that we don't go through the virtual call mechanism, we need |
| 7347 | // to qualify the operator= name with the base class (see below). However, |
| 7348 | // this means that if the base class has a protected copy assignment |
| 7349 | // operator, the protected member access check will fail. So, we |
| 7350 | // rewrite "protected" access to "public" access in this case, since we |
| 7351 | // know by construction that we're calling from a derived class. |
| 7352 | if (CopyingBaseSubobject) { |
| 7353 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 7354 | L != LEnd; ++L) { |
| 7355 | if (L.getAccess() == AS_protected) |
| 7356 | L.setAccess(AS_public); |
| 7357 | } |
| 7358 | } |
| 7359 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7360 | // Create the nested-name-specifier that will be used to qualify the |
| 7361 | // reference to operator=; this is required to suppress the virtual |
| 7362 | // call mechanism. |
| 7363 | CXXScopeSpec SS; |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 7364 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 7365 | SS.MakeTrivial(S.Context, |
| 7366 | NestedNameSpecifier::Create(S.Context, 0, false, |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 7367 | CanonicalT), |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 7368 | Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7369 | |
| 7370 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7371 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7372 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 7373 | /*TemplateKWLoc=*/SourceLocation(), |
| 7374 | /*FirstQualifierInScope=*/0, |
| 7375 | OpLookup, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7376 | /*TemplateArgs=*/0, |
| 7377 | /*SuppressQualifierCheck=*/true); |
| 7378 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7379 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7380 | |
| 7381 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7382 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7383 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7384 | OpEqualRef.takeAs<Expr>(), |
| 7385 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7386 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7387 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7388 | |
| 7389 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7390 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 7391 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7392 | // - if the subobject is of scalar type, the built-in assignment |
| 7393 | // operator is used. |
| 7394 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 7395 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7396 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7397 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7398 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7399 | |
| 7400 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7401 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7402 | |
| 7403 | // - if the subobject is an array, each element is assigned, in the |
| 7404 | // manner appropriate to the element type; |
| 7405 | |
| 7406 | // Construct a loop over the array bounds, e.g., |
| 7407 | // |
| 7408 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 7409 | // |
| 7410 | // that will copy each of the array elements. |
| 7411 | QualType SizeType = S.Context.getSizeType(); |
| 7412 | |
| 7413 | // Create the iteration variable. |
| 7414 | IdentifierInfo *IterationVarName = 0; |
| 7415 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7416 | SmallString<8> Str; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7417 | llvm::raw_svector_ostream OS(Str); |
| 7418 | OS << "__i" << Depth; |
| 7419 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 7420 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7421 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7422 | IterationVarName, SizeType, |
| 7423 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7424 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7425 | |
| 7426 | // Initialize the iteration variable to zero. |
| 7427 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7428 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7429 | |
| 7430 | // Create a reference to the iteration variable; we'll use this several |
| 7431 | // times throughout. |
| 7432 | Expr *IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7433 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7434 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7435 | Expr *IterationVarRefRVal = S.DefaultLvalueConversion(IterationVarRef).take(); |
| 7436 | assert(IterationVarRefRVal && "Conversion of invented variable cannot fail!"); |
| 7437 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7438 | // Create the DeclStmt that holds the iteration variable. |
| 7439 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 7440 | |
| 7441 | // Create the comparison against the array bound. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 7442 | llvm::APInt Upper |
| 7443 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7444 | Expr *Comparison |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7445 | = new (S.Context) BinaryOperator(IterationVarRefRVal, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7446 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 7447 | BO_NE, S.Context.BoolTy, |
| 7448 | VK_RValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7449 | |
| 7450 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7451 | Expr *Increment |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7452 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 7453 | VK_LValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7454 | |
| 7455 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7456 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7457 | IterationVarRefRVal, |
| 7458 | Loc)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7459 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7460 | IterationVarRefRVal, |
| 7461 | Loc)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7462 | if (!Copying) // Cast to rvalue |
| 7463 | From = CastForMoving(S, From); |
| 7464 | |
| 7465 | // Build the copy/move for an individual element of the array. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7466 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, ArrayTy->getElementType(), |
| 7467 | To, From, CopyingBaseSubobject, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7468 | Copying, Depth + 1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7469 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7470 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7471 | |
| 7472 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7473 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7474 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7475 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7476 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7477 | } |
| 7478 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7479 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 7480 | Sema::ComputeDefaultedCopyAssignmentExceptionSpecAndConst( |
| 7481 | CXXRecordDecl *ClassDecl) { |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7482 | if (ClassDecl->isInvalidDecl()) |
| 7483 | return std::make_pair(ImplicitExceptionSpecification(Context), false); |
| 7484 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7485 | // C++ [class.copy]p10: |
| 7486 | // If the class definition does not explicitly declare a copy |
| 7487 | // assignment operator, one is declared implicitly. |
| 7488 | // The implicitly-defined copy assignment operator for a class X |
| 7489 | // will have the form |
| 7490 | // |
| 7491 | // X& X::operator=(const X&) |
| 7492 | // |
| 7493 | // if |
| 7494 | bool HasConstCopyAssignment = true; |
| 7495 | |
| 7496 | // -- each direct base class B of X has a copy assignment operator |
| 7497 | // whose parameter is of type const B&, const volatile B& or B, |
| 7498 | // and |
| 7499 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7500 | BaseEnd = ClassDecl->bases_end(); |
| 7501 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7502 | // We'll handle this below |
| 7503 | if (LangOpts.CPlusPlus0x && Base->isVirtual()) |
| 7504 | continue; |
| 7505 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7506 | assert(!Base->getType()->isDependentType() && |
| 7507 | "Cannot generate implicit members for class with dependent bases."); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7508 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 7509 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 7510 | &HasConstCopyAssignment); |
| 7511 | } |
| 7512 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7513 | // In C++11, the above citation has "or virtual" added |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7514 | if (LangOpts.CPlusPlus0x) { |
| 7515 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7516 | BaseEnd = ClassDecl->vbases_end(); |
| 7517 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 7518 | assert(!Base->getType()->isDependentType() && |
| 7519 | "Cannot generate implicit members for class with dependent bases."); |
| 7520 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 7521 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 7522 | &HasConstCopyAssignment); |
| 7523 | } |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7524 | } |
| 7525 | |
| 7526 | // -- for all the nonstatic data members of X that are of a class |
| 7527 | // type M (or array thereof), each such class type has a copy |
| 7528 | // assignment operator whose parameter is of type const M&, |
| 7529 | // const volatile M& or M. |
| 7530 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7531 | FieldEnd = ClassDecl->field_end(); |
| 7532 | HasConstCopyAssignment && Field != FieldEnd; |
| 7533 | ++Field) { |
| 7534 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7535 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 7536 | LookupCopyingAssignment(FieldClassDecl, Qualifiers::Const, false, 0, |
| 7537 | &HasConstCopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7538 | } |
| 7539 | } |
| 7540 | |
| 7541 | // Otherwise, the implicitly declared copy assignment operator will |
| 7542 | // have the form |
| 7543 | // |
| 7544 | // X& X::operator=(X&) |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7545 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7546 | // C++ [except.spec]p14: |
| 7547 | // An implicitly declared special member function (Clause 12) shall have an |
| 7548 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7549 | |
| 7550 | // It is unspecified whether or not an implicit copy assignment operator |
| 7551 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 7552 | // made. As such, this exception specification is effectively unspecified. |
| 7553 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 7554 | // the side of assuming such calls to be made regardless of whether they |
| 7555 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7556 | ImplicitExceptionSpecification ExceptSpec(Context); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7557 | unsigned ArgQuals = HasConstCopyAssignment ? Qualifiers::Const : 0; |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7558 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7559 | BaseEnd = ClassDecl->bases_end(); |
| 7560 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7561 | if (Base->isVirtual()) |
| 7562 | continue; |
| 7563 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7564 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7565 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7566 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 7567 | ArgQuals, false, 0)) |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7568 | ExceptSpec.CalledDecl(CopyAssign); |
| 7569 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7570 | |
| 7571 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7572 | BaseEnd = ClassDecl->vbases_end(); |
| 7573 | Base != BaseEnd; ++Base) { |
| 7574 | CXXRecordDecl *BaseClassDecl |
| 7575 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 7576 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 7577 | ArgQuals, false, 0)) |
| 7578 | ExceptSpec.CalledDecl(CopyAssign); |
| 7579 | } |
| 7580 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7581 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7582 | FieldEnd = ClassDecl->field_end(); |
| 7583 | Field != FieldEnd; |
| 7584 | ++Field) { |
| 7585 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7586 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 7587 | if (CXXMethodDecl *CopyAssign = |
| 7588 | LookupCopyingAssignment(FieldClassDecl, ArgQuals, false, 0)) |
| 7589 | ExceptSpec.CalledDecl(CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7590 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7591 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7592 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7593 | return std::make_pair(ExceptSpec, HasConstCopyAssignment); |
| 7594 | } |
| 7595 | |
| 7596 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 7597 | // Note: The following rules are largely analoguous to the copy |
| 7598 | // constructor rules. Note that virtual bases are not taken into account |
| 7599 | // for determining the argument type of the operator. Note also that |
| 7600 | // operators taking an object instead of a reference are allowed. |
| 7601 | |
| 7602 | ImplicitExceptionSpecification Spec(Context); |
| 7603 | bool Const; |
| 7604 | llvm::tie(Spec, Const) = |
| 7605 | ComputeDefaultedCopyAssignmentExceptionSpecAndConst(ClassDecl); |
| 7606 | |
| 7607 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 7608 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 7609 | if (Const) |
| 7610 | ArgType = ArgType.withConst(); |
| 7611 | ArgType = Context.getLValueReferenceType(ArgType); |
| 7612 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7613 | // An implicitly-declared copy assignment operator is an inline public |
| 7614 | // member of its class. |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7615 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7616 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7617 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 7618 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7619 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7620 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7621 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7622 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7623 | /*StorageClassAsWritten=*/SC_None, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7624 | /*isInline=*/true, /*isConstexpr=*/false, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 7625 | SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7626 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7627 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7628 | CopyAssignment->setImplicit(); |
| 7629 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7630 | |
| 7631 | // Add the parameter to the operator. |
| 7632 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7633 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7634 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7635 | SC_None, |
| 7636 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7637 | CopyAssignment->setParams(FromParam); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7638 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7639 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7640 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7641 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7642 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7643 | PushOnScopeChains(CopyAssignment, S, false); |
| 7644 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7645 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 7646 | // C++0x [class.copy]p19: |
| 7647 | // .... If the class definition does not explicitly declare a copy |
| 7648 | // assignment operator, there is no user-declared move constructor, and |
| 7649 | // there is no user-declared move assignment operator, a copy assignment |
| 7650 | // operator is implicitly declared as defaulted. |
| 7651 | if ((ClassDecl->hasUserDeclaredMoveConstructor() && |
Nico Weber | 2897660 | 2012-01-23 04:01:33 +0000 | [diff] [blame] | 7652 | !getLangOptions().MicrosoftMode) || |
| 7653 | ClassDecl->hasUserDeclaredMoveAssignment() || |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 7654 | ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7655 | CopyAssignment->setDeletedAsWritten(); |
| 7656 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7657 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 7658 | return CopyAssignment; |
| 7659 | } |
| 7660 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7661 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 7662 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7663 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7664 | CopyAssignOperator->isOverloadedOperator() && |
| 7665 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 7666 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 7667 | !CopyAssignOperator->isDeleted()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7668 | "DefineImplicitCopyAssignment called for wrong function"); |
| 7669 | |
| 7670 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 7671 | |
| 7672 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 7673 | CopyAssignOperator->setInvalidDecl(); |
| 7674 | return; |
| 7675 | } |
| 7676 | |
| 7677 | CopyAssignOperator->setUsed(); |
| 7678 | |
| 7679 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7680 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7681 | |
| 7682 | // C++0x [class.copy]p30: |
| 7683 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 7684 | // for a non-union class X performs memberwise copy assignment of its |
| 7685 | // subobjects. The direct base classes of X are assigned first, in the |
| 7686 | // order of their declaration in the base-specifier-list, and then the |
| 7687 | // immediate non-static data members of X are assigned, in the order in |
| 7688 | // which they were declared in the class definition. |
| 7689 | |
| 7690 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7691 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7692 | |
| 7693 | // The parameter for the "other" object, which we are copying from. |
| 7694 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 7695 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 7696 | QualType OtherRefType = Other->getType(); |
| 7697 | if (const LValueReferenceType *OtherRef |
| 7698 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 7699 | OtherRefType = OtherRef->getPointeeType(); |
| 7700 | OtherQuals = OtherRefType.getQualifiers(); |
| 7701 | } |
| 7702 | |
| 7703 | // Our location for everything implicitly-generated. |
| 7704 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 7705 | |
| 7706 | // Construct a reference to the "other" object. We'll be using this |
| 7707 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7708 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7709 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 7710 | |
| 7711 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 7712 | // ASTs. |
| 7713 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 7714 | assert(This && "Reference to this cannot fail!"); |
| 7715 | |
| 7716 | // Assign base classes. |
| 7717 | bool Invalid = false; |
| 7718 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7719 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 7720 | // Form the assignment: |
| 7721 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 7722 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 7723 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7724 | Invalid = true; |
| 7725 | continue; |
| 7726 | } |
| 7727 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7728 | CXXCastPath BasePath; |
| 7729 | BasePath.push_back(Base); |
| 7730 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7731 | // Construct the "from" expression, which is an implicit cast to the |
| 7732 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 7733 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7734 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 7735 | CK_UncheckedDerivedToBase, |
| 7736 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7737 | |
| 7738 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7739 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7740 | |
| 7741 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7742 | To = ImpCastExprToType(To.take(), |
| 7743 | Context.getCVRQualifiedType(BaseType, |
| 7744 | CopyAssignOperator->getTypeQualifiers()), |
| 7745 | CK_UncheckedDerivedToBase, |
| 7746 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7747 | |
| 7748 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7749 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7750 | To.get(), From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7751 | /*CopyingBaseSubobject=*/true, |
| 7752 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7753 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7754 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7755 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7756 | CopyAssignOperator->setInvalidDecl(); |
| 7757 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
| 7760 | // Success! Record the copy. |
| 7761 | Statements.push_back(Copy.takeAs<Expr>()); |
| 7762 | } |
| 7763 | |
| 7764 | // \brief Reference to the __builtin_memcpy function. |
| 7765 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 7766 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7767 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7768 | |
| 7769 | // Assign non-static members. |
| 7770 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7771 | FieldEnd = ClassDecl->field_end(); |
| 7772 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 7773 | if (Field->isUnnamedBitfield()) |
| 7774 | continue; |
| 7775 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7776 | // Check for members of reference type; we can't copy those. |
| 7777 | if (Field->getType()->isReferenceType()) { |
| 7778 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7779 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 7780 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7781 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7782 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7783 | Invalid = true; |
| 7784 | continue; |
| 7785 | } |
| 7786 | |
| 7787 | // Check for members of const-qualified, non-class type. |
| 7788 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 7789 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 7790 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7791 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 7792 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7793 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7794 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7795 | Invalid = true; |
| 7796 | continue; |
| 7797 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 7798 | |
| 7799 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7800 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 7801 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7802 | |
| 7803 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 7804 | if (FieldType->isIncompleteArrayType()) { |
| 7805 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 7806 | "Incomplete array type is not valid"); |
| 7807 | continue; |
| 7808 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7809 | |
| 7810 | // Build references to the field in the object we're copying from and to. |
| 7811 | CXXScopeSpec SS; // Intentionally empty |
| 7812 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 7813 | LookupMemberName); |
| 7814 | MemberLookup.addDecl(*Field); |
| 7815 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7816 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7817 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 7818 | SS, SourceLocation(), 0, |
| 7819 | MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7820 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7821 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 7822 | SS, SourceLocation(), 0, |
| 7823 | MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7824 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 7825 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 7826 | |
| 7827 | // If the field should be copied with __builtin_memcpy rather than via |
| 7828 | // explicit assignments, do so. This optimization only applies for arrays |
| 7829 | // of scalars and arrays of class type with trivial copy-assignment |
| 7830 | // operators. |
Fariborz Jahanian | 6b167f4 | 2011-08-09 00:26:11 +0000 | [diff] [blame] | 7831 | if (FieldType->isArrayType() && !FieldType.isVolatileQualified() |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7832 | && BaseType.hasTrivialAssignment(Context, /*Copying=*/true)) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7833 | // Compute the size of the memory buffer to be copied. |
| 7834 | QualType SizeType = Context.getSizeType(); |
| 7835 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 7836 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 7837 | for (const ConstantArrayType *Array |
| 7838 | = Context.getAsConstantArrayType(FieldType); |
| 7839 | Array; |
| 7840 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 7841 | llvm::APInt ArraySize |
| 7842 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7843 | Size *= ArraySize; |
| 7844 | } |
| 7845 | |
| 7846 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7847 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 7848 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7849 | |
| 7850 | bool NeedsCollectableMemCpy = |
| 7851 | (BaseType->isRecordType() && |
| 7852 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 7853 | |
| 7854 | if (NeedsCollectableMemCpy) { |
| 7855 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 7856 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 7857 | LookupResult R(*this, |
| 7858 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7859 | Loc, LookupOrdinaryName); |
| 7860 | LookupName(R, TUScope, true); |
| 7861 | |
| 7862 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 7863 | if (!CollectableMemCpy) { |
| 7864 | // Something went horribly wrong earlier, and we will have |
| 7865 | // complained about it. |
| 7866 | Invalid = true; |
| 7867 | continue; |
| 7868 | } |
| 7869 | |
| 7870 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 7871 | CollectableMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7872 | VK_LValue, Loc, 0).take(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7873 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 7874 | } |
| 7875 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7876 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7877 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7878 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 7879 | LookupOrdinaryName); |
| 7880 | LookupName(R, TUScope, true); |
| 7881 | |
| 7882 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 7883 | if (!BuiltinMemCpy) { |
| 7884 | // Something went horribly wrong earlier, and we will have complained |
| 7885 | // about it. |
| 7886 | Invalid = true; |
| 7887 | continue; |
| 7888 | } |
| 7889 | |
| 7890 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 7891 | BuiltinMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7892 | VK_LValue, Loc, 0).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7893 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 7894 | } |
| 7895 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7896 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7897 | CallArgs.push_back(To.takeAs<Expr>()); |
| 7898 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7899 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7900 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7901 | if (NeedsCollectableMemCpy) |
| 7902 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7903 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7904 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7905 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7906 | else |
| 7907 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7908 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7909 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7910 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7911 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7912 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 7913 | Statements.push_back(Call.takeAs<Expr>()); |
| 7914 | continue; |
| 7915 | } |
| 7916 | |
| 7917 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7918 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7919 | To.get(), From.get(), |
| 7920 | /*CopyingBaseSubobject=*/false, |
| 7921 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7922 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7923 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7924 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7925 | CopyAssignOperator->setInvalidDecl(); |
| 7926 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | // Success! Record the copy. |
| 7930 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 7931 | } |
| 7932 | |
| 7933 | if (!Invalid) { |
| 7934 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7935 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7936 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7937 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7938 | if (Return.isInvalid()) |
| 7939 | Invalid = true; |
| 7940 | else { |
| 7941 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7942 | |
| 7943 | if (Trap.hasErrorOccurred()) { |
| 7944 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7945 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7946 | Invalid = true; |
| 7947 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7948 | } |
| 7949 | } |
| 7950 | |
| 7951 | if (Invalid) { |
| 7952 | CopyAssignOperator->setInvalidDecl(); |
| 7953 | return; |
| 7954 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 7955 | |
| 7956 | StmtResult Body; |
| 7957 | { |
| 7958 | CompoundScopeRAII CompoundScope(*this); |
| 7959 | Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
| 7960 | /*isStmtExpr=*/false); |
| 7961 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 7962 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7963 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7964 | |
| 7965 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7966 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 7967 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7968 | } |
| 7969 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7970 | Sema::ImplicitExceptionSpecification |
| 7971 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXRecordDecl *ClassDecl) { |
| 7972 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 7973 | |
| 7974 | if (ClassDecl->isInvalidDecl()) |
| 7975 | return ExceptSpec; |
| 7976 | |
| 7977 | // C++0x [except.spec]p14: |
| 7978 | // An implicitly declared special member function (Clause 12) shall have an |
| 7979 | // exception-specification. [...] |
| 7980 | |
| 7981 | // It is unspecified whether or not an implicit move assignment operator |
| 7982 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 7983 | // made. As such, this exception specification is effectively unspecified. |
| 7984 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 7985 | // the side of assuming such calls to be made regardless of whether they |
| 7986 | // actually happen. |
| 7987 | // Note that a move constructor is not implicitly declared when there are |
| 7988 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| 7989 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7990 | BaseEnd = ClassDecl->bases_end(); |
| 7991 | Base != BaseEnd; ++Base) { |
| 7992 | if (Base->isVirtual()) |
| 7993 | continue; |
| 7994 | |
| 7995 | CXXRecordDecl *BaseClassDecl |
| 7996 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 7997 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
| 7998 | false, 0)) |
| 7999 | ExceptSpec.CalledDecl(MoveAssign); |
| 8000 | } |
| 8001 | |
| 8002 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8003 | BaseEnd = ClassDecl->vbases_end(); |
| 8004 | Base != BaseEnd; ++Base) { |
| 8005 | CXXRecordDecl *BaseClassDecl |
| 8006 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8007 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
| 8008 | false, 0)) |
| 8009 | ExceptSpec.CalledDecl(MoveAssign); |
| 8010 | } |
| 8011 | |
| 8012 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8013 | FieldEnd = ClassDecl->field_end(); |
| 8014 | Field != FieldEnd; |
| 8015 | ++Field) { |
| 8016 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 8017 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 8018 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(FieldClassDecl, |
| 8019 | false, 0)) |
| 8020 | ExceptSpec.CalledDecl(MoveAssign); |
| 8021 | } |
| 8022 | } |
| 8023 | |
| 8024 | return ExceptSpec; |
| 8025 | } |
| 8026 | |
| 8027 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
| 8028 | // Note: The following rules are largely analoguous to the move |
| 8029 | // constructor rules. |
| 8030 | |
| 8031 | ImplicitExceptionSpecification Spec( |
| 8032 | ComputeDefaultedMoveAssignmentExceptionSpec(ClassDecl)); |
| 8033 | |
| 8034 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 8035 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 8036 | ArgType = Context.getRValueReferenceType(ArgType); |
| 8037 | |
| 8038 | // An implicitly-declared move assignment operator is an inline public |
| 8039 | // member of its class. |
| 8040 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 8041 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 8042 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8043 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 8044 | CXXMethodDecl *MoveAssignment |
| 8045 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 8046 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
| 8047 | /*TInfo=*/0, /*isStatic=*/false, |
| 8048 | /*StorageClassAsWritten=*/SC_None, |
| 8049 | /*isInline=*/true, |
| 8050 | /*isConstexpr=*/false, |
| 8051 | SourceLocation()); |
| 8052 | MoveAssignment->setAccess(AS_public); |
| 8053 | MoveAssignment->setDefaulted(); |
| 8054 | MoveAssignment->setImplicit(); |
| 8055 | MoveAssignment->setTrivial(ClassDecl->hasTrivialMoveAssignment()); |
| 8056 | |
| 8057 | // Add the parameter to the operator. |
| 8058 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 8059 | ClassLoc, ClassLoc, /*Id=*/0, |
| 8060 | ArgType, /*TInfo=*/0, |
| 8061 | SC_None, |
| 8062 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8063 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8064 | |
| 8065 | // Note that we have added this copy-assignment operator. |
| 8066 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 8067 | |
| 8068 | // C++0x [class.copy]p9: |
| 8069 | // If the definition of a class X does not explicitly declare a move |
| 8070 | // assignment operator, one will be implicitly declared as defaulted if and |
| 8071 | // only if: |
| 8072 | // [...] |
| 8073 | // - the move assignment operator would not be implicitly defined as |
| 8074 | // deleted. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 8075 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8076 | // Cache this result so that we don't try to generate this over and over |
| 8077 | // on every lookup, leaking memory and wasting time. |
| 8078 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 8079 | return 0; |
| 8080 | } |
| 8081 | |
| 8082 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8083 | PushOnScopeChains(MoveAssignment, S, false); |
| 8084 | ClassDecl->addDecl(MoveAssignment); |
| 8085 | |
| 8086 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 8087 | return MoveAssignment; |
| 8088 | } |
| 8089 | |
| 8090 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 8091 | CXXMethodDecl *MoveAssignOperator) { |
| 8092 | assert((MoveAssignOperator->isDefaulted() && |
| 8093 | MoveAssignOperator->isOverloadedOperator() && |
| 8094 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8095 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 8096 | !MoveAssignOperator->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8097 | "DefineImplicitMoveAssignment called for wrong function"); |
| 8098 | |
| 8099 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 8100 | |
| 8101 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 8102 | MoveAssignOperator->setInvalidDecl(); |
| 8103 | return; |
| 8104 | } |
| 8105 | |
| 8106 | MoveAssignOperator->setUsed(); |
| 8107 | |
| 8108 | ImplicitlyDefinedFunctionScope Scope(*this, MoveAssignOperator); |
| 8109 | DiagnosticErrorTrap Trap(Diags); |
| 8110 | |
| 8111 | // C++0x [class.copy]p28: |
| 8112 | // The implicitly-defined or move assignment operator for a non-union class |
| 8113 | // X performs memberwise move assignment of its subobjects. The direct base |
| 8114 | // classes of X are assigned first, in the order of their declaration in the |
| 8115 | // base-specifier-list, and then the immediate non-static data members of X |
| 8116 | // are assigned, in the order in which they were declared in the class |
| 8117 | // definition. |
| 8118 | |
| 8119 | // The statements that form the synthesized function body. |
| 8120 | ASTOwningVector<Stmt*> Statements(*this); |
| 8121 | |
| 8122 | // The parameter for the "other" object, which we are move from. |
| 8123 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 8124 | QualType OtherRefType = Other->getType()-> |
| 8125 | getAs<RValueReferenceType>()->getPointeeType(); |
| 8126 | assert(OtherRefType.getQualifiers() == 0 && |
| 8127 | "Bad argument type of defaulted move assignment"); |
| 8128 | |
| 8129 | // Our location for everything implicitly-generated. |
| 8130 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 8131 | |
| 8132 | // Construct a reference to the "other" object. We'll be using this |
| 8133 | // throughout the generated ASTs. |
| 8134 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
| 8135 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 8136 | // Cast to rvalue. |
| 8137 | OtherRef = CastForMoving(*this, OtherRef); |
| 8138 | |
| 8139 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 8140 | // ASTs. |
| 8141 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 8142 | assert(This && "Reference to this cannot fail!"); |
| 8143 | |
| 8144 | // Assign base classes. |
| 8145 | bool Invalid = false; |
| 8146 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8147 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 8148 | // Form the assignment: |
| 8149 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| 8150 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 8151 | if (!BaseType->isRecordType()) { |
| 8152 | Invalid = true; |
| 8153 | continue; |
| 8154 | } |
| 8155 | |
| 8156 | CXXCastPath BasePath; |
| 8157 | BasePath.push_back(Base); |
| 8158 | |
| 8159 | // Construct the "from" expression, which is an implicit cast to the |
| 8160 | // appropriately-qualified base type. |
| 8161 | Expr *From = OtherRef; |
| 8162 | From = ImpCastExprToType(From, BaseType, CK_UncheckedDerivedToBase, |
Douglas Gregor | b2b5658 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 8163 | VK_XValue, &BasePath).take(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8164 | |
| 8165 | // Dereference "this". |
| 8166 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 8167 | |
| 8168 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 8169 | To = ImpCastExprToType(To.take(), |
| 8170 | Context.getCVRQualifiedType(BaseType, |
| 8171 | MoveAssignOperator->getTypeQualifiers()), |
| 8172 | CK_UncheckedDerivedToBase, |
| 8173 | VK_LValue, &BasePath); |
| 8174 | |
| 8175 | // Build the move. |
| 8176 | StmtResult Move = BuildSingleCopyAssign(*this, Loc, BaseType, |
| 8177 | To.get(), From, |
| 8178 | /*CopyingBaseSubobject=*/true, |
| 8179 | /*Copying=*/false); |
| 8180 | if (Move.isInvalid()) { |
| 8181 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8182 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8183 | MoveAssignOperator->setInvalidDecl(); |
| 8184 | return; |
| 8185 | } |
| 8186 | |
| 8187 | // Success! Record the move. |
| 8188 | Statements.push_back(Move.takeAs<Expr>()); |
| 8189 | } |
| 8190 | |
| 8191 | // \brief Reference to the __builtin_memcpy function. |
| 8192 | Expr *BuiltinMemCpyRef = 0; |
| 8193 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
| 8194 | Expr *CollectableMemCpyRef = 0; |
| 8195 | |
| 8196 | // Assign non-static members. |
| 8197 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8198 | FieldEnd = ClassDecl->field_end(); |
| 8199 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 8200 | if (Field->isUnnamedBitfield()) |
| 8201 | continue; |
| 8202 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8203 | // Check for members of reference type; we can't move those. |
| 8204 | if (Field->getType()->isReferenceType()) { |
| 8205 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8206 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 8207 | Diag(Field->getLocation(), diag::note_declared_at); |
| 8208 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8209 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8210 | Invalid = true; |
| 8211 | continue; |
| 8212 | } |
| 8213 | |
| 8214 | // Check for members of const-qualified, non-class type. |
| 8215 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 8216 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 8217 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8218 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 8219 | Diag(Field->getLocation(), diag::note_declared_at); |
| 8220 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8221 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8222 | Invalid = true; |
| 8223 | continue; |
| 8224 | } |
| 8225 | |
| 8226 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8227 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 8228 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8229 | |
| 8230 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 8231 | if (FieldType->isIncompleteArrayType()) { |
| 8232 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 8233 | "Incomplete array type is not valid"); |
| 8234 | continue; |
| 8235 | } |
| 8236 | |
| 8237 | // Build references to the field in the object we're copying from and to. |
| 8238 | CXXScopeSpec SS; // Intentionally empty |
| 8239 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 8240 | LookupMemberName); |
| 8241 | MemberLookup.addDecl(*Field); |
| 8242 | MemberLookup.resolveKind(); |
| 8243 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
| 8244 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8245 | SS, SourceLocation(), 0, |
| 8246 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8247 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
| 8248 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8249 | SS, SourceLocation(), 0, |
| 8250 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8251 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 8252 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 8253 | |
| 8254 | assert(!From.get()->isLValue() && // could be xvalue or prvalue |
| 8255 | "Member reference with rvalue base must be rvalue except for reference " |
| 8256 | "members, which aren't allowed for move assignment."); |
| 8257 | |
| 8258 | // If the field should be copied with __builtin_memcpy rather than via |
| 8259 | // explicit assignments, do so. This optimization only applies for arrays |
| 8260 | // of scalars and arrays of class type with trivial move-assignment |
| 8261 | // operators. |
| 8262 | if (FieldType->isArrayType() && !FieldType.isVolatileQualified() |
| 8263 | && BaseType.hasTrivialAssignment(Context, /*Copying=*/false)) { |
| 8264 | // Compute the size of the memory buffer to be copied. |
| 8265 | QualType SizeType = Context.getSizeType(); |
| 8266 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 8267 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 8268 | for (const ConstantArrayType *Array |
| 8269 | = Context.getAsConstantArrayType(FieldType); |
| 8270 | Array; |
| 8271 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
| 8272 | llvm::APInt ArraySize |
| 8273 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
| 8274 | Size *= ArraySize; |
| 8275 | } |
| 8276 | |
Douglas Gregor | 45d3d71 | 2011-09-01 02:09:07 +0000 | [diff] [blame] | 8277 | // Take the address of the field references for "from" and "to". We |
| 8278 | // directly construct UnaryOperators here because semantic analysis |
| 8279 | // does not permit us to take the address of an xvalue. |
| 8280 | From = new (Context) UnaryOperator(From.get(), UO_AddrOf, |
| 8281 | Context.getPointerType(From.get()->getType()), |
| 8282 | VK_RValue, OK_Ordinary, Loc); |
| 8283 | To = new (Context) UnaryOperator(To.get(), UO_AddrOf, |
| 8284 | Context.getPointerType(To.get()->getType()), |
| 8285 | VK_RValue, OK_Ordinary, Loc); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8286 | |
| 8287 | bool NeedsCollectableMemCpy = |
| 8288 | (BaseType->isRecordType() && |
| 8289 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 8290 | |
| 8291 | if (NeedsCollectableMemCpy) { |
| 8292 | if (!CollectableMemCpyRef) { |
| 8293 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 8294 | LookupResult R(*this, |
| 8295 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
| 8296 | Loc, LookupOrdinaryName); |
| 8297 | LookupName(R, TUScope, true); |
| 8298 | |
| 8299 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 8300 | if (!CollectableMemCpy) { |
| 8301 | // Something went horribly wrong earlier, and we will have |
| 8302 | // complained about it. |
| 8303 | Invalid = true; |
| 8304 | continue; |
| 8305 | } |
| 8306 | |
| 8307 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 8308 | CollectableMemCpy->getType(), |
| 8309 | VK_LValue, Loc, 0).take(); |
| 8310 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 8311 | } |
| 8312 | } |
| 8313 | // Create a reference to the __builtin_memcpy builtin function. |
| 8314 | else if (!BuiltinMemCpyRef) { |
| 8315 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 8316 | LookupOrdinaryName); |
| 8317 | LookupName(R, TUScope, true); |
| 8318 | |
| 8319 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 8320 | if (!BuiltinMemCpy) { |
| 8321 | // Something went horribly wrong earlier, and we will have complained |
| 8322 | // about it. |
| 8323 | Invalid = true; |
| 8324 | continue; |
| 8325 | } |
| 8326 | |
| 8327 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 8328 | BuiltinMemCpy->getType(), |
| 8329 | VK_LValue, Loc, 0).take(); |
| 8330 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 8331 | } |
| 8332 | |
| 8333 | ASTOwningVector<Expr*> CallArgs(*this); |
| 8334 | CallArgs.push_back(To.takeAs<Expr>()); |
| 8335 | CallArgs.push_back(From.takeAs<Expr>()); |
| 8336 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
| 8337 | ExprResult Call = ExprError(); |
| 8338 | if (NeedsCollectableMemCpy) |
| 8339 | Call = ActOnCallExpr(/*Scope=*/0, |
| 8340 | CollectableMemCpyRef, |
| 8341 | Loc, move_arg(CallArgs), |
| 8342 | Loc); |
| 8343 | else |
| 8344 | Call = ActOnCallExpr(/*Scope=*/0, |
| 8345 | BuiltinMemCpyRef, |
| 8346 | Loc, move_arg(CallArgs), |
| 8347 | Loc); |
| 8348 | |
| 8349 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 8350 | Statements.push_back(Call.takeAs<Expr>()); |
| 8351 | continue; |
| 8352 | } |
| 8353 | |
| 8354 | // Build the move of this field. |
| 8355 | StmtResult Move = BuildSingleCopyAssign(*this, Loc, FieldType, |
| 8356 | To.get(), From.get(), |
| 8357 | /*CopyingBaseSubobject=*/false, |
| 8358 | /*Copying=*/false); |
| 8359 | if (Move.isInvalid()) { |
| 8360 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8361 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8362 | MoveAssignOperator->setInvalidDecl(); |
| 8363 | return; |
| 8364 | } |
| 8365 | |
| 8366 | // Success! Record the copy. |
| 8367 | Statements.push_back(Move.takeAs<Stmt>()); |
| 8368 | } |
| 8369 | |
| 8370 | if (!Invalid) { |
| 8371 | // Add a "return *this;" |
| 8372 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 8373 | |
| 8374 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 8375 | if (Return.isInvalid()) |
| 8376 | Invalid = true; |
| 8377 | else { |
| 8378 | Statements.push_back(Return.takeAs<Stmt>()); |
| 8379 | |
| 8380 | if (Trap.hasErrorOccurred()) { |
| 8381 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8382 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8383 | Invalid = true; |
| 8384 | } |
| 8385 | } |
| 8386 | } |
| 8387 | |
| 8388 | if (Invalid) { |
| 8389 | MoveAssignOperator->setInvalidDecl(); |
| 8390 | return; |
| 8391 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8392 | |
| 8393 | StmtResult Body; |
| 8394 | { |
| 8395 | CompoundScopeRAII CompoundScope(*this); |
| 8396 | Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
| 8397 | /*isStmtExpr=*/false); |
| 8398 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 8399 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8400 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 8401 | |
| 8402 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8403 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 8404 | } |
| 8405 | } |
| 8406 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8407 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 8408 | Sema::ComputeDefaultedCopyCtorExceptionSpecAndConst(CXXRecordDecl *ClassDecl) { |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8409 | if (ClassDecl->isInvalidDecl()) |
| 8410 | return std::make_pair(ImplicitExceptionSpecification(Context), false); |
| 8411 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8412 | // C++ [class.copy]p5: |
| 8413 | // The implicitly-declared copy constructor for a class X will |
| 8414 | // have the form |
| 8415 | // |
| 8416 | // X::X(const X&) |
| 8417 | // |
| 8418 | // if |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8419 | // FIXME: It ought to be possible to store this on the record. |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8420 | bool HasConstCopyConstructor = true; |
| 8421 | |
| 8422 | // -- each direct or virtual base class B of X has a copy |
| 8423 | // constructor whose first parameter is of type const B& or |
| 8424 | // const volatile B&, and |
| 8425 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8426 | BaseEnd = ClassDecl->bases_end(); |
| 8427 | HasConstCopyConstructor && Base != BaseEnd; |
| 8428 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8429 | // Virtual bases are handled below. |
| 8430 | if (Base->isVirtual()) |
| 8431 | continue; |
| 8432 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8433 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8434 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8435 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 8436 | &HasConstCopyConstructor); |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8437 | } |
| 8438 | |
| 8439 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8440 | BaseEnd = ClassDecl->vbases_end(); |
| 8441 | HasConstCopyConstructor && Base != BaseEnd; |
| 8442 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8443 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8444 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8445 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 8446 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8447 | } |
| 8448 | |
| 8449 | // -- for all the nonstatic data members of X that are of a |
| 8450 | // class type M (or array thereof), each such class type |
| 8451 | // has a copy constructor whose first parameter is of type |
| 8452 | // const M& or const volatile M&. |
| 8453 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8454 | FieldEnd = ClassDecl->field_end(); |
| 8455 | HasConstCopyConstructor && Field != FieldEnd; |
| 8456 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 8457 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8458 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8459 | LookupCopyingConstructor(FieldClassDecl, Qualifiers::Const, |
| 8460 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8461 | } |
| 8462 | } |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8463 | // Otherwise, the implicitly declared copy constructor will have |
| 8464 | // the form |
| 8465 | // |
| 8466 | // X::X(X&) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8467 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8468 | // C++ [except.spec]p14: |
| 8469 | // An implicitly declared special member function (Clause 12) shall have an |
| 8470 | // exception-specification. [...] |
| 8471 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 8472 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 8473 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8474 | BaseEnd = ClassDecl->bases_end(); |
| 8475 | Base != BaseEnd; |
| 8476 | ++Base) { |
| 8477 | // Virtual bases are handled below. |
| 8478 | if (Base->isVirtual()) |
| 8479 | continue; |
| 8480 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8481 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8482 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8483 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8484 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8485 | ExceptSpec.CalledDecl(CopyConstructor); |
| 8486 | } |
| 8487 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8488 | BaseEnd = ClassDecl->vbases_end(); |
| 8489 | Base != BaseEnd; |
| 8490 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8491 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8492 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8493 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8494 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8495 | ExceptSpec.CalledDecl(CopyConstructor); |
| 8496 | } |
| 8497 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8498 | FieldEnd = ClassDecl->field_end(); |
| 8499 | Field != FieldEnd; |
| 8500 | ++Field) { |
| 8501 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8502 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 8503 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8504 | LookupCopyingConstructor(FieldClassDecl, Quals)) |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8505 | ExceptSpec.CalledDecl(CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8506 | } |
| 8507 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8508 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8509 | return std::make_pair(ExceptSpec, HasConstCopyConstructor); |
| 8510 | } |
| 8511 | |
| 8512 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 8513 | CXXRecordDecl *ClassDecl) { |
| 8514 | // C++ [class.copy]p4: |
| 8515 | // If the class definition does not explicitly declare a copy |
| 8516 | // constructor, one is declared implicitly. |
| 8517 | |
| 8518 | ImplicitExceptionSpecification Spec(Context); |
| 8519 | bool Const; |
| 8520 | llvm::tie(Spec, Const) = |
| 8521 | ComputeDefaultedCopyCtorExceptionSpecAndConst(ClassDecl); |
| 8522 | |
| 8523 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 8524 | QualType ArgType = ClassType; |
| 8525 | if (Const) |
| 8526 | ArgType = ArgType.withConst(); |
| 8527 | ArgType = Context.getLValueReferenceType(ArgType); |
| 8528 | |
| 8529 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 8530 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8531 | DeclarationName Name |
| 8532 | = Context.DeclarationNames.getCXXConstructorName( |
| 8533 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8534 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8535 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8536 | |
| 8537 | // An implicitly-declared copy constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8538 | // member of its class. |
| 8539 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
| 8540 | Context, ClassDecl, ClassLoc, NameInfo, |
| 8541 | Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI), /*TInfo=*/0, |
| 8542 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
| 8543 | /*isConstexpr=*/ClassDecl->defaultedCopyConstructorIsConstexpr() && |
| 8544 | getLangOptions().CPlusPlus0x); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8545 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8546 | CopyConstructor->setDefaulted(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8547 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8548 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8549 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8550 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 8551 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8552 | // Add the parameter to the constructor. |
| 8553 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8554 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8555 | /*IdentifierInfo=*/0, |
| 8556 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8557 | SC_None, |
| 8558 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8559 | CopyConstructor->setParams(FromParam); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8560 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8561 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8562 | PushOnScopeChains(CopyConstructor, S, false); |
| 8563 | ClassDecl->addDecl(CopyConstructor); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8564 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 8565 | // C++11 [class.copy]p8: |
| 8566 | // ... If the class definition does not explicitly declare a copy |
| 8567 | // constructor, there is no user-declared move constructor, and there is no |
| 8568 | // user-declared move assignment operator, a copy constructor is implicitly |
| 8569 | // declared as defaulted. |
Sean Hunt | 1ccbc54 | 2011-06-22 01:05:13 +0000 | [diff] [blame] | 8570 | if (ClassDecl->hasUserDeclaredMoveConstructor() || |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 8571 | (ClassDecl->hasUserDeclaredMoveAssignment() && |
Nico Weber | 2897660 | 2012-01-23 04:01:33 +0000 | [diff] [blame] | 8572 | !getLangOptions().MicrosoftMode) || |
Sean Hunt | c32d684 | 2011-10-11 04:55:36 +0000 | [diff] [blame] | 8573 | ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8574 | CopyConstructor->setDeletedAsWritten(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8575 | |
| 8576 | return CopyConstructor; |
| 8577 | } |
| 8578 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8579 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8580 | CXXConstructorDecl *CopyConstructor) { |
| 8581 | assert((CopyConstructor->isDefaulted() && |
| 8582 | CopyConstructor->isCopyConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8583 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 8584 | !CopyConstructor->isDeleted()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8585 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8586 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 8587 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8588 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8589 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 8590 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8591 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8592 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 8593 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8594 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 8595 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8596 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 8597 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8598 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8599 | Sema::CompoundScopeRAII CompoundScope(*this); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8600 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 8601 | CopyConstructor->getLocation(), |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8602 | MultiStmtArg(*this, 0, 0), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8603 | /*isStmtExpr=*/false) |
| 8604 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 8605 | CopyConstructor->setImplicitlyDefined(true); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 8606 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8607 | |
| 8608 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8609 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8610 | L->CompletedImplicitDefinition(CopyConstructor); |
| 8611 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8612 | } |
| 8613 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8614 | Sema::ImplicitExceptionSpecification |
| 8615 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
| 8616 | // C++ [except.spec]p14: |
| 8617 | // An implicitly declared special member function (Clause 12) shall have an |
| 8618 | // exception-specification. [...] |
| 8619 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 8620 | if (ClassDecl->isInvalidDecl()) |
| 8621 | return ExceptSpec; |
| 8622 | |
| 8623 | // Direct base-class constructors. |
| 8624 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8625 | BEnd = ClassDecl->bases_end(); |
| 8626 | B != BEnd; ++B) { |
| 8627 | if (B->isVirtual()) // Handled below. |
| 8628 | continue; |
| 8629 | |
| 8630 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8631 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8632 | CXXConstructorDecl *Constructor = LookupMovingConstructor(BaseClassDecl); |
| 8633 | // If this is a deleted function, add it anyway. This might be conformant |
| 8634 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8635 | if (Constructor) |
| 8636 | ExceptSpec.CalledDecl(Constructor); |
| 8637 | } |
| 8638 | } |
| 8639 | |
| 8640 | // Virtual base-class constructors. |
| 8641 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8642 | BEnd = ClassDecl->vbases_end(); |
| 8643 | B != BEnd; ++B) { |
| 8644 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8645 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8646 | CXXConstructorDecl *Constructor = LookupMovingConstructor(BaseClassDecl); |
| 8647 | // If this is a deleted function, add it anyway. This might be conformant |
| 8648 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8649 | if (Constructor) |
| 8650 | ExceptSpec.CalledDecl(Constructor); |
| 8651 | } |
| 8652 | } |
| 8653 | |
| 8654 | // Field constructors. |
| 8655 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8656 | FEnd = ClassDecl->field_end(); |
| 8657 | F != FEnd; ++F) { |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 8658 | if (const RecordType *RecordTy |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8659 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 8660 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8661 | CXXConstructorDecl *Constructor = LookupMovingConstructor(FieldRecDecl); |
| 8662 | // If this is a deleted function, add it anyway. This might be conformant |
| 8663 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8664 | // In particular, the problem is that this function never gets called. It |
| 8665 | // might just be ill-formed because this function attempts to refer to |
| 8666 | // a deleted function here. |
| 8667 | if (Constructor) |
| 8668 | ExceptSpec.CalledDecl(Constructor); |
| 8669 | } |
| 8670 | } |
| 8671 | |
| 8672 | return ExceptSpec; |
| 8673 | } |
| 8674 | |
| 8675 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 8676 | CXXRecordDecl *ClassDecl) { |
| 8677 | ImplicitExceptionSpecification Spec( |
| 8678 | ComputeDefaultedMoveCtorExceptionSpec(ClassDecl)); |
| 8679 | |
| 8680 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 8681 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
| 8682 | |
| 8683 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 8684 | |
| 8685 | DeclarationName Name |
| 8686 | = Context.DeclarationNames.getCXXConstructorName( |
| 8687 | Context.getCanonicalType(ClassType)); |
| 8688 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8689 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 8690 | |
| 8691 | // C++0x [class.copy]p11: |
| 8692 | // An implicitly-declared copy/move constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8693 | // member of its class. |
| 8694 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
| 8695 | Context, ClassDecl, ClassLoc, NameInfo, |
| 8696 | Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI), /*TInfo=*/0, |
| 8697 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
| 8698 | /*isConstexpr=*/ClassDecl->defaultedMoveConstructorIsConstexpr() && |
| 8699 | getLangOptions().CPlusPlus0x); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8700 | MoveConstructor->setAccess(AS_public); |
| 8701 | MoveConstructor->setDefaulted(); |
| 8702 | MoveConstructor->setTrivial(ClassDecl->hasTrivialMoveConstructor()); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8703 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8704 | // Add the parameter to the constructor. |
| 8705 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 8706 | ClassLoc, ClassLoc, |
| 8707 | /*IdentifierInfo=*/0, |
| 8708 | ArgType, /*TInfo=*/0, |
| 8709 | SC_None, |
| 8710 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8711 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8712 | |
| 8713 | // C++0x [class.copy]p9: |
| 8714 | // If the definition of a class X does not explicitly declare a move |
| 8715 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 8716 | // [...] |
| 8717 | // - the move constructor would not be implicitly defined as deleted. |
Sean Hunt | 769bb2d | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 8718 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8719 | // Cache this result so that we don't try to generate this over and over |
| 8720 | // on every lookup, leaking memory and wasting time. |
| 8721 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 8722 | return 0; |
| 8723 | } |
| 8724 | |
| 8725 | // Note that we have declared this constructor. |
| 8726 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 8727 | |
| 8728 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8729 | PushOnScopeChains(MoveConstructor, S, false); |
| 8730 | ClassDecl->addDecl(MoveConstructor); |
| 8731 | |
| 8732 | return MoveConstructor; |
| 8733 | } |
| 8734 | |
| 8735 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 8736 | CXXConstructorDecl *MoveConstructor) { |
| 8737 | assert((MoveConstructor->isDefaulted() && |
| 8738 | MoveConstructor->isMoveConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8739 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 8740 | !MoveConstructor->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8741 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 8742 | |
| 8743 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 8744 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 8745 | |
| 8746 | ImplicitlyDefinedFunctionScope Scope(*this, MoveConstructor); |
| 8747 | DiagnosticErrorTrap Trap(Diags); |
| 8748 | |
| 8749 | if (SetCtorInitializers(MoveConstructor, 0, 0, /*AnyErrors=*/false) || |
| 8750 | Trap.hasErrorOccurred()) { |
| 8751 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8752 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 8753 | MoveConstructor->setInvalidDecl(); |
| 8754 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8755 | Sema::CompoundScopeRAII CompoundScope(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8756 | MoveConstructor->setBody(ActOnCompoundStmt(MoveConstructor->getLocation(), |
| 8757 | MoveConstructor->getLocation(), |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8758 | MultiStmtArg(*this, 0, 0), |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8759 | /*isStmtExpr=*/false) |
| 8760 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 8761 | MoveConstructor->setImplicitlyDefined(true); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8762 | } |
| 8763 | |
| 8764 | MoveConstructor->setUsed(); |
| 8765 | |
| 8766 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8767 | L->CompletedImplicitDefinition(MoveConstructor); |
| 8768 | } |
| 8769 | } |
| 8770 | |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8771 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
| 8772 | return FD->isDeleted() && |
| 8773 | (FD->isDefaulted() || FD->isImplicit()) && |
| 8774 | isa<CXXMethodDecl>(FD); |
| 8775 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8776 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8777 | /// \brief Mark the call operator of the given lambda closure type as "used". |
| 8778 | static void markLambdaCallOperatorUsed(Sema &S, CXXRecordDecl *Lambda) { |
| 8779 | CXXMethodDecl *CallOperator |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8780 | = cast<CXXMethodDecl>( |
| 8781 | *Lambda->lookup( |
| 8782 | S.Context.DeclarationNames.getCXXOperatorName(OO_Call)).first); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8783 | CallOperator->setReferenced(); |
| 8784 | CallOperator->setUsed(); |
| 8785 | } |
| 8786 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8787 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
| 8788 | SourceLocation CurrentLocation, |
| 8789 | CXXConversionDecl *Conv) |
| 8790 | { |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8791 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 8792 | |
| 8793 | // Make sure that the lambda call operator is marked used. |
| 8794 | markLambdaCallOperatorUsed(*this, Lambda); |
| 8795 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8796 | Conv->setUsed(); |
| 8797 | |
| 8798 | ImplicitlyDefinedFunctionScope Scope(*this, Conv); |
| 8799 | DiagnosticErrorTrap Trap(Diags); |
| 8800 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8801 | // Return the address of the __invoke function. |
| 8802 | DeclarationName InvokeName = &Context.Idents.get("__invoke"); |
| 8803 | CXXMethodDecl *Invoke |
| 8804 | = cast<CXXMethodDecl>(*Lambda->lookup(InvokeName).first); |
| 8805 | Expr *FunctionRef = BuildDeclRefExpr(Invoke, Invoke->getType(), |
| 8806 | VK_LValue, Conv->getLocation()).take(); |
| 8807 | assert(FunctionRef && "Can't refer to __invoke function?"); |
| 8808 | Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take(); |
| 8809 | Conv->setBody(new (Context) CompoundStmt(Context, &Return, 1, |
| 8810 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8811 | Conv->getLocation())); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8812 | |
| 8813 | // Fill in the __invoke function with a dummy implementation. IR generation |
| 8814 | // will fill in the actual details. |
| 8815 | Invoke->setUsed(); |
| 8816 | Invoke->setReferenced(); |
| 8817 | Invoke->setBody(new (Context) CompoundStmt(Context, 0, 0, Conv->getLocation(), |
| 8818 | Conv->getLocation())); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8819 | |
| 8820 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8821 | L->CompletedImplicitDefinition(Conv); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8822 | L->CompletedImplicitDefinition(Invoke); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8823 | } |
| 8824 | } |
| 8825 | |
| 8826 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 8827 | SourceLocation CurrentLocation, |
| 8828 | CXXConversionDecl *Conv) |
| 8829 | { |
| 8830 | Conv->setUsed(); |
| 8831 | |
| 8832 | ImplicitlyDefinedFunctionScope Scope(*this, Conv); |
| 8833 | DiagnosticErrorTrap Trap(Diags); |
| 8834 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8835 | // Copy-initialize the lambda object as needed to capture it. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8836 | Expr *This = ActOnCXXThis(CurrentLocation).take(); |
| 8837 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).take(); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8838 | |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 8839 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 8840 | Conv->getLocation(), |
| 8841 | Conv, DerefThis); |
| 8842 | |
| 8843 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 8844 | // behavior. Note that only the general conversion function does this |
| 8845 | // (since it's unusable otherwise); in the case where we inline the |
| 8846 | // block literal, it has block literal lifetime semantics. |
| 8847 | if (!BuildBlock.isInvalid() && !getLangOptions().ObjCAutoRefCount) |
| 8848 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 8849 | CK_CopyAndAutoreleaseBlockObject, |
| 8850 | BuildBlock.get(), 0, VK_RValue); |
| 8851 | |
| 8852 | if (BuildBlock.isInvalid()) { |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8853 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8854 | Conv->setInvalidDecl(); |
| 8855 | return; |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8856 | } |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8857 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8858 | // Create the return statement that returns the block from the conversion |
| 8859 | // function. |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 8860 | StmtResult Return = ActOnReturnStmt(Conv->getLocation(), BuildBlock.get()); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8861 | if (Return.isInvalid()) { |
| 8862 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 8863 | Conv->setInvalidDecl(); |
| 8864 | return; |
| 8865 | } |
| 8866 | |
| 8867 | // Set the body of the conversion function. |
| 8868 | Stmt *ReturnS = Return.take(); |
| 8869 | Conv->setBody(new (Context) CompoundStmt(Context, &ReturnS, 1, |
| 8870 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8871 | Conv->getLocation())); |
| 8872 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8873 | // We're done; notify the mutation listener, if any. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8874 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8875 | L->CompletedImplicitDefinition(Conv); |
| 8876 | } |
| 8877 | } |
| 8878 | |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 8879 | /// \brief Determine whether the given list arguments contains exactly one |
| 8880 | /// "real" (non-default) argument. |
| 8881 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 8882 | switch (Args.size()) { |
| 8883 | case 0: |
| 8884 | return false; |
| 8885 | |
| 8886 | default: |
| 8887 | if (!Args.get()[1]->isDefaultArgument()) |
| 8888 | return false; |
| 8889 | |
| 8890 | // fall through |
| 8891 | case 1: |
| 8892 | return !Args.get()[0]->isDefaultArgument(); |
| 8893 | } |
| 8894 | |
| 8895 | return false; |
| 8896 | } |
| 8897 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8898 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 8899 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8900 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8901 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8902 | bool HadMultipleCandidates, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8903 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8904 | unsigned ConstructKind, |
| 8905 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 8906 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8907 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 8908 | // C++0x [class.copy]p34: |
| 8909 | // When certain criteria are met, an implementation is allowed to |
| 8910 | // omit the copy/move construction of a class object, even if the |
| 8911 | // copy/move constructor and/or destructor for the object have |
| 8912 | // side effects. [...] |
| 8913 | // - when a temporary class object that has not been bound to a |
| 8914 | // reference (12.2) would be copied/moved to a class object |
| 8915 | // with the same cv-unqualified type, the copy/move operation |
| 8916 | // can be omitted by constructing the temporary object |
| 8917 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 8918 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 8919 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 8920 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 8921 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 8922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8923 | |
| 8924 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8925 | Elidable, move(ExprArgs), HadMultipleCandidates, |
| 8926 | RequiresZeroInit, ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 8927 | } |
| 8928 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 8929 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 8930 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8931 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 8932 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 8933 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8934 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8935 | bool HadMultipleCandidates, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8936 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8937 | unsigned ConstructKind, |
| 8938 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 8939 | unsigned NumExprs = ExprArgs.size(); |
| 8940 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8941 | |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 8942 | for (specific_attr_iterator<NonNullAttr> |
| 8943 | i = Constructor->specific_attr_begin<NonNullAttr>(), |
| 8944 | e = Constructor->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 8945 | const NonNullAttr *NonNull = *i; |
| 8946 | CheckNonNullArguments(NonNull, ExprArgs.get(), ConstructLoc); |
| 8947 | } |
| 8948 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 8949 | MarkFunctionReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 8950 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8951 | Constructor, Elidable, Exprs, NumExprs, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 8952 | HadMultipleCandidates, /*FIXME*/false, |
| 8953 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8954 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 8955 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 8956 | } |
| 8957 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8958 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 8959 | CXXConstructorDecl *Constructor, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8960 | MultiExprArg Exprs, |
| 8961 | bool HadMultipleCandidates) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8962 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8963 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 8964 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8965 | move(Exprs), HadMultipleCandidates, false, |
| 8966 | CXXConstructExpr::CK_Complete, SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 8967 | if (TempResult.isInvalid()) |
| 8968 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8969 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 8970 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 8971 | CheckImplicitConversions(Temp, VD->getLocation()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 8972 | MarkFunctionReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 8973 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 8974 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8975 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 8976 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 8977 | } |
| 8978 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 8979 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8980 | if (VD->isInvalidDecl()) return; |
| 8981 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 8982 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8983 | if (ClassDecl->isInvalidDecl()) return; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 8984 | if (ClassDecl->hasIrrelevantDestructor()) return; |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8985 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 8986 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8987 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 8988 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8989 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 8990 | PDiag(diag::err_access_dtor_var) |
| 8991 | << VD->getDeclName() |
| 8992 | << VD->getType()); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 8993 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 8994 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 8995 | if (!VD->hasGlobalStorage()) return; |
| 8996 | |
| 8997 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 8998 | // class-static, function-static). |
| 8999 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 9000 | |
| 9001 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 9002 | if (!VD->isStaticLocal()) |
| 9003 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9004 | } |
| 9005 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9006 | /// \brief Given a constructor and the set of arguments provided for the |
| 9007 | /// constructor, convert the arguments and add any required default arguments |
| 9008 | /// to form a proper call to this constructor. |
| 9009 | /// |
| 9010 | /// \returns true if an error occurred, false otherwise. |
| 9011 | bool |
| 9012 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 9013 | MultiExprArg ArgsPtr, |
| 9014 | SourceLocation Loc, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9015 | ASTOwningVector<Expr*> &ConvertedArgs, |
| 9016 | bool AllowExplicit) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9017 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 9018 | unsigned NumArgs = ArgsPtr.size(); |
| 9019 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 9020 | |
| 9021 | const FunctionProtoType *Proto |
| 9022 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 9023 | assert(Proto && "Constructor without a prototype?"); |
| 9024 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9025 | |
| 9026 | // 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] | 9027 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9028 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9029 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9030 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9031 | |
| 9032 | VariadicCallType CallType = |
| 9033 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9034 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9035 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 9036 | Proto, 0, Args, NumArgs, AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9037 | CallType, AllowExplicit); |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 9038 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 9039 | |
| 9040 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs.data(), AllArgs.size()); |
| 9041 | |
| 9042 | // FIXME: Missing call to CheckFunctionCall or equivalent |
| 9043 | |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9044 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 9045 | } |
| 9046 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9047 | static inline bool |
| 9048 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 9049 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 9050 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9051 | if (isa<NamespaceDecl>(DC)) { |
| 9052 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9053 | diag::err_operator_new_delete_declared_in_namespace) |
| 9054 | << FnDecl->getDeclName(); |
| 9055 | } |
| 9056 | |
| 9057 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9058 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9059 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9060 | diag::err_operator_new_delete_declared_static) |
| 9061 | << FnDecl->getDeclName(); |
| 9062 | } |
| 9063 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 9064 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9065 | } |
| 9066 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9067 | static inline bool |
| 9068 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 9069 | CanQualType ExpectedResultType, |
| 9070 | CanQualType ExpectedFirstParamType, |
| 9071 | unsigned DependentParamTypeDiag, |
| 9072 | unsigned InvalidParamTypeDiag) { |
| 9073 | QualType ResultType = |
| 9074 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 9075 | |
| 9076 | // Check that the result type is not dependent. |
| 9077 | if (ResultType->isDependentType()) |
| 9078 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9079 | diag::err_operator_new_delete_dependent_result_type) |
| 9080 | << FnDecl->getDeclName() << ExpectedResultType; |
| 9081 | |
| 9082 | // Check that the result type is what we expect. |
| 9083 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 9084 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9085 | diag::err_operator_new_delete_invalid_result_type) |
| 9086 | << FnDecl->getDeclName() << ExpectedResultType; |
| 9087 | |
| 9088 | // A function template must have at least 2 parameters. |
| 9089 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 9090 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9091 | diag::err_operator_new_delete_template_too_few_parameters) |
| 9092 | << FnDecl->getDeclName(); |
| 9093 | |
| 9094 | // The function decl must have at least 1 parameter. |
| 9095 | if (FnDecl->getNumParams() == 0) |
| 9096 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9097 | diag::err_operator_new_delete_too_few_parameters) |
| 9098 | << FnDecl->getDeclName(); |
| 9099 | |
| 9100 | // Check the the first parameter type is not dependent. |
| 9101 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 9102 | if (FirstParamType->isDependentType()) |
| 9103 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 9104 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 9105 | |
| 9106 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 9107 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9108 | ExpectedFirstParamType) |
| 9109 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 9110 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 9111 | |
| 9112 | return false; |
| 9113 | } |
| 9114 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9115 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9116 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9117 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9118 | // A program is ill-formed if an allocation function is declared in a |
| 9119 | // namespace scope other than global scope or declared static in global |
| 9120 | // scope. |
| 9121 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 9122 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9123 | |
| 9124 | CanQualType SizeTy = |
| 9125 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 9126 | |
| 9127 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9128 | // The return type shall be void*. The first parameter shall have type |
| 9129 | // std::size_t. |
| 9130 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 9131 | SizeTy, |
| 9132 | diag::err_operator_new_dependent_param_type, |
| 9133 | diag::err_operator_new_param_type)) |
| 9134 | return true; |
| 9135 | |
| 9136 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9137 | // The first parameter shall not have an associated default argument. |
| 9138 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9139 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9140 | diag::err_operator_new_default_arg) |
| 9141 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 9142 | |
| 9143 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9144 | } |
| 9145 | |
| 9146 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9147 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 9148 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 9149 | // A program is ill-formed if deallocation functions are declared in a |
| 9150 | // namespace scope other than global scope or declared static in global |
| 9151 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9152 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 9153 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9154 | |
| 9155 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 9156 | // Each deallocation function shall return void and its first parameter |
| 9157 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9158 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 9159 | SemaRef.Context.VoidPtrTy, |
| 9160 | diag::err_operator_delete_dependent_param_type, |
| 9161 | diag::err_operator_delete_param_type)) |
| 9162 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9163 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9164 | return false; |
| 9165 | } |
| 9166 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9167 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 9168 | /// of this overloaded operator is well-formed. If so, returns false; |
| 9169 | /// otherwise, emits appropriate diagnostics and returns true. |
| 9170 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9171 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9172 | "Expected an overloaded operator declaration"); |
| 9173 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9174 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 9175 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9176 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9177 | // The allocation and deallocation functions, operator new, |
| 9178 | // operator new[], operator delete and operator delete[], are |
| 9179 | // described completely in 3.7.3. The attributes and restrictions |
| 9180 | // found in the rest of this subclause do not apply to them unless |
| 9181 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 9182 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9183 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 9184 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9185 | if (Op == OO_New || Op == OO_Array_New) |
| 9186 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9187 | |
| 9188 | // C++ [over.oper]p6: |
| 9189 | // An operator function shall either be a non-static member |
| 9190 | // function or be a non-member function and have at least one |
| 9191 | // parameter whose type is a class, a reference to a class, an |
| 9192 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9193 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 9194 | if (MethodDecl->isStatic()) |
| 9195 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9196 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9197 | } else { |
| 9198 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9199 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 9200 | ParamEnd = FnDecl->param_end(); |
| 9201 | Param != ParamEnd; ++Param) { |
| 9202 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 9203 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 9204 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9205 | ClassOrEnumParam = true; |
| 9206 | break; |
| 9207 | } |
| 9208 | } |
| 9209 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9210 | if (!ClassOrEnumParam) |
| 9211 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9212 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9213 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9214 | } |
| 9215 | |
| 9216 | // C++ [over.oper]p8: |
| 9217 | // An operator function cannot have default arguments (8.3.6), |
| 9218 | // except where explicitly stated below. |
| 9219 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9220 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9221 | // (C++ [over.call]p1). |
| 9222 | if (Op != OO_Call) { |
| 9223 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 9224 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9225 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9226 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 9227 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9228 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9229 | } |
| 9230 | } |
| 9231 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9232 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 9233 | { false, false, false } |
| 9234 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 9235 | , { Unary, Binary, MemberOnly } |
| 9236 | #include "clang/Basic/OperatorKinds.def" |
| 9237 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9238 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9239 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 9240 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 9241 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9242 | |
| 9243 | // C++ [over.oper]p8: |
| 9244 | // [...] Operator functions cannot have more or fewer parameters |
| 9245 | // than the number required for the corresponding operator, as |
| 9246 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9247 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9248 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9249 | if (Op != OO_Call && |
| 9250 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 9251 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 9252 | (NumParams < 1) || (NumParams > 2))) { |
| 9253 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9254 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9255 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9256 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9257 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9258 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9259 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 9260 | assert(CanBeBinaryOperator && |
| 9261 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9262 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9263 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9264 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9265 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9266 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9267 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 9268 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9269 | // Overloaded operators other than operator() cannot be variadic. |
| 9270 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9271 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9272 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9273 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9274 | } |
| 9275 | |
| 9276 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9277 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 9278 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9279 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9280 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9281 | } |
| 9282 | |
| 9283 | // C++ [over.inc]p1: |
| 9284 | // The user-defined function called operator++ implements the |
| 9285 | // prefix and postfix ++ operator. If this function is a member |
| 9286 | // function with no parameters, or a non-member function with one |
| 9287 | // parameter of class or enumeration type, it defines the prefix |
| 9288 | // increment operator ++ for objects of that type. If the function |
| 9289 | // is a member function with one parameter (which shall be of type |
| 9290 | // int) or a non-member function with two parameters (the second |
| 9291 | // of which shall be of type int), it defines the postfix |
| 9292 | // increment operator ++ for objects of that type. |
| 9293 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 9294 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 9295 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9296 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9297 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 9298 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 9299 | if (!ParamIsInt) |
| 9300 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9301 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 9302 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9303 | } |
| 9304 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9305 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9306 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 9307 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9308 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 9309 | /// of this literal operator function is well-formed. If so, returns |
| 9310 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 9311 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
Richard Smith | e5658f0 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 9312 | if (isa<CXXMethodDecl>(FnDecl)) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9313 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 9314 | << FnDecl->getDeclName(); |
| 9315 | return true; |
| 9316 | } |
| 9317 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9318 | if (FnDecl->isExternC()) { |
| 9319 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 9320 | return true; |
| 9321 | } |
| 9322 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9323 | bool Valid = false; |
| 9324 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 9325 | // This might be the definition of a literal operator template. |
| 9326 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 9327 | // This might be a specialization of a literal operator template. |
| 9328 | if (!TpDecl) |
| 9329 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 9330 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9331 | // template <char...> type operator "" name() is the only valid template |
| 9332 | // signature, and the only valid signature with no parameters. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 9333 | if (TpDecl) { |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9334 | if (FnDecl->param_size() == 0) { |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9335 | // Must have only one template parameter |
| 9336 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 9337 | if (Params->size() == 1) { |
| 9338 | NonTypeTemplateParmDecl *PmDecl = |
| 9339 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9340 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9341 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9342 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 9343 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 9344 | Valid = true; |
| 9345 | } |
| 9346 | } |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9347 | } else if (FnDecl->param_size()) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9348 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9349 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 9350 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9351 | QualType T = (*Param)->getType().getUnqualifiedType(); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9352 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9353 | // unsigned long long int, long double, and any character type are allowed |
| 9354 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9355 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 9356 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 9357 | Context.hasSameType(T, Context.CharTy) || |
| 9358 | Context.hasSameType(T, Context.WCharTy) || |
| 9359 | Context.hasSameType(T, Context.Char16Ty) || |
| 9360 | Context.hasSameType(T, Context.Char32Ty)) { |
| 9361 | if (++Param == FnDecl->param_end()) |
| 9362 | Valid = true; |
| 9363 | goto FinishedParams; |
| 9364 | } |
| 9365 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9366 | // 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] | 9367 | const PointerType *PT = T->getAs<PointerType>(); |
| 9368 | if (!PT) |
| 9369 | goto FinishedParams; |
| 9370 | T = PT->getPointeeType(); |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9371 | if (!T.isConstQualified() || T.isVolatileQualified()) |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9372 | goto FinishedParams; |
| 9373 | T = T.getUnqualifiedType(); |
| 9374 | |
| 9375 | // Move on to the second parameter; |
| 9376 | ++Param; |
| 9377 | |
| 9378 | // If there is no second parameter, the first must be a const char * |
| 9379 | if (Param == FnDecl->param_end()) { |
| 9380 | if (Context.hasSameType(T, Context.CharTy)) |
| 9381 | Valid = true; |
| 9382 | goto FinishedParams; |
| 9383 | } |
| 9384 | |
| 9385 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 9386 | // are allowed as the first parameter to a two-parameter function |
| 9387 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 9388 | Context.hasSameType(T, Context.WCharTy) || |
| 9389 | Context.hasSameType(T, Context.Char16Ty) || |
| 9390 | Context.hasSameType(T, Context.Char32Ty))) |
| 9391 | goto FinishedParams; |
| 9392 | |
| 9393 | // The second and final parameter must be an std::size_t |
| 9394 | T = (*Param)->getType().getUnqualifiedType(); |
| 9395 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 9396 | ++Param == FnDecl->param_end()) |
| 9397 | Valid = true; |
| 9398 | } |
| 9399 | |
| 9400 | // FIXME: This diagnostic is absolutely terrible. |
| 9401 | FinishedParams: |
| 9402 | if (!Valid) { |
| 9403 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 9404 | << FnDecl->getDeclName(); |
| 9405 | return true; |
| 9406 | } |
| 9407 | |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 9408 | // A parameter-declaration-clause containing a default argument is not |
| 9409 | // equivalent to any of the permitted forms. |
| 9410 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 9411 | ParamEnd = FnDecl->param_end(); |
| 9412 | Param != ParamEnd; ++Param) { |
| 9413 | if ((*Param)->hasDefaultArg()) { |
| 9414 | Diag((*Param)->getDefaultArgRange().getBegin(), |
| 9415 | diag::err_literal_operator_default_argument) |
| 9416 | << (*Param)->getDefaultArgRange(); |
| 9417 | break; |
| 9418 | } |
| 9419 | } |
| 9420 | |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9421 | StringRef LiteralName |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 9422 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 9423 | if (LiteralName[0] != '_') { |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9424 | // C++11 [usrlit.suffix]p1: |
| 9425 | // Literal suffix identifiers that do not start with an underscore |
| 9426 | // are reserved for future standardization. |
| 9427 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved); |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 9428 | } |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9429 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9430 | return false; |
| 9431 | } |
| 9432 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9433 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 9434 | /// linkage specification, including the language and (if present) |
| 9435 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 9436 | /// the location of the language string literal, which is provided |
| 9437 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 9438 | /// the '{' brace. Otherwise, this linkage specification does not |
| 9439 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 9440 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 9441 | SourceLocation LangLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9442 | StringRef Lang, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 9443 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9444 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 9445 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9446 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 9447 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9448 | Language = LinkageSpecDecl::lang_cxx; |
| 9449 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9450 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9451 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9453 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9454 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9455 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9456 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9457 | ExternLoc, LangLoc, Language); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9458 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9459 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9460 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9461 | } |
| 9462 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 9463 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9464 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 9465 | /// valid, it's the position of the closing '}' brace in a linkage |
| 9466 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9467 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 9468 | Decl *LinkageSpec, |
| 9469 | SourceLocation RBraceLoc) { |
| 9470 | if (LinkageSpec) { |
| 9471 | if (RBraceLoc.isValid()) { |
| 9472 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 9473 | LSDecl->setRBraceLoc(RBraceLoc); |
| 9474 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9475 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 9476 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9477 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 9478 | } |
| 9479 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9480 | /// \brief Perform semantic analysis for the variable declaration that |
| 9481 | /// occurs within a C++ catch clause, returning the newly-created |
| 9482 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9483 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9484 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9485 | SourceLocation StartLoc, |
| 9486 | SourceLocation Loc, |
| 9487 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9488 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9489 | QualType ExDeclType = TInfo->getType(); |
| 9490 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9491 | // Arrays and functions decay. |
| 9492 | if (ExDeclType->isArrayType()) |
| 9493 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 9494 | else if (ExDeclType->isFunctionType()) |
| 9495 | ExDeclType = Context.getPointerType(ExDeclType); |
| 9496 | |
| 9497 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 9498 | // The exception-declaration shall not denote a pointer or reference to an |
| 9499 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9500 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9501 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9502 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9503 | Invalid = true; |
| 9504 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9505 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9506 | QualType BaseType = ExDeclType; |
| 9507 | 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] | 9508 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 9509 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9510 | BaseType = Ptr->getPointeeType(); |
| 9511 | Mode = 1; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 9512 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9513 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9514 | // 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] | 9515 | BaseType = Ref->getPointeeType(); |
| 9516 | Mode = 2; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 9517 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9518 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9519 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 9520 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9521 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9522 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9523 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9524 | RequireNonAbstractType(Loc, ExDeclType, |
| 9525 | diag::err_abstract_type_in_decl, |
| 9526 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 9527 | Invalid = true; |
| 9528 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 9529 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 9530 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 9531 | if (!Invalid && getLangOptions().ObjC1) { |
| 9532 | QualType T = ExDeclType; |
| 9533 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 9534 | T = RT->getPointeeType(); |
| 9535 | |
| 9536 | if (T->isObjCObjectType()) { |
| 9537 | Diag(Loc, diag::err_objc_object_catch); |
| 9538 | Invalid = true; |
| 9539 | } else if (T->isObjCObjectPointerType()) { |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 9540 | if (!getLangOptions().ObjCNonFragileABI) |
| 9541 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 9542 | } |
| 9543 | } |
| 9544 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9545 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 9546 | ExDeclType, TInfo, SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 9547 | ExDecl->setExceptionVariable(true); |
| 9548 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 9549 | // In ARC, infer 'retaining' for variables of retainable type. |
| 9550 | if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
| 9551 | Invalid = true; |
| 9552 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 9553 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9554 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9555 | // C++ [except.handle]p16: |
| 9556 | // The object declared in an exception-declaration or, if the |
| 9557 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 9558 | // copy-initialized (8.5) from the exception object. [...] |
| 9559 | // The object is destroyed when the handler exits, after the destruction |
| 9560 | // of any automatic objects initialized within the handler. |
| 9561 | // |
| 9562 | // We just pretend to initialize the object with itself, then make sure |
| 9563 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9564 | QualType initType = ExDeclType; |
| 9565 | |
| 9566 | InitializedEntity entity = |
| 9567 | InitializedEntity::InitializeVariable(ExDecl); |
| 9568 | InitializationKind initKind = |
| 9569 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 9570 | |
| 9571 | Expr *opaqueValue = |
| 9572 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 9573 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 9574 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 9575 | MultiExprArg(&opaqueValue, 1)); |
| 9576 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9577 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9578 | else { |
| 9579 | // If the constructor used was non-trivial, set this as the |
| 9580 | // "initializer". |
| 9581 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 9582 | if (!construct->getConstructor()->isTrivial()) { |
| 9583 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 9584 | ExDecl->setInit(init); |
| 9585 | } |
| 9586 | |
| 9587 | // And make sure it's destructable. |
| 9588 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 9589 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9590 | } |
| 9591 | } |
| 9592 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9593 | if (Invalid) |
| 9594 | ExDecl->setInvalidDecl(); |
| 9595 | |
| 9596 | return ExDecl; |
| 9597 | } |
| 9598 | |
| 9599 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 9600 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9601 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9602 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 9603 | bool Invalid = D.isInvalidType(); |
| 9604 | |
| 9605 | // Check for unexpanded parameter packs. |
| 9606 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 9607 | UPPC_ExceptionType)) { |
| 9608 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 9609 | D.getIdentifierLoc()); |
| 9610 | Invalid = true; |
| 9611 | } |
| 9612 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9613 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 9614 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 9615 | LookupOrdinaryName, |
| 9616 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9617 | // The scope should be freshly made just for us. There is just no way |
| 9618 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9619 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9620 | if (PrevDecl->isTemplateParameter()) { |
| 9621 | // Maybe we will complain about the shadowed template parameter. |
| 9622 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 9623 | PrevDecl = 0; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9624 | } |
| 9625 | } |
| 9626 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9627 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9628 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 9629 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9630 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9631 | } |
| 9632 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9633 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9634 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9635 | D.getIdentifierLoc(), |
| 9636 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9637 | if (Invalid) |
| 9638 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9639 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9640 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9641 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9642 | PushOnScopeChains(ExDecl, S); |
| 9643 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9644 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9645 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9646 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9647 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9648 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9649 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9650 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9651 | Expr *AssertExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9652 | Expr *AssertMessageExpr_, |
| 9653 | SourceLocation RParenLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9654 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9655 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9656 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9657 | // In a static_assert-declaration, the constant-expression shall be a |
| 9658 | // constant expression that can be contextually converted to bool. |
| 9659 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 9660 | if (Converted.isInvalid()) |
| 9661 | return 0; |
| 9662 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9663 | llvm::APSInt Cond; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9664 | if (VerifyIntegerConstantExpression(Converted.get(), &Cond, |
| 9665 | PDiag(diag::err_static_assert_expression_is_not_constant), |
| 9666 | /*AllowFold=*/false).isInvalid()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9667 | return 0; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9668 | |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 9669 | if (!Cond) { |
| 9670 | llvm::SmallString<256> MsgBuffer; |
| 9671 | llvm::raw_svector_ostream Msg(MsgBuffer); |
| 9672 | AssertMessage->printPretty(Msg, Context, 0, getPrintingPolicy()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9673 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 9674 | << Msg.str() << AssertExpr->getSourceRange(); |
| 9675 | } |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9676 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9677 | |
Douglas Gregor | 399ad97 | 2010-12-15 23:55:21 +0000 | [diff] [blame] | 9678 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 9679 | return 0; |
| 9680 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9681 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
| 9682 | AssertExpr, AssertMessage, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9683 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9684 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9685 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9686 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 9687 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9688 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 9689 | /// |
| 9690 | /// \returns A friend declaration that. |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 9691 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation Loc, |
| 9692 | SourceLocation FriendLoc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9693 | TypeSourceInfo *TSInfo) { |
| 9694 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 9695 | |
| 9696 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9697 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9698 | |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 9699 | // C++03 [class.friend]p2: |
| 9700 | // An elaborated-type-specifier shall be used in a friend declaration |
| 9701 | // for a class.* |
| 9702 | // |
| 9703 | // * The class-key of the elaborated-type-specifier is required. |
| 9704 | if (!ActiveTemplateInstantiations.empty()) { |
| 9705 | // Do not complain about the form of friend template types during |
| 9706 | // template instantiation; we will already have complained when the |
| 9707 | // template was declared. |
| 9708 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 9709 | // If we evaluated the type to a record type, suggest putting |
| 9710 | // a tag in front. |
| 9711 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 9712 | RecordDecl *RD = RT->getDecl(); |
| 9713 | |
| 9714 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 9715 | |
| 9716 | Diag(TypeRange.getBegin(), |
| 9717 | getLangOptions().CPlusPlus0x ? |
| 9718 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 9719 | diag::ext_unelaborated_friend_type) |
| 9720 | << (unsigned) RD->getTagKind() |
| 9721 | << T |
| 9722 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 9723 | InsertionText); |
| 9724 | } else { |
| 9725 | Diag(FriendLoc, |
| 9726 | getLangOptions().CPlusPlus0x ? |
| 9727 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 9728 | diag::ext_nonclass_type_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9729 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9730 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9731 | } |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 9732 | } else if (T->getAs<EnumType>()) { |
| 9733 | Diag(FriendLoc, |
| 9734 | getLangOptions().CPlusPlus0x ? |
| 9735 | diag::warn_cxx98_compat_enum_friend : |
| 9736 | diag::ext_enum_friend) |
| 9737 | << T |
| 9738 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9739 | } |
| 9740 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 9741 | // C++0x [class.friend]p3: |
| 9742 | // If the type specifier in a friend declaration designates a (possibly |
| 9743 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 9744 | // the friend declaration is ignored. |
| 9745 | |
| 9746 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 9747 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9748 | |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 9749 | return FriendDecl::Create(Context, CurContext, Loc, TSInfo, FriendLoc); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9750 | } |
| 9751 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9752 | /// Handle a friend tag declaration where the scope specifier was |
| 9753 | /// templated. |
| 9754 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 9755 | unsigned TagSpec, SourceLocation TagLoc, |
| 9756 | CXXScopeSpec &SS, |
| 9757 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 9758 | AttributeList *Attr, |
| 9759 | MultiTemplateParamsArg TempParamLists) { |
| 9760 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 9761 | |
| 9762 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9763 | bool Invalid = false; |
| 9764 | |
| 9765 | if (TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 9766 | = MatchTemplateParametersToScopeSpecifier(TagLoc, NameLoc, SS, |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9767 | TempParamLists.get(), |
| 9768 | TempParamLists.size(), |
| 9769 | /*friend*/ true, |
| 9770 | isExplicitSpecialization, |
| 9771 | Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9772 | if (TemplateParams->size() > 0) { |
| 9773 | // This is a declaration of a class template. |
| 9774 | if (Invalid) |
| 9775 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 9776 | |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 9777 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 9778 | SS, Name, NameLoc, Attr, |
| 9779 | TemplateParams, AS_public, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 9780 | /*ModulePrivateLoc=*/SourceLocation(), |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 9781 | TempParamLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 9782 | (TemplateParameterList**) TempParamLists.release()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9783 | } else { |
| 9784 | // The "template<>" header is extraneous. |
| 9785 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 9786 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 9787 | isExplicitSpecialization = true; |
| 9788 | } |
| 9789 | } |
| 9790 | |
| 9791 | if (Invalid) return 0; |
| 9792 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9793 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 9794 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9795 | if (TempParamLists.get()[I]->size()) { |
| 9796 | isAllExplicitSpecializations = false; |
| 9797 | break; |
| 9798 | } |
| 9799 | } |
| 9800 | |
| 9801 | // FIXME: don't ignore attributes. |
| 9802 | |
| 9803 | // If it's explicit specializations all the way down, just forget |
| 9804 | // about the template header and build an appropriate non-templated |
| 9805 | // friend. TODO: for source fidelity, remember the headers. |
| 9806 | if (isAllExplicitSpecializations) { |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 9807 | if (SS.isEmpty()) { |
| 9808 | bool Owned = false; |
| 9809 | bool IsDependent = false; |
| 9810 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
| 9811 | Attr, AS_public, |
| 9812 | /*ModulePrivateLoc=*/SourceLocation(), |
| 9813 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 9814 | /*ScopedEnumKWLoc=*/SourceLocation(), |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 9815 | /*ScopedEnumUsesClassTag=*/false, |
| 9816 | /*UnderlyingType=*/TypeResult()); |
| 9817 | } |
| 9818 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9819 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9820 | ElaboratedTypeKeyword Keyword |
| 9821 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9822 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9823 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9824 | if (T.isNull()) |
| 9825 | return 0; |
| 9826 | |
| 9827 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9828 | if (isa<DependentNameType>(T)) { |
| 9829 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9830 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9831 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9832 | TL.setNameLoc(NameLoc); |
| 9833 | } else { |
| 9834 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9835 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9836 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9837 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 9838 | } |
| 9839 | |
| 9840 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 9841 | TSI, FriendLoc); |
| 9842 | Friend->setAccess(AS_public); |
| 9843 | CurContext->addDecl(Friend); |
| 9844 | return Friend; |
| 9845 | } |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 9846 | |
| 9847 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 9848 | |
| 9849 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9850 | |
| 9851 | // Handle the case of a templated-scope friend class. e.g. |
| 9852 | // template <class T> class A<T>::B; |
| 9853 | // FIXME: we don't support these right now. |
| 9854 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 9855 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 9856 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9857 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9858 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9859 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9860 | TL.setNameLoc(NameLoc); |
| 9861 | |
| 9862 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 9863 | TSI, FriendLoc); |
| 9864 | Friend->setAccess(AS_public); |
| 9865 | Friend->setUnsupportedFriend(true); |
| 9866 | CurContext->addDecl(Friend); |
| 9867 | return Friend; |
| 9868 | } |
| 9869 | |
| 9870 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9871 | /// Handle a friend type declaration. This works in tandem with |
| 9872 | /// ActOnTag. |
| 9873 | /// |
| 9874 | /// Notes on friend class templates: |
| 9875 | /// |
| 9876 | /// We generally treat friend class declarations as if they were |
| 9877 | /// declaring a class. So, for example, the elaborated type specifier |
| 9878 | /// in a friend declaration is required to obey the restrictions of a |
| 9879 | /// class-head (i.e. no typedefs in the scope chain), template |
| 9880 | /// parameters are required to match up with simple template-ids, &c. |
| 9881 | /// However, unlike when declaring a template specialization, it's |
| 9882 | /// okay to refer to a template specialization without an empty |
| 9883 | /// template parameter declaration, e.g. |
| 9884 | /// friend class A<T>::B<unsigned>; |
| 9885 | /// We permit this as a special case; if there are any template |
| 9886 | /// parameters present at all, require proper matching, i.e. |
| 9887 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9888 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 9889 | MultiTemplateParamsArg TempParams) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9890 | SourceLocation Loc = DS.getLocStart(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9891 | |
| 9892 | assert(DS.isFriendSpecified()); |
| 9893 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 9894 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9895 | // Try to convert the decl specifier to a type. This works for |
| 9896 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 9897 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 9898 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9899 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 9900 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 9901 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9902 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9903 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 9904 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 9905 | return 0; |
| 9906 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9907 | // This is definitely an error in C++98. It's probably meant to |
| 9908 | // be forbidden in C++0x, too, but the specification is just |
| 9909 | // poorly written. |
| 9910 | // |
| 9911 | // The problem is with declarations like the following: |
| 9912 | // template <T> friend A<T>::foo; |
| 9913 | // where deciding whether a class C is a friend or not now hinges |
| 9914 | // on whether there exists an instantiation of A that causes |
| 9915 | // 'foo' to equal C. There are restrictions on class-heads |
| 9916 | // (which we declare (by fiat) elaborated friend declarations to |
| 9917 | // be) that makes this tractable. |
| 9918 | // |
| 9919 | // FIXME: handle "template <> friend class A<T>;", which |
| 9920 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 9921 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9922 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 9923 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9924 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9925 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9926 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9927 | // C++98 [class.friend]p1: A friend of a class is a function |
| 9928 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 9929 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 9930 | // deadline. It's also a very silly restriction that seriously |
| 9931 | // affects inner classes and which nobody else seems to implement; |
| 9932 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 9933 | // |
| 9934 | // But note that we could warn about it: it's always useless to |
| 9935 | // friend one of your own members (it's not, however, worthless to |
| 9936 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9937 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9938 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9939 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9940 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9941 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 9942 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 9943 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9944 | DS.getFriendSpecLoc()); |
| 9945 | else |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 9946 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9947 | |
| 9948 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9949 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9950 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9951 | D->setAccess(AS_public); |
| 9952 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9953 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9954 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9955 | } |
| 9956 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 9957 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 9958 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9959 | const DeclSpec &DS = D.getDeclSpec(); |
| 9960 | |
| 9961 | assert(DS.isFriendSpecified()); |
| 9962 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 9963 | |
| 9964 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9965 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9966 | |
| 9967 | // C++ [class.friend]p1 |
| 9968 | // A friend of a class is a function or class.... |
| 9969 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 9970 | // It *doesn't* see through dependent types, which is correct |
| 9971 | // according to [temp.arg.type]p3: |
| 9972 | // If a declaration acquires a function type through a |
| 9973 | // type dependent on a template-parameter and this causes |
| 9974 | // a declaration that does not use the syntactic form of a |
| 9975 | // function declarator to have a function type, the program |
| 9976 | // is ill-formed. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 9977 | if (!TInfo->getType()->isFunctionType()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9978 | Diag(Loc, diag::err_unexpected_friend); |
| 9979 | |
| 9980 | // It might be worthwhile to try to recover by creating an |
| 9981 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9982 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9983 | } |
| 9984 | |
| 9985 | // C++ [namespace.memdef]p3 |
| 9986 | // - If a friend declaration in a non-local class first declares a |
| 9987 | // class or function, the friend class or function is a member |
| 9988 | // of the innermost enclosing namespace. |
| 9989 | // - The name of the friend is not found by simple name lookup |
| 9990 | // until a matching declaration is provided in that namespace |
| 9991 | // scope (either before or after the class declaration granting |
| 9992 | // friendship). |
| 9993 | // - If a friend function is called, its name may be found by the |
| 9994 | // name lookup that considers functions from namespaces and |
| 9995 | // classes associated with the types of the function arguments. |
| 9996 | // - When looking for a prior declaration of a class or a function |
| 9997 | // declared as a friend, scopes outside the innermost enclosing |
| 9998 | // namespace scope are not considered. |
| 9999 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10000 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10001 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 10002 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10003 | assert(Name); |
| 10004 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 10005 | // Check for unexpanded parameter packs. |
| 10006 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 10007 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 10008 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 10009 | return 0; |
| 10010 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10011 | // The context we found the declaration in, or in which we should |
| 10012 | // create the declaration. |
| 10013 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 10014 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10015 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10016 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10017 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10018 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10019 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10020 | // There are four cases here. |
| 10021 | // - 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] | 10022 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10023 | // there as appropriate. |
| 10024 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 10025 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10026 | // C++0x [namespace.memdef]p3: |
| 10027 | // If the name in a friend declaration is neither qualified nor |
| 10028 | // a template-id and the declaration is a function or an |
| 10029 | // elaborated-type-specifier, the lookup to determine whether |
| 10030 | // the entity has been previously declared shall not consider |
| 10031 | // any scopes outside the innermost enclosing namespace. |
| 10032 | // C++0x [class.friend]p11: |
| 10033 | // If a friend declaration appears in a local class and the name |
| 10034 | // specified is an unqualified name, a prior declaration is |
| 10035 | // looked up without considering scopes that are outside the |
| 10036 | // innermost enclosing non-class scope. For a friend function |
| 10037 | // declaration, if there is no prior declaration, the program is |
| 10038 | // ill-formed. |
| 10039 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 10040 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10041 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10042 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10043 | DC = CurContext; |
| 10044 | while (true) { |
| 10045 | // Skip class contexts. If someone can cite chapter and verse |
| 10046 | // for this behavior, that would be nice --- it's what GCC and |
| 10047 | // EDG do, and it seems like a reasonable intent, but the spec |
| 10048 | // really only says that checks for unqualified existing |
| 10049 | // declarations should stop at the nearest enclosing namespace, |
| 10050 | // not that they should only consider the nearest enclosing |
| 10051 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10052 | while (DC->isRecord()) |
| 10053 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10054 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10055 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10056 | |
| 10057 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10058 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10059 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10060 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 10061 | if (isTemplateId) { |
| 10062 | if (isa<TranslationUnitDecl>(DC)) break; |
| 10063 | } else { |
| 10064 | if (DC->isFileContext()) break; |
| 10065 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10066 | DC = DC->getParent(); |
| 10067 | } |
| 10068 | |
| 10069 | // C++ [class.friend]p1: A friend of a class is a function or |
| 10070 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10071 | // C++11 changes this for both friend types and functions. |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 10072 | // Most C++ 98 compilers do seem to give an error here, so |
| 10073 | // we do, too. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10074 | if (!Previous.empty() && DC->Equals(CurContext)) |
| 10075 | Diag(DS.getFriendSpecLoc(), |
| 10076 | getLangOptions().CPlusPlus0x ? |
| 10077 | diag::warn_cxx98_compat_friend_is_member : |
| 10078 | diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10079 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 10080 | DCScope = getScopeForDeclContext(S, DC); |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 10081 | |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10082 | // C++ [class.friend]p6: |
| 10083 | // A function can be defined in a friend declaration of a class if and |
| 10084 | // only if the class is a non-local class (9.8), the function name is |
| 10085 | // unqualified, and the function has namespace scope. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10086 | if (isLocal && D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10087 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 10088 | } |
| 10089 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10090 | // - There's a non-dependent scope specifier, in which case we |
| 10091 | // compute it and do a previous lookup there for a function |
| 10092 | // or function template. |
| 10093 | } else if (!SS.getScopeRep()->isDependent()) { |
| 10094 | DC = computeDeclContext(SS); |
| 10095 | if (!DC) return 0; |
| 10096 | |
| 10097 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 10098 | |
| 10099 | LookupQualifiedName(Previous, DC); |
| 10100 | |
| 10101 | // Ignore things found implicitly in the wrong scope. |
| 10102 | // TODO: better diagnostics for this case. Suggesting the right |
| 10103 | // qualified scope would be nice... |
| 10104 | LookupResult::Filter F = Previous.makeFilter(); |
| 10105 | while (F.hasNext()) { |
| 10106 | NamedDecl *D = F.next(); |
| 10107 | if (!DC->InEnclosingNamespaceSetOf( |
| 10108 | D->getDeclContext()->getRedeclContext())) |
| 10109 | F.erase(); |
| 10110 | } |
| 10111 | F.done(); |
| 10112 | |
| 10113 | if (Previous.empty()) { |
| 10114 | D.setInvalidType(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10115 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 10116 | << Name << TInfo->getType(); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10117 | return 0; |
| 10118 | } |
| 10119 | |
| 10120 | // C++ [class.friend]p1: A friend of a class is a function or |
| 10121 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10122 | if (DC->Equals(CurContext)) |
| 10123 | Diag(DS.getFriendSpecLoc(), |
| 10124 | getLangOptions().CPlusPlus0x ? |
| 10125 | diag::warn_cxx98_compat_friend_is_member : |
| 10126 | diag::err_friend_is_member); |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10127 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10128 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10129 | // C++ [class.friend]p6: |
| 10130 | // A function can be defined in a friend declaration of a class if and |
| 10131 | // only if the class is a non-local class (9.8), the function name is |
| 10132 | // unqualified, and the function has namespace scope. |
| 10133 | SemaDiagnosticBuilder DB |
| 10134 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 10135 | |
| 10136 | DB << SS.getScopeRep(); |
| 10137 | if (DC->isFileContext()) |
| 10138 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 10139 | SS.clear(); |
| 10140 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10141 | |
| 10142 | // - There's a scope specifier that does not match any template |
| 10143 | // parameter lists, in which case we use some arbitrary context, |
| 10144 | // create a method or method template, and wait for instantiation. |
| 10145 | // - There's a scope specifier that does match some template |
| 10146 | // parameter lists, which we don't handle right now. |
| 10147 | } else { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10148 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10149 | // C++ [class.friend]p6: |
| 10150 | // A function can be defined in a friend declaration of a class if and |
| 10151 | // only if the class is a non-local class (9.8), the function name is |
| 10152 | // unqualified, and the function has namespace scope. |
| 10153 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 10154 | << SS.getScopeRep(); |
| 10155 | } |
| 10156 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10157 | DC = CurContext; |
| 10158 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10159 | } |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10160 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10161 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10162 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 10163 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 10164 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 10165 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10166 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 10167 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 10168 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10169 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10170 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10171 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10172 | |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 10173 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 10174 | // does not contain the declaration context, i.e., in an out-of-line |
| 10175 | // definition of a class. |
| 10176 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 10177 | if (!DCScope) { |
| 10178 | FakeDCScope.setEntity(DC); |
| 10179 | DCScope = &FakeDCScope; |
| 10180 | } |
| 10181 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 10182 | bool AddToScope = true; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10183 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
| 10184 | move(TemplateParams), AddToScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10185 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10186 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10187 | assert(ND->getDeclContext() == DC); |
| 10188 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 10189 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10190 | // Add the function declaration to the appropriate lookup tables, |
| 10191 | // adjusting the redeclarations list as necessary. We don't |
| 10192 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10193 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10194 | // Also update the scope-based lookup if the target context's |
| 10195 | // lookup context is in lexical scope. |
| 10196 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10197 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10198 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10199 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10200 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10201 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10202 | |
| 10203 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10204 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10205 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 10206 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10207 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10208 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10209 | if (ND->isInvalidDecl()) |
| 10210 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 10211 | else { |
| 10212 | FunctionDecl *FD; |
| 10213 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 10214 | FD = FTD->getTemplatedDecl(); |
| 10215 | else |
| 10216 | FD = cast<FunctionDecl>(ND); |
| 10217 | |
| 10218 | // Mark templated-scope function declarations as unsupported. |
| 10219 | if (FD->getNumTemplateParameterLists()) |
| 10220 | FrD->setUnsupportedFriend(true); |
| 10221 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10222 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10223 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 10224 | } |
| 10225 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10226 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 10227 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10228 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10229 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 10230 | if (!Fn) { |
| 10231 | Diag(DelLoc, diag::err_deleted_non_function); |
| 10232 | return; |
| 10233 | } |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 10234 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10235 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 10236 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 10237 | // If the declaration wasn't the first, we delete the function anyway for |
| 10238 | // recovery. |
| 10239 | } |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 10240 | Fn->setDeletedAsWritten(); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 10241 | |
| 10242 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Dcl); |
| 10243 | if (!MD) |
| 10244 | return; |
| 10245 | |
| 10246 | // A deleted special member function is trivial if the corresponding |
| 10247 | // implicitly-declared function would have been. |
| 10248 | switch (getSpecialMember(MD)) { |
| 10249 | case CXXInvalid: |
| 10250 | break; |
| 10251 | case CXXDefaultConstructor: |
| 10252 | MD->setTrivial(MD->getParent()->hasTrivialDefaultConstructor()); |
| 10253 | break; |
| 10254 | case CXXCopyConstructor: |
| 10255 | MD->setTrivial(MD->getParent()->hasTrivialCopyConstructor()); |
| 10256 | break; |
| 10257 | case CXXMoveConstructor: |
| 10258 | MD->setTrivial(MD->getParent()->hasTrivialMoveConstructor()); |
| 10259 | break; |
| 10260 | case CXXCopyAssignment: |
| 10261 | MD->setTrivial(MD->getParent()->hasTrivialCopyAssignment()); |
| 10262 | break; |
| 10263 | case CXXMoveAssignment: |
| 10264 | MD->setTrivial(MD->getParent()->hasTrivialMoveAssignment()); |
| 10265 | break; |
| 10266 | case CXXDestructor: |
| 10267 | MD->setTrivial(MD->getParent()->hasTrivialDestructor()); |
| 10268 | break; |
| 10269 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10270 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10271 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10272 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
| 10273 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Dcl); |
| 10274 | |
| 10275 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 10276 | if (MD->getParent()->isDependentType()) { |
| 10277 | MD->setDefaulted(); |
| 10278 | MD->setExplicitlyDefaulted(); |
| 10279 | return; |
| 10280 | } |
| 10281 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10282 | CXXSpecialMember Member = getSpecialMember(MD); |
| 10283 | if (Member == CXXInvalid) { |
| 10284 | Diag(DefaultLoc, diag::err_default_special_members); |
| 10285 | return; |
| 10286 | } |
| 10287 | |
| 10288 | MD->setDefaulted(); |
| 10289 | MD->setExplicitlyDefaulted(); |
| 10290 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 10291 | // If this definition appears within the record, do the checking when |
| 10292 | // the record is complete. |
| 10293 | const FunctionDecl *Primary = MD; |
| 10294 | if (MD->getTemplatedKind() != FunctionDecl::TK_NonTemplate) |
| 10295 | // Find the uninstantiated declaration that actually had the '= default' |
| 10296 | // on it. |
| 10297 | MD->getTemplateInstantiationPattern()->isDefined(Primary); |
| 10298 | |
| 10299 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10300 | return; |
| 10301 | |
| 10302 | switch (Member) { |
| 10303 | case CXXDefaultConstructor: { |
| 10304 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 10305 | CheckExplicitlyDefaultedDefaultConstructor(CD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10306 | if (!CD->isInvalidDecl()) |
| 10307 | DefineImplicitDefaultConstructor(DefaultLoc, CD); |
| 10308 | break; |
| 10309 | } |
| 10310 | |
| 10311 | case CXXCopyConstructor: { |
| 10312 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 10313 | CheckExplicitlyDefaultedCopyConstructor(CD); |
| 10314 | if (!CD->isInvalidDecl()) |
| 10315 | DefineImplicitCopyConstructor(DefaultLoc, CD); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10316 | break; |
| 10317 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10318 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10319 | case CXXCopyAssignment: { |
| 10320 | CheckExplicitlyDefaultedCopyAssignment(MD); |
| 10321 | if (!MD->isInvalidDecl()) |
| 10322 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
| 10323 | break; |
| 10324 | } |
| 10325 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10326 | case CXXDestructor: { |
| 10327 | CXXDestructorDecl *DD = cast<CXXDestructorDecl>(MD); |
| 10328 | CheckExplicitlyDefaultedDestructor(DD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10329 | if (!DD->isInvalidDecl()) |
| 10330 | DefineImplicitDestructor(DefaultLoc, DD); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10331 | break; |
| 10332 | } |
| 10333 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10334 | case CXXMoveConstructor: { |
| 10335 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 10336 | CheckExplicitlyDefaultedMoveConstructor(CD); |
| 10337 | if (!CD->isInvalidDecl()) |
| 10338 | DefineImplicitMoveConstructor(DefaultLoc, CD); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 10339 | break; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10340 | } |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 10341 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10342 | case CXXMoveAssignment: { |
| 10343 | CheckExplicitlyDefaultedMoveAssignment(MD); |
| 10344 | if (!MD->isInvalidDecl()) |
| 10345 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
| 10346 | break; |
| 10347 | } |
| 10348 | |
| 10349 | case CXXInvalid: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10350 | llvm_unreachable("Invalid special member."); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10351 | } |
| 10352 | } else { |
| 10353 | Diag(DefaultLoc, diag::err_default_special_members); |
| 10354 | } |
| 10355 | } |
| 10356 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10357 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 10358 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10359 | Stmt *SubStmt = *CI; |
| 10360 | if (!SubStmt) |
| 10361 | continue; |
| 10362 | if (isa<ReturnStmt>(SubStmt)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10363 | Self.Diag(SubStmt->getLocStart(), |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10364 | diag::err_return_in_constructor_handler); |
| 10365 | if (!isa<Expr>(SubStmt)) |
| 10366 | SearchForReturnInStmt(Self, SubStmt); |
| 10367 | } |
| 10368 | } |
| 10369 | |
| 10370 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 10371 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 10372 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 10373 | SearchForReturnInStmt(*this, Handler); |
| 10374 | } |
| 10375 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10376 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10377 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10378 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10379 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 10380 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10381 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 10382 | if (Context.hasSameType(NewTy, OldTy) || |
| 10383 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10384 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10385 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10386 | // Check if the return types are covariant |
| 10387 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10388 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10389 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10390 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 10391 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10392 | NewClassTy = NewPT->getPointeeType(); |
| 10393 | OldClassTy = OldPT->getPointeeType(); |
| 10394 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10395 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 10396 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 10397 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 10398 | NewClassTy = NewRT->getPointeeType(); |
| 10399 | OldClassTy = OldRT->getPointeeType(); |
| 10400 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10401 | } |
| 10402 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10403 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10404 | // The return types aren't either both pointers or references to a class type. |
| 10405 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10406 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10407 | diag::err_different_return_type_for_overriding_virtual_function) |
| 10408 | << New->getDeclName() << NewTy << OldTy; |
| 10409 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10410 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10411 | return true; |
| 10412 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10413 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10414 | // C++ [class.virtual]p6: |
| 10415 | // If the return type of D::f differs from the return type of B::f, the |
| 10416 | // class type in the return type of D::f shall be complete at the point of |
| 10417 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 10418 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 10419 | if (!RT->isBeingDefined() && |
| 10420 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 10421 | PDiag(diag::err_covariant_return_incomplete) |
| 10422 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10423 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 10424 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10425 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 10426 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10427 | // Check if the new class derives from the old class. |
| 10428 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 10429 | Diag(New->getLocation(), |
| 10430 | diag::err_covariant_return_not_derived) |
| 10431 | << New->getDeclName() << NewTy << OldTy; |
| 10432 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10433 | return true; |
| 10434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10435 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10436 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 10437 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 10438 | diag::err_covariant_return_inaccessible_base, |
| 10439 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 10440 | // FIXME: Should this point to the return type? |
| 10441 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 10442 | // FIXME: this note won't trigger for delayed access control |
| 10443 | // diagnostics, and it's impossible to get an undelayed error |
| 10444 | // here from access control during the original parse because |
| 10445 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10446 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10447 | return true; |
| 10448 | } |
| 10449 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10450 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10451 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10452 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10453 | Diag(New->getLocation(), |
| 10454 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10455 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10456 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10457 | return true; |
| 10458 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10459 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10460 | |
| 10461 | // The new class type must have the same or less qualifiers as the old type. |
| 10462 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 10463 | Diag(New->getLocation(), |
| 10464 | diag::err_covariant_return_type_class_type_more_qualified) |
| 10465 | << New->getDeclName() << NewTy << OldTy; |
| 10466 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10467 | return true; |
| 10468 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10469 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10470 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10471 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10472 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10473 | /// \brief Mark the given method pure. |
| 10474 | /// |
| 10475 | /// \param Method the method to be marked pure. |
| 10476 | /// |
| 10477 | /// \param InitRange the source range that covers the "0" initializer. |
| 10478 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 10479 | SourceLocation EndLoc = InitRange.getEnd(); |
| 10480 | if (EndLoc.isValid()) |
| 10481 | Method->setRangeEnd(EndLoc); |
| 10482 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10483 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 10484 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10485 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 10486 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10487 | |
| 10488 | if (!Method->isInvalidDecl()) |
| 10489 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 10490 | << Method->getDeclName() << InitRange; |
| 10491 | return true; |
| 10492 | } |
| 10493 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 10494 | /// \brief Determine whether the given declaration is a static data member. |
| 10495 | static bool isStaticDataMember(Decl *D) { |
| 10496 | VarDecl *Var = dyn_cast_or_null<VarDecl>(D); |
| 10497 | if (!Var) |
| 10498 | return false; |
| 10499 | |
| 10500 | return Var->isStaticDataMember(); |
| 10501 | } |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10502 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 10503 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 10504 | /// is a fresh scope pushed for just this purpose. |
| 10505 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10506 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 10507 | /// static data member of class X, names should be looked up in the scope of |
| 10508 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10509 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10510 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 10511 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10512 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10513 | // We should only get called for declarations with scope specifiers, like: |
| 10514 | // int foo::bar; |
| 10515 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 10516 | EnterDeclaratorContext(S, D->getDeclContext()); |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 10517 | |
| 10518 | // If we are parsing the initializer for a static data member, push a |
| 10519 | // new expression evaluation context that is associated with this static |
| 10520 | // data member. |
| 10521 | if (isStaticDataMember(D)) |
| 10522 | PushExpressionEvaluationContext(PotentiallyEvaluated, D); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10523 | } |
| 10524 | |
| 10525 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10526 | /// initializer for the out-of-line declaration 'D'. |
| 10527 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10528 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 10529 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10530 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 10531 | if (isStaticDataMember(D)) |
| 10532 | PopExpressionEvaluationContext(); |
| 10533 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10534 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 10535 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10536 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10537 | |
| 10538 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 10539 | /// C++ if/switch/while/for statement. |
| 10540 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10541 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10542 | // C++ 6.4p2: |
| 10543 | // The declarator shall not specify a function or an array. |
| 10544 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 10545 | // new class or enumeration. |
| 10546 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 10547 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 10548 | |
| 10549 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 10550 | if (!Dcl) |
| 10551 | return true; |
| 10552 | |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 10553 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 10554 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10555 | << D.getSourceRange(); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 10556 | return true; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10557 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10558 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10559 | return Dcl; |
| 10560 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10561 | |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10562 | void Sema::LoadExternalVTableUses() { |
| 10563 | if (!ExternalSource) |
| 10564 | return; |
| 10565 | |
| 10566 | SmallVector<ExternalVTableUse, 4> VTables; |
| 10567 | ExternalSource->ReadUsedVTables(VTables); |
| 10568 | SmallVector<VTableUse, 4> NewUses; |
| 10569 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 10570 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 10571 | = VTablesUsed.find(VTables[I].Record); |
| 10572 | // Even if a definition wasn't required before, it may be required now. |
| 10573 | if (Pos != VTablesUsed.end()) { |
| 10574 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 10575 | Pos->second = true; |
| 10576 | continue; |
| 10577 | } |
| 10578 | |
| 10579 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 10580 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 10581 | } |
| 10582 | |
| 10583 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 10584 | } |
| 10585 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10586 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 10587 | bool DefinitionRequired) { |
| 10588 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 10589 | // not have a vtable. |
| 10590 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 10591 | CurContext->isDependentContext() || |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10592 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 10593 | return; |
| 10594 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10595 | // Try to insert this class into the map. |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10596 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10597 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 10598 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 10599 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 10600 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 10601 | // If we already had an entry, check to see if we are promoting this vtable |
| 10602 | // to required a definition. If so, we need to reappend to the VTableUses |
| 10603 | // list, since we may have already processed the first entry. |
| 10604 | if (DefinitionRequired && !Pos.first->second) { |
| 10605 | Pos.first->second = true; |
| 10606 | } else { |
| 10607 | // Otherwise, we can early exit. |
| 10608 | return; |
| 10609 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10610 | } |
| 10611 | |
| 10612 | // Local classes need to have their virtual members marked |
| 10613 | // immediately. For all other classes, we mark their virtual members |
| 10614 | // at the end of the translation unit. |
| 10615 | if (Class->isLocalClass()) |
| 10616 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 10617 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10618 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 10619 | } |
| 10620 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10621 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10622 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10623 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10624 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 10625 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10626 | // Note: The VTableUses vector could grow as a result of marking |
| 10627 | // the members of a class as "used", so we check the size each |
| 10628 | // time through the loop and prefer indices (with are stable) to |
| 10629 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10630 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10631 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 10632 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10633 | if (!Class) |
| 10634 | continue; |
| 10635 | |
| 10636 | SourceLocation Loc = VTableUses[I].second; |
| 10637 | |
| 10638 | // If this class has a key function, but that key function is |
| 10639 | // defined in another translation unit, we don't need to emit the |
| 10640 | // vtable even though we're using it. |
| 10641 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 10642 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10643 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 10644 | case TSK_Undeclared: |
| 10645 | case TSK_ExplicitSpecialization: |
| 10646 | case TSK_ExplicitInstantiationDeclaration: |
| 10647 | // The key function is in another translation unit. |
| 10648 | continue; |
| 10649 | |
| 10650 | case TSK_ExplicitInstantiationDefinition: |
| 10651 | case TSK_ImplicitInstantiation: |
| 10652 | // We will be instantiating the key function. |
| 10653 | break; |
| 10654 | } |
| 10655 | } else if (!KeyFunction) { |
| 10656 | // If we have a class with no key function that is the subject |
| 10657 | // of an explicit instantiation declaration, suppress the |
| 10658 | // vtable; it will live with the explicit instantiation |
| 10659 | // definition. |
| 10660 | bool IsExplicitInstantiationDeclaration |
| 10661 | = Class->getTemplateSpecializationKind() |
| 10662 | == TSK_ExplicitInstantiationDeclaration; |
| 10663 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 10664 | REnd = Class->redecls_end(); |
| 10665 | R != REnd; ++R) { |
| 10666 | TemplateSpecializationKind TSK |
| 10667 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 10668 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 10669 | IsExplicitInstantiationDeclaration = true; |
| 10670 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 10671 | IsExplicitInstantiationDeclaration = false; |
| 10672 | break; |
| 10673 | } |
| 10674 | } |
| 10675 | |
| 10676 | if (IsExplicitInstantiationDeclaration) |
| 10677 | continue; |
| 10678 | } |
| 10679 | |
| 10680 | // Mark all of the virtual members of this class as referenced, so |
| 10681 | // that we can build a vtable. Then, tell the AST consumer that a |
| 10682 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10683 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10684 | MarkVirtualMembersReferenced(Loc, Class); |
| 10685 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 10686 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 10687 | |
| 10688 | // Optionally warn if we're emitting a weak vtable. |
| 10689 | if (Class->getLinkage() == ExternalLinkage && |
| 10690 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Douglas Gregor | a120d01 | 2011-09-23 19:04:03 +0000 | [diff] [blame] | 10691 | const FunctionDecl *KeyFunctionDef = 0; |
| 10692 | if (!KeyFunction || |
| 10693 | (KeyFunction->hasBody(KeyFunctionDef) && |
| 10694 | KeyFunctionDef->isInlined())) |
David Blaikie | 44d95b5 | 2011-12-09 18:32:50 +0000 | [diff] [blame] | 10695 | Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == |
| 10696 | TSK_ExplicitInstantiationDefinition |
| 10697 | ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) |
| 10698 | << Class; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10699 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10700 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10701 | VTableUses.clear(); |
| 10702 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10703 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10704 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10705 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10706 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 10707 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10708 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 10709 | e = RD->method_end(); i != e; ++i) { |
| 10710 | CXXMethodDecl *MD = *i; |
| 10711 | |
| 10712 | // C++ [basic.def.odr]p2: |
| 10713 | // [...] A virtual member function is used if it is not pure. [...] |
| 10714 | if (MD->isVirtual() && !MD->isPure()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10715 | MarkFunctionReferenced(Loc, MD); |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10716 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10717 | |
| 10718 | // Only classes that have virtual bases need a VTT. |
| 10719 | if (RD->getNumVBases() == 0) |
| 10720 | return; |
| 10721 | |
| 10722 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 10723 | e = RD->bases_end(); i != e; ++i) { |
| 10724 | const CXXRecordDecl *Base = |
| 10725 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10726 | if (Base->getNumVBases() == 0) |
| 10727 | continue; |
| 10728 | MarkVirtualMembersReferenced(Loc, Base); |
| 10729 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10730 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10731 | |
| 10732 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 10733 | /// Objective-C implementation whose ivars need be initialized. |
| 10734 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 10735 | if (!getLangOptions().CPlusPlus) |
| 10736 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 10737 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10738 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10739 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 10740 | if (ivars.empty()) |
| 10741 | return; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10742 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10743 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 10744 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10745 | if (Field->isInvalidDecl()) |
| 10746 | continue; |
| 10747 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 10748 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10749 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 10750 | InitializationKind InitKind = |
| 10751 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 10752 | |
| 10753 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10754 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10755 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 10756 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10757 | // Note, MemberInit could actually come back empty if no initialization |
| 10758 | // is required (e.g., because it would call a trivial default constructor) |
| 10759 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 10760 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 10761 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10762 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 10763 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 10764 | SourceLocation(), |
| 10765 | MemberInit.takeAs<Expr>(), |
| 10766 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10767 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10768 | |
| 10769 | // Be sure that the destructor is accessible and is marked as referenced. |
| 10770 | if (const RecordType *RecordTy |
| 10771 | = Context.getBaseElementType(Field->getType()) |
| 10772 | ->getAs<RecordType>()) { |
| 10773 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 10774 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10775 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10776 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 10777 | PDiag(diag::err_access_dtor_ivar) |
| 10778 | << Context.getBaseElementType(Field->getType())); |
| 10779 | } |
| 10780 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10781 | } |
| 10782 | ObjCImplementation->setIvarInitializers(Context, |
| 10783 | AllToInit.data(), AllToInit.size()); |
| 10784 | } |
| 10785 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10786 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10787 | static |
| 10788 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 10789 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 10790 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 10791 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 10792 | Sema &S) { |
| 10793 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 10794 | CE = Current.end(); |
| 10795 | if (Ctor->isInvalidDecl()) |
| 10796 | return; |
| 10797 | |
| 10798 | const FunctionDecl *FNTarget = 0; |
| 10799 | CXXConstructorDecl *Target; |
| 10800 | |
| 10801 | // We ignore the result here since if we don't have a body, Target will be |
| 10802 | // null below. |
| 10803 | (void)Ctor->getTargetConstructor()->hasBody(FNTarget); |
| 10804 | Target |
| 10805 | = const_cast<CXXConstructorDecl*>(cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 10806 | |
| 10807 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 10808 | // Avoid dereferencing a null pointer here. |
| 10809 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 10810 | |
| 10811 | if (!Current.insert(Canonical)) |
| 10812 | return; |
| 10813 | |
| 10814 | // We know that beyond here, we aren't chaining into a cycle. |
| 10815 | if (!Target || !Target->isDelegatingConstructor() || |
| 10816 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| 10817 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 10818 | Valid.insert(*CI); |
| 10819 | Current.clear(); |
| 10820 | // We've hit a cycle. |
| 10821 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 10822 | Current.count(TCanonical)) { |
| 10823 | // If we haven't diagnosed this cycle yet, do so now. |
| 10824 | if (!Invalid.count(TCanonical)) { |
| 10825 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 10826 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10827 | << Ctor; |
| 10828 | |
| 10829 | // Don't add a note for a function delegating directo to itself. |
| 10830 | if (TCanonical != Canonical) |
| 10831 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 10832 | |
| 10833 | CXXConstructorDecl *C = Target; |
| 10834 | while (C->getCanonicalDecl() != Canonical) { |
| 10835 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 10836 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 10837 | |
| 10838 | C |
| 10839 | = const_cast<CXXConstructorDecl*>(cast<CXXConstructorDecl>(FNTarget)); |
| 10840 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 10841 | } |
| 10842 | } |
| 10843 | |
| 10844 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 10845 | Invalid.insert(*CI); |
| 10846 | Current.clear(); |
| 10847 | } else { |
| 10848 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 10849 | } |
| 10850 | } |
| 10851 | |
| 10852 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10853 | void Sema::CheckDelegatingCtorCycles() { |
| 10854 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 10855 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10856 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 10857 | CE = Current.end(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10858 | |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 10859 | for (DelegatingCtorDeclsType::iterator |
| 10860 | I = DelegatingCtorDecls.begin(ExternalSource), |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10861 | E = DelegatingCtorDecls.end(); |
| 10862 | I != E; ++I) { |
| 10863 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10864 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10865 | |
| 10866 | for (CI = Invalid.begin(), CE = Invalid.end(); CI != CE; ++CI) |
| 10867 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10868 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 10869 | |
| 10870 | /// IdentifyCUDATarget - Determine the CUDA compilation target for this function |
| 10871 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D) { |
| 10872 | // Implicitly declared functions (e.g. copy constructors) are |
| 10873 | // __host__ __device__ |
| 10874 | if (D->isImplicit()) |
| 10875 | return CFT_HostDevice; |
| 10876 | |
| 10877 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 10878 | return CFT_Global; |
| 10879 | |
| 10880 | if (D->hasAttr<CUDADeviceAttr>()) { |
| 10881 | if (D->hasAttr<CUDAHostAttr>()) |
| 10882 | return CFT_HostDevice; |
| 10883 | else |
| 10884 | return CFT_Device; |
| 10885 | } |
| 10886 | |
| 10887 | return CFT_Host; |
| 10888 | } |
| 10889 | |
| 10890 | bool Sema::CheckCUDATarget(CUDAFunctionTarget CallerTarget, |
| 10891 | CUDAFunctionTarget CalleeTarget) { |
| 10892 | // CUDA B.1.1 "The __device__ qualifier declares a function that is... |
| 10893 | // Callable from the device only." |
| 10894 | if (CallerTarget == CFT_Host && CalleeTarget == CFT_Device) |
| 10895 | return true; |
| 10896 | |
| 10897 | // CUDA B.1.2 "The __global__ qualifier declares a function that is... |
| 10898 | // Callable from the host only." |
| 10899 | // CUDA B.1.3 "The __host__ qualifier declares a function that is... |
| 10900 | // Callable from the host only." |
| 10901 | if ((CallerTarget == CFT_Device || CallerTarget == CFT_Global) && |
| 10902 | (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)) |
| 10903 | return true; |
| 10904 | |
| 10905 | if (CallerTarget == CFT_HostDevice && CalleeTarget != CFT_HostDevice) |
| 10906 | return true; |
| 10907 | |
| 10908 | return false; |
| 10909 | } |