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" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTLambda.h" |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 21 | #include "clang/AST/EvaluatedExprVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Richard Smith | 4ac537b | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 30 | #include "clang/Lex/LiteralSupport.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "clang/Sema/CXXFieldCollector.h" |
| 33 | #include "clang/Sema/DeclSpec.h" |
| 34 | #include "clang/Sema/Initialization.h" |
| 35 | #include "clang/Sema/Lookup.h" |
| 36 | #include "clang/Sema/ParsedTemplate.h" |
| 37 | #include "clang/Sema/Scope.h" |
| 38 | #include "clang/Sema/ScopeInfo.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 39 | #include "clang/Sema/Template.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 42 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 43 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace clang; |
| 46 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // CheckDefaultArgumentVisitor |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | namespace { |
| 52 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 53 | /// the default argument of a parameter to determine whether it |
| 54 | /// contains any ill-formed subexpressions. For example, this will |
| 55 | /// diagnose the use of local variables or parameters within the |
| 56 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 57 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 58 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | Expr *DefaultArg; |
| 60 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 62 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 64 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 66 | bool VisitExpr(Expr *Node); |
| 67 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 68 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 69 | bool VisitLambdaExpr(LambdaExpr *Lambda); |
John McCall | 045d252 | 2013-04-09 01:56:28 +0000 | [diff] [blame] | 70 | bool VisitPseudoObjectExpr(PseudoObjectExpr *POE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 71 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 73 | /// VisitExpr - Visit all of the children of this expression. |
| 74 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 75 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 76 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 77 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 78 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 81 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 82 | /// determine whether this declaration can be used in the default |
| 83 | /// argument expression. |
| 84 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 85 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 86 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 87 | // C++ [dcl.fct.default]p9 |
| 88 | // Default arguments are evaluated each time the function is |
| 89 | // called. The order of evaluation of function arguments is |
| 90 | // unspecified. Consequently, parameters of a function shall not |
| 91 | // be used in default argument expressions, even if they are not |
| 92 | // evaluated. Parameters of a function declared before a default |
| 93 | // argument expression are in scope and can hide namespace and |
| 94 | // class member names. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 95 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 96 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 97 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 98 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 99 | // C++ [dcl.fct.default]p7 |
| 100 | // Local variables shall not be used in default argument |
| 101 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 102 | if (VDecl->isLocalVarDecl()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 103 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 104 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 105 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 106 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 108 | return false; |
| 109 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 111 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 112 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 113 | // C++ [dcl.fct.default]p8: |
| 114 | // The keyword this shall not be used in a default argument of a |
| 115 | // member function. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 116 | return S->Diag(ThisE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 117 | diag::err_param_default_argument_references_this) |
| 118 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 119 | } |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 120 | |
John McCall | 045d252 | 2013-04-09 01:56:28 +0000 | [diff] [blame] | 121 | bool CheckDefaultArgumentVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *POE) { |
| 122 | bool Invalid = false; |
| 123 | for (PseudoObjectExpr::semantics_iterator |
| 124 | i = POE->semantics_begin(), e = POE->semantics_end(); i != e; ++i) { |
| 125 | Expr *E = *i; |
| 126 | |
| 127 | // Look through bindings. |
| 128 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 129 | E = OVE->getSourceExpr(); |
| 130 | assert(E && "pseudo-object binding without source expression?"); |
| 131 | } |
| 132 | |
| 133 | Invalid |= Visit(E); |
| 134 | } |
| 135 | return Invalid; |
| 136 | } |
| 137 | |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 138 | bool CheckDefaultArgumentVisitor::VisitLambdaExpr(LambdaExpr *Lambda) { |
| 139 | // C++11 [expr.lambda.prim]p13: |
| 140 | // A lambda-expression appearing in a default argument shall not |
| 141 | // implicitly or explicitly capture any entity. |
| 142 | if (Lambda->capture_begin() == Lambda->capture_end()) |
| 143 | return false; |
| 144 | |
| 145 | return S->Diag(Lambda->getLocStart(), |
| 146 | diag::err_lambda_capture_default_arg); |
| 147 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 150 | void |
| 151 | Sema::ImplicitExceptionSpecification::CalledDecl(SourceLocation CallLoc, |
| 152 | const CXXMethodDecl *Method) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 153 | // If we have an MSAny spec already, don't bother. |
| 154 | if (!Method || ComputedEST == EST_MSAny) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
| 157 | const FunctionProtoType *Proto |
| 158 | = Method->getType()->getAs<FunctionProtoType>(); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 159 | Proto = Self->ResolveExceptionSpec(CallLoc, Proto); |
| 160 | if (!Proto) |
| 161 | return; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 162 | |
| 163 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 164 | |
| 165 | // If this function can throw any exceptions, make a note of that. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 166 | if (EST == EST_MSAny || EST == EST_None) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 167 | ClearExceptions(); |
| 168 | ComputedEST = EST; |
| 169 | return; |
| 170 | } |
| 171 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 172 | // FIXME: If the call to this decl is using any of its default arguments, we |
| 173 | // need to search them for potentially-throwing calls. |
| 174 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 175 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 176 | if (EST == EST_BasicNoexcept) |
| 177 | return; |
| 178 | |
| 179 | // If we have a throw-all spec at this point, ignore the function. |
| 180 | if (ComputedEST == EST_None) |
| 181 | return; |
| 182 | |
| 183 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 184 | // change to that specification. |
| 185 | if (EST == EST_DynamicNone) { |
| 186 | if (ComputedEST == EST_BasicNoexcept) |
| 187 | ComputedEST = EST_DynamicNone; |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // Check out noexcept specs. |
| 192 | if (EST == EST_ComputedNoexcept) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 193 | FunctionProtoType::NoexceptResult NR = |
| 194 | Proto->getNoexceptSpec(Self->Context); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 195 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 196 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 197 | assert(NR != FunctionProtoType::NR_Dependent && |
| 198 | "Should not generate implicit declarations for dependent cases, " |
| 199 | "and don't know how to handle them anyway."); |
| 200 | |
| 201 | // noexcept(false) -> no spec on the new function |
| 202 | if (NR == FunctionProtoType::NR_Throw) { |
| 203 | ClearExceptions(); |
| 204 | ComputedEST = EST_None; |
| 205 | } |
| 206 | // noexcept(true) won't change anything either. |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 211 | assert(ComputedEST != EST_None && |
| 212 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 213 | ComputedEST = EST_Dynamic; |
| 214 | // Record the exceptions in this function's exception specification. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 215 | for (const auto &E : Proto->exceptions()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 216 | if (ExceptionsSeen.insert(Self->Context.getCanonicalType(E)).second) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 217 | Exceptions.push_back(E); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 220 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 221 | if (!E || ComputedEST == EST_MSAny) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 222 | return; |
| 223 | |
| 224 | // FIXME: |
| 225 | // |
| 226 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 4857947 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 227 | // [An] implicit exception-specification specifies the type-id T if and |
| 228 | // only if T is allowed by the exception-specification of a function directly |
| 229 | // 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] | 230 | // function it directly invokes allows all exceptions, and f shall allow no |
| 231 | // exceptions if every function it directly invokes allows no exceptions. |
| 232 | // |
| 233 | // Note in particular that if an implicit exception-specification is generated |
| 234 | // for a function containing a throw-expression, that specification can still |
| 235 | // be noexcept(true). |
| 236 | // |
| 237 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 238 | // is no indication that we should only consider potentially-evaluated calls. |
| 239 | // |
| 240 | // Ultimately we should implement the intent of the standard: the exception |
| 241 | // specification should be the set of exceptions which can be thrown by the |
| 242 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 243 | // throw any exception. |
| 244 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 245 | if (Self->canThrow(E)) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 246 | ComputedEST = EST_None; |
| 247 | } |
| 248 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 249 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 250 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 252 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 253 | diag::err_typecheck_decl_incomplete_type)) { |
| 254 | Param->setInvalidDecl(); |
| 255 | return true; |
| 256 | } |
| 257 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 258 | // C++ [dcl.fct.default]p5 |
| 259 | // A default argument expression is implicitly converted (clause |
| 260 | // 4) to the parameter type. The default argument expression has |
| 261 | // the same semantic constraints as the initializer expression in |
| 262 | // a declaration of a variable of the parameter type, using the |
| 263 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 264 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 265 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 266 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 267 | EqualLoc); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 268 | InitializationSequence InitSeq(*this, Entity, Kind, Arg); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 269 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Arg); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 270 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 271 | return true; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 272 | Arg = Result.getAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 273 | |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 274 | CheckCompletedExpr(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 275 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 277 | // Okay: add the default argument to the parameter |
| 278 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 280 | // We have already instantiated this parameter; provide each of the |
| 281 | // instantiations with the uninstantiated default argument. |
| 282 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 283 | = UnparsedDefaultArgInstantiations.find(Param); |
| 284 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 285 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 286 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 287 | |
| 288 | // We're done tracking this parameter's instantiations. |
| 289 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 290 | } |
| 291 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 292 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 295 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 296 | /// provided for a function parameter is well-formed. If so, attach it |
| 297 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 298 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 299 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 300 | Expr *DefaultArg) { |
| 301 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 302 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 304 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 305 | UnparsedDefaultArgLocs.erase(Param); |
| 306 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 307 | // Default arguments are only permitted in C++ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 308 | if (!getLangOpts().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 309 | Diag(EqualLoc, diag::err_param_default_argument) |
| 310 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 311 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 312 | return; |
| 313 | } |
| 314 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 315 | // Check for unexpanded parameter packs. |
| 316 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 317 | Param->setInvalidDecl(); |
| 318 | return; |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 319 | } |
| 320 | |
| 321 | // C++11 [dcl.fct.default]p3 |
| 322 | // A default argument expression [...] shall not be specified for a |
| 323 | // parameter pack. |
| 324 | if (Param->isParameterPack()) { |
| 325 | Diag(EqualLoc, diag::err_param_default_argument_on_parameter_pack) |
| 326 | << DefaultArg->getSourceRange(); |
| 327 | return; |
| 328 | } |
| 329 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 330 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 331 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 332 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 333 | Param->setInvalidDecl(); |
| 334 | return; |
| 335 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 337 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 340 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 341 | /// argument for a function parameter, but we can't parse it yet |
| 342 | /// because we're inside a class definition. Note that this default |
| 343 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 344 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 345 | SourceLocation EqualLoc, |
| 346 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 347 | if (!param) |
| 348 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 350 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Nick Lewycky | ee0bc3b | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 351 | Param->setUnparsedDefaultArg(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 352 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 355 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 356 | /// the default argument for the parameter param failed. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 357 | void Sema::ActOnParamDefaultArgumentError(Decl *param, |
| 358 | SourceLocation EqualLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 359 | if (!param) |
| 360 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 362 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 363 | Param->setInvalidDecl(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 364 | UnparsedDefaultArgLocs.erase(Param); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 365 | Param->setDefaultArg(new(Context) |
| 366 | OpaqueValueExpr(EqualLoc, |
| 367 | Param->getType().getNonReferenceType(), |
| 368 | VK_RValue)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 371 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 372 | /// arguments in the declarator, which is not a function declaration |
| 373 | /// or definition and therefore is not permitted to have default |
| 374 | /// arguments. This routine should be invoked for every declarator |
| 375 | /// that is not a function declaration or definition. |
| 376 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 377 | // C++ [dcl.fct.default]p3 |
| 378 | // A default argument expression shall be specified only in the |
| 379 | // parameter-declaration-clause of a function declaration or in a |
| 380 | // template-parameter (14.1). It shall not be specified for a |
| 381 | // parameter pack. If it is specified in a |
| 382 | // parameter-declaration-clause, it shall not occur within a |
| 383 | // declarator or abstract-declarator of a parameter-declaration. |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 384 | bool MightBeFunction = D.isFunctionDeclarationContext(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 385 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 386 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 387 | if (chunk.Kind == DeclaratorChunk::Function) { |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 388 | if (MightBeFunction) { |
| 389 | // This is a function declaration. It can have default arguments, but |
| 390 | // keep looking in case its return type is a function type with default |
| 391 | // arguments. |
| 392 | MightBeFunction = false; |
| 393 | continue; |
| 394 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 395 | for (unsigned argIdx = 0, e = chunk.Fun.NumParams; argIdx != e; |
| 396 | ++argIdx) { |
| 397 | ParmVarDecl *Param = cast<ParmVarDecl>(chunk.Fun.Params[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 398 | if (Param->hasUnparsedDefaultArg()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 399 | CachedTokens *Toks = chunk.Fun.Params[argIdx].DefaultArgTokens; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 400 | SourceRange SR; |
| 401 | if (Toks->size() > 1) |
| 402 | SR = SourceRange((*Toks)[1].getLocation(), |
| 403 | Toks->back().getLocation()); |
| 404 | else |
| 405 | SR = UnparsedDefaultArgLocs[Param]; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 406 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 407 | << SR; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 408 | delete Toks; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 409 | chunk.Fun.Params[argIdx].DefaultArgTokens = nullptr; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 410 | } else if (Param->getDefaultArg()) { |
| 411 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 412 | << Param->getDefaultArg()->getSourceRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 413 | Param->setDefaultArg(nullptr); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 416 | } else if (chunk.Kind != DeclaratorChunk::Paren) { |
| 417 | MightBeFunction = false; |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 422 | static bool functionDeclHasDefaultArgument(const FunctionDecl *FD) { |
| 423 | for (unsigned NumParams = FD->getNumParams(); NumParams > 0; --NumParams) { |
| 424 | const ParmVarDecl *PVD = FD->getParamDecl(NumParams-1); |
| 425 | if (!PVD->hasDefaultArg()) |
| 426 | return false; |
| 427 | if (!PVD->hasInheritedDefaultArg()) |
| 428 | return true; |
| 429 | } |
| 430 | return false; |
| 431 | } |
| 432 | |
Craig Topper | 1a6eac8 | 2012-09-21 04:33:26 +0000 | [diff] [blame] | 433 | /// MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 434 | /// function, once we already know that they have the same |
| 435 | /// type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 436 | /// error, false otherwise. |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 437 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, |
| 438 | Scope *S) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 439 | bool Invalid = false; |
| 440 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 441 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 442 | // For non-template functions, default arguments can be added in |
| 443 | // later declarations of a function in the same |
| 444 | // scope. Declarations in different scopes have completely |
| 445 | // distinct sets of default arguments. That is, declarations in |
| 446 | // inner scopes do not acquire default arguments from |
| 447 | // declarations in outer scopes, and vice versa. In a given |
| 448 | // function declaration, all parameters subsequent to a |
| 449 | // parameter with a default argument shall have default |
| 450 | // arguments supplied in this or previous declarations. A |
| 451 | // default argument shall not be redefined by a later |
| 452 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 453 | // |
| 454 | // C++ [dcl.fct.default]p6: |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 455 | // Except for member functions of class templates, the default arguments |
| 456 | // in a member function definition that appears outside of the class |
| 457 | // definition are added to the set of default arguments provided by the |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 458 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 459 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 460 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 461 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 462 | |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 463 | bool OldParamHasDfl = OldParam->hasDefaultArg(); |
| 464 | bool NewParamHasDfl = NewParam->hasDefaultArg(); |
| 465 | |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 466 | // The declaration context corresponding to the scope is the semantic |
| 467 | // parent, unless this is a local function declaration, in which case |
| 468 | // it is that surrounding function. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 469 | DeclContext *ScopeDC = New->isLocalExternDecl() |
| 470 | ? New->getLexicalDeclContext() |
| 471 | : New->getDeclContext(); |
| 472 | if (S && !isDeclInScope(Old, ScopeDC, S) && |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 473 | !New->getDeclContext()->isRecord()) |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 474 | // Ignore default parameters of old decl if they are not in |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 475 | // the same scope and this is not an out-of-line definition of |
| 476 | // a member function. |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 477 | OldParamHasDfl = false; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 478 | if (New->isLocalExternDecl() != Old->isLocalExternDecl()) |
| 479 | // If only one of these is a local function declaration, then they are |
| 480 | // declared in different scopes, even though isDeclInScope may think |
| 481 | // they're in the same scope. (If both are local, the scope check is |
| 482 | // sufficent, and if neither is local, then they are in the same scope.) |
| 483 | OldParamHasDfl = false; |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 484 | |
| 485 | if (OldParamHasDfl && NewParamHasDfl) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 486 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 487 | unsigned DiagDefaultParamID = |
| 488 | diag::err_param_default_argument_redefinition; |
| 489 | |
| 490 | // MSVC accepts that default parameters be redefined for member functions |
| 491 | // of template class. The new default parameter's value is ignored. |
| 492 | Invalid = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 493 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 494 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 495 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 496 | // Merge the old default argument into the new parameter. |
| 497 | NewParam->setHasInheritedDefaultArg(); |
| 498 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 499 | NewParam->setUninstantiatedDefaultArg( |
| 500 | OldParam->getUninstantiatedDefaultArg()); |
| 501 | else |
| 502 | NewParam->setDefaultArg(OldParam->getInit()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 503 | DiagDefaultParamID = diag::ext_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 504 | Invalid = false; |
| 505 | } |
| 506 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 507 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 508 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 509 | // hint here. Alternatively, we could walk the type-source information |
| 510 | // for NewParam to find the last source location in the type... but it |
| 511 | // isn't worth the effort right now. This is the kind of test case that |
| 512 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 513 | // int f(int); |
| 514 | // void g(int (*fp)(int) = f); |
| 515 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 516 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 517 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 518 | |
| 519 | // Look for the function declaration where the default argument was |
| 520 | // actually written, which may be a declaration prior to Old. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 521 | for (auto Older = Old; OldParam->hasInheritedDefaultArg();) { |
| 522 | Older = Older->getPreviousDecl(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 523 | OldParam = Older->getParamDecl(p); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 526 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 527 | << OldParam->getDefaultArgRange(); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 528 | } else if (OldParamHasDfl) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 529 | // Merge the old default argument into the new parameter. |
| 530 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 531 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 532 | NewParam->setHasInheritedDefaultArg(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 533 | if (OldParam->hasUnparsedDefaultArg()) |
| 534 | NewParam->setUnparsedDefaultArg(); |
| 535 | else if (OldParam->hasUninstantiatedDefaultArg()) |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 536 | NewParam->setUninstantiatedDefaultArg( |
| 537 | OldParam->getUninstantiatedDefaultArg()); |
| 538 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 539 | NewParam->setDefaultArg(OldParam->getInit()); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 540 | } else if (NewParamHasDfl) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 541 | if (New->getDescribedFunctionTemplate()) { |
| 542 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 543 | Diag(NewParam->getLocation(), |
| 544 | diag::err_param_default_argument_template_redecl) |
| 545 | << NewParam->getDefaultArgRange(); |
| 546 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 547 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 548 | } else if (New->getTemplateSpecializationKind() |
| 549 | != TSK_ImplicitInstantiation && |
| 550 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 551 | // C++ [temp.expr.spec]p21: |
| 552 | // Default function arguments shall not be specified in a declaration |
| 553 | // or a definition for one of the following explicit specializations: |
| 554 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 555 | // - the explicit specialization of a member function template; |
| 556 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 557 | // template where the class template specialization to which the |
| 558 | // member function specialization belongs is implicitly |
| 559 | // instantiated. |
| 560 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 561 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 562 | << New->getDeclName() |
| 563 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 564 | } else if (New->getDeclContext()->isDependentContext()) { |
| 565 | // C++ [dcl.fct.default]p6 (DR217): |
| 566 | // Default arguments for a member function of a class template shall |
| 567 | // be specified on the initial declaration of the member function |
| 568 | // within the class template. |
| 569 | // |
| 570 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 571 | // leads me to the conclusion that one cannot add default function |
| 572 | // arguments for an out-of-line definition of a member function of a |
| 573 | // dependent type. |
| 574 | int WhichKind = 2; |
| 575 | if (CXXRecordDecl *Record |
| 576 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 577 | if (Record->getDescribedClassTemplate()) |
| 578 | WhichKind = 0; |
| 579 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 580 | WhichKind = 1; |
| 581 | else |
| 582 | WhichKind = 2; |
| 583 | } |
| 584 | |
| 585 | Diag(NewParam->getLocation(), |
| 586 | diag::err_param_default_argument_member_template_redecl) |
| 587 | << WhichKind |
| 588 | << NewParam->getDefaultArgRange(); |
| 589 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Richard Smith | b8abff6 | 2012-11-28 03:45:24 +0000 | [diff] [blame] | 593 | // DR1344: If a default argument is added outside a class definition and that |
| 594 | // default argument makes the function a special member function, the program |
| 595 | // is ill-formed. This can only happen for constructors. |
| 596 | if (isa<CXXConstructorDecl>(New) && |
| 597 | New->getMinRequiredArguments() < Old->getMinRequiredArguments()) { |
| 598 | CXXSpecialMember NewSM = getSpecialMember(cast<CXXMethodDecl>(New)), |
| 599 | OldSM = getSpecialMember(cast<CXXMethodDecl>(Old)); |
| 600 | if (NewSM != OldSM) { |
| 601 | ParmVarDecl *NewParam = New->getParamDecl(New->getMinRequiredArguments()); |
| 602 | assert(NewParam->hasDefaultArg()); |
| 603 | Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special) |
| 604 | << NewParam->getDefaultArgRange() << NewSM; |
| 605 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 606 | } |
| 607 | } |
| 608 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 609 | const FunctionDecl *Def; |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 610 | // 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] | 611 | // template has a constexpr specifier then all its declarations shall |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 612 | // contain the constexpr specifier. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 613 | if (New->isConstexpr() != Old->isConstexpr()) { |
| 614 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 615 | << New << New->isConstexpr(); |
| 616 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 617 | Invalid = true; |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 618 | } else if (!Old->getMostRecentDecl()->isInlined() && New->isInlined() && |
| 619 | Old->isDefined(Def)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 620 | // C++11 [dcl.fcn.spec]p4: |
| 621 | // If the definition of a function appears in a translation unit before its |
| 622 | // first declaration as inline, the program is ill-formed. |
| 623 | Diag(New->getLocation(), diag::err_inline_decl_follows_def) << New; |
| 624 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 625 | Invalid = true; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 626 | } |
| 627 | |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 628 | // C++11 [dcl.fct.default]p4: If a friend declaration specifies a default |
NAKAMURA Takumi | fd527a4 | 2013-07-17 17:57:52 +0000 | [diff] [blame] | 629 | // argument expression, that declaration shall be a definition and shall be |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 630 | // the only declaration of the function or function template in the |
| 631 | // translation unit. |
| 632 | if (Old->getFriendObjectKind() == Decl::FOK_Undeclared && |
| 633 | functionDeclHasDefaultArgument(Old)) { |
| 634 | Diag(New->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 635 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 636 | Invalid = true; |
| 637 | } |
| 638 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 639 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 640 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 641 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 642 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 645 | /// \brief Merge the exception specifications of two variable declarations. |
| 646 | /// |
| 647 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 648 | /// checks if the redeclaration might have an exception specification and |
| 649 | /// validates compatibility and merges the specs if necessary. |
| 650 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 651 | // Shortcut if exceptions are disabled. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 652 | if (!getLangOpts().CXXExceptions) |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 653 | return; |
| 654 | |
| 655 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 656 | "Should only be called if types are otherwise the same."); |
| 657 | |
| 658 | QualType NewType = New->getType(); |
| 659 | QualType OldType = Old->getType(); |
| 660 | |
| 661 | // We're only interested in pointers and references to functions, as well |
| 662 | // as pointers to member functions. |
| 663 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 664 | NewType = R->getPointeeType(); |
| 665 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 666 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 667 | NewType = P->getPointeeType(); |
| 668 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 669 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 670 | NewType = M->getPointeeType(); |
| 671 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 672 | } |
| 673 | |
| 674 | if (!NewType->isFunctionProtoType()) |
| 675 | return; |
| 676 | |
| 677 | // There's lots of special cases for functions. For function pointers, system |
| 678 | // libraries are hopefully not as broken so that we don't need these |
| 679 | // workarounds. |
| 680 | if (CheckEquivalentExceptionSpec( |
| 681 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 682 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 683 | New->setInvalidDecl(); |
| 684 | } |
| 685 | } |
| 686 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 687 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 688 | /// function declaration are well-formed according to C++ |
| 689 | /// [dcl.fct.default]. |
| 690 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 691 | unsigned NumParams = FD->getNumParams(); |
| 692 | unsigned p; |
| 693 | |
| 694 | // Find first parameter with a default argument |
| 695 | for (p = 0; p < NumParams; ++p) { |
| 696 | ParmVarDecl *Param = FD->getParamDecl(p); |
Richard Smith | 7974c60 | 2013-04-17 16:25:20 +0000 | [diff] [blame] | 697 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 698 | break; |
| 699 | } |
| 700 | |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 701 | // C++11 [dcl.fct.default]p4: |
| 702 | // In a given function declaration, each parameter subsequent to a parameter |
| 703 | // with a default argument shall have a default argument supplied in this or |
| 704 | // a previous declaration or shall be a function parameter pack. A default |
| 705 | // argument shall not be redefined by a later declaration (not even to the |
| 706 | // same value). |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 707 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 709 | ParmVarDecl *Param = FD->getParamDecl(p); |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 710 | if (!Param->hasDefaultArg() && !Param->isParameterPack()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 711 | if (Param->isInvalidDecl()) |
| 712 | /* We already complained about this parameter. */; |
| 713 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 715 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 716 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 717 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 719 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 721 | LastMissingDefaultArg = p; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if (LastMissingDefaultArg > 0) { |
| 726 | // Some default arguments were missing. Clear out all of the |
| 727 | // default arguments up to (and including) the last missing |
| 728 | // default argument, so that we leave the function parameters |
| 729 | // in a semantically valid state. |
| 730 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 731 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 732 | if (Param->hasDefaultArg()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 733 | Param->setDefaultArg(nullptr); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | } |
| 737 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 738 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 739 | // CheckConstexprParameterTypes - Check whether a function's parameter types |
| 740 | // 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] | 741 | // diagnostic and return false. |
| 742 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 743 | const FunctionDecl *FD) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 744 | unsigned ArgIndex = 0; |
| 745 | const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 746 | for (FunctionProtoType::param_type_iterator i = FT->param_type_begin(), |
| 747 | e = FT->param_type_end(); |
| 748 | i != e; ++i, ++ArgIndex) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 749 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 750 | SourceLocation ParamLoc = PD->getLocation(); |
| 751 | if (!(*i)->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 752 | SemaRef.RequireLiteralType(ParamLoc, *i, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 753 | diag::err_constexpr_non_literal_param, |
| 754 | ArgIndex+1, PD->getSourceRange(), |
| 755 | isa<CXXConstructorDecl>(FD))) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 756 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 757 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 758 | return true; |
| 759 | } |
| 760 | |
| 761 | /// \brief Get diagnostic %select index for tag kind for |
| 762 | /// record diagnostic message. |
| 763 | /// WARNING: Indexes apply to particular diagnostics only! |
| 764 | /// |
| 765 | /// \returns diagnostic %select index. |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 766 | static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 767 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 768 | case TTK_Struct: return 0; |
| 769 | case TTK_Interface: return 1; |
| 770 | case TTK_Class: return 2; |
| 771 | default: llvm_unreachable("Invalid tag kind for record diagnostic!"); |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 772 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // CheckConstexprFunctionDecl - Check whether a function declaration satisfies |
| 776 | // the requirements of a constexpr function definition or a constexpr |
| 777 | // constructor definition. If so, return true. If not, produce appropriate |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 778 | // diagnostics and return false. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 779 | // |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 780 | // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. |
| 781 | bool Sema::CheckConstexprFunctionDecl(const FunctionDecl *NewFD) { |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 782 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 783 | if (MD && MD->isInstance()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 784 | // C++11 [dcl.constexpr]p4: |
| 785 | // The definition of a constexpr constructor shall satisfy the following |
| 786 | // constraints: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 787 | // - the class shall not have any virtual base classes; |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 788 | const CXXRecordDecl *RD = MD->getParent(); |
| 789 | if (RD->getNumVBases()) { |
| 790 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 791 | << isa<CXXConstructorDecl>(NewFD) |
| 792 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 793 | for (const auto &I : RD->vbases()) |
| 794 | Diag(I.getLocStart(), |
| 795 | diag::note_constexpr_virtual_base_here) << I.getSourceRange(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 796 | return false; |
| 797 | } |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 801 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 802 | // The definition of a constexpr function shall satisfy the following |
| 803 | // constraints: |
| 804 | // - it shall not be virtual; |
| 805 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 806 | if (Method && Method->isVirtual()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 807 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 808 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 809 | // If it's not obvious why this function is virtual, find an overridden |
| 810 | // function which uses the 'virtual' keyword. |
| 811 | const CXXMethodDecl *WrittenVirtual = Method; |
| 812 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 813 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 814 | if (WrittenVirtual != Method) |
| 815 | Diag(WrittenVirtual->getLocation(), |
| 816 | diag::note_overridden_virtual_function); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 817 | return false; |
| 818 | } |
| 819 | |
| 820 | // - its return type shall be a literal type; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 821 | QualType RT = NewFD->getReturnType(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 822 | if (!RT->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 823 | RequireLiteralType(NewFD->getLocation(), RT, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 824 | diag::err_constexpr_non_literal_return)) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 825 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 828 | // - each of its parameter types shall be a literal type; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 829 | if (!CheckConstexprParameterTypes(*this, NewFD)) |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 830 | return false; |
| 831 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 832 | return true; |
| 833 | } |
| 834 | |
| 835 | /// Check the given declaration statement is legal within a constexpr function |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 836 | /// body. C++11 [dcl.constexpr]p3,p4, and C++1y [dcl.constexpr]p3. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 837 | /// |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 838 | /// \return true if the body is OK (maybe only as an extension), false if we |
| 839 | /// have diagnosed a problem. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 840 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 841 | DeclStmt *DS, SourceLocation &Cxx1yLoc) { |
| 842 | // C++11 [dcl.constexpr]p3 and p4: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 843 | // The definition of a constexpr function(p3) or constructor(p4) [...] shall |
| 844 | // contain only |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 845 | for (const auto *DclIt : DS->decls()) { |
| 846 | switch (DclIt->getKind()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 847 | case Decl::StaticAssert: |
| 848 | case Decl::Using: |
| 849 | case Decl::UsingShadow: |
| 850 | case Decl::UsingDirective: |
| 851 | case Decl::UnresolvedUsingTypename: |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 852 | case Decl::UnresolvedUsingValue: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 853 | // - static_assert-declarations |
| 854 | // - using-declarations, |
| 855 | // - using-directives, |
| 856 | continue; |
| 857 | |
| 858 | case Decl::Typedef: |
| 859 | case Decl::TypeAlias: { |
| 860 | // - typedef declarations and alias-declarations that do not define |
| 861 | // classes or enumerations, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 862 | const auto *TN = cast<TypedefNameDecl>(DclIt); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 863 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 864 | // Don't allow variably-modified types in constexpr functions. |
| 865 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 866 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 867 | << TL.getSourceRange() << TL.getType() |
| 868 | << isa<CXXConstructorDecl>(Dcl); |
| 869 | return false; |
| 870 | } |
| 871 | continue; |
| 872 | } |
| 873 | |
| 874 | case Decl::Enum: |
| 875 | case Decl::CXXRecord: |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 876 | // C++1y allows types to be defined, not just declared. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 877 | if (cast<TagDecl>(DclIt)->isThisDeclarationADefinition()) |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 878 | SemaRef.Diag(DS->getLocStart(), |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 879 | SemaRef.getLangOpts().CPlusPlus14 |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 880 | ? diag::warn_cxx11_compat_constexpr_type_definition |
| 881 | : diag::ext_constexpr_type_definition) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 882 | << isa<CXXConstructorDecl>(Dcl); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 883 | continue; |
| 884 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 885 | case Decl::EnumConstant: |
| 886 | case Decl::IndirectField: |
| 887 | case Decl::ParmVar: |
| 888 | // These can only appear with other declarations which are banned in |
| 889 | // C++11 and permitted in C++1y, so ignore them. |
| 890 | continue; |
| 891 | |
| 892 | case Decl::Var: { |
| 893 | // C++1y [dcl.constexpr]p3 allows anything except: |
| 894 | // a definition of a variable of non-literal type or of static or |
| 895 | // thread storage duration or for which no initialization is performed. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 896 | const auto *VD = cast<VarDecl>(DclIt); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 897 | if (VD->isThisDeclarationADefinition()) { |
| 898 | if (VD->isStaticLocal()) { |
| 899 | SemaRef.Diag(VD->getLocation(), |
| 900 | diag::err_constexpr_local_var_static) |
| 901 | << isa<CXXConstructorDecl>(Dcl) |
| 902 | << (VD->getTLSKind() == VarDecl::TLS_Dynamic); |
| 903 | return false; |
| 904 | } |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 905 | if (!VD->getType()->isDependentType() && |
| 906 | SemaRef.RequireLiteralType( |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 907 | VD->getLocation(), VD->getType(), |
| 908 | diag::err_constexpr_local_var_non_literal_type, |
| 909 | isa<CXXConstructorDecl>(Dcl))) |
| 910 | return false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 911 | if (!VD->getType()->isDependentType() && |
| 912 | !VD->hasInit() && !VD->isCXXForRangeDecl()) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 913 | SemaRef.Diag(VD->getLocation(), |
| 914 | diag::err_constexpr_local_var_no_init) |
| 915 | << isa<CXXConstructorDecl>(Dcl); |
| 916 | return false; |
| 917 | } |
| 918 | } |
| 919 | SemaRef.Diag(VD->getLocation(), |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 920 | SemaRef.getLangOpts().CPlusPlus14 |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 921 | ? diag::warn_cxx11_compat_constexpr_local_var |
| 922 | : diag::ext_constexpr_local_var) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 923 | << isa<CXXConstructorDecl>(Dcl); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 924 | continue; |
| 925 | } |
| 926 | |
| 927 | case Decl::NamespaceAlias: |
| 928 | case Decl::Function: |
| 929 | // These are disallowed in C++11 and permitted in C++1y. Allow them |
| 930 | // everywhere as an extension. |
| 931 | if (!Cxx1yLoc.isValid()) |
| 932 | Cxx1yLoc = DS->getLocStart(); |
| 933 | continue; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 934 | |
| 935 | default: |
| 936 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 937 | << isa<CXXConstructorDecl>(Dcl); |
| 938 | return false; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | /// Check that the given field is initialized within a constexpr constructor. |
| 946 | /// |
| 947 | /// \param Dcl The constexpr constructor being checked. |
| 948 | /// \param Field The field being checked. This may be a member of an anonymous |
| 949 | /// struct or union nested within the class being checked. |
| 950 | /// \param Inits All declarations, including anonymous struct/union members and |
| 951 | /// indirect members, for which any initialization was provided. |
| 952 | /// \param Diagnosed Set to true if an error is produced. |
| 953 | static void CheckConstexprCtorInitializer(Sema &SemaRef, |
| 954 | const FunctionDecl *Dcl, |
| 955 | FieldDecl *Field, |
| 956 | llvm::SmallSet<Decl*, 16> &Inits, |
| 957 | bool &Diagnosed) { |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 958 | if (Field->isInvalidDecl()) |
| 959 | return; |
| 960 | |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 961 | if (Field->isUnnamedBitfield()) |
| 962 | return; |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 963 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 964 | // Anonymous unions with no variant members and empty anonymous structs do not |
| 965 | // need to be explicitly initialized. FIXME: Anonymous structs that contain no |
| 966 | // indirect fields don't need initializing. |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 967 | if (Field->isAnonymousStructOrUnion() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 968 | (Field->getType()->isUnionType() |
| 969 | ? !Field->getType()->getAsCXXRecordDecl()->hasVariantMembers() |
| 970 | : Field->getType()->getAsCXXRecordDecl()->isEmpty())) |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 971 | return; |
| 972 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 973 | if (!Inits.count(Field)) { |
| 974 | if (!Diagnosed) { |
| 975 | SemaRef.Diag(Dcl->getLocation(), diag::err_constexpr_ctor_missing_init); |
| 976 | Diagnosed = true; |
| 977 | } |
| 978 | SemaRef.Diag(Field->getLocation(), diag::note_constexpr_ctor_missing_init); |
| 979 | } else if (Field->isAnonymousStructOrUnion()) { |
| 980 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 981 | for (auto *I : RD->fields()) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 982 | // If an anonymous union contains an anonymous struct of which any member |
| 983 | // is initialized, all members must be initialized. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 984 | if (!RD->isUnion() || Inits.count(I)) |
| 985 | CheckConstexprCtorInitializer(SemaRef, Dcl, I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 989 | /// Check the provided statement is allowed in a constexpr function |
| 990 | /// definition. |
| 991 | static bool |
| 992 | CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S, |
Robert Wilhelm | e7205c0 | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 993 | SmallVectorImpl<SourceLocation> &ReturnStmts, |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 994 | SourceLocation &Cxx1yLoc) { |
| 995 | // - its function-body shall be [...] a compound-statement that contains only |
| 996 | switch (S->getStmtClass()) { |
| 997 | case Stmt::NullStmtClass: |
| 998 | // - null statements, |
| 999 | return true; |
| 1000 | |
| 1001 | case Stmt::DeclStmtClass: |
| 1002 | // - static_assert-declarations |
| 1003 | // - using-declarations, |
| 1004 | // - using-directives, |
| 1005 | // - typedef declarations and alias-declarations that do not define |
| 1006 | // classes or enumerations, |
| 1007 | if (!CheckConstexprDeclStmt(SemaRef, Dcl, cast<DeclStmt>(S), Cxx1yLoc)) |
| 1008 | return false; |
| 1009 | return true; |
| 1010 | |
| 1011 | case Stmt::ReturnStmtClass: |
| 1012 | // - and exactly one return statement; |
| 1013 | if (isa<CXXConstructorDecl>(Dcl)) { |
| 1014 | // C++1y allows return statements in constexpr constructors. |
| 1015 | if (!Cxx1yLoc.isValid()) |
| 1016 | Cxx1yLoc = S->getLocStart(); |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | ReturnStmts.push_back(S->getLocStart()); |
| 1021 | return true; |
| 1022 | |
| 1023 | case Stmt::CompoundStmtClass: { |
| 1024 | // C++1y allows compound-statements. |
| 1025 | if (!Cxx1yLoc.isValid()) |
| 1026 | Cxx1yLoc = S->getLocStart(); |
| 1027 | |
| 1028 | CompoundStmt *CompStmt = cast<CompoundStmt>(S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1029 | for (auto *BodyIt : CompStmt->body()) { |
| 1030 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, BodyIt, ReturnStmts, |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1031 | Cxx1yLoc)) |
| 1032 | return false; |
| 1033 | } |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | case Stmt::AttributedStmtClass: |
| 1038 | if (!Cxx1yLoc.isValid()) |
| 1039 | Cxx1yLoc = S->getLocStart(); |
| 1040 | return true; |
| 1041 | |
| 1042 | case Stmt::IfStmtClass: { |
| 1043 | // C++1y allows if-statements. |
| 1044 | if (!Cxx1yLoc.isValid()) |
| 1045 | Cxx1yLoc = S->getLocStart(); |
| 1046 | |
| 1047 | IfStmt *If = cast<IfStmt>(S); |
| 1048 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, If->getThen(), ReturnStmts, |
| 1049 | Cxx1yLoc)) |
| 1050 | return false; |
| 1051 | if (If->getElse() && |
| 1052 | !CheckConstexprFunctionStmt(SemaRef, Dcl, If->getElse(), ReturnStmts, |
| 1053 | Cxx1yLoc)) |
| 1054 | return false; |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | case Stmt::WhileStmtClass: |
| 1059 | case Stmt::DoStmtClass: |
| 1060 | case Stmt::ForStmtClass: |
| 1061 | case Stmt::CXXForRangeStmtClass: |
| 1062 | case Stmt::ContinueStmtClass: |
| 1063 | // C++1y allows all of these. We don't allow them as extensions in C++11, |
| 1064 | // because they don't make sense without variable mutation. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1065 | if (!SemaRef.getLangOpts().CPlusPlus14) |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1066 | break; |
| 1067 | if (!Cxx1yLoc.isValid()) |
| 1068 | Cxx1yLoc = S->getLocStart(); |
| 1069 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1070 | if (*Children && |
| 1071 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1072 | Cxx1yLoc)) |
| 1073 | return false; |
| 1074 | return true; |
| 1075 | |
| 1076 | case Stmt::SwitchStmtClass: |
| 1077 | case Stmt::CaseStmtClass: |
| 1078 | case Stmt::DefaultStmtClass: |
| 1079 | case Stmt::BreakStmtClass: |
| 1080 | // C++1y allows switch-statements, and since they don't need variable |
| 1081 | // mutation, we can reasonably allow them in C++11 as an extension. |
| 1082 | if (!Cxx1yLoc.isValid()) |
| 1083 | Cxx1yLoc = S->getLocStart(); |
| 1084 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1085 | if (*Children && |
| 1086 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1087 | Cxx1yLoc)) |
| 1088 | return false; |
| 1089 | return true; |
| 1090 | |
| 1091 | default: |
| 1092 | if (!isa<Expr>(S)) |
| 1093 | break; |
| 1094 | |
| 1095 | // C++1y allows expression-statements. |
| 1096 | if (!Cxx1yLoc.isValid()) |
| 1097 | Cxx1yLoc = S->getLocStart(); |
| 1098 | return true; |
| 1099 | } |
| 1100 | |
| 1101 | SemaRef.Diag(S->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 1102 | << isa<CXXConstructorDecl>(Dcl); |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1106 | /// Check the body for the given constexpr function declaration only contains |
| 1107 | /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4. |
| 1108 | /// |
| 1109 | /// \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] | 1110 | bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1111 | if (isa<CXXTryStmt>(Body)) { |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1112 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1113 | // The definition of a constexpr function shall satisfy the following |
| 1114 | // constraints: [...] |
| 1115 | // - its function-body shall be = delete, = default, or a |
| 1116 | // compound-statement |
| 1117 | // |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1118 | // C++11 [dcl.constexpr]p4: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1119 | // In the definition of a constexpr constructor, [...] |
| 1120 | // - its function-body shall not be a function-try-block; |
| 1121 | Diag(Body->getLocStart(), diag::err_constexpr_function_try_block) |
| 1122 | << isa<CXXConstructorDecl>(Dcl); |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1126 | SmallVector<SourceLocation, 4> ReturnStmts; |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1127 | |
| 1128 | // - its function-body shall be [...] a compound-statement that contains only |
| 1129 | // [... list of cases ...] |
| 1130 | CompoundStmt *CompBody = cast<CompoundStmt>(Body); |
| 1131 | SourceLocation Cxx1yLoc; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1132 | for (auto *BodyIt : CompBody->body()) { |
| 1133 | if (!CheckConstexprFunctionStmt(*this, Dcl, BodyIt, ReturnStmts, Cxx1yLoc)) |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1134 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1137 | if (Cxx1yLoc.isValid()) |
| 1138 | Diag(Cxx1yLoc, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1139 | getLangOpts().CPlusPlus14 |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1140 | ? diag::warn_cxx11_compat_constexpr_body_invalid_stmt |
| 1141 | : diag::ext_constexpr_body_invalid_stmt) |
| 1142 | << isa<CXXConstructorDecl>(Dcl); |
| 1143 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1144 | if (const CXXConstructorDecl *Constructor |
| 1145 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 1146 | const CXXRecordDecl *RD = Constructor->getParent(); |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 1147 | // DR1359: |
| 1148 | // - every non-variant non-static data member and base class sub-object |
| 1149 | // shall be initialized; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1150 | // DR1460: |
| 1151 | // - if the class is a union having variant members, exactly one of them |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 1152 | // shall be initialized; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1153 | if (RD->isUnion()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1154 | if (Constructor->getNumCtorInitializers() == 0 && |
| 1155 | RD->hasVariantMembers()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1156 | Diag(Dcl->getLocation(), diag::err_constexpr_union_ctor_no_init); |
| 1157 | return false; |
| 1158 | } |
Richard Smith | 6e43375 | 2011-10-10 16:38:04 +0000 | [diff] [blame] | 1159 | } else if (!Constructor->isDependentContext() && |
| 1160 | !Constructor->isDelegatingConstructor()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1161 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 1162 | |
| 1163 | // Skip detailed checking if we have enough initializers, and we would |
| 1164 | // allow at most one initializer per member. |
| 1165 | bool AnyAnonStructUnionMembers = false; |
| 1166 | unsigned Fields = 0; |
| 1167 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1168 | E = RD->field_end(); I != E; ++I, ++Fields) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1169 | if (I->isAnonymousStructOrUnion()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1170 | AnyAnonStructUnionMembers = true; |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1174 | // DR1460: |
| 1175 | // - if the class is a union-like class, but is not a union, for each of |
| 1176 | // its anonymous union members having variant members, exactly one of |
| 1177 | // them shall be initialized; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1178 | if (AnyAnonStructUnionMembers || |
| 1179 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 1180 | // Check initialization of non-static data members. Base classes are |
| 1181 | // always initialized so do not need to be checked. Dependent bases |
| 1182 | // might not have initializers in the member initializer list. |
| 1183 | llvm::SmallSet<Decl*, 16> Inits; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1184 | for (const auto *I: Constructor->inits()) { |
| 1185 | if (FieldDecl *FD = I->getMember()) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1186 | Inits.insert(FD); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1187 | else if (IndirectFieldDecl *ID = I->getIndirectMember()) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1188 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 1189 | } |
| 1190 | |
| 1191 | bool Diagnosed = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1192 | for (auto *I : RD->fields()) |
| 1193 | CheckConstexprCtorInitializer(*this, Dcl, I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1194 | if (Diagnosed) |
| 1195 | return false; |
| 1196 | } |
| 1197 | } |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1198 | } else { |
| 1199 | if (ReturnStmts.empty()) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1200 | // C++1y doesn't require constexpr functions to contain a 'return' |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1201 | // statement. We still do, unless the return type might be void, because |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1202 | // otherwise if there's no return statement, the function cannot |
| 1203 | // be used in a core constant expression. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1204 | bool OK = getLangOpts().CPlusPlus14 && |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1205 | (Dcl->getReturnType()->isVoidType() || |
| 1206 | Dcl->getReturnType()->isDependentType()); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1207 | Diag(Dcl->getLocation(), |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1208 | OK ? diag::warn_cxx11_compat_constexpr_body_no_return |
| 1209 | : diag::err_constexpr_body_no_return); |
| 1210 | return OK; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1211 | } |
| 1212 | if (ReturnStmts.size() > 1) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1213 | Diag(ReturnStmts.back(), |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1214 | getLangOpts().CPlusPlus14 |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1215 | ? diag::warn_cxx11_compat_constexpr_body_multiple_return |
| 1216 | : diag::ext_constexpr_body_multiple_return); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1217 | for (unsigned I = 0; I < ReturnStmts.size() - 1; ++I) |
| 1218 | Diag(ReturnStmts[I], diag::note_constexpr_body_previous_return); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1222 | // C++11 [dcl.constexpr]p5: |
| 1223 | // if no function argument values exist such that the function invocation |
| 1224 | // substitution would produce a constant expression, the program is |
| 1225 | // ill-formed; no diagnostic required. |
| 1226 | // C++11 [dcl.constexpr]p3: |
| 1227 | // - every constructor call and implicit conversion used in initializing the |
| 1228 | // return value shall be one of those allowed in a constant expression. |
| 1229 | // C++11 [dcl.constexpr]p4: |
| 1230 | // - every constructor involved in initializing non-static data members and |
| 1231 | // base class sub-objects shall be a constexpr constructor. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1232 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1233 | if (!Expr::isPotentialConstantExpr(Dcl, Diags)) { |
Richard Smith | afee0ff | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 1234 | Diag(Dcl->getLocation(), diag::ext_constexpr_function_never_constant_expr) |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1235 | << isa<CXXConstructorDecl>(Dcl); |
| 1236 | for (size_t I = 0, N = Diags.size(); I != N; ++I) |
| 1237 | Diag(Diags[I].first, Diags[I].second); |
Richard Smith | afee0ff | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 1238 | // Don't return false here: we allow this for compatibility in |
| 1239 | // system headers. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1242 | return true; |
| 1243 | } |
| 1244 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1245 | /// isCurrentClassName - Determine whether the identifier II is the |
| 1246 | /// name of the class type currently being defined. In the case of |
| 1247 | /// nested classes, this will only return true if II is the name of |
| 1248 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1249 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 1250 | const CXXScopeSpec *SS) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1251 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 1252 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1253 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1254 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 1255 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1256 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 1257 | } else |
| 1258 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1259 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 1260 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1261 | return &II == CurDecl->getIdentifier(); |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 1262 | return false; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 1265 | /// \brief Determine whether the identifier II is a typo for the name of |
| 1266 | /// the class type currently being defined. If so, update it to the identifier |
| 1267 | /// that should have been used. |
| 1268 | bool Sema::isCurrentClassNameTypo(IdentifierInfo *&II, const CXXScopeSpec *SS) { |
| 1269 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
| 1270 | |
| 1271 | if (!getLangOpts().SpellChecking) |
| 1272 | return false; |
| 1273 | |
| 1274 | CXXRecordDecl *CurDecl; |
| 1275 | if (SS && SS->isSet() && !SS->isInvalid()) { |
| 1276 | DeclContext *DC = computeDeclContext(*SS, true); |
| 1277 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 1278 | } else |
| 1279 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1280 | |
| 1281 | if (CurDecl && CurDecl->getIdentifier() && II != CurDecl->getIdentifier() && |
| 1282 | 3 * II->getName().edit_distance(CurDecl->getIdentifier()->getName()) |
| 1283 | < II->getLength()) { |
| 1284 | II = CurDecl->getIdentifier(); |
| 1285 | return true; |
| 1286 | } |
| 1287 | |
| 1288 | return false; |
| 1289 | } |
| 1290 | |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1291 | /// \brief Determine whether the given class is a base class of the given |
| 1292 | /// class, including looking at dependent bases. |
| 1293 | static bool findCircularInheritance(const CXXRecordDecl *Class, |
| 1294 | const CXXRecordDecl *Current) { |
| 1295 | SmallVector<const CXXRecordDecl*, 8> Queue; |
| 1296 | |
| 1297 | Class = Class->getCanonicalDecl(); |
| 1298 | while (true) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1299 | for (const auto &I : Current->bases()) { |
| 1300 | CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1301 | if (!Base) |
| 1302 | continue; |
| 1303 | |
| 1304 | Base = Base->getDefinition(); |
| 1305 | if (!Base) |
| 1306 | continue; |
| 1307 | |
| 1308 | if (Base->getCanonicalDecl() == Class) |
| 1309 | return true; |
| 1310 | |
| 1311 | Queue.push_back(Base); |
| 1312 | } |
| 1313 | |
| 1314 | if (Queue.empty()) |
| 1315 | return false; |
| 1316 | |
Robert Wilhelm | 344472e | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1317 | Current = Queue.pop_back_val(); |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | return false; |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1323 | /// \brief Perform propagation of DLL attributes from a derived class to a |
| 1324 | /// templated base class for MS compatibility. |
| 1325 | static void propagateDLLAttrToBaseClassTemplate( |
| 1326 | Sema &S, CXXRecordDecl *Class, Attr *ClassAttr, |
| 1327 | ClassTemplateSpecializationDecl *BaseTemplateSpec, SourceLocation BaseLoc) { |
| 1328 | if (getDLLAttr( |
| 1329 | BaseTemplateSpec->getSpecializedTemplate()->getTemplatedDecl())) { |
| 1330 | // If the base class template has a DLL attribute, don't try to change it. |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | if (BaseTemplateSpec->getSpecializationKind() == TSK_Undeclared) { |
| 1335 | // If the base class is not already specialized, we can do the propagation. |
| 1336 | auto *NewAttr = cast<InheritableAttr>(ClassAttr->clone(S.getASTContext())); |
| 1337 | NewAttr->setInherited(true); |
| 1338 | BaseTemplateSpec->addAttr(NewAttr); |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | bool DifferentAttribute = false; |
| 1343 | if (Attr *SpecializationAttr = getDLLAttr(BaseTemplateSpec)) { |
| 1344 | if (!SpecializationAttr->isInherited()) { |
| 1345 | // The template has previously been specialized or instantiated with an |
| 1346 | // explicit attribute. We should not try to change it. |
| 1347 | return; |
| 1348 | } |
| 1349 | if (SpecializationAttr->getKind() == ClassAttr->getKind()) { |
| 1350 | // The specialization already has the right attribute. |
| 1351 | return; |
| 1352 | } |
| 1353 | DifferentAttribute = true; |
| 1354 | } |
| 1355 | |
| 1356 | // The template was previously instantiated or explicitly specialized without |
| 1357 | // a dll attribute, or the template was previously instantiated with a |
| 1358 | // different inherited attribute. It's too late for us to change the |
| 1359 | // attribute, so warn that this is unsupported. |
| 1360 | S.Diag(BaseLoc, diag::warn_attribute_dll_instantiated_base_class) |
| 1361 | << BaseTemplateSpec->isExplicitSpecialization() << DifferentAttribute; |
| 1362 | S.Diag(ClassAttr->getLocation(), diag::note_attribute); |
| 1363 | if (BaseTemplateSpec->isExplicitSpecialization()) { |
| 1364 | S.Diag(BaseTemplateSpec->getLocation(), |
| 1365 | diag::note_template_class_explicit_specialization_was_here) |
| 1366 | << BaseTemplateSpec; |
| 1367 | } else { |
| 1368 | S.Diag(BaseTemplateSpec->getPointOfInstantiation(), |
| 1369 | diag::note_template_class_instantiation_was_here) |
| 1370 | << BaseTemplateSpec; |
| 1371 | } |
| 1372 | } |
| 1373 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1375 | /// |
| 1376 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 1377 | /// and returns NULL otherwise. |
| 1378 | CXXBaseSpecifier * |
| 1379 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 1380 | SourceRange SpecifierRange, |
| 1381 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1382 | TypeSourceInfo *TInfo, |
| 1383 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1384 | QualType BaseType = TInfo->getType(); |
| 1385 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1386 | // C++ [class.union]p1: |
| 1387 | // A union shall not have base classes. |
| 1388 | if (Class->isUnion()) { |
| 1389 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1390 | << SpecifierRange; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1391 | return nullptr; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1394 | if (EllipsisLoc.isValid() && |
| 1395 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1396 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1397 | << TInfo->getTypeLoc().getSourceRange(); |
| 1398 | EllipsisLoc = SourceLocation(); |
| 1399 | } |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1400 | |
| 1401 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 1402 | |
| 1403 | if (BaseType->isDependentType()) { |
| 1404 | // Make sure that we don't have circular inheritance among our dependent |
| 1405 | // bases. For non-dependent bases, the check for completeness below handles |
| 1406 | // this. |
| 1407 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 1408 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 1409 | ((BaseDecl = BaseDecl->getDefinition()) && |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1410 | findCircularInheritance(Class, BaseDecl))) { |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1411 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 1412 | << BaseType << Context.getTypeDeclType(Class); |
| 1413 | |
| 1414 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 1415 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 1416 | << BaseType; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1417 | |
| 1418 | return nullptr; |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1423 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1424 | Access, TInfo, EllipsisLoc); |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1425 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1426 | |
| 1427 | // Base specifiers must be record types. |
| 1428 | if (!BaseType->isRecordType()) { |
| 1429 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1430 | return nullptr; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | // C++ [class.union]p1: |
| 1434 | // A union shall not be used as a base class. |
| 1435 | if (BaseType->isUnionType()) { |
| 1436 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1437 | return nullptr; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1440 | // For the MS ABI, propagate DLL attributes to base class templates. |
| 1441 | if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 1442 | if (Attr *ClassAttr = getDLLAttr(Class)) { |
| 1443 | if (auto *BaseTemplate = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 1444 | BaseType->getAsCXXRecordDecl())) { |
| 1445 | propagateDLLAttrToBaseClassTemplate(*this, Class, ClassAttr, |
| 1446 | BaseTemplate, BaseLoc); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1451 | // C++ [class.derived]p2: |
| 1452 | // The class-name in a base-specifier shall not be an incompletely |
| 1453 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | if (RequireCompleteType(BaseLoc, BaseType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1455 | diag::err_incomplete_base_class, SpecifierRange)) { |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1456 | Class->setInvalidDecl(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1457 | return nullptr; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1458 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1459 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1460 | // 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] | 1461 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1462 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1463 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1464 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1465 | CXXRecordDecl *CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1466 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1467 | |
David Majnemer | 28165b7 | 2013-11-02 12:00:36 +0000 | [diff] [blame] | 1468 | // A class which contains a flexible array member is not suitable for use as a |
| 1469 | // base class: |
| 1470 | // - If the layout determines that a base comes before another base, |
| 1471 | // the flexible array member would index into the subsequent base. |
| 1472 | // - If the layout determines that base comes before the derived class, |
| 1473 | // the flexible array member would index into the derived class. |
| 1474 | if (CXXBaseDecl->hasFlexibleArrayMember()) { |
| 1475 | Diag(BaseLoc, diag::err_base_class_has_flexible_array_member) |
| 1476 | << CXXBaseDecl->getDeclName(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1477 | return nullptr; |
David Majnemer | 28165b7 | 2013-11-02 12:00:36 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1480 | // C++ [class]p3: |
David Majnemer | 7041fcc | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1481 | // If a class is marked final and it appears as a base-type-specifier in |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1482 | // base-clause, the program is ill-formed. |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1483 | if (FinalAttr *FA = CXXBaseDecl->getAttr<FinalAttr>()) { |
David Majnemer | 7041fcc | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1484 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1485 | << CXXBaseDecl->getDeclName() |
| 1486 | << FA->isSpelledAsSealed(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1487 | Diag(CXXBaseDecl->getLocation(), diag::note_entity_declared_at) |
| 1488 | << CXXBaseDecl->getDeclName() << FA->getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1489 | return nullptr; |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1492 | if (BaseDecl->isInvalidDecl()) |
| 1493 | Class->setInvalidDecl(); |
David Majnemer | 7041fcc | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1494 | |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1495 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1496 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1497 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1498 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1501 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1502 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | /// example: |
| 1504 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1505 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1506 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1507 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1508 | ParsedAttributes &Attributes, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1509 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1510 | ParsedType basetype, SourceLocation BaseLoc, |
| 1511 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1512 | if (!classdecl) |
| 1513 | return true; |
| 1514 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1515 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1516 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1517 | if (!Class) |
| 1518 | return true; |
| 1519 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1520 | // We haven't yet attached the base specifiers. |
| 1521 | Class->setIsParsingBaseSpecifiers(); |
| 1522 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1523 | // We do not support any C++11 attributes on base-specifiers yet. |
| 1524 | // Diagnose any attributes we see. |
| 1525 | if (!Attributes.empty()) { |
| 1526 | for (AttributeList *Attr = Attributes.getList(); Attr; |
| 1527 | Attr = Attr->getNext()) { |
| 1528 | if (Attr->isInvalid() || |
| 1529 | Attr->getKind() == AttributeList::IgnoredAttribute) |
| 1530 | continue; |
| 1531 | Diag(Attr->getLoc(), |
| 1532 | Attr->getKind() == AttributeList::UnknownAttribute |
| 1533 | ? diag::warn_unknown_attribute_ignored |
| 1534 | : diag::err_base_specifier_attribute) |
| 1535 | << Attr->getName(); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1539 | TypeSourceInfo *TInfo = nullptr; |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1540 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1541 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1542 | if (EllipsisLoc.isInvalid() && |
| 1543 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1544 | UPPC_BaseType)) |
| 1545 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1547 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1548 | Virtual, Access, TInfo, |
| 1549 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1550 | return BaseSpec; |
Douglas Gregor | 8a50fe0 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 1551 | else |
| 1552 | Class->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1554 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1555 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1556 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1557 | /// Use small set to collect indirect bases. As this is only used |
| 1558 | /// locally, there's no need to abstract the small size parameter. |
| 1559 | typedef llvm::SmallPtrSet<QualType, 4> IndirectBaseSet; |
| 1560 | |
| 1561 | /// \brief Recursively add the bases of Type. Don't add Type itself. |
| 1562 | static void |
| 1563 | NoteIndirectBases(ASTContext &Context, IndirectBaseSet &Set, |
| 1564 | const QualType &Type) |
| 1565 | { |
| 1566 | // Even though the incoming type is a base, it might not be |
| 1567 | // a class -- it could be a template parm, for instance. |
| 1568 | if (auto Rec = Type->getAs<RecordType>()) { |
| 1569 | auto Decl = Rec->getAsCXXRecordDecl(); |
| 1570 | |
| 1571 | // Iterate over its bases. |
| 1572 | for (const auto &BaseSpec : Decl->bases()) { |
| 1573 | QualType Base = Context.getCanonicalType(BaseSpec.getType()) |
| 1574 | .getUnqualifiedType(); |
| 1575 | if (Set.insert(Base).second) |
| 1576 | // If we've not already seen it, recurse. |
| 1577 | NoteIndirectBases(Context, Set, Base); |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1582 | /// \brief Performs the actual work of attaching the given base class |
| 1583 | /// specifiers to a C++ class. |
| 1584 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1585 | unsigned NumBases) { |
| 1586 | if (NumBases == 0) |
| 1587 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1588 | |
| 1589 | // Used to keep track of which base types we have already seen, so |
| 1590 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1591 | // that the key is always the unqualified canonical type of the base |
| 1592 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1593 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1594 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1595 | // Used to track indirect bases so we can see if a direct base is |
| 1596 | // ambiguous. |
| 1597 | IndirectBaseSet IndirectBaseTypes; |
| 1598 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1599 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1600 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1601 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1602 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1604 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1605 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1606 | |
| 1607 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1608 | if (KnownBase) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1609 | // C++ [class.mi]p3: |
| 1610 | // A class shall not be specified as a direct base class of a |
| 1611 | // derived class more than once. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1612 | Diag(Bases[idx]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1613 | diag::err_duplicate_base_class) |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1614 | << KnownBase->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1615 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1616 | |
| 1617 | // Delete the duplicate base class specifier; we're going to |
| 1618 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1619 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1620 | |
| 1621 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1622 | } else { |
| 1623 | // Okay, add this new base class. |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1624 | KnownBase = Bases[idx]; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1625 | Bases[NumGoodBases++] = Bases[idx]; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1626 | |
| 1627 | // Note this base's direct & indirect bases, if there could be ambiguity. |
| 1628 | if (NumBases > 1) |
| 1629 | NoteIndirectBases(Context, IndirectBaseTypes, NewBaseType); |
| 1630 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1631 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 1632 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 1633 | if (Class->isInterface() && |
| 1634 | (!RD->isInterface() || |
| 1635 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 1636 | // The Microsoft extension __interface does not permit bases that |
| 1637 | // are not themselves public interfaces. |
| 1638 | Diag(KnownBase->getLocStart(), diag::err_invalid_base_in_interface) |
| 1639 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getName() |
| 1640 | << RD->getSourceRange(); |
| 1641 | Invalid = true; |
| 1642 | } |
| 1643 | if (RD->hasAttr<WeakAttr>()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1644 | Class->addAttr(WeakAttr::CreateImplicit(Context)); |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1645 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1650 | Class->setBases(Bases, NumGoodBases); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1651 | |
| 1652 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) { |
| 1653 | // Check whether this direct base is inaccessible due to ambiguity. |
| 1654 | QualType BaseType = Bases[idx]->getType(); |
| 1655 | CanQualType CanonicalBase = Context.getCanonicalType(BaseType) |
| 1656 | .getUnqualifiedType(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1657 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1658 | if (IndirectBaseTypes.count(CanonicalBase)) { |
| 1659 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1660 | /*DetectVirtual=*/true); |
| 1661 | bool found |
| 1662 | = Class->isDerivedFrom(CanonicalBase->getAsCXXRecordDecl(), Paths); |
| 1663 | assert(found); |
| 1664 | (void)found; |
| 1665 | |
| 1666 | if (Paths.isAmbiguous(CanonicalBase)) |
| 1667 | Diag(Bases[idx]->getLocStart (), diag::warn_inaccessible_base_class) |
| 1668 | << BaseType << getAmbiguousPathsDisplayString(Paths) |
| 1669 | << Bases[idx]->getSourceRange(); |
| 1670 | else |
| 1671 | assert(Bases[idx]->isVirtual()); |
| 1672 | } |
| 1673 | |
| 1674 | // Delete the base class specifier, since its data has been copied |
| 1675 | // into the CXXRecordDecl. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1676 | Context.Deallocate(Bases[idx]); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1677 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1678 | |
| 1679 | return Invalid; |
| 1680 | } |
| 1681 | |
| 1682 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1683 | /// class, after checking whether there are any duplicate base |
| 1684 | /// classes. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1685 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1686 | unsigned NumBases) { |
| 1687 | if (!ClassDecl || !Bases || !NumBases) |
| 1688 | return; |
| 1689 | |
| 1690 | AdjustDeclIfTemplate(ClassDecl); |
Robert Wilhelm | 0d317a0 | 2013-07-22 05:04:01 +0000 | [diff] [blame] | 1691 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases, NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1692 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1693 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1694 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1695 | /// derived from the type \p Base. |
| 1696 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1697 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1698 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1700 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1701 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1702 | return false; |
| 1703 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1704 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1705 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1706 | return false; |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1707 | |
| 1708 | // If either the base or the derived type is invalid, don't try to |
| 1709 | // check whether one is derived from the other. |
| 1710 | if (BaseRD->isInvalidDecl() || DerivedRD->isInvalidDecl()) |
| 1711 | return false; |
| 1712 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1713 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1714 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1718 | /// derived from the type \p Base. |
| 1719 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1720 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1721 | return false; |
| 1722 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1723 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1724 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1725 | return false; |
| 1726 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1727 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1728 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1729 | return false; |
| 1730 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1731 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1732 | } |
| 1733 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1734 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1735 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1736 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1737 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1738 | |
| 1739 | const CXXBasePath &Path = Paths.front(); |
| 1740 | |
| 1741 | // We first go backward and check if we have a virtual base. |
| 1742 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1743 | // the nearest virtual base. |
| 1744 | unsigned Start = 0; |
| 1745 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1746 | if (Path[I - 1].Base->isVirtual()) { |
| 1747 | Start = I - 1; |
| 1748 | break; |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | // Now add all bases. |
| 1753 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1754 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1757 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1758 | /// conversion (where Derived and Base are class types) is |
| 1759 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1760 | /// that all of the base classes are accessible). Returns true |
| 1761 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1762 | /// otherwise. Loc is the location where this routine should point to |
| 1763 | /// if there is an error, and Range is the source range to highlight |
| 1764 | /// if there is an error. |
| 1765 | bool |
| 1766 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1767 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1768 | unsigned AmbigiousBaseConvID, |
| 1769 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1770 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1771 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1772 | // First, determine whether the path from Derived to Base is |
| 1773 | // ambiguous. This is slightly more expensive than checking whether |
| 1774 | // the Derived to Base conversion exists, because here we need to |
| 1775 | // explore multiple paths to determine if there is an ambiguity. |
| 1776 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1777 | /*DetectVirtual=*/false); |
| 1778 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1779 | assert(DerivationOkay && |
| 1780 | "Can only be used with a derived-to-base conversion"); |
| 1781 | (void)DerivationOkay; |
| 1782 | |
| 1783 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1784 | if (InaccessibleBaseID) { |
| 1785 | // Check that the base class can be accessed. |
| 1786 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1787 | InaccessibleBaseID)) { |
| 1788 | case AR_inaccessible: |
| 1789 | return true; |
| 1790 | case AR_accessible: |
| 1791 | case AR_dependent: |
| 1792 | case AR_delayed: |
| 1793 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1794 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1795 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1796 | |
| 1797 | // Build a base path if necessary. |
| 1798 | if (BasePath) |
| 1799 | BuildBasePathArray(Paths, *BasePath); |
| 1800 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1803 | if (AmbigiousBaseConvID) { |
| 1804 | // We know that the derived-to-base conversion is ambiguous, and |
| 1805 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1806 | // search just one more time to compute all of the possible paths so |
| 1807 | // that we can print them out. This is more expensive than any of |
| 1808 | // the previous derived-to-base checks we've done, but at this point |
| 1809 | // performance isn't as much of an issue. |
| 1810 | Paths.clear(); |
| 1811 | Paths.setRecordingPaths(true); |
| 1812 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1813 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1814 | (void)StillOkay; |
| 1815 | |
| 1816 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1817 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1818 | // conversions in the diagnostic. We only print one of the paths |
| 1819 | // to each base class subobject. |
| 1820 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1821 | |
| 1822 | Diag(Loc, AmbigiousBaseConvID) |
| 1823 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1824 | } |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1825 | return true; |
| 1826 | } |
| 1827 | |
| 1828 | bool |
| 1829 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1830 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1831 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1832 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1833 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1834 | IgnoreAccess ? 0 |
| 1835 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1836 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1837 | Loc, Range, DeclarationName(), |
| 1838 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | /// @brief Builds a string representing ambiguous paths from a |
| 1843 | /// specific derived class to different subobjects of the same base |
| 1844 | /// class. |
| 1845 | /// |
| 1846 | /// This function builds a string that can be used in error messages |
| 1847 | /// to show the different paths that one can take through the |
| 1848 | /// inheritance hierarchy to go from the derived class to different |
| 1849 | /// subobjects of a base class. The result looks something like this: |
| 1850 | /// @code |
| 1851 | /// struct D -> struct B -> struct A |
| 1852 | /// struct D -> struct C -> struct A |
| 1853 | /// @endcode |
| 1854 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1855 | std::string PathDisplayStr; |
| 1856 | std::set<unsigned> DisplayedPaths; |
| 1857 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1858 | Path != Paths.end(); ++Path) { |
| 1859 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1860 | // We haven't displayed a path to this particular base |
| 1861 | // class subobject yet. |
| 1862 | PathDisplayStr += "\n "; |
| 1863 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1864 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1865 | Element != Path->end(); ++Element) |
| 1866 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | return PathDisplayStr; |
| 1871 | } |
| 1872 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1873 | //===----------------------------------------------------------------------===// |
| 1874 | // C++ class member Handling |
| 1875 | //===----------------------------------------------------------------------===// |
| 1876 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1877 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1878 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1879 | SourceLocation ASLoc, |
| 1880 | SourceLocation ColonLoc, |
| 1881 | AttributeList *Attrs) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1882 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1883 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1884 | ASLoc, ColonLoc); |
| 1885 | CurContext->addHiddenDecl(ASDecl); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1886 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1889 | /// CheckOverrideControl - Check C++11 override control semantics. |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1890 | void Sema::CheckOverrideControl(NamedDecl *D) { |
Richard Smith | cddbc1d | 2012-09-06 18:32:18 +0000 | [diff] [blame] | 1891 | if (D->isInvalidDecl()) |
| 1892 | return; |
| 1893 | |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1894 | // We only care about "override" and "final" declarations. |
| 1895 | if (!D->hasAttr<OverrideAttr>() && !D->hasAttr<FinalAttr>()) |
| 1896 | return; |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1897 | |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1898 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1899 | |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1900 | // We can't check dependent instance methods. |
| 1901 | if (MD && MD->isInstance() && |
| 1902 | (MD->getParent()->hasAnyDependentBases() || |
| 1903 | MD->getType()->isDependentType())) |
| 1904 | return; |
| 1905 | |
| 1906 | if (MD && !MD->isVirtual()) { |
| 1907 | // If we have a non-virtual method, check if if hides a virtual method. |
| 1908 | // (In that case, it's most likely the method has the wrong type.) |
| 1909 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 1910 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 1911 | |
| 1912 | if (!OverloadedMethods.empty()) { |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1913 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1914 | Diag(OA->getLocation(), |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1915 | diag::override_keyword_hides_virtual_member_function) |
| 1916 | << "override" << (OverloadedMethods.size() > 1); |
| 1917 | } else if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1918 | Diag(FA->getLocation(), |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1919 | diag::override_keyword_hides_virtual_member_function) |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1920 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 1921 | << (OverloadedMethods.size() > 1); |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1922 | } |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1923 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
| 1924 | MD->setInvalidDecl(); |
| 1925 | return; |
| 1926 | } |
| 1927 | // Fall through into the general case diagnostic. |
| 1928 | // FIXME: We might want to attempt typo correction here. |
| 1929 | } |
| 1930 | |
| 1931 | if (!MD || !MD->isVirtual()) { |
| 1932 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1933 | Diag(OA->getLocation(), |
| 1934 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1935 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 1936 | D->dropAttr<OverrideAttr>(); |
| 1937 | } |
| 1938 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 1939 | Diag(FA->getLocation(), |
| 1940 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1941 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 1942 | << FixItHint::CreateRemoval(FA->getLocation()); |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1943 | D->dropAttr<FinalAttr>(); |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1944 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1945 | return; |
| 1946 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1947 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1948 | // C++11 [class.virtual]p5: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1949 | // If a function is marked with the virt-specifier override and |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1950 | // does not override a member function of a base class, the program is |
| 1951 | // ill-formed. |
| 1952 | bool HasOverriddenMethods = |
| 1953 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
| 1954 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 1955 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 1956 | << MD->getDeclName(); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1959 | void Sema::DiagnoseAbsenceOfOverrideControl(NamedDecl *D) { |
| 1960 | if (D->isInvalidDecl() || D->hasAttr<OverrideAttr>()) |
| 1961 | return; |
| 1962 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
| 1963 | if (!MD || MD->isImplicit() || MD->hasAttr<FinalAttr>() || |
| 1964 | isa<CXXDestructorDecl>(MD)) |
| 1965 | return; |
| 1966 | |
| 1967 | SourceLocation Loc = MD->getLocation(); |
| 1968 | SourceLocation SpellingLoc = Loc; |
| 1969 | if (getSourceManager().isMacroArgExpansion(Loc)) |
| 1970 | SpellingLoc = getSourceManager().getImmediateExpansionRange(Loc).first; |
| 1971 | SpellingLoc = getSourceManager().getSpellingLoc(SpellingLoc); |
| 1972 | if (SpellingLoc.isValid() && getSourceManager().isInSystemHeader(SpellingLoc)) |
| 1973 | return; |
| 1974 | |
| 1975 | if (MD->size_overridden_methods() > 0) { |
| 1976 | Diag(MD->getLocation(), diag::warn_function_marked_not_override_overriding) |
| 1977 | << MD->getDeclName(); |
| 1978 | const CXXMethodDecl *OMD = *MD->begin_overridden_methods(); |
| 1979 | Diag(OMD->getLocation(), diag::note_overridden_virtual_function); |
| 1980 | } |
| 1981 | } |
| 1982 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1983 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1984 | /// function overrides a virtual member function marked 'final', according to |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1985 | /// C++11 [class.virtual]p4. |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1986 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1987 | const CXXMethodDecl *Old) { |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1988 | FinalAttr *FA = Old->getAttr<FinalAttr>(); |
| 1989 | if (!FA) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1990 | return false; |
| 1991 | |
| 1992 | Diag(New->getLocation(), diag::err_final_function_overridden) |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1993 | << New->getDeclName() |
| 1994 | << FA->isSpelledAsSealed(); |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1995 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1996 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1999 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2000 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 2001 | // FIXME: Destruction of ObjC lifetime types has side-effects. |
| 2002 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 2003 | return !RD->isCompleteDefinition() || |
| 2004 | !RD->hasTrivialDefaultConstructor() || |
| 2005 | !RD->hasTrivialDestructor(); |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2006 | return false; |
| 2007 | } |
| 2008 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2009 | static AttributeList *getMSPropertyAttr(AttributeList *list) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2010 | for (AttributeList *it = list; it != nullptr; it = it->getNext()) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2011 | if (it->isDeclspecPropertyAttribute()) |
| 2012 | return it; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2013 | return nullptr; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2016 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 2017 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2018 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2019 | /// one has been parsed, and 'InitStyle' is set if an in-class initializer is |
| 2020 | /// present (but parsing it has been deferred). |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 2021 | NamedDecl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2022 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2023 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 2024 | Expr *BW, const VirtSpecifiers &VS, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2025 | InClassInitStyle InitStyle) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2026 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2027 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 2028 | DeclarationName Name = NameInfo.getName(); |
| 2029 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 2030 | |
| 2031 | // For anonymous bitfields, the location should point to the type. |
| 2032 | if (Loc.isInvalid()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2033 | Loc = D.getLocStart(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 2034 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2035 | Expr *BitWidth = static_cast<Expr*>(BW); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2036 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 2037 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2038 | assert(!DS.isFriendSpecified()); |
| 2039 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 2040 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 2041 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2042 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 2043 | // The Microsoft extension __interface only permits public member functions |
| 2044 | // and prohibits constructors, destructors, operators, non-public member |
| 2045 | // functions, static methods and data members. |
| 2046 | unsigned InvalidDecl; |
| 2047 | bool ShowDeclName = true; |
| 2048 | if (!isFunc) |
| 2049 | InvalidDecl = (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) ? 0 : 1; |
| 2050 | else if (AS != AS_public) |
| 2051 | InvalidDecl = 2; |
| 2052 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 2053 | InvalidDecl = 3; |
| 2054 | else switch (Name.getNameKind()) { |
| 2055 | case DeclarationName::CXXConstructorName: |
| 2056 | InvalidDecl = 4; |
| 2057 | ShowDeclName = false; |
| 2058 | break; |
| 2059 | |
| 2060 | case DeclarationName::CXXDestructorName: |
| 2061 | InvalidDecl = 5; |
| 2062 | ShowDeclName = false; |
| 2063 | break; |
| 2064 | |
| 2065 | case DeclarationName::CXXOperatorName: |
| 2066 | case DeclarationName::CXXConversionFunctionName: |
| 2067 | InvalidDecl = 6; |
| 2068 | break; |
| 2069 | |
| 2070 | default: |
| 2071 | InvalidDecl = 0; |
| 2072 | break; |
| 2073 | } |
| 2074 | |
| 2075 | if (InvalidDecl) { |
| 2076 | if (ShowDeclName) |
| 2077 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 2078 | << (InvalidDecl-1) << Name; |
| 2079 | else |
| 2080 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 2081 | << (InvalidDecl-1) << ""; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2082 | return nullptr; |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 2083 | } |
| 2084 | } |
| 2085 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2086 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 2087 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2088 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 2089 | // data members and cannot be applied to names declared const or static, |
| 2090 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2091 | switch (DS.getStorageClassSpec()) { |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2092 | case DeclSpec::SCS_unspecified: |
| 2093 | case DeclSpec::SCS_typedef: |
| 2094 | case DeclSpec::SCS_static: |
| 2095 | break; |
| 2096 | case DeclSpec::SCS_mutable: |
| 2097 | if (isFunc) { |
| 2098 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2100 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 2101 | // declarator. Otherwise we could get follow-up errors. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2102 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2103 | } |
| 2104 | break; |
| 2105 | default: |
| 2106 | Diag(DS.getStorageClassSpecLoc(), |
| 2107 | diag::err_storageclass_invalid_for_member); |
| 2108 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 2109 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2112 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 2113 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 2114 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2115 | |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2116 | if (DS.isConstexprSpecified() && isInstField) { |
| 2117 | SemaDiagnosticBuilder B = |
| 2118 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr_member); |
| 2119 | SourceLocation ConstexprLoc = DS.getConstexprSpecLoc(); |
| 2120 | if (InitStyle == ICIS_NoInit) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2121 | B << 0 << 0; |
| 2122 | if (D.getDeclSpec().getTypeQualifiers() & DeclSpec::TQ_const) |
| 2123 | B << FixItHint::CreateRemoval(ConstexprLoc); |
| 2124 | else { |
| 2125 | B << FixItHint::CreateReplacement(ConstexprLoc, "const"); |
| 2126 | D.getMutableDeclSpec().ClearConstexprSpec(); |
| 2127 | const char *PrevSpec; |
| 2128 | unsigned DiagID; |
| 2129 | bool Failed = D.getMutableDeclSpec().SetTypeQual( |
| 2130 | DeclSpec::TQ_const, ConstexprLoc, PrevSpec, DiagID, getLangOpts()); |
| 2131 | (void)Failed; |
| 2132 | assert(!Failed && "Making a constexpr member const shouldn't fail"); |
| 2133 | } |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2134 | } else { |
| 2135 | B << 1; |
| 2136 | const char *PrevSpec; |
| 2137 | unsigned DiagID; |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2138 | if (D.getMutableDeclSpec().SetStorageClassSpec( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2139 | *this, DeclSpec::SCS_static, ConstexprLoc, PrevSpec, DiagID, |
| 2140 | Context.getPrintingPolicy())) { |
Matt Beaumont-Gay | 3e55e3e | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 2141 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_mutable && |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 2142 | "This is the only DeclSpec that should fail to be applied"); |
| 2143 | B << 1; |
| 2144 | } else { |
| 2145 | B << 0 << FixItHint::CreateInsertion(ConstexprLoc, "static "); |
| 2146 | isInstField = false; |
| 2147 | } |
| 2148 | } |
| 2149 | } |
| 2150 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 2151 | NamedDecl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 2152 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2153 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Data members must have identifiers for names. |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 2156 | if (!Name.isIdentifier()) { |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 2157 | Diag(Loc, diag::err_bad_variable_name) |
| 2158 | << Name; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2159 | return nullptr; |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 2160 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2161 | |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 2162 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 2163 | |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2164 | // Member field could not be with "template" keyword. |
| 2165 | // So TemplateParameterLists should be empty in this case. |
| 2166 | if (TemplateParameterLists.size()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2167 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2168 | if (TemplateParams->size()) { |
| 2169 | // There is no such thing as a member field template. |
| 2170 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 2171 | << II |
| 2172 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 2173 | TemplateParams->getRAngleLoc()); |
| 2174 | } else { |
| 2175 | // There is an extraneous 'template<>' for this member. |
| 2176 | Diag(TemplateParams->getTemplateLoc(), |
| 2177 | diag::err_template_member_noparams) |
| 2178 | << II |
| 2179 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 2180 | TemplateParams->getRAngleLoc()); |
| 2181 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2182 | return nullptr; |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2185 | if (SS.isSet() && !SS.isInvalid()) { |
| 2186 | // The user provided a superfluous scope specifier inside a class |
| 2187 | // definition: |
| 2188 | // |
| 2189 | // class X { |
| 2190 | // int X::member; |
| 2191 | // }; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 2192 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 2193 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc()); |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2194 | else |
| 2195 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 2196 | << Name << SS.getRange(); |
Douglas Gregor | 5d8419c | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 2197 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2198 | SS.clear(); |
| 2199 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2200 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2201 | AttributeList *MSPropertyAttr = |
| 2202 | getMSPropertyAttr(D.getDeclSpec().getAttributes().getList()); |
Eli Friedman | b26f012 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 2203 | if (MSPropertyAttr) { |
| 2204 | Member = HandleMSProperty(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 2205 | BitWidth, InitStyle, AS, MSPropertyAttr); |
| 2206 | if (!Member) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2207 | return nullptr; |
Eli Friedman | b26f012 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 2208 | isInstField = false; |
| 2209 | } else { |
| 2210 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 2211 | BitWidth, InitStyle, AS); |
| 2212 | assert(Member && "HandleField never returns null"); |
| 2213 | } |
| 2214 | } else { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2215 | assert(InitStyle == ICIS_NoInit || |
| 2216 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static); |
Eli Friedman | b26f012 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 2217 | |
| 2218 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
| 2219 | if (!Member) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2220 | return nullptr; |
Eli Friedman | b26f012 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 2221 | |
| 2222 | // Non-instance-fields can't have a bitfield. |
| 2223 | if (BitWidth) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2224 | if (Member->isInvalidDecl()) { |
| 2225 | // don't emit another diagnostic. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2226 | } else if (isa<VarDecl>(Member) || isa<VarTemplateDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2227 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 2228 | // "static member 'A' cannot be a bit-field" |
| 2229 | Diag(Loc, diag::err_static_not_bitfield) |
| 2230 | << Name << BitWidth->getSourceRange(); |
| 2231 | } else if (isa<TypedefDecl>(Member)) { |
| 2232 | // "typedef member 'x' cannot be a bit-field" |
| 2233 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 2234 | << Name << BitWidth->getSourceRange(); |
| 2235 | } else { |
| 2236 | // A function typedef ("typedef int f(); f a;"). |
| 2237 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 2238 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 2240 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2243 | BitWidth = nullptr; |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2244 | Member->setInvalidDecl(); |
| 2245 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 2246 | |
| 2247 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2249 | // If we have declared a member function template or static data member |
| 2250 | // template, set the access of the templated declaration as well. |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2251 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 2252 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2253 | else if (VarTemplateDecl *VarTmpl = dyn_cast<VarTemplateDecl>(Member)) |
| 2254 | VarTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 2255 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2256 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2257 | if (VS.isOverrideSpecified()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2258 | Member->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context, 0)); |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2259 | if (VS.isFinalSpecified()) |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2260 | Member->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context, |
| 2261 | VS.isFinalSpelledSealed())); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2262 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2263 | if (VS.getLastLocation().isValid()) { |
| 2264 | // Update the end location of a method that has a virt-specifiers. |
| 2265 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 2266 | MD->setRangeEnd(VS.getLastLocation()); |
| 2267 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2268 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 2269 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 2271 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2272 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2273 | if (isInstField) { |
| 2274 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 2275 | FieldCollector->Add(FD); |
| 2276 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2277 | if (!Diags.isIgnored(diag::warn_unused_private_field, FD->getLocation())) { |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2278 | // Remember all explicit private FieldDecls that have a name, no side |
| 2279 | // effects and are not part of a dependent type declaration. |
| 2280 | if (!FD->isImplicit() && FD->getDeclName() && |
| 2281 | FD->getAccess() == AS_private && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 2282 | !FD->hasAttr<UnusedAttr>() && |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2283 | !FD->getParent()->isDependentContext() && |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2284 | !InitializationHasSideEffects(*FD)) |
| 2285 | UnusedPrivateFields.insert(FD); |
| 2286 | } |
| 2287 | } |
| 2288 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2289 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2292 | namespace { |
| 2293 | class UninitializedFieldVisitor |
| 2294 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 2295 | Sema &S; |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2296 | // List of Decls to generate a warning on. Also remove Decls that become |
| 2297 | // initialized. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2298 | llvm::SmallPtrSetImpl<ValueDecl*> &Decls; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2299 | // List of base classes of the record. Classes are removed after their |
| 2300 | // initializers. |
| 2301 | llvm::SmallPtrSetImpl<QualType> &BaseClasses; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2302 | // Vector of decls to be removed from the Decl set prior to visiting the |
| 2303 | // nodes. These Decls may have been initialized in the prior initializer. |
| 2304 | llvm::SmallVector<ValueDecl*, 4> DeclsToRemove; |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2305 | // If non-null, add a note to the warning pointing back to the constructor. |
| 2306 | const CXXConstructorDecl *Constructor; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2307 | // Variables to hold state when processing an initializer list. When |
| 2308 | // InitList is true, special case initialization of FieldDecls matching |
| 2309 | // InitListFieldDecl. |
| 2310 | bool InitList; |
| 2311 | FieldDecl *InitListFieldDecl; |
| 2312 | llvm::SmallVector<unsigned, 4> InitFieldIndex; |
| 2313 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2314 | public: |
| 2315 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2316 | UninitializedFieldVisitor(Sema &S, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2317 | llvm::SmallPtrSetImpl<ValueDecl*> &Decls, |
| 2318 | llvm::SmallPtrSetImpl<QualType> &BaseClasses) |
| 2319 | : Inherited(S.Context), S(S), Decls(Decls), BaseClasses(BaseClasses), |
| 2320 | Constructor(nullptr), InitList(false), InitListFieldDecl(nullptr) {} |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2321 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2322 | // Returns true if the use of ME is not an uninitialized use. |
| 2323 | bool IsInitListMemberExprInitialized(MemberExpr *ME, |
| 2324 | bool CheckReferenceOnly) { |
| 2325 | llvm::SmallVector<FieldDecl*, 4> Fields; |
| 2326 | bool ReferenceField = false; |
| 2327 | while (ME) { |
| 2328 | FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 2329 | if (!FD) |
| 2330 | return false; |
| 2331 | Fields.push_back(FD); |
| 2332 | if (FD->getType()->isReferenceType()) |
| 2333 | ReferenceField = true; |
| 2334 | ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 2335 | } |
| 2336 | |
| 2337 | // Binding a reference to an unintialized field is not an |
| 2338 | // uninitialized use. |
| 2339 | if (CheckReferenceOnly && !ReferenceField) |
| 2340 | return true; |
| 2341 | |
| 2342 | llvm::SmallVector<unsigned, 4> UsedFieldIndex; |
| 2343 | // Discard the first field since it is the field decl that is being |
| 2344 | // initialized. |
| 2345 | for (auto I = Fields.rbegin() + 1, E = Fields.rend(); I != E; ++I) { |
| 2346 | UsedFieldIndex.push_back((*I)->getFieldIndex()); |
| 2347 | } |
| 2348 | |
| 2349 | for (auto UsedIter = UsedFieldIndex.begin(), |
| 2350 | UsedEnd = UsedFieldIndex.end(), |
| 2351 | OrigIter = InitFieldIndex.begin(), |
| 2352 | OrigEnd = InitFieldIndex.end(); |
| 2353 | UsedIter != UsedEnd && OrigIter != OrigEnd; ++UsedIter, ++OrigIter) { |
| 2354 | if (*UsedIter < *OrigIter) |
| 2355 | return true; |
| 2356 | if (*UsedIter > *OrigIter) |
| 2357 | break; |
| 2358 | } |
| 2359 | |
| 2360 | return false; |
| 2361 | } |
| 2362 | |
| 2363 | void HandleMemberExpr(MemberExpr *ME, bool CheckReferenceOnly, |
| 2364 | bool AddressOf) { |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2365 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
| 2366 | return; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2367 | |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2368 | // FieldME is the inner-most MemberExpr that is not an anonymous struct |
| 2369 | // or union. |
| 2370 | MemberExpr *FieldME = ME; |
| 2371 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2372 | bool AllPODFields = FieldME->getType().isPODType(S.Context); |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2373 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2374 | Expr *Base = ME; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2375 | while (MemberExpr *SubME = |
| 2376 | dyn_cast<MemberExpr>(Base->IgnoreParenImpCasts())) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2377 | |
| 2378 | if (isa<VarDecl>(SubME->getMemberDecl())) |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2379 | return; |
| 2380 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2381 | if (FieldDecl *FD = dyn_cast<FieldDecl>(SubME->getMemberDecl())) |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2382 | if (!FD->isAnonymousStructOrUnion()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2383 | FieldME = SubME; |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2384 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2385 | if (!FieldME->getType().isPODType(S.Context)) |
| 2386 | AllPODFields = false; |
| 2387 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2388 | Base = SubME->getBase(); |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2391 | if (!isa<CXXThisExpr>(Base->IgnoreParenImpCasts())) |
Richard Trieu | 3ddec88 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2392 | return; |
| 2393 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2394 | if (AddressOf && AllPODFields) |
| 2395 | return; |
| 2396 | |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2397 | ValueDecl* FoundVD = FieldME->getMemberDecl(); |
| 2398 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2399 | if (ImplicitCastExpr *BaseCast = dyn_cast<ImplicitCastExpr>(Base)) { |
| 2400 | while (isa<ImplicitCastExpr>(BaseCast->getSubExpr())) { |
| 2401 | BaseCast = cast<ImplicitCastExpr>(BaseCast->getSubExpr()); |
| 2402 | } |
| 2403 | |
| 2404 | if (BaseCast->getCastKind() == CK_UncheckedDerivedToBase) { |
| 2405 | QualType T = BaseCast->getType(); |
| 2406 | if (T->isPointerType() && |
| 2407 | BaseClasses.count(T->getPointeeType())) { |
| 2408 | S.Diag(FieldME->getExprLoc(), diag::warn_base_class_is_uninit) |
| 2409 | << T->getPointeeType() << FoundVD; |
| 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2414 | if (!Decls.count(FoundVD)) |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2415 | return; |
| 2416 | |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2417 | const bool IsReference = FoundVD->getType()->isReferenceType(); |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2418 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2419 | if (InitList && !AddressOf && FoundVD == InitListFieldDecl) { |
| 2420 | // Special checking for initializer lists. |
| 2421 | if (IsInitListMemberExprInitialized(ME, CheckReferenceOnly)) { |
| 2422 | return; |
| 2423 | } |
| 2424 | } else { |
| 2425 | // Prevent double warnings on use of unbounded references. |
| 2426 | if (CheckReferenceOnly && !IsReference) |
| 2427 | return; |
| 2428 | } |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2429 | |
| 2430 | unsigned diag = IsReference |
| 2431 | ? diag::warn_reference_field_is_uninit |
| 2432 | : diag::warn_field_is_uninit; |
| 2433 | S.Diag(FieldME->getExprLoc(), diag) << FoundVD; |
| 2434 | if (Constructor) |
| 2435 | S.Diag(Constructor->getLocation(), |
| 2436 | diag::note_uninit_in_this_constructor) |
| 2437 | << (Constructor->isDefaultConstructor() && Constructor->isImplicit()); |
| 2438 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2441 | void HandleValue(Expr *E, bool AddressOf) { |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2442 | E = E->IgnoreParens(); |
| 2443 | |
| 2444 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2445 | HandleMemberExpr(ME, false /*CheckReferenceOnly*/, |
| 2446 | AddressOf /*AddressOf*/); |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2447 | return; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2451 | Visit(CO->getCond()); |
| 2452 | HandleValue(CO->getTrueExpr(), AddressOf); |
| 2453 | HandleValue(CO->getFalseExpr(), AddressOf); |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2454 | return; |
| 2455 | } |
| 2456 | |
| 2457 | if (BinaryConditionalOperator *BCO = |
| 2458 | dyn_cast<BinaryConditionalOperator>(E)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2459 | Visit(BCO->getCond()); |
| 2460 | HandleValue(BCO->getFalseExpr(), AddressOf); |
| 2461 | return; |
| 2462 | } |
| 2463 | |
| 2464 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 2465 | HandleValue(OVE->getSourceExpr(), AddressOf); |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2466 | return; |
| 2467 | } |
| 2468 | |
| 2469 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2470 | switch (BO->getOpcode()) { |
| 2471 | default: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2472 | break; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2473 | case(BO_PtrMemD): |
| 2474 | case(BO_PtrMemI): |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2475 | HandleValue(BO->getLHS(), AddressOf); |
| 2476 | Visit(BO->getRHS()); |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2477 | return; |
| 2478 | case(BO_Comma): |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2479 | Visit(BO->getLHS()); |
| 2480 | HandleValue(BO->getRHS(), AddressOf); |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2481 | return; |
| 2482 | } |
| 2483 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2484 | |
| 2485 | Visit(E); |
| 2486 | } |
| 2487 | |
| 2488 | void CheckInitListExpr(InitListExpr *ILE) { |
| 2489 | InitFieldIndex.push_back(0); |
| 2490 | for (auto Child : ILE->children()) { |
| 2491 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(Child)) { |
| 2492 | CheckInitListExpr(SubList); |
| 2493 | } else { |
| 2494 | Visit(Child); |
| 2495 | } |
| 2496 | ++InitFieldIndex.back(); |
| 2497 | } |
| 2498 | InitFieldIndex.pop_back(); |
| 2499 | } |
| 2500 | |
| 2501 | void CheckInitializer(Expr *E, const CXXConstructorDecl *FieldConstructor, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2502 | FieldDecl *Field, const Type *BaseClass) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2503 | // Remove Decls that may have been initialized in the previous |
| 2504 | // initializer. |
| 2505 | for (ValueDecl* VD : DeclsToRemove) |
| 2506 | Decls.erase(VD); |
| 2507 | DeclsToRemove.clear(); |
| 2508 | |
| 2509 | Constructor = FieldConstructor; |
| 2510 | InitListExpr *ILE = dyn_cast<InitListExpr>(E); |
| 2511 | |
| 2512 | if (ILE && Field) { |
| 2513 | InitList = true; |
| 2514 | InitListFieldDecl = Field; |
| 2515 | InitFieldIndex.clear(); |
| 2516 | CheckInitListExpr(ILE); |
| 2517 | } else { |
| 2518 | InitList = false; |
| 2519 | Visit(E); |
| 2520 | } |
| 2521 | |
| 2522 | if (Field) |
| 2523 | Decls.erase(Field); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2524 | if (BaseClass) |
| 2525 | BaseClasses.erase(BaseClass->getCanonicalTypeInternal()); |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2528 | void VisitMemberExpr(MemberExpr *ME) { |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2529 | // All uses of unbounded reference fields will warn. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2530 | HandleMemberExpr(ME, true /*CheckReferenceOnly*/, false /*AddressOf*/); |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2533 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2534 | if (E->getCastKind() == CK_LValueToRValue) { |
| 2535 | HandleValue(E->getSubExpr(), false /*AddressOf*/); |
| 2536 | return; |
| 2537 | } |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2538 | |
| 2539 | Inherited::VisitImplicitCastExpr(E); |
| 2540 | } |
| 2541 | |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2542 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2543 | if (E->getConstructor()->isCopyConstructor()) { |
| 2544 | Expr *ArgExpr = E->getArg(0); |
| 2545 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(ArgExpr)) |
| 2546 | if (ILE->getNumInits() == 1) |
| 2547 | ArgExpr = ILE->getInit(0); |
| 2548 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2549 | if (ICE->getCastKind() == CK_NoOp) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2550 | ArgExpr = ICE->getSubExpr(); |
| 2551 | HandleValue(ArgExpr, false /*AddressOf*/); |
| 2552 | return; |
| 2553 | } |
Richard Trieu | fbb08b5 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2554 | Inherited::VisitCXXConstructExpr(E); |
| 2555 | } |
| 2556 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2557 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 2558 | Expr *Callee = E->getCallee(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2559 | if (isa<MemberExpr>(Callee)) { |
| 2560 | HandleValue(Callee, false /*AddressOf*/); |
| 2561 | for (auto Arg : E->arguments()) |
| 2562 | Visit(Arg); |
| 2563 | return; |
| 2564 | } |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2565 | |
| 2566 | Inherited::VisitCXXMemberCallExpr(E); |
| 2567 | } |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2568 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2569 | void VisitCallExpr(CallExpr *E) { |
| 2570 | // Treat std::move as a use. |
| 2571 | if (E->getNumArgs() == 1) { |
| 2572 | if (FunctionDecl *FD = E->getDirectCallee()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2573 | if (FD->isInStdNamespace() && FD->getIdentifier() && |
| 2574 | FD->getIdentifier()->isStr("move")) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2575 | HandleValue(E->getArg(0), false /*AddressOf*/); |
| 2576 | return; |
| 2577 | } |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | Inherited::VisitCallExpr(E); |
| 2582 | } |
| 2583 | |
| 2584 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
| 2585 | Expr *Callee = E->getCallee(); |
| 2586 | |
| 2587 | if (isa<UnresolvedLookupExpr>(Callee)) |
| 2588 | return Inherited::VisitCXXOperatorCallExpr(E); |
| 2589 | |
| 2590 | Visit(Callee); |
| 2591 | for (auto Arg : E->arguments()) |
| 2592 | HandleValue(Arg->IgnoreParenImpCasts(), false /*AddressOf*/); |
| 2593 | } |
| 2594 | |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2595 | void VisitBinaryOperator(BinaryOperator *E) { |
| 2596 | // If a field assignment is detected, remove the field from the |
| 2597 | // uninitiailized field set. |
| 2598 | if (E->getOpcode() == BO_Assign) |
| 2599 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E->getLHS())) |
| 2600 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2601 | if (!FD->getType()->isReferenceType()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2602 | DeclsToRemove.push_back(FD); |
| 2603 | |
| 2604 | if (E->isCompoundAssignmentOp()) { |
| 2605 | HandleValue(E->getLHS(), false /*AddressOf*/); |
| 2606 | Visit(E->getRHS()); |
| 2607 | return; |
| 2608 | } |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2609 | |
| 2610 | Inherited::VisitBinaryOperator(E); |
| 2611 | } |
Richard Trieu | ef8f90c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2612 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2613 | void VisitUnaryOperator(UnaryOperator *E) { |
| 2614 | if (E->isIncrementDecrementOp()) { |
| 2615 | HandleValue(E->getSubExpr(), false /*AddressOf*/); |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2616 | return; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2617 | } |
| 2618 | if (E->getOpcode() == UO_AddrOf) { |
| 2619 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E->getSubExpr())) { |
| 2620 | HandleValue(ME->getBase(), true /*AddressOf*/); |
| 2621 | return; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | Inherited::VisitUnaryOperator(E); |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2626 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2627 | }; |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2628 | |
| 2629 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2630 | // foo(foo) |
| 2631 | // where foo is not also a parameter to the constructor. |
| 2632 | // Also diagnose across field uninitialized use such as |
| 2633 | // x(y), y(x) |
| 2634 | // TODO: implement -Wuninitialized and fold this into that framework. |
| 2635 | static void DiagnoseUninitializedFields( |
| 2636 | Sema &SemaRef, const CXXConstructorDecl *Constructor) { |
| 2637 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2638 | if (SemaRef.getDiagnostics().isIgnored(diag::warn_field_is_uninit, |
| 2639 | Constructor->getLocation())) { |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2640 | return; |
| 2641 | } |
| 2642 | |
| 2643 | if (Constructor->isInvalidDecl()) |
| 2644 | return; |
| 2645 | |
| 2646 | const CXXRecordDecl *RD = Constructor->getParent(); |
| 2647 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2648 | if (RD->getDescribedClassTemplate()) |
| 2649 | return; |
| 2650 | |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2651 | // Holds fields that are uninitialized. |
| 2652 | llvm::SmallPtrSet<ValueDecl*, 4> UninitializedFields; |
| 2653 | |
| 2654 | // At the beginning, all fields are uninitialized. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2655 | for (auto *I : RD->decls()) { |
| 2656 | if (auto *FD = dyn_cast<FieldDecl>(I)) { |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2657 | UninitializedFields.insert(FD); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2658 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(I)) { |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2659 | UninitializedFields.insert(IFD->getAnonField()); |
| 2660 | } |
| 2661 | } |
| 2662 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2663 | llvm::SmallPtrSet<QualType, 4> UninitializedBaseClasses; |
| 2664 | for (auto I : RD->bases()) |
| 2665 | UninitializedBaseClasses.insert(I.getType().getCanonicalType()); |
| 2666 | |
| 2667 | if (UninitializedFields.empty() && UninitializedBaseClasses.empty()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2668 | return; |
| 2669 | |
| 2670 | UninitializedFieldVisitor UninitializedChecker(SemaRef, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2671 | UninitializedFields, |
| 2672 | UninitializedBaseClasses); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2673 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2674 | for (const auto *FieldInit : Constructor->inits()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2675 | if (UninitializedFields.empty() && UninitializedBaseClasses.empty()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2676 | break; |
| 2677 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2678 | Expr *InitExpr = FieldInit->getInit(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2679 | if (!InitExpr) |
| 2680 | continue; |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2681 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2682 | if (CXXDefaultInitExpr *Default = |
| 2683 | dyn_cast<CXXDefaultInitExpr>(InitExpr)) { |
| 2684 | InitExpr = Default->getExpr(); |
| 2685 | if (!InitExpr) |
| 2686 | continue; |
| 2687 | // In class initializers will point to the constructor. |
| 2688 | UninitializedChecker.CheckInitializer(InitExpr, Constructor, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2689 | FieldInit->getAnyMember(), |
| 2690 | FieldInit->getBaseClass()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2691 | } else { |
| 2692 | UninitializedChecker.CheckInitializer(InitExpr, nullptr, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2693 | FieldInit->getAnyMember(), |
| 2694 | FieldInit->getBaseClass()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2695 | } |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2696 | } |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2697 | } |
| 2698 | } // namespace |
| 2699 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2700 | /// \brief Enter a new C++ default initializer scope. After calling this, the |
| 2701 | /// caller must call \ref ActOnFinishCXXInClassMemberInitializer, even if |
| 2702 | /// parsing or instantiating the initializer failed. |
| 2703 | void Sema::ActOnStartCXXInClassMemberInitializer() { |
| 2704 | // Create a synthetic function scope to represent the call to the constructor |
| 2705 | // that notionally surrounds a use of this initializer. |
| 2706 | PushFunctionScope(); |
| 2707 | } |
| 2708 | |
| 2709 | /// \brief This is invoked after parsing an in-class initializer for a |
| 2710 | /// non-static C++ class member, and after instantiating an in-class initializer |
| 2711 | /// in a class template. Such actions are deferred until the class is complete. |
| 2712 | void Sema::ActOnFinishCXXInClassMemberInitializer(Decl *D, |
| 2713 | SourceLocation InitLoc, |
| 2714 | Expr *InitExpr) { |
| 2715 | // Pop the notional constructor scope we created earlier. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2716 | PopFunctionScopeInfo(nullptr, D); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2717 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2718 | FieldDecl *FD = dyn_cast<FieldDecl>(D); |
| 2719 | assert((isa<MSPropertyDecl>(D) || FD->getInClassInitStyle() != ICIS_NoInit) && |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2720 | "must set init style when field is created"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2721 | |
| 2722 | if (!InitExpr) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2723 | D->setInvalidDecl(); |
| 2724 | if (FD) |
| 2725 | FD->removeInClassInitializer(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2726 | return; |
| 2727 | } |
| 2728 | |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2729 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 2730 | FD->setInvalidDecl(); |
| 2731 | FD->removeInClassInitializer(); |
| 2732 | return; |
| 2733 | } |
| 2734 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2735 | ExprResult Init = InitExpr; |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2736 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2737 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2738 | InitializationKind Kind = FD->getInClassInitStyle() == ICIS_ListInit |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2739 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2740 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), InitLoc); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2741 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
| 2742 | Init = Seq.Perform(*this, Entity, Kind, InitExpr); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2743 | if (Init.isInvalid()) { |
| 2744 | FD->setInvalidDecl(); |
| 2745 | return; |
| 2746 | } |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2749 | // C++11 [class.base.init]p7: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2750 | // The initialization of each base and member constitutes a |
| 2751 | // full-expression. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2752 | Init = ActOnFinishFullExpr(Init.get(), InitLoc); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2753 | if (Init.isInvalid()) { |
| 2754 | FD->setInvalidDecl(); |
| 2755 | return; |
| 2756 | } |
| 2757 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2758 | InitExpr = Init.get(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2759 | |
| 2760 | FD->setInClassInitializer(InitExpr); |
| 2761 | } |
| 2762 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2763 | /// \brief Find the direct and/or virtual base specifiers that |
| 2764 | /// correspond to the given base type, for use in base initialization |
| 2765 | /// within a constructor. |
| 2766 | static bool FindBaseInitializer(Sema &SemaRef, |
| 2767 | CXXRecordDecl *ClassDecl, |
| 2768 | QualType BaseType, |
| 2769 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 2770 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 2771 | // First, check for a direct base class. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2772 | DirectBaseSpec = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2773 | for (const auto &Base : ClassDecl->bases()) { |
| 2774 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base.getType())) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2775 | // We found a direct base of this type. That's what we're |
| 2776 | // initializing. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2777 | DirectBaseSpec = &Base; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2778 | break; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | // Check for a virtual base class. |
| 2783 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 2784 | // there are no virtual bases. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2785 | VirtualBaseSpec = nullptr; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2786 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 2787 | // We haven't found a base yet; search the class hierarchy for a |
| 2788 | // virtual base class. |
| 2789 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 2790 | /*DetectVirtual=*/false); |
| 2791 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 2792 | BaseType, Paths)) { |
| 2793 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2794 | Path != Paths.end(); ++Path) { |
| 2795 | if (Path->back().Base->isVirtual()) { |
| 2796 | VirtualBaseSpec = Path->back().Base; |
| 2797 | break; |
| 2798 | } |
| 2799 | } |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | return DirectBaseSpec || VirtualBaseSpec; |
| 2804 | } |
| 2805 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2806 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 2807 | MemInitResult |
| 2808 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 2809 | Scope *S, |
| 2810 | CXXScopeSpec &SS, |
| 2811 | IdentifierInfo *MemberOrBase, |
| 2812 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2813 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2814 | SourceLocation IdLoc, |
| 2815 | Expr *InitList, |
| 2816 | SourceLocation EllipsisLoc) { |
| 2817 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2818 | DS, IdLoc, InitList, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2819 | EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | /// \brief Handle a C++ member initializer using parentheses syntax. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2823 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2824 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2825 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2826 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2827 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2828 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2829 | const DeclSpec &DS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2830 | SourceLocation IdLoc, |
| 2831 | SourceLocation LParenLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2832 | ArrayRef<Expr *> Args, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2833 | SourceLocation RParenLoc, |
| 2834 | SourceLocation EllipsisLoc) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2835 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2836 | Args, RParenLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2837 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2838 | DS, IdLoc, List, EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2841 | namespace { |
| 2842 | |
Kaelyn Uhrain | dc98cd0 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 2843 | // Callback to only accept typo corrections that can be a valid C++ member |
| 2844 | // intializer: either a non-static field member or a base class. |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2845 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2846 | public: |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2847 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 2848 | : ClassDecl(ClassDecl) {} |
| 2849 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2850 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2851 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 2852 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 2853 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2854 | return isa<TypeDecl>(ND); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2855 | } |
| 2856 | return false; |
| 2857 | } |
| 2858 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2859 | private: |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2860 | CXXRecordDecl *ClassDecl; |
| 2861 | }; |
| 2862 | |
| 2863 | } |
| 2864 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2865 | /// \brief Handle a C++ member initializer. |
| 2866 | MemInitResult |
| 2867 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 2868 | Scope *S, |
| 2869 | CXXScopeSpec &SS, |
| 2870 | IdentifierInfo *MemberOrBase, |
| 2871 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2872 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2873 | SourceLocation IdLoc, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2874 | Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2875 | SourceLocation EllipsisLoc) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2876 | ExprResult Res = CorrectDelayedTyposInExpr(Init); |
| 2877 | if (!Res.isUsable()) |
| 2878 | return true; |
| 2879 | Init = Res.get(); |
| 2880 | |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2881 | if (!ConstructorD) |
| 2882 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2884 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2885 | |
| 2886 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2887 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2888 | if (!Constructor) { |
| 2889 | // The user wrote a constructor initializer on a function that is |
| 2890 | // not a C++ constructor. Ignore the error for now, because we may |
| 2891 | // have more member initializers coming; we'll diagnose it just |
| 2892 | // once in ActOnMemInitializers. |
| 2893 | return true; |
| 2894 | } |
| 2895 | |
| 2896 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2897 | |
| 2898 | // C++ [class.base.init]p2: |
| 2899 | // 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] | 2900 | // constructor's class and, if not found in that scope, are looked |
| 2901 | // up in the scope containing the constructor's definition. |
| 2902 | // [Note: if the constructor's class contains a member with the |
| 2903 | // same name as a direct or virtual base class of the class, a |
| 2904 | // mem-initializer-id naming the member or base class and composed |
| 2905 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2906 | // mem-initializer-id for the hidden base class may be specified |
| 2907 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2908 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2909 | // Look for a member, first. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2910 | DeclContext::lookup_result Result = ClassDecl->lookup(MemberOrBase); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2911 | if (!Result.empty()) { |
Peter Collingbourne | dc69be2 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 2912 | ValueDecl *Member; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2913 | if ((Member = dyn_cast<FieldDecl>(Result.front())) || |
| 2914 | (Member = dyn_cast<IndirectFieldDecl>(Result.front()))) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2915 | if (EllipsisLoc.isValid()) |
| 2916 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2917 | << MemberOrBase |
| 2918 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2919 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2920 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2921 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2922 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2923 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2924 | // 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] | 2925 | QualType BaseType; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2926 | TypeSourceInfo *TInfo = nullptr; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2927 | |
| 2928 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2929 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2930 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 2931 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2932 | } else { |
| 2933 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 2934 | LookupParsedName(R, S, &SS); |
| 2935 | |
| 2936 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 2937 | if (!TyD) { |
| 2938 | if (R.isAmbiguous()) return true; |
| 2939 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 2940 | // We don't want access-control diagnostics here. |
| 2941 | R.suppressDiagnostics(); |
| 2942 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2943 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 2944 | bool NotUnknownSpecialization = false; |
| 2945 | DeclContext *DC = computeDeclContext(SS, false); |
| 2946 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 2947 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 2948 | |
| 2949 | if (!NotUnknownSpecialization) { |
| 2950 | // When the scope specifier can refer to a member of an unknown |
| 2951 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 2952 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 2953 | SS.getWithLocInContext(Context), |
| 2954 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 2955 | if (BaseType.isNull()) |
| 2956 | return true; |
| 2957 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2958 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2959 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2963 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2964 | TypoCorrection Corr; |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2965 | if (R.empty() && BaseType.isNull() && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2966 | (Corr = CorrectTypo( |
| 2967 | R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
| 2968 | llvm::make_unique<MemInitializerValidatorCCC>(ClassDecl), |
| 2969 | CTK_ErrorRecovery, ClassDecl))) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2970 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2971 | // We have found a non-static data member with a similar |
| 2972 | // name to what was typed; complain and initialize that |
| 2973 | // member. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2974 | diagnoseTypo(Corr, |
| 2975 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2976 | << MemberOrBase << true); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2977 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2978 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2979 | const CXXBaseSpecifier *DirectBaseSpec; |
| 2980 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 2981 | if (FindBaseInitializer(*this, ClassDecl, |
| 2982 | Context.getTypeDeclType(Type), |
| 2983 | DirectBaseSpec, VirtualBaseSpec)) { |
| 2984 | // We have found a direct or virtual base class with a |
| 2985 | // similar name to what was typed; complain and initialize |
| 2986 | // that base class. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2987 | diagnoseTypo(Corr, |
| 2988 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2989 | << MemberOrBase << false, |
| 2990 | PDiag() /*Suppress note, we provide our own.*/); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2991 | |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2992 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec ? DirectBaseSpec |
| 2993 | : VirtualBaseSpec; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2994 | Diag(BaseSpec->getLocStart(), |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2995 | diag::note_base_class_specified_here) |
| 2996 | << BaseSpec->getType() |
| 2997 | << BaseSpec->getSourceRange(); |
| 2998 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2999 | TyD = Type; |
| 3000 | } |
| 3001 | } |
| 3002 | } |
| 3003 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 3004 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 3005 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3006 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 3007 | return true; |
| 3008 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 3011 | if (BaseType.isNull()) { |
| 3012 | BaseType = Context.getTypeDeclType(TyD); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3013 | MarkAnyDeclReferenced(TyD->getLocation(), TyD, /*OdrUse=*/false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3014 | if (SS.isSet()) |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 3015 | // FIXME: preserve source range information |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3016 | BaseType = Context.getElaboratedType(ETK_None, SS.getScopeRep(), |
| 3017 | BaseType); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 3018 | } |
| 3019 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3020 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3021 | if (!TInfo) |
| 3022 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3023 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3024 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 3027 | /// Checks a member initializer expression for cases where reference (or |
| 3028 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 3029 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 3030 | Expr *Init, |
| 3031 | SourceLocation IdLoc) { |
| 3032 | QualType MemberTy = Member->getType(); |
| 3033 | |
| 3034 | // We only handle pointers and references currently. |
| 3035 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 3036 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 3037 | return; |
| 3038 | |
| 3039 | const bool IsPointer = MemberTy->isPointerType(); |
| 3040 | if (IsPointer) { |
| 3041 | if (const UnaryOperator *Op |
| 3042 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 3043 | // The only case we're worried about with pointers requires taking the |
| 3044 | // address. |
| 3045 | if (Op->getOpcode() != UO_AddrOf) |
| 3046 | return; |
| 3047 | |
| 3048 | Init = Op->getSubExpr(); |
| 3049 | } else { |
| 3050 | // We only handle address-of expression initializers for pointers. |
| 3051 | return; |
| 3052 | } |
| 3053 | } |
| 3054 | |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 3055 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 3056 | // We only warn when referring to a non-reference parameter declaration. |
| 3057 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 3058 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 3059 | return; |
| 3060 | |
| 3061 | S.Diag(Init->getExprLoc(), |
| 3062 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 3063 | : diag::warn_bind_ref_member_to_parameter) |
| 3064 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 3065 | } else { |
| 3066 | // Other initializers are fine. |
| 3067 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 3068 | } |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 3069 | |
| 3070 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 3071 | << (unsigned)IsPointer; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3074 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3075 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3076 | SourceLocation IdLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3077 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 3078 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 3079 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3080 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 3081 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3082 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 3083 | return true; |
| 3084 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 3085 | if (Member->isInvalidDecl()) |
| 3086 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3087 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3088 | MultiExprArg Args; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3089 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3090 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 3091 | } else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3092 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 3093 | } else { |
| 3094 | // Template instantiation doesn't reconstruct ParenListExprs for us. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3095 | Args = Init; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3096 | } |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 3097 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3098 | SourceRange InitRange = Init->getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 3099 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3100 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3101 | // Can't check initialization for a member of dependent type or when |
| 3102 | // any of the arguments are type-dependent expressions. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3103 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3104 | } else { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3105 | bool InitList = false; |
| 3106 | if (isa<InitListExpr>(Init)) { |
| 3107 | InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3108 | Args = Init; |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3111 | // Initialize the member. |
| 3112 | InitializedEntity MemberEntity = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3113 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, nullptr) |
| 3114 | : InitializedEntity::InitializeMember(IndirectMember, |
| 3115 | nullptr); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3116 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3117 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 3118 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 3119 | InitRange.getEnd()); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3120 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3121 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3122 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, Args, |
| 3123 | nullptr); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3124 | if (MemberInit.isInvalid()) |
| 3125 | return true; |
| 3126 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 3127 | CheckForDanglingReferenceOrPointer(*this, Member, MemberInit.get(), IdLoc); |
| 3128 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3129 | // C++11 [class.base.init]p7: |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3130 | // The initialization of each base and member constitutes a |
| 3131 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3132 | MemberInit = ActOnFinishFullExpr(MemberInit.get(), InitRange.getBegin()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3133 | if (MemberInit.isInvalid()) |
| 3134 | return true; |
| 3135 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 3136 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3139 | if (DirectMember) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3140 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 3141 | InitRange.getBegin(), Init, |
| 3142 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3143 | } else { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3144 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 3145 | InitRange.getBegin(), Init, |
| 3146 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 3147 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3150 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3151 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3152 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3153 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3154 | if (!LangOpts.CPlusPlus11) |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3155 | return Diag(NameLoc, diag::err_delegating_ctor) |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 3156 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3157 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 3158 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3159 | bool InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3160 | MultiExprArg Args = Init; |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3161 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 3162 | InitList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3163 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3166 | SourceRange InitRange = Init->getSourceRange(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3167 | // Initialize the object. |
| 3168 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 3169 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 3170 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3171 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 3172 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 3173 | InitRange.getEnd()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3174 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3175 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3176 | Args, nullptr); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3177 | if (DelegationInit.isInvalid()) |
| 3178 | return true; |
| 3179 | |
Matt Beaumont-Gay | 2eb0ce3 | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 3180 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 3181 | "Delegating constructor with no target?"); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3182 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3183 | // C++11 [class.base.init]p7: |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3184 | // The initialization of each base and member constitutes a |
| 3185 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3186 | DelegationInit = ActOnFinishFullExpr(DelegationInit.get(), |
| 3187 | InitRange.getBegin()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3188 | if (DelegationInit.isInvalid()) |
| 3189 | return true; |
| 3190 | |
Eli Friedman | d21016f | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 3191 | // If we are in a dependent context, template instantiation will |
| 3192 | // perform this type-checking again. Just save the arguments that we |
| 3193 | // received in a ParenListExpr. |
| 3194 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 3195 | // of the information that we have about the base |
| 3196 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 3197 | // and this approach is far more likely to get the corner cases right. |
| 3198 | if (CurContext->isDependentContext()) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3199 | DelegationInit = Init; |
Eli Friedman | d21016f | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 3200 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3201 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3202 | DelegationInit.getAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3203 | InitRange.getEnd()); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3207 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3208 | Expr *Init, CXXRecordDecl *ClassDecl, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3209 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 3210 | SourceLocation BaseLoc |
| 3211 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3212 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 3213 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 3214 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 3215 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 3216 | |
| 3217 | // C++ [class.base.init]p2: |
| 3218 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3219 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 3220 | // of that class, the mem-initializer is ill-formed. A |
| 3221 | // mem-initializer-list can initialize a base class using any |
| 3222 | // name that denotes that base class type. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3223 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 3224 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3225 | SourceRange InitRange = Init->getSourceRange(); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3226 | if (EllipsisLoc.isValid()) { |
| 3227 | // This is a pack expansion. |
| 3228 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 3229 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3230 | << SourceRange(BaseLoc, InitRange.getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3232 | EllipsisLoc = SourceLocation(); |
| 3233 | } |
| 3234 | } else { |
| 3235 | // Check for any unexpanded parameter packs. |
| 3236 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 3237 | return true; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3238 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3239 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3240 | return true; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3241 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3242 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 3243 | // Check for direct and virtual base classes. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3244 | const CXXBaseSpecifier *DirectBaseSpec = nullptr; |
| 3245 | const CXXBaseSpecifier *VirtualBaseSpec = nullptr; |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 3246 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 3247 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 3248 | BaseType)) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3249 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 3250 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 3251 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 3252 | VirtualBaseSpec); |
| 3253 | |
| 3254 | // C++ [base.class.init]p2: |
| 3255 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 3256 | // constructor's class or a direct or virtual base of that class, the |
| 3257 | // mem-initializer is ill-formed. |
| 3258 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 3259 | // If the class has any dependent bases, then it's possible that |
| 3260 | // one of those types will resolve to the same type as |
| 3261 | // BaseType. Therefore, just treat this as a dependent base |
| 3262 | // class initialization. FIXME: Should we try to check the |
| 3263 | // initialization anyway? It seems odd. |
| 3264 | if (ClassDecl->hasAnyDependentBases()) |
| 3265 | Dependent = true; |
| 3266 | else |
| 3267 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 3268 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 3269 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | if (Dependent) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3274 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3276 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 3277 | /*IsVirtual=*/false, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3278 | InitRange.getBegin(), Init, |
| 3279 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3280 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3281 | |
| 3282 | // C++ [base.class.init]p2: |
| 3283 | // If a mem-initializer-id is ambiguous because it designates both |
| 3284 | // a direct non-virtual base class and an inherited virtual base |
| 3285 | // class, the mem-initializer is ill-formed. |
| 3286 | if (DirectBaseSpec && VirtualBaseSpec) |
| 3287 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3288 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3289 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3290 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3291 | if (!BaseSpec) |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3292 | BaseSpec = VirtualBaseSpec; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Initialize the base. |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3295 | bool InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3296 | MultiExprArg Args = Init; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3297 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3298 | InitList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3299 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3300 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3301 | |
| 3302 | InitializedEntity BaseEntity = |
| 3303 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 3304 | InitializationKind Kind = |
| 3305 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 3306 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 3307 | InitRange.getEnd()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3308 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3309 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, Args, nullptr); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3310 | if (BaseInit.isInvalid()) |
| 3311 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3312 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3313 | // C++11 [class.base.init]p7: |
| 3314 | // The initialization of each base and member constitutes a |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3315 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3316 | BaseInit = ActOnFinishFullExpr(BaseInit.get(), InitRange.getBegin()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3317 | if (BaseInit.isInvalid()) |
| 3318 | return true; |
| 3319 | |
| 3320 | // If we are in a dependent context, template instantiation will |
| 3321 | // perform this type-checking again. Just save the arguments that we |
| 3322 | // received in a ParenListExpr. |
| 3323 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 3324 | // of the information that we have about the base |
| 3325 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 3326 | // 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] | 3327 | if (CurContext->isDependentContext()) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3328 | BaseInit = Init; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3329 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3330 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 3331 | BaseSpec->isVirtual(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3332 | InitRange.getBegin(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3333 | BaseInit.getAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3334 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3337 | // Create a static_cast\<T&&>(expr). |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3338 | static Expr *CastForMoving(Sema &SemaRef, Expr *E, QualType T = QualType()) { |
| 3339 | if (T.isNull()) T = E->getType(); |
| 3340 | QualType TargetType = SemaRef.BuildReferenceType( |
| 3341 | T, /*SpelledAsLValue*/false, SourceLocation(), DeclarationName()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3342 | SourceLocation ExprLoc = E->getLocStart(); |
| 3343 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 3344 | TargetType, ExprLoc); |
| 3345 | |
| 3346 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 3347 | SourceRange(ExprLoc, ExprLoc), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3348 | E->getSourceRange()).get(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3351 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 3352 | /// initialize its base or member. |
| 3353 | enum ImplicitInitializerKind { |
| 3354 | IIK_Default, |
| 3355 | IIK_Copy, |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3356 | IIK_Move, |
| 3357 | IIK_Inherit |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3358 | }; |
| 3359 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3360 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3361 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3362 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3363 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3364 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3365 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3366 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3367 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 3368 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3369 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3370 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3371 | |
| 3372 | switch (ImplicitInitKind) { |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3373 | case IIK_Inherit: { |
| 3374 | const CXXRecordDecl *Inherited = |
| 3375 | Constructor->getInheritedConstructor()->getParent(); |
| 3376 | const CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 3377 | if (Base && Inherited->getCanonicalDecl() == Base->getCanonicalDecl()) { |
| 3378 | // C++11 [class.inhctor]p8: |
| 3379 | // Each expression in the expression-list is of the form |
| 3380 | // static_cast<T&&>(p), where p is the name of the corresponding |
| 3381 | // constructor parameter and T is the declared type of p. |
| 3382 | SmallVector<Expr*, 16> Args; |
| 3383 | for (unsigned I = 0, E = Constructor->getNumParams(); I != E; ++I) { |
| 3384 | ParmVarDecl *PD = Constructor->getParamDecl(I); |
| 3385 | ExprResult ArgExpr = |
| 3386 | SemaRef.BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), |
| 3387 | VK_LValue, SourceLocation()); |
| 3388 | if (ArgExpr.isInvalid()) |
| 3389 | return true; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3390 | Args.push_back(CastForMoving(SemaRef, ArgExpr.get(), PD->getType())); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | InitializationKind InitKind = InitializationKind::CreateDirect( |
| 3394 | Constructor->getLocation(), SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3395 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, Args); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3396 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, Args); |
| 3397 | break; |
| 3398 | } |
| 3399 | } |
| 3400 | // Fall through. |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3401 | case IIK_Default: { |
| 3402 | InitializationKind InitKind |
| 3403 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3404 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3405 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3406 | break; |
| 3407 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3408 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3409 | case IIK_Move: |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3410 | case IIK_Copy: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3411 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3412 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 3413 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | cf7c14c | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3414 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3415 | Expr *CopyCtorArg = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3416 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3417 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3418 | Constructor->getLocation(), ParamType, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3419 | VK_LValue, nullptr); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3420 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3421 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 3422 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 3423 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 3424 | QualType ArgTy = |
| 3425 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 3426 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3427 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3428 | if (Moving) { |
| 3429 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 3430 | } |
| 3431 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3432 | CXXCastPath BasePath; |
| 3433 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3434 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 3435 | CK_UncheckedDerivedToBase, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3436 | Moving ? VK_XValue : VK_LValue, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3437 | &BasePath).get(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 3438 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3439 | InitializationKind InitKind |
| 3440 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 3441 | SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3442 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| 3443 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, CopyCtorArg); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3444 | break; |
| 3445 | } |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3446 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3447 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3448 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3449 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3450 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3451 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3452 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3453 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3454 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 3455 | SourceLocation()), |
| 3456 | BaseSpec->isVirtual(), |
| 3457 | SourceLocation(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3458 | BaseInit.getAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3459 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3460 | SourceLocation()); |
| 3461 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3462 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3465 | static bool RefersToRValueRef(Expr *MemRef) { |
| 3466 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 3467 | return Referenced->getType()->isRValueReferenceType(); |
| 3468 | } |
| 3469 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3470 | static bool |
| 3471 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3472 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3473 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3474 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3475 | if (Field->isInvalidDecl()) |
| 3476 | return true; |
| 3477 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3478 | SourceLocation Loc = Constructor->getLocation(); |
| 3479 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3480 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 3481 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3482 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 3483 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 3484 | |
| 3485 | // Suppress copying zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3486 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 3487 | return false; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3488 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3489 | Expr *MemberExprBase = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3490 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3491 | SourceLocation(), Param, false, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3492 | Loc, ParamType, VK_LValue, nullptr); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3493 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3494 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 3495 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3496 | if (Moving) { |
| 3497 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 3498 | } |
| 3499 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3500 | // Build a reference to this field within the parameter. |
| 3501 | CXXScopeSpec SS; |
| 3502 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 3503 | Sema::LookupMemberName); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3504 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 3505 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3506 | MemberLookup.resolveKind(); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3507 | ExprResult CtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3508 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3509 | ParamType, Loc, |
| 3510 | /*IsArrow=*/false, |
| 3511 | SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3512 | /*TemplateKWLoc=*/SourceLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3513 | /*FirstQualifierInScope=*/nullptr, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3514 | MemberLookup, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3515 | /*TemplateArgs=*/nullptr); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3516 | if (CtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3517 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3518 | |
| 3519 | // C++11 [class.copy]p15: |
| 3520 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 3521 | // with static_cast<T&&>(x.m); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3522 | if (RefersToRValueRef(CtorArg.get())) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3523 | CtorArg = CastForMoving(SemaRef, CtorArg.get()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3526 | // When the field we are copying is an array, create index variables for |
| 3527 | // each dimension of the array. We use these index variables to subscript |
| 3528 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 3529 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3530 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3531 | QualType BaseType = Field->getType(); |
| 3532 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3533 | bool InitializingArray = false; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3534 | while (const ConstantArrayType *Array |
| 3535 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3536 | InitializingArray = true; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3537 | // Create the iteration variable for this array index. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3538 | IdentifierInfo *IterationVarName = nullptr; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3539 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3540 | SmallString<8> Str; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3541 | llvm::raw_svector_ostream OS(Str); |
| 3542 | OS << "__i" << IndexVariables.size(); |
| 3543 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 3544 | } |
| 3545 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3546 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3547 | IterationVarName, SizeType, |
| 3548 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3549 | SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3550 | IndexVariables.push_back(IterationVar); |
| 3551 | |
| 3552 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3553 | ExprResult IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3554 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3555 | assert(!IterationVarRef.isInvalid() && |
| 3556 | "Reference to invented variable cannot fail!"); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3557 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.get()); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3558 | assert(!IterationVarRef.isInvalid() && |
| 3559 | "Conversion of invented variable cannot fail!"); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3560 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3561 | // Subscript the array with this iteration variable. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3562 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.get(), Loc, |
| 3563 | IterationVarRef.get(), |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3564 | Loc); |
| 3565 | if (CtorArg.isInvalid()) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3566 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3567 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3568 | BaseType = Array->getElementType(); |
| 3569 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3570 | |
| 3571 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 3572 | if (Moving && InitializingArray) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3573 | CtorArg = CastForMoving(SemaRef, CtorArg.get()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3574 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3575 | // Construct the entity that we will be initializing. For an array, this |
| 3576 | // will be first element in the array, which may require several levels |
| 3577 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3578 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3579 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3580 | if (Indirect) |
| 3581 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 3582 | else |
| 3583 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3584 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 3585 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 3586 | 0, |
| 3587 | Entities.back())); |
| 3588 | |
| 3589 | // Direct-initialize to use the copy constructor. |
| 3590 | InitializationKind InitKind = |
| 3591 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 3592 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3593 | Expr *CtorArgE = CtorArg.getAs<Expr>(); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3594 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 3595 | CtorArgE); |
| 3596 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3597 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3598 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3599 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3600 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3601 | if (MemberInit.isInvalid()) |
| 3602 | return true; |
| 3603 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3604 | if (Indirect) { |
| 3605 | assert(IndexVariables.size() == 0 && |
| 3606 | "Indirect field improperly initialized"); |
| 3607 | CXXMemberInit |
| 3608 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3609 | Loc, Loc, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3610 | MemberInit.getAs<Expr>(), |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3611 | Loc); |
| 3612 | } else |
| 3613 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3614 | Loc, MemberInit.getAs<Expr>(), |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3615 | Loc, |
| 3616 | IndexVariables.data(), |
| 3617 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3618 | return false; |
| 3619 | } |
| 3620 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3621 | assert((ImplicitInitKind == IIK_Default || ImplicitInitKind == IIK_Inherit) && |
| 3622 | "Unhandled implicit init kind!"); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3623 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3624 | QualType FieldBaseElementType = |
| 3625 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 3626 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3627 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3628 | InitializedEntity InitEntity |
| 3629 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 3630 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3631 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3632 | InitializationKind::CreateDefault(Loc); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3633 | |
| 3634 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3635 | ExprResult MemberInit = |
| 3636 | InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3637 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3638 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3639 | if (MemberInit.isInvalid()) |
| 3640 | return true; |
| 3641 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3642 | if (Indirect) |
| 3643 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3644 | Indirect, Loc, |
| 3645 | Loc, |
| 3646 | MemberInit.get(), |
| 3647 | Loc); |
| 3648 | else |
| 3649 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3650 | Field, Loc, Loc, |
| 3651 | MemberInit.get(), |
| 3652 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3653 | return false; |
| 3654 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3655 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3656 | if (!Field->getParent()->isUnion()) { |
| 3657 | if (FieldBaseElementType->isReferenceType()) { |
| 3658 | SemaRef.Diag(Constructor->getLocation(), |
| 3659 | diag::err_uninitialized_member_in_ctor) |
| 3660 | << (int)Constructor->isImplicit() |
| 3661 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3662 | << 0 << Field->getDeclName(); |
| 3663 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3664 | return true; |
| 3665 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3666 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3667 | if (FieldBaseElementType.isConstQualified()) { |
| 3668 | SemaRef.Diag(Constructor->getLocation(), |
| 3669 | diag::err_uninitialized_member_in_ctor) |
| 3670 | << (int)Constructor->isImplicit() |
| 3671 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3672 | << 1 << Field->getDeclName(); |
| 3673 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3674 | return true; |
| 3675 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3676 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3677 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3678 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3679 | FieldBaseElementType->isObjCRetainableType() && |
| 3680 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 3681 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
Douglas Gregor | 3fe52ff | 2012-07-23 04:23:39 +0000 | [diff] [blame] | 3682 | // ARC: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3683 | // Default-initialize Objective-C pointers to NULL. |
| 3684 | CXXMemberInit |
| 3685 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3686 | Loc, Loc, |
| 3687 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 3688 | Loc); |
| 3689 | return false; |
| 3690 | } |
| 3691 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3692 | // Nothing to initialize. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3693 | CXXMemberInit = nullptr; |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3694 | return false; |
| 3695 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3696 | |
| 3697 | namespace { |
| 3698 | struct BaseAndFieldInfo { |
| 3699 | Sema &S; |
| 3700 | CXXConstructorDecl *Ctor; |
| 3701 | bool AnyErrorsInInits; |
| 3702 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3703 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3704 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3705 | llvm::DenseMap<TagDecl*, FieldDecl*> ActiveUnionMember; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3706 | |
| 3707 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 3708 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3709 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 3710 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3711 | IIK = IIK_Copy; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3712 | else if (Generated && Ctor->isMoveConstructor()) |
| 3713 | IIK = IIK_Move; |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3714 | else if (Ctor->getInheritedConstructor()) |
| 3715 | IIK = IIK_Inherit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3716 | else |
| 3717 | IIK = IIK_Default; |
| 3718 | } |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3719 | |
| 3720 | bool isImplicitCopyOrMove() const { |
| 3721 | switch (IIK) { |
| 3722 | case IIK_Copy: |
| 3723 | case IIK_Move: |
| 3724 | return true; |
| 3725 | |
| 3726 | case IIK_Default: |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3727 | case IIK_Inherit: |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3728 | return false; |
| 3729 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3730 | |
| 3731 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3732 | } |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3733 | |
| 3734 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 3735 | AllToInit.push_back(Init); |
| 3736 | |
| 3737 | // Check whether this initializer makes the field "used". |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3738 | if (Init->getInit()->HasSideEffects(S.Context)) |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3739 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 3740 | |
| 3741 | return false; |
| 3742 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3743 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3744 | bool isInactiveUnionMember(FieldDecl *Field) { |
| 3745 | RecordDecl *Record = Field->getParent(); |
| 3746 | if (!Record->isUnion()) |
| 3747 | return false; |
| 3748 | |
| 3749 | if (FieldDecl *Active = |
| 3750 | ActiveUnionMember.lookup(Record->getCanonicalDecl())) |
| 3751 | return Active != Field->getCanonicalDecl(); |
| 3752 | |
| 3753 | // In an implicit copy or move constructor, ignore any in-class initializer. |
| 3754 | if (isImplicitCopyOrMove()) |
| 3755 | return true; |
| 3756 | |
| 3757 | // If there's no explicit initialization, the field is active only if it |
| 3758 | // has an in-class initializer... |
| 3759 | if (Field->hasInClassInitializer()) |
| 3760 | return false; |
| 3761 | // ... or it's an anonymous struct or union whose class has an in-class |
| 3762 | // initializer. |
| 3763 | if (!Field->isAnonymousStructOrUnion()) |
| 3764 | return true; |
| 3765 | CXXRecordDecl *FieldRD = Field->getType()->getAsCXXRecordDecl(); |
| 3766 | return !FieldRD->hasInClassInitializer(); |
| 3767 | } |
| 3768 | |
| 3769 | /// \brief Determine whether the given field is, or is within, a union member |
| 3770 | /// that is inactive (because there was an initializer given for a different |
| 3771 | /// member of the union, or because the union was not initialized at all). |
| 3772 | bool isWithinInactiveUnionMember(FieldDecl *Field, |
| 3773 | IndirectFieldDecl *Indirect) { |
| 3774 | if (!Indirect) |
| 3775 | return isInactiveUnionMember(Field); |
| 3776 | |
| 3777 | for (auto *C : Indirect->chain()) { |
| 3778 | FieldDecl *Field = dyn_cast<FieldDecl>(C); |
| 3779 | if (Field && isInactiveUnionMember(Field)) |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3780 | return true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3781 | } |
| 3782 | return false; |
| 3783 | } |
| 3784 | }; |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3787 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 3788 | /// array type. |
| 3789 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 3790 | if (T->isIncompleteArrayType()) |
| 3791 | return true; |
| 3792 | |
| 3793 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 3794 | if (!ArrayT->getSize()) |
| 3795 | return true; |
| 3796 | |
| 3797 | T = ArrayT->getElementType(); |
| 3798 | } |
| 3799 | |
| 3800 | return false; |
| 3801 | } |
| 3802 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3803 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3804 | FieldDecl *Field, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3805 | IndirectFieldDecl *Indirect = nullptr) { |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3806 | if (Field->isInvalidDecl()) |
| 3807 | return false; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3808 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 3809 | // Overwhelmingly common case: we have a direct initializer for this field. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3810 | if (CXXCtorInitializer *Init = |
| 3811 | Info.AllBaseFields.lookup(Field->getCanonicalDecl())) |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3812 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3813 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3814 | // C++11 [class.base.init]p8: |
| 3815 | // if the entity is a non-static data member that has a |
| 3816 | // brace-or-equal-initializer and either |
| 3817 | // -- the constructor's class is a union and no other variant member of that |
| 3818 | // union is designated by a mem-initializer-id or |
| 3819 | // -- the constructor's class is not a union, and, if the entity is a member |
| 3820 | // of an anonymous union, no other member of that union is designated by |
| 3821 | // a mem-initializer-id, |
| 3822 | // the entity is initialized as specified in [dcl.init]. |
| 3823 | // |
| 3824 | // We also apply the same rules to handle anonymous structs within anonymous |
| 3825 | // unions. |
| 3826 | if (Info.isWithinInactiveUnionMember(Field, Indirect)) |
| 3827 | return false; |
| 3828 | |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3829 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3830 | ExprResult DIE = |
| 3831 | SemaRef.BuildCXXDefaultInitExpr(Info.Ctor->getLocation(), Field); |
| 3832 | if (DIE.isInvalid()) |
| 3833 | return true; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3834 | CXXCtorInitializer *Init; |
| 3835 | if (Indirect) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3836 | Init = new (SemaRef.Context) |
| 3837 | CXXCtorInitializer(SemaRef.Context, Indirect, SourceLocation(), |
| 3838 | SourceLocation(), DIE.get(), SourceLocation()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3839 | else |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3840 | Init = new (SemaRef.Context) |
| 3841 | CXXCtorInitializer(SemaRef.Context, Field, SourceLocation(), |
| 3842 | SourceLocation(), DIE.get(), SourceLocation()); |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3843 | return Info.addFieldInitializer(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3846 | // Don't initialize incomplete or zero-length arrays. |
| 3847 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 3848 | return false; |
| 3849 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3850 | // Don't try to build an implicit initializer if there were semantic |
| 3851 | // errors in any of the initializers (and therefore we might be |
| 3852 | // missing some that the user actually wrote). |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3853 | if (Info.AnyErrorsInInits) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3854 | return false; |
| 3855 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3856 | CXXCtorInitializer *Init = nullptr; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3857 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 3858 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3859 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3860 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3861 | if (!Init) |
| 3862 | return false; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3863 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3864 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3865 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3866 | |
| 3867 | bool |
| 3868 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 3869 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3870 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 3871 | Constructor->setNumCtorInitializers(1); |
| 3872 | CXXCtorInitializer **initializer = |
| 3873 | new (Context) CXXCtorInitializer*[1]; |
| 3874 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 3875 | Constructor->setCtorInitializers(initializer); |
| 3876 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3877 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3878 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3879 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 3880 | } |
| 3881 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 3882 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3883 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3884 | DiagnoseUninitializedFields(*this, Constructor); |
| 3885 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3886 | return false; |
| 3887 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3888 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3889 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors, |
| 3890 | ArrayRef<CXXCtorInitializer *> Initializers) { |
Douglas Gregor | d836c0d | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 3891 | if (Constructor->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3892 | // Just store the initializers as written, they will be checked during |
| 3893 | // instantiation. |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3894 | if (!Initializers.empty()) { |
| 3895 | Constructor->setNumCtorInitializers(Initializers.size()); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3896 | CXXCtorInitializer **baseOrMemberInitializers = |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3897 | new (Context) CXXCtorInitializer*[Initializers.size()]; |
| 3898 | memcpy(baseOrMemberInitializers, Initializers.data(), |
| 3899 | Initializers.size() * sizeof(CXXCtorInitializer*)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3900 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3901 | } |
Richard Smith | 54b3ba8 | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3902 | |
| 3903 | // Let template instantiation know whether we had errors. |
| 3904 | if (AnyErrors) |
| 3905 | Constructor->setInvalidDecl(); |
| 3906 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3907 | return false; |
| 3908 | } |
| 3909 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3910 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3911 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3912 | // We need to build the initializer AST according to order of construction |
| 3913 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3914 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 3915 | if (!ClassDecl) |
| 3916 | return true; |
| 3917 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3918 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3920 | for (unsigned i = 0; i < Initializers.size(); i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3921 | CXXCtorInitializer *Member = Initializers[i]; |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3922 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3923 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3924 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3925 | else { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3926 | Info.AllBaseFields[Member->getAnyMember()->getCanonicalDecl()] = Member; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3927 | |
| 3928 | if (IndirectFieldDecl *F = Member->getIndirectMember()) { |
| 3929 | for (auto *C : F->chain()) { |
| 3930 | FieldDecl *FD = dyn_cast<FieldDecl>(C); |
| 3931 | if (FD && FD->getParent()->isUnion()) |
| 3932 | Info.ActiveUnionMember.insert(std::make_pair( |
| 3933 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 3934 | } |
| 3935 | } else if (FieldDecl *FD = Member->getMember()) { |
| 3936 | if (FD->getParent()->isUnion()) |
| 3937 | Info.ActiveUnionMember.insert(std::make_pair( |
| 3938 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 3939 | } |
| 3940 | } |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3943 | // Keep track of the direct virtual bases. |
| 3944 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3945 | for (auto &I : ClassDecl->bases()) { |
| 3946 | if (I.isVirtual()) |
| 3947 | DirectVBases.insert(&I); |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3950 | // Push virtual bases before others. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3951 | for (auto &VBase : ClassDecl->vbases()) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3952 | if (CXXCtorInitializer *Value |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3953 | = Info.AllBaseFields.lookup(VBase.getType()->getAs<RecordType>())) { |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3954 | // [class.base.init]p7, per DR257: |
| 3955 | // A mem-initializer where the mem-initializer-id names a virtual base |
| 3956 | // class is ignored during execution of a constructor of any class that |
| 3957 | // is not the most derived class. |
| 3958 | if (ClassDecl->isAbstract()) { |
| 3959 | // FIXME: Provide a fixit to remove the base specifier. This requires |
| 3960 | // tracking the location of the associated comma for a base specifier. |
| 3961 | Diag(Value->getSourceLocation(), diag::warn_abstract_vbase_init_ignored) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3962 | << VBase.getType() << ClassDecl; |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3963 | DiagnoseAbstractType(ClassDecl); |
| 3964 | } |
| 3965 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3966 | Info.AllToInit.push_back(Value); |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3967 | } else if (!AnyErrors && !ClassDecl->isAbstract()) { |
| 3968 | // [class.base.init]p8, per DR257: |
| 3969 | // If a given [...] base class is not named by a mem-initializer-id |
| 3970 | // [...] and the entity is not a virtual base class of an abstract |
| 3971 | // class, then [...] the entity is default-initialized. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3972 | bool IsInheritedVirtualBase = !DirectVBases.count(&VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3973 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3974 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3975 | &VBase, IsInheritedVirtualBase, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3976 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3977 | HadError = true; |
| 3978 | continue; |
| 3979 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3980 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3981 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3982 | } |
| 3983 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3984 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3985 | // Non-virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3986 | for (auto &Base : ClassDecl->bases()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3987 | // Virtuals are in the virtual base list and already constructed. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3988 | if (Base.isVirtual()) |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3989 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3990 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3991 | if (CXXCtorInitializer *Value |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3992 | = Info.AllBaseFields.lookup(Base.getType()->getAs<RecordType>())) { |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3993 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3994 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3995 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3996 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3997 | &Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3998 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3999 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 4000 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 4001 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 4002 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 4003 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 4004 | } |
| 4005 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 4007 | // Fields. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4008 | for (auto *Mem : ClassDecl->decls()) { |
| 4009 | if (auto *F = dyn_cast<FieldDecl>(Mem)) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4010 | // C++ [class.bit]p2: |
| 4011 | // A declaration for a bit-field that omits the identifier declares an |
| 4012 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 4013 | // initialized. |
| 4014 | if (F->isUnnamedBitfield()) |
| 4015 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 4016 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4017 | // 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] | 4018 | // handle anonymous struct/union fields based on their individual |
| 4019 | // indirect fields. |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4020 | if (F->isAnonymousStructOrUnion() && !Info.isImplicitCopyOrMove()) |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 4021 | continue; |
| 4022 | |
| 4023 | if (CollectFieldInitializer(*this, Info, F)) |
| 4024 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 4025 | continue; |
| 4026 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 4027 | |
| 4028 | // Beyond this point, we only consider default initialization. |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4029 | if (Info.isImplicitCopyOrMove()) |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 4030 | continue; |
| 4031 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4032 | if (auto *F = dyn_cast<IndirectFieldDecl>(Mem)) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 4033 | if (F->getType()->isIncompleteArrayType()) { |
| 4034 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 4035 | "Incomplete array type is not valid"); |
| 4036 | continue; |
| 4037 | } |
| 4038 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 4039 | // Initialize each field of an anonymous struct individually. |
| 4040 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 4041 | HadError = true; |
| 4042 | |
| 4043 | continue; |
| 4044 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 4045 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4046 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4047 | unsigned NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 4048 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4049 | Constructor->setNumCtorInitializers(NumInitializers); |
| 4050 | CXXCtorInitializer **baseOrMemberInitializers = |
| 4051 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 4052 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4053 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 4054 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 4055 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4056 | // Constructors implicitly reference the base and member |
| 4057 | // destructors. |
| 4058 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 4059 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 4060 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4061 | |
| 4062 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 4065 | static void PopulateKeysForFields(FieldDecl *Field, SmallVectorImpl<const void*> &IdealInits) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4066 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 4067 | const RecordDecl *RD = RT->getDecl(); |
| 4068 | if (RD->isAnonymousStructOrUnion()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4069 | for (auto *Field : RD->fields()) |
| 4070 | PopulateKeysForFields(Field, IdealInits); |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 4071 | return; |
| 4072 | } |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 4073 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4074 | IdealInits.push_back(Field->getCanonicalDecl()); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4077 | static const void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 4078 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4081 | static const void *GetKeyForMember(ASTContext &Context, |
| 4082 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4083 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 4084 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 4085 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4086 | return Member->getAnyMember()->getCanonicalDecl(); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4089 | static void DiagnoseBaseOrMemInitializerOrder( |
| 4090 | Sema &SemaRef, const CXXConstructorDecl *Constructor, |
| 4091 | ArrayRef<CXXCtorInitializer *> Inits) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4092 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 4093 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 4095 | // Don't check initializers order unless the warning is enabled at the |
| 4096 | // location of at least one initializer. |
| 4097 | bool ShouldCheckOrder = false; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4098 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4099 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4100 | if (!SemaRef.Diags.isIgnored(diag::warn_initializer_out_of_order, |
| 4101 | Init->getSourceLocation())) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 4102 | ShouldCheckOrder = true; |
| 4103 | break; |
| 4104 | } |
| 4105 | } |
| 4106 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 4107 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4108 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4109 | // Build the list of bases and members in the order that they'll |
| 4110 | // actually be initialized. The explicit initializers should be in |
| 4111 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4112 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 4114 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4115 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4116 | // 1. Virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4117 | for (const auto &VBase : ClassDecl->vbases()) |
| 4118 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase.getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4120 | // 2. Non-virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4121 | for (const auto &Base : ClassDecl->bases()) { |
| 4122 | if (Base.isVirtual()) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 4123 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4124 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base.getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 4125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4127 | // 3. Direct fields. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4128 | for (auto *Field : ClassDecl->fields()) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4129 | if (Field->isUnnamedBitfield()) |
| 4130 | continue; |
| 4131 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4132 | PopulateKeysForFields(Field, IdealInitKeys); |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4135 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 4136 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 4137 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4138 | CXXCtorInitializer *PrevInit = nullptr; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4139 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4140 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4141 | const void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4142 | |
| 4143 | // Scan forward to try to find this initializer in the idealized |
| 4144 | // initializers list. |
| 4145 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 4146 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 4147 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4148 | |
| 4149 | // If we didn't find this initializer, it must be because we |
| 4150 | // scanned past it on a previous iteration. That can only |
| 4151 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 4152 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4153 | Sema::SemaDiagnosticBuilder D = |
| 4154 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 4155 | diag::warn_initializer_out_of_order); |
| 4156 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4157 | if (PrevInit->isAnyMemberInitializer()) |
| 4158 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4159 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4160 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4161 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4162 | if (Init->isAnyMemberInitializer()) |
| 4163 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4164 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4165 | D << 1 << Init->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4166 | |
| 4167 | // Move back to the initializer's location in the ideal list. |
| 4168 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 4169 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 4170 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4171 | |
| 4172 | assert(IdealIndex != NumIdealInits && |
| 4173 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 4174 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 4175 | |
| 4176 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 4177 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4180 | namespace { |
| 4181 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4182 | CXXCtorInitializer *Init, |
| 4183 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4184 | if (!PrevInit) { |
| 4185 | PrevInit = Init; |
| 4186 | return false; |
| 4187 | } |
| 4188 | |
Douglas Gregor | dc392c1 | 2013-03-25 23:28:23 +0000 | [diff] [blame] | 4189 | if (FieldDecl *Field = Init->getAnyMember()) |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4190 | S.Diag(Init->getSourceLocation(), |
| 4191 | diag::err_multiple_mem_initialization) |
| 4192 | << Field->getDeclName() |
| 4193 | << Init->getSourceRange(); |
| 4194 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4195 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4196 | assert(BaseClass && "neither field nor base"); |
| 4197 | S.Diag(Init->getSourceLocation(), |
| 4198 | diag::err_multiple_base_initialization) |
| 4199 | << QualType(BaseClass, 0) |
| 4200 | << Init->getSourceRange(); |
| 4201 | } |
| 4202 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 4203 | << 0 << PrevInit->getSourceRange(); |
| 4204 | |
| 4205 | return true; |
| 4206 | } |
| 4207 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4208 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4209 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 4210 | |
| 4211 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4212 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4213 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4214 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4215 | RecordDecl *Parent = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4216 | NamedDecl *Child = Field; |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 4217 | |
| 4218 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4219 | if (Parent->isUnion()) { |
| 4220 | UnionEntry &En = Unions[Parent]; |
| 4221 | if (En.first && En.first != Child) { |
| 4222 | S.Diag(Init->getSourceLocation(), |
| 4223 | diag::err_multiple_mem_union_initialization) |
| 4224 | << Field->getDeclName() |
| 4225 | << Init->getSourceRange(); |
| 4226 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 4227 | << 0 << En.second->getSourceRange(); |
| 4228 | return true; |
David Blaikie | 5bbe816 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 4229 | } |
| 4230 | if (!En.first) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4231 | En.first = Child; |
| 4232 | En.second = Init; |
| 4233 | } |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 4234 | if (!Parent->isAnonymousStructOrUnion()) |
| 4235 | return false; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | Child = Parent; |
| 4239 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 4240 | } |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4241 | |
| 4242 | return false; |
| 4243 | } |
| 4244 | } |
| 4245 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4246 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4247 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4248 | SourceLocation ColonLoc, |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4249 | ArrayRef<CXXCtorInitializer*> MemInits, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4250 | bool AnyErrors) { |
| 4251 | if (!ConstructorDecl) |
| 4252 | return; |
| 4253 | |
| 4254 | AdjustDeclIfTemplate(ConstructorDecl); |
| 4255 | |
| 4256 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4257 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4258 | |
| 4259 | if (!Constructor) { |
| 4260 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 4261 | return; |
| 4262 | } |
| 4263 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4264 | // Mapping for the duplicate initializers check. |
| 4265 | // For member initializers, this is keyed with a FieldDecl*. |
| 4266 | // For base initializers, this is keyed with a Type*. |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4267 | llvm::DenseMap<const void *, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4268 | |
| 4269 | // Mapping for the inconsistent anonymous-union initializers check. |
| 4270 | RedundantUnionMap MemberUnions; |
| 4271 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 4272 | bool HadError = false; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4273 | for (unsigned i = 0; i < MemInits.size(); i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4274 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4275 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 4276 | // Set the source order index. |
| 4277 | Init->setSourceOrder(i); |
| 4278 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4279 | if (Init->isAnyMemberInitializer()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4280 | const void *Key = GetKeyForMember(Context, Init); |
| 4281 | if (CheckRedundantInit(*this, Init, Members[Key]) || |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4282 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 4283 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 4284 | } else if (Init->isBaseInitializer()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4285 | const void *Key = GetKeyForMember(Context, Init); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 4286 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 4287 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 4288 | } else { |
| 4289 | assert(Init->isDelegatingInitializer()); |
| 4290 | // This must be the only initializer |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4291 | if (MemInits.size() != 1) { |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 4292 | Diag(Init->getSourceLocation(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 4293 | diag::err_delegating_initializer_alone) |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 4294 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4295 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 4296 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 4297 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4298 | // Return immediately as the initializer is set. |
| 4299 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4300 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 4303 | if (HadError) |
| 4304 | return; |
| 4305 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4306 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 4307 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4308 | SetCtorInitializers(Constructor, AnyErrors, MemInits); |
Richard Trieu | 225e982 | 2013-09-16 21:54:53 +0000 | [diff] [blame] | 4309 | |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 4310 | DiagnoseUninitializedFields(*this, Constructor); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4313 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4314 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 4315 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 4316 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 4317 | // have destructors implicitly called. |
| 4318 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4319 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4320 | |
| 4321 | // FIXME: all the access-control diagnostics are positioned on the |
| 4322 | // field/base declaration. That's probably good; that said, the |
| 4323 | // user might reasonably want to know why the destructor is being |
| 4324 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4325 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4326 | // Non-static data members. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4327 | for (auto *Field : ClassDecl->fields()) { |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 4328 | if (Field->isInvalidDecl()) |
| 4329 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 4330 | |
| 4331 | // Don't destroy incomplete or zero-length arrays. |
| 4332 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 4333 | continue; |
| 4334 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4335 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 4336 | |
| 4337 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 4338 | if (!RT) |
| 4339 | continue; |
| 4340 | |
| 4341 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4342 | if (FieldClassDecl->isInvalidDecl()) |
| 4343 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4344 | if (FieldClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4345 | continue; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4346 | // The destructor for an implicit anonymous union member is never invoked. |
| 4347 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 4348 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4350 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4351 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4352 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4353 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4354 | << Field->getDeclName() |
| 4355 | << FieldType); |
| 4356 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4357 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4358 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4361 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 4362 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4363 | // Bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4364 | for (const auto &Base : ClassDecl->bases()) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4365 | // Bases are always records in a well-formed non-dependent class. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4366 | const RecordType *RT = Base.getType()->getAs<RecordType>(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4367 | |
| 4368 | // Remember direct virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4369 | if (Base.isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4370 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4371 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4372 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4373 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 4374 | if (BaseClassDecl->isInvalidDecl()) |
| 4375 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4376 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4377 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4378 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4379 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4380 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4381 | |
| 4382 | // FIXME: caret should be on the start of the class name |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4383 | CheckDestructorAccess(Base.getLocStart(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4384 | PDiag(diag::err_access_dtor_base) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4385 | << Base.getType() |
| 4386 | << Base.getSourceRange(), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4387 | Context.getTypeDeclType(ClassDecl)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4388 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4389 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4390 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | // Virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4394 | for (const auto &VBase : ClassDecl->vbases()) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4395 | // Bases are always records in a well-formed non-dependent class. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4396 | const RecordType *RT = VBase.getType()->castAs<RecordType>(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4397 | |
| 4398 | // Ignore direct virtual bases. |
| 4399 | if (DirectVirtualBases.count(RT)) |
| 4400 | continue; |
| 4401 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4402 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4403 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 4404 | if (BaseClassDecl->isInvalidDecl()) |
| 4405 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4406 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4407 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4409 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4410 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4411 | if (CheckDestructorAccess( |
| 4412 | ClassDecl->getLocation(), Dtor, |
| 4413 | PDiag(diag::err_access_dtor_vbase) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4414 | << Context.getTypeDeclType(ClassDecl) << VBase.getType(), |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4415 | Context.getTypeDeclType(ClassDecl)) == |
| 4416 | AR_accessible) { |
| 4417 | CheckDerivedToBaseConversion( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4418 | Context.getTypeDeclType(ClassDecl), VBase.getType(), |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4419 | diag::err_access_dtor_vbase, 0, ClassDecl->getLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4420 | SourceRange(), DeclarationName(), nullptr); |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4421 | } |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4422 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4423 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4424 | DiagnoseUseOfDecl(Dtor, Location); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4425 | } |
| 4426 | } |
| 4427 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4428 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 4429 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 4430 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | if (CXXConstructorDecl *Constructor |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 4433 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) { |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4434 | SetCtorInitializers(Constructor, /*AnyErrors=*/false); |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 4435 | DiagnoseUninitializedFields(*this, Constructor); |
| 4436 | } |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4439 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4440 | unsigned DiagID, AbstractDiagSelID SelID) { |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4441 | class NonAbstractTypeDiagnoser : public TypeDiagnoser { |
| 4442 | unsigned DiagID; |
| 4443 | AbstractDiagSelID SelID; |
| 4444 | |
| 4445 | public: |
| 4446 | NonAbstractTypeDiagnoser(unsigned DiagID, AbstractDiagSelID SelID) |
| 4447 | : TypeDiagnoser(DiagID == 0), DiagID(DiagID), SelID(SelID) { } |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4448 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4449 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { |
Eli Friedman | 2217f85 | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4450 | if (Suppressed) return; |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4451 | if (SelID == -1) |
| 4452 | S.Diag(Loc, DiagID) << T; |
| 4453 | else |
| 4454 | S.Diag(Loc, DiagID) << SelID << T; |
| 4455 | } |
| 4456 | } Diagnoser(DiagID, SelID); |
| 4457 | |
| 4458 | return RequireNonAbstractType(Loc, T, Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 4461 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4462 | TypeDiagnoser &Diagnoser) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4463 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4464 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 4466 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4467 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4469 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4470 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4471 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4472 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4473 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4474 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4475 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4477 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4478 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4479 | if (!RT) |
| 4480 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4481 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4482 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4483 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4484 | // We can't answer whether something is abstract until it has a |
| 4485 | // definition. If it's currently being defined, we'll walk back |
| 4486 | // over all the declarations when we have a full definition. |
| 4487 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 4488 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4489 | return false; |
| 4490 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4491 | if (!RD->isAbstract()) |
| 4492 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4493 | |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4494 | Diagnoser.diagnose(*this, Loc, T); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4495 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4497 | return true; |
| 4498 | } |
| 4499 | |
| 4500 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 4501 | // Check if we've already emitted the list of pure virtual functions |
| 4502 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4503 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4504 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4505 | |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 4506 | // If the diagnostic is suppressed, don't emit the notes. We're only |
| 4507 | // going to emit them once, so try to attach them to a diagnostic we're |
| 4508 | // actually going to show. |
| 4509 | if (Diags.isLastDiagnosticIgnored()) |
| 4510 | return; |
| 4511 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4512 | CXXFinalOverriderMap FinalOverriders; |
| 4513 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 4515 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 4516 | // more than once. |
| 4517 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 4518 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4519 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 4520 | MEnd = FinalOverriders.end(); |
| 4521 | M != MEnd; |
| 4522 | ++M) { |
| 4523 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 4524 | SOEnd = M->second.end(); |
| 4525 | SO != SOEnd; ++SO) { |
| 4526 | // C++ [class.abstract]p4: |
| 4527 | // A class is abstract if it contains or inherits at least one |
| 4528 | // pure virtual function for which the final overrider is pure |
| 4529 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4531 | // |
| 4532 | if (SO->second.size() != 1) |
| 4533 | continue; |
| 4534 | |
| 4535 | if (!SO->second.front().Method->isPure()) |
| 4536 | continue; |
| 4537 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4538 | if (!SeenPureMethods.insert(SO->second.front().Method).second) |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 4539 | continue; |
| 4540 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4541 | Diag(SO->second.front().Method->getLocation(), |
| 4542 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 4543 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4544 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | if (!PureVirtualClassDiagSet) |
| 4548 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 4549 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4552 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4553 | struct AbstractUsageInfo { |
| 4554 | Sema &S; |
| 4555 | CXXRecordDecl *Record; |
| 4556 | CanQualType AbstractType; |
| 4557 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4558 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4559 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 4560 | : S(S), Record(Record), |
| 4561 | AbstractType(S.Context.getCanonicalType( |
| 4562 | S.Context.getTypeDeclType(Record))), |
| 4563 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4564 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4565 | void DiagnoseAbstractType() { |
| 4566 | if (Invalid) return; |
| 4567 | S.DiagnoseAbstractType(Record); |
| 4568 | Invalid = true; |
| 4569 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4570 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4571 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 4572 | }; |
| 4573 | |
| 4574 | struct CheckAbstractUsage { |
| 4575 | AbstractUsageInfo &Info; |
| 4576 | const NamedDecl *Ctx; |
| 4577 | |
| 4578 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 4579 | : Info(Info), Ctx(Ctx) {} |
| 4580 | |
| 4581 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4582 | switch (TL.getTypeLocClass()) { |
| 4583 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 4584 | #define TYPELOC(CLASS, PARENT) \ |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4585 | case TypeLoc::CLASS: Check(TL.castAs<CLASS##TypeLoc>(), Sel); break; |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4586 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4587 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4590 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4591 | Visit(TL.getReturnLoc(), Sema::AbstractReturnType); |
| 4592 | for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) { |
| 4593 | if (!TL.getParam(I)) |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 4594 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4595 | |
| 4596 | TypeSourceInfo *TSI = TL.getParam(I)->getTypeSourceInfo(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4597 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4598 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4599 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4600 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4601 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4602 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 4603 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4604 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4605 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4606 | // Visit the type parameters from a permissive context. |
| 4607 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 4608 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 4609 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 4610 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 4611 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 4612 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4613 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4614 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4615 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4616 | // Visit pointee types from a permissive context. |
| 4617 | #define CheckPolymorphic(Type) \ |
| 4618 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 4619 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 4620 | } |
| 4621 | CheckPolymorphic(PointerTypeLoc) |
| 4622 | CheckPolymorphic(ReferenceTypeLoc) |
| 4623 | CheckPolymorphic(MemberPointerTypeLoc) |
| 4624 | CheckPolymorphic(BlockPointerTypeLoc) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4625 | CheckPolymorphic(AtomicTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4627 | /// Handle all the types we haven't given a more specific |
| 4628 | /// implementation for above. |
| 4629 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4630 | // Every other kind of type that we haven't called out already |
| 4631 | // that has an inner type is either (1) sugar or (2) contains that |
| 4632 | // inner type in some way as a subobject. |
| 4633 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 4634 | return Visit(Next, Sel); |
| 4635 | |
| 4636 | // If there's no inner type and we're in a permissive context, |
| 4637 | // don't diagnose. |
| 4638 | if (Sel == Sema::AbstractNone) return; |
| 4639 | |
| 4640 | // Check whether the type matches the abstract type. |
| 4641 | QualType T = TL.getType(); |
| 4642 | if (T->isArrayType()) { |
| 4643 | Sel = Sema::AbstractArrayType; |
| 4644 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4645 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4646 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 4647 | if (CT != Info.AbstractType) return; |
| 4648 | |
| 4649 | // It matched; do some magic. |
| 4650 | if (Sel == Sema::AbstractArrayType) { |
| 4651 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 4652 | << T << TL.getSourceRange(); |
| 4653 | } else { |
| 4654 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 4655 | << Sel << T << TL.getSourceRange(); |
| 4656 | } |
| 4657 | Info.DiagnoseAbstractType(); |
| 4658 | } |
| 4659 | }; |
| 4660 | |
| 4661 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 4662 | Sema::AbstractDiagSelID Sel) { |
| 4663 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 4664 | } |
| 4665 | |
| 4666 | } |
| 4667 | |
| 4668 | /// Check for invalid uses of an abstract type in a method declaration. |
| 4669 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4670 | CXXMethodDecl *MD) { |
| 4671 | // No need to do the check on definitions, which require that |
| 4672 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 4673 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4674 | return; |
| 4675 | |
| 4676 | // For safety's sake, just ignore it if we don't have type source |
| 4677 | // information. This should never happen for non-implicit methods, |
| 4678 | // but... |
| 4679 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 4680 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 4681 | } |
| 4682 | |
| 4683 | /// Check for invalid uses of an abstract type within a class definition. |
| 4684 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4685 | CXXRecordDecl *RD) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4686 | for (auto *D : RD->decls()) { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4687 | if (D->isImplicit()) continue; |
| 4688 | |
| 4689 | // Methods and method templates. |
| 4690 | if (isa<CXXMethodDecl>(D)) { |
| 4691 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 4692 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 4693 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 4694 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 4695 | |
| 4696 | // Fields and static variables. |
| 4697 | } else if (isa<FieldDecl>(D)) { |
| 4698 | FieldDecl *FD = cast<FieldDecl>(D); |
| 4699 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 4700 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 4701 | } else if (isa<VarDecl>(D)) { |
| 4702 | VarDecl *VD = cast<VarDecl>(D); |
| 4703 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 4704 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 4705 | |
| 4706 | // Nested classes and class templates. |
| 4707 | } else if (isa<CXXRecordDecl>(D)) { |
| 4708 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 4709 | } else if (isa<ClassTemplateDecl>(D)) { |
| 4710 | CheckAbstractClassUsage(Info, |
| 4711 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 4712 | } |
| 4713 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4716 | /// \brief Check class-level dllimport/dllexport attribute. |
| 4717 | static void checkDLLAttribute(Sema &S, CXXRecordDecl *Class) { |
| 4718 | Attr *ClassAttr = getDLLAttr(Class); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4719 | |
| 4720 | // MSVC inherits DLL attributes to partial class template specializations. |
| 4721 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && !ClassAttr) { |
| 4722 | if (auto *Spec = dyn_cast<ClassTemplatePartialSpecializationDecl>(Class)) { |
| 4723 | if (Attr *TemplateAttr = |
| 4724 | getDLLAttr(Spec->getSpecializedTemplate()->getTemplatedDecl())) { |
| 4725 | auto *A = cast<InheritableAttr>(TemplateAttr->clone(S.getASTContext())); |
| 4726 | A->setInherited(true); |
| 4727 | ClassAttr = A; |
| 4728 | } |
| 4729 | } |
| 4730 | } |
| 4731 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4732 | if (!ClassAttr) |
| 4733 | return; |
| 4734 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4735 | if (!Class->isExternallyVisible()) { |
| 4736 | S.Diag(Class->getLocation(), diag::err_attribute_dll_not_extern) |
| 4737 | << Class << ClassAttr; |
| 4738 | return; |
| 4739 | } |
| 4740 | |
| 4741 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4742 | !ClassAttr->isInherited()) { |
| 4743 | // Diagnose dll attributes on members of class with dll attribute. |
| 4744 | for (Decl *Member : Class->decls()) { |
| 4745 | if (!isa<VarDecl>(Member) && !isa<CXXMethodDecl>(Member)) |
| 4746 | continue; |
| 4747 | InheritableAttr *MemberAttr = getDLLAttr(Member); |
| 4748 | if (!MemberAttr || MemberAttr->isInherited() || Member->isInvalidDecl()) |
| 4749 | continue; |
| 4750 | |
| 4751 | S.Diag(MemberAttr->getLocation(), |
| 4752 | diag::err_attribute_dll_member_of_dll_class) |
| 4753 | << MemberAttr << ClassAttr; |
| 4754 | S.Diag(ClassAttr->getLocation(), diag::note_previous_attribute); |
| 4755 | Member->setInvalidDecl(); |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | if (Class->getDescribedClassTemplate()) |
| 4760 | // Don't inherit dll attribute until the template is instantiated. |
| 4761 | return; |
| 4762 | |
| 4763 | // The class is either imported or exported. |
| 4764 | const bool ClassExported = ClassAttr->getKind() == attr::DLLExport; |
| 4765 | const bool ClassImported = !ClassExported; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4766 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4767 | TemplateSpecializationKind TSK = Class->getTemplateSpecializationKind(); |
| 4768 | |
| 4769 | // Don't dllexport explicit class template instantiation declarations. |
| 4770 | if (ClassExported && TSK == TSK_ExplicitInstantiationDeclaration) { |
| 4771 | Class->dropAttr<DLLExportAttr>(); |
| 4772 | return; |
| 4773 | } |
| 4774 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4775 | // Force declaration of implicit members so they can inherit the attribute. |
| 4776 | S.ForceDeclarationOfImplicitMembers(Class); |
| 4777 | |
| 4778 | // FIXME: MSVC's docs say all bases must be exportable, but this doesn't |
| 4779 | // seem to be true in practice? |
| 4780 | |
| 4781 | for (Decl *Member : Class->decls()) { |
| 4782 | VarDecl *VD = dyn_cast<VarDecl>(Member); |
| 4783 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 4784 | |
| 4785 | // Only methods and static fields inherit the attributes. |
| 4786 | if (!VD && !MD) |
| 4787 | continue; |
| 4788 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4789 | if (MD) { |
| 4790 | // Don't process deleted methods. |
| 4791 | if (MD->isDeleted()) |
| 4792 | continue; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4793 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4794 | if (MD->isMoveAssignmentOperator() && ClassImported && MD->isInlined()) { |
| 4795 | // Current MSVC versions don't export the move assignment operators, so |
| 4796 | // don't attempt to import them if we have a definition. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4797 | continue; |
| 4798 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4799 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4800 | if (MD->isInlined() && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4801 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4802 | // MinGW does not import or export inline methods. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4803 | continue; |
| 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | if (!getDLLAttr(Member)) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4808 | auto *NewAttr = |
| 4809 | cast<InheritableAttr>(ClassAttr->clone(S.getASTContext())); |
| 4810 | NewAttr->setInherited(true); |
| 4811 | Member->addAttr(NewAttr); |
| 4812 | } |
| 4813 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4814 | if (MD && ClassExported) { |
| 4815 | if (MD->isUserProvided()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4816 | // Instantiate non-default class member functions ... |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4817 | |
| 4818 | // .. except for certain kinds of template specializations. |
| 4819 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 4820 | continue; |
| 4821 | if (TSK == TSK_ImplicitInstantiation && !ClassAttr->isInherited()) |
| 4822 | continue; |
| 4823 | |
| 4824 | S.MarkFunctionReferenced(Class->getLocation(), MD); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4825 | |
| 4826 | // The function will be passed to the consumer when its definition is |
| 4827 | // encountered. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4828 | } else if (!MD->isTrivial() || MD->isExplicitlyDefaulted() || |
| 4829 | MD->isCopyAssignmentOperator() || |
| 4830 | MD->isMoveAssignmentOperator()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4831 | // Synthesize and instantiate non-trivial implicit methods, explicitly |
| 4832 | // defaulted methods, and the copy and move assignment operators. The |
| 4833 | // latter are exported even if they are trivial, because the address of |
| 4834 | // an operator can be taken and should compare equal accross libraries. |
| 4835 | DiagnosticErrorTrap Trap(S.Diags); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4836 | S.MarkFunctionReferenced(Class->getLocation(), MD); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4837 | if (Trap.hasErrorOccurred()) { |
| 4838 | S.Diag(ClassAttr->getLocation(), diag::note_due_to_dllexported_class) |
| 4839 | << Class->getName() << !S.getLangOpts().CPlusPlus11; |
| 4840 | break; |
| 4841 | } |
| 4842 | |
| 4843 | // There is no later point when we will see the definition of this |
| 4844 | // function, so pass it to the consumer now. |
| 4845 | S.Consumer.HandleTopLevelDecl(DeclGroupRef(MD)); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4846 | } |
| 4847 | } |
| 4848 | } |
| 4849 | } |
| 4850 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4851 | /// \brief Perform semantic checks on a class definition that has been |
| 4852 | /// completing, introducing implicitly-declared members, checking for |
| 4853 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4854 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 4855 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4856 | return; |
| 4857 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4858 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 4859 | AbstractUsageInfo Info(*this, Record); |
| 4860 | CheckAbstractClassUsage(Info, Record); |
| 4861 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4862 | |
| 4863 | // If this is not an aggregate type and has no user-declared constructor, |
| 4864 | // complain about any non-static data members of reference or const scalar |
| 4865 | // type, since they will never get initializers. |
| 4866 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
Douglas Gregor | 5e058eb | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 4867 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 4868 | !Record->isLambda()) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4869 | bool Complained = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4870 | for (const auto *F : Record->fields()) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4871 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4872 | continue; |
| 4873 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4874 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 4875 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4876 | if (!Complained) { |
| 4877 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 4878 | << Record->getTagKind() << Record; |
| 4879 | Complained = true; |
| 4880 | } |
| 4881 | |
| 4882 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 4883 | << F->getType()->isReferenceType() |
| 4884 | << F->getDeclName(); |
| 4885 | } |
| 4886 | } |
| 4887 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4888 | |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4889 | if (Record->getIdentifier()) { |
| 4890 | // C++ [class.mem]p13: |
| 4891 | // If T is the name of a class, then each of the following shall have a |
| 4892 | // name different from T: |
| 4893 | // - every member of every anonymous union that is a member of class T. |
| 4894 | // |
| 4895 | // C++ [class.mem]p14: |
| 4896 | // In addition, if class T has a user-declared constructor (12.1), every |
| 4897 | // non-static data member of class T shall have a name different from T. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4898 | DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 4899 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 4900 | ++I) { |
| 4901 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4902 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 4903 | isa<IndirectFieldDecl>(D)) { |
| 4904 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 4905 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4906 | break; |
| 4907 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4908 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4909 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4910 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4911 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 4912 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4913 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4914 | if ((!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) && |
| 4915 | !Record->hasAttr<FinalAttr>()) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4916 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 4917 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 4918 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4919 | |
David Majnemer | 7121bdb | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4920 | if (Record->isAbstract()) { |
| 4921 | if (FinalAttr *FA = Record->getAttr<FinalAttr>()) { |
| 4922 | Diag(Record->getLocation(), diag::warn_abstract_final_class) |
| 4923 | << FA->isSpelledAsSealed(); |
| 4924 | DiagnoseAbstractType(Record); |
| 4925 | } |
David Blaikie | b6b5b97 | 2012-09-21 03:21:07 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4928 | bool HasMethodWithOverrideControl = false, |
| 4929 | HasOverridingMethodWithoutOverrideControl = false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4930 | if (!Record->isDependentType()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4931 | for (auto *M : Record->methods()) { |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4932 | // See if a method overloads virtual methods in a base |
| 4933 | // class without overriding any. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4934 | if (!M->isStatic()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4935 | DiagnoseHiddenVirtualMethods(M); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4936 | if (M->hasAttr<OverrideAttr>()) |
| 4937 | HasMethodWithOverrideControl = true; |
| 4938 | else if (M->size_overridden_methods() > 0) |
| 4939 | HasOverridingMethodWithoutOverrideControl = true; |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4940 | // Check whether the explicitly-defaulted special members are valid. |
| 4941 | if (!M->isInvalidDecl() && M->isExplicitlyDefaulted()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4942 | CheckExplicitlyDefaultedSpecialMember(M); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4943 | |
| 4944 | // For an explicitly defaulted or deleted special member, we defer |
| 4945 | // determining triviality until the class is complete. That time is now! |
| 4946 | if (!M->isImplicit() && !M->isUserProvided()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4947 | CXXSpecialMember CSM = getSpecialMember(M); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4948 | if (CSM != CXXInvalid) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4949 | M->setTrivial(SpecialMemberIsTrivial(M, CSM)); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4950 | |
| 4951 | // Inform the class that we've finished declaring this member. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4952 | Record->finishedDefaultedOrDeletedMember(M); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4953 | } |
| 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4958 | if (HasMethodWithOverrideControl && |
| 4959 | HasOverridingMethodWithoutOverrideControl) { |
| 4960 | // At least one method has the 'override' control declared. |
| 4961 | // Diagnose all other overridden methods which do not have 'override' specified on them. |
| 4962 | for (auto *M : Record->methods()) |
| 4963 | DiagnoseAbsenceOfOverrideControl(M); |
| 4964 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4965 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4966 | // ms_struct is a request to use the same ABI rules as MSVC. Check |
| 4967 | // whether this class uses any C++ features that are implemented |
| 4968 | // completely differently in MSVC, and if so, emit a diagnostic. |
| 4969 | // That diagnostic defaults to an error, but we allow projects to |
| 4970 | // map it down to a warning (or ignore it). It's a fairly common |
| 4971 | // practice among users of the ms_struct pragma to mass-annotate |
| 4972 | // headers, sweeping up a bunch of types that the project doesn't |
| 4973 | // really rely on MSVC-compatible layout for. We must therefore |
| 4974 | // support "ms_struct except for C++ stuff" as a secondary ABI. |
| 4975 | if (Record->isMsStruct(Context) && |
| 4976 | (Record->isPolymorphic() || Record->getNumBases())) { |
| 4977 | Diag(Record->getLocation(), diag::warn_cxx_ms_struct); |
Warren Hunt | b2969b1 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4980 | // Declare inheriting constructors. We do this eagerly here because: |
| 4981 | // - The standard requires an eager diagnostic for conflicting inheriting |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4982 | // constructors from different classes. |
| 4983 | // - The lazy declaration of the other implicit constructors is so as to not |
| 4984 | // waste space and performance on classes that are not meant to be |
| 4985 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4986 | // have inheriting constructors. |
| 4987 | DeclareInheritingConstructors(Record); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4988 | |
| 4989 | checkDLLAttribute(*this, Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4992 | /// Look up the special member function that would be called by a special |
| 4993 | /// member function for a subobject of class type. |
| 4994 | /// |
| 4995 | /// \param Class The class type of the subobject. |
| 4996 | /// \param CSM The kind of special member function. |
| 4997 | /// \param FieldQuals If the subobject is a field, its cv-qualifiers. |
| 4998 | /// \param ConstRHS True if this is a copy operation with a const object |
| 4999 | /// on its RHS, that is, if the argument to the outer special member |
| 5000 | /// function is 'const' and this is not a field marked 'mutable'. |
| 5001 | static Sema::SpecialMemberOverloadResult *lookupCallFromSpecialMember( |
| 5002 | Sema &S, CXXRecordDecl *Class, Sema::CXXSpecialMember CSM, |
| 5003 | unsigned FieldQuals, bool ConstRHS) { |
| 5004 | unsigned LHSQuals = 0; |
| 5005 | if (CSM == Sema::CXXCopyAssignment || CSM == Sema::CXXMoveAssignment) |
| 5006 | LHSQuals = FieldQuals; |
| 5007 | |
| 5008 | unsigned RHSQuals = FieldQuals; |
| 5009 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 5010 | RHSQuals = 0; |
| 5011 | else if (ConstRHS) |
| 5012 | RHSQuals |= Qualifiers::Const; |
| 5013 | |
| 5014 | return S.LookupSpecialMember(Class, CSM, |
| 5015 | RHSQuals & Qualifiers::Const, |
| 5016 | RHSQuals & Qualifiers::Volatile, |
| 5017 | false, |
| 5018 | LHSQuals & Qualifiers::Const, |
| 5019 | LHSQuals & Qualifiers::Volatile); |
| 5020 | } |
| 5021 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5022 | /// Is the special member function which would be selected to perform the |
| 5023 | /// specified operation on the specified class type a constexpr constructor? |
| 5024 | static bool specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 5025 | Sema::CXXSpecialMember CSM, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5026 | unsigned Quals, bool ConstRHS) { |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5027 | Sema::SpecialMemberOverloadResult *SMOR = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5028 | lookupCallFromSpecialMember(S, ClassDecl, CSM, Quals, ConstRHS); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5029 | if (!SMOR || !SMOR->getMethod()) |
| 5030 | // A constructor we wouldn't select can't be "involved in initializing" |
| 5031 | // anything. |
| 5032 | return true; |
| 5033 | return SMOR->getMethod()->isConstexpr(); |
| 5034 | } |
| 5035 | |
| 5036 | /// Determine whether the specified special member function would be constexpr |
| 5037 | /// if it were implicitly defined. |
| 5038 | static bool defaultedSpecialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 5039 | Sema::CXXSpecialMember CSM, |
| 5040 | bool ConstArg) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5041 | if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5042 | return false; |
| 5043 | |
| 5044 | // C++11 [dcl.constexpr]p4: |
| 5045 | // In the definition of a constexpr constructor [...] |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5046 | bool Ctor = true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5047 | switch (CSM) { |
| 5048 | case Sema::CXXDefaultConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 5049 | // Since default constructor lookup is essentially trivial (and cannot |
| 5050 | // involve, for instance, template instantiation), we compute whether a |
| 5051 | // defaulted default constructor is constexpr directly within CXXRecordDecl. |
| 5052 | // |
| 5053 | // This is important for performance; we need to know whether the default |
| 5054 | // constructor is constexpr to determine whether the type is a literal type. |
| 5055 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 5056 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5057 | case Sema::CXXCopyConstructor: |
| 5058 | case Sema::CXXMoveConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 5059 | // For copy or move constructors, we need to perform overload resolution. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5060 | break; |
| 5061 | |
| 5062 | case Sema::CXXCopyAssignment: |
| 5063 | case Sema::CXXMoveAssignment: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5064 | if (!S.getLangOpts().CPlusPlus14) |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5065 | return false; |
| 5066 | // In C++1y, we need to perform overload resolution. |
| 5067 | Ctor = false; |
| 5068 | break; |
| 5069 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5070 | case Sema::CXXDestructor: |
| 5071 | case Sema::CXXInvalid: |
| 5072 | return false; |
| 5073 | } |
| 5074 | |
| 5075 | // -- if the class is a non-empty union, or for each non-empty anonymous |
| 5076 | // union member of a non-union class, exactly one non-static data member |
| 5077 | // shall be initialized; [DR1359] |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 5078 | // |
| 5079 | // If we squint, this is guaranteed, since exactly one non-static data member |
| 5080 | // will be initialized (if the constructor isn't deleted), we just don't know |
| 5081 | // which one. |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5082 | if (Ctor && ClassDecl->isUnion()) |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 5083 | return true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5084 | |
| 5085 | // -- the class shall not have any virtual base classes; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5086 | if (Ctor && ClassDecl->getNumVBases()) |
| 5087 | return false; |
| 5088 | |
| 5089 | // C++1y [class.copy]p26: |
| 5090 | // -- [the class] is a literal type, and |
| 5091 | if (!Ctor && !ClassDecl->isLiteral()) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5092 | return false; |
| 5093 | |
| 5094 | // -- every constructor involved in initializing [...] base class |
| 5095 | // sub-objects shall be a constexpr constructor; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5096 | // -- the assignment operator selected to copy/move each direct base |
| 5097 | // class is a constexpr function, and |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5098 | for (const auto &B : ClassDecl->bases()) { |
| 5099 | const RecordType *BaseType = B.getType()->getAs<RecordType>(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5100 | if (!BaseType) continue; |
| 5101 | |
| 5102 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5103 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, 0, ConstArg)) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5104 | return false; |
| 5105 | } |
| 5106 | |
| 5107 | // -- every constructor involved in initializing non-static data members |
| 5108 | // [...] shall be a constexpr constructor; |
| 5109 | // -- every non-static data member and base class sub-object shall be |
| 5110 | // initialized |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5111 | // -- for each non-static data member of X that is of class type (or array |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5112 | // thereof), the assignment operator selected to copy/move that member is |
| 5113 | // a constexpr function |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5114 | for (const auto *F : ClassDecl->fields()) { |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5115 | if (F->isInvalidDecl()) |
| 5116 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5117 | QualType BaseType = S.Context.getBaseElementType(F->getType()); |
| 5118 | if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5119 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5120 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, |
| 5121 | BaseType.getCVRQualifiers(), |
| 5122 | ConstArg && !F->isMutable())) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5123 | return false; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | // All OK, it's constexpr! |
| 5128 | return true; |
| 5129 | } |
| 5130 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 5131 | static Sema::ImplicitExceptionSpecification |
| 5132 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, CXXMethodDecl *MD) { |
| 5133 | switch (S.getSpecialMember(MD)) { |
| 5134 | case Sema::CXXDefaultConstructor: |
| 5135 | return S.ComputeDefaultedDefaultCtorExceptionSpec(Loc, MD); |
| 5136 | case Sema::CXXCopyConstructor: |
| 5137 | return S.ComputeDefaultedCopyCtorExceptionSpec(MD); |
| 5138 | case Sema::CXXCopyAssignment: |
| 5139 | return S.ComputeDefaultedCopyAssignmentExceptionSpec(MD); |
| 5140 | case Sema::CXXMoveConstructor: |
| 5141 | return S.ComputeDefaultedMoveCtorExceptionSpec(MD); |
| 5142 | case Sema::CXXMoveAssignment: |
| 5143 | return S.ComputeDefaultedMoveAssignmentExceptionSpec(MD); |
| 5144 | case Sema::CXXDestructor: |
| 5145 | return S.ComputeDefaultedDtorExceptionSpec(MD); |
| 5146 | case Sema::CXXInvalid: |
| 5147 | break; |
| 5148 | } |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 5149 | assert(cast<CXXConstructorDecl>(MD)->getInheritedConstructor() && |
| 5150 | "only special members have implicit exception specs"); |
| 5151 | return S.ComputeInheritingCtorExceptionSpec(cast<CXXConstructorDecl>(MD)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 5154 | static FunctionProtoType::ExtProtoInfo getImplicitMethodEPI(Sema &S, |
| 5155 | CXXMethodDecl *MD) { |
| 5156 | FunctionProtoType::ExtProtoInfo EPI; |
| 5157 | |
| 5158 | // Build an exception specification pointing back at this member. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5159 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 5160 | EPI.ExceptionSpec.SourceDecl = MD; |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 5161 | |
| 5162 | // Set the calling convention to the default for C++ instance methods. |
| 5163 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv( |
| 5164 | S.Context.getDefaultCallingConvention(/*IsVariadic=*/false, |
| 5165 | /*IsCXXMethod=*/true)); |
| 5166 | return EPI; |
| 5167 | } |
| 5168 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 5169 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD) { |
| 5170 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 5171 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 5172 | return; |
| 5173 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 5174 | // Evaluate the exception specification. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5175 | auto ESI = computeImplicitExceptionSpec(*this, Loc, MD).getExceptionSpec(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5176 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 5177 | // Update the type of the special member to use it. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5178 | UpdateExceptionSpec(MD, ESI); |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 5179 | |
| 5180 | // A user-provided destructor can be defined outside the class. When that |
| 5181 | // happens, be sure to update the exception specification on both |
| 5182 | // declarations. |
| 5183 | const FunctionProtoType *CanonicalFPT = |
| 5184 | MD->getCanonicalDecl()->getType()->castAs<FunctionProtoType>(); |
| 5185 | if (CanonicalFPT->getExceptionSpecType() == EST_Unevaluated) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5186 | UpdateExceptionSpec(MD->getCanonicalDecl(), ESI); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5189 | void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) { |
| 5190 | CXXRecordDecl *RD = MD->getParent(); |
| 5191 | CXXSpecialMember CSM = getSpecialMember(MD); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 5192 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5193 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 5194 | "not an explicitly-defaulted special member"); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 5195 | |
| 5196 | // Whether this was the first-declared instance of the constructor. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5197 | // This affects whether we implicitly add an exception spec and constexpr. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 5198 | bool First = MD == MD->getCanonicalDecl(); |
| 5199 | |
| 5200 | bool HadError = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5201 | |
| 5202 | // C++11 [dcl.fct.def.default]p1: |
| 5203 | // A function that is explicitly defaulted shall |
| 5204 | // -- be a special member function (checked elsewhere), |
| 5205 | // -- have the same type (except for ref-qualifiers, and except that a |
| 5206 | // copy operation can take a non-const reference) as an implicit |
| 5207 | // declaration, and |
| 5208 | // -- not have default arguments. |
| 5209 | unsigned ExpectedParams = 1; |
| 5210 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 5211 | ExpectedParams = 0; |
| 5212 | if (MD->getNumParams() != ExpectedParams) { |
| 5213 | // This also checks for default arguments: a copy or move constructor with a |
| 5214 | // default argument is classified as a default constructor, and assignment |
| 5215 | // operations and destructors can't have default arguments. |
| 5216 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 5217 | << CSM << MD->getSourceRange(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 5218 | HadError = true; |
Richard Smith | 5046439 | 2012-12-07 02:10:28 +0000 | [diff] [blame] | 5219 | } else if (MD->isVariadic()) { |
| 5220 | Diag(MD->getLocation(), diag::err_defaulted_special_member_variadic) |
| 5221 | << CSM << MD->getSourceRange(); |
| 5222 | HadError = true; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5225 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 5226 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5227 | bool CanHaveConstParam = false; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5228 | if (CSM == CXXCopyConstructor) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 5229 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5230 | else if (CSM == CXXCopyAssignment) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 5231 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 5232 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5233 | QualType ReturnType = Context.VoidTy; |
| 5234 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 5235 | // Check for return type matching. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5236 | ReturnType = Type->getReturnType(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5237 | QualType ExpectedReturnType = |
| 5238 | Context.getLValueReferenceType(Context.getTypeDeclType(RD)); |
| 5239 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 5240 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 5241 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 5242 | HadError = true; |
| 5243 | } |
| 5244 | |
| 5245 | // A defaulted special member cannot have cv-qualifiers. |
| 5246 | if (Type->getTypeQuals()) { |
| 5247 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5248 | << (CSM == CXXMoveAssignment) << getLangOpts().CPlusPlus14; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5249 | HadError = true; |
| 5250 | } |
| 5251 | } |
| 5252 | |
| 5253 | // Check for parameter type matching. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5254 | QualType ArgType = ExpectedParams ? Type->getParamType(0) : QualType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5255 | bool HasConstParam = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5256 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 5257 | // Argument must be reference to possibly-const T. |
| 5258 | QualType ReferentType = ArgType->getPointeeType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5259 | HasConstParam = ReferentType.isConstQualified(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5260 | |
| 5261 | if (ReferentType.isVolatileQualified()) { |
| 5262 | Diag(MD->getLocation(), |
| 5263 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 5264 | HadError = true; |
| 5265 | } |
| 5266 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5267 | if (HasConstParam && !CanHaveConstParam) { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5268 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 5269 | Diag(MD->getLocation(), |
| 5270 | diag::err_defaulted_special_member_copy_const_param) |
| 5271 | << (CSM == CXXCopyAssignment); |
| 5272 | // FIXME: Explain why this special member can't be const. |
| 5273 | } else { |
| 5274 | Diag(MD->getLocation(), |
| 5275 | diag::err_defaulted_special_member_move_const_param) |
| 5276 | << (CSM == CXXMoveAssignment); |
| 5277 | } |
| 5278 | HadError = true; |
| 5279 | } |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5280 | } else if (ExpectedParams) { |
| 5281 | // A copy assignment operator can take its argument by value, but a |
| 5282 | // defaulted one cannot. |
| 5283 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 5284 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 5285 | HadError = true; |
| 5286 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 5287 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5288 | // C++11 [dcl.fct.def.default]p2: |
| 5289 | // An explicitly-defaulted function may be declared constexpr only if it |
| 5290 | // would have been implicitly declared as constexpr, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5291 | // Do not apply this rule to members of class templates, since core issue 1358 |
| 5292 | // makes such functions always instantiate to constexpr functions. For |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5293 | // functions which cannot be constexpr (for non-constructors in C++11 and for |
| 5294 | // destructors in C++1y), this is checked elsewhere. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 5295 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 5296 | HasConstParam); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5297 | if ((getLangOpts().CPlusPlus14 ? !isa<CXXDestructorDecl>(MD) |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5298 | : isa<CXXConstructorDecl>(MD)) && |
| 5299 | MD->isConstexpr() && !Constexpr && |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5300 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 5301 | Diag(MD->getLocStart(), diag::err_incorrect_defaulted_constexpr) << CSM; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5302 | // FIXME: Explain why the special member can't be constexpr. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5303 | HadError = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5304 | } |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5305 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5306 | // and may have an explicit exception-specification only if it is compatible |
| 5307 | // with the exception-specification on the implicit declaration. |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5308 | if (Type->hasExceptionSpec()) { |
| 5309 | // Delay the check if this is the first declaration of the special member, |
| 5310 | // since we may not have parsed some necessary in-class initializers yet. |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 5311 | if (First) { |
| 5312 | // If the exception specification needs to be instantiated, do so now, |
| 5313 | // before we clobber it with an EST_Unevaluated specification below. |
| 5314 | if (Type->getExceptionSpecType() == EST_Uninstantiated) { |
| 5315 | InstantiateExceptionSpec(MD->getLocStart(), MD); |
| 5316 | Type = MD->getType()->getAs<FunctionProtoType>(); |
| 5317 | } |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5318 | DelayedDefaultedMemberExceptionSpecs.push_back(std::make_pair(MD, Type)); |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 5319 | } else |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5320 | CheckExplicitlyDefaultedMemberExceptionSpec(MD, Type); |
| 5321 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5322 | |
| 5323 | // If a function is explicitly defaulted on its first declaration, |
| 5324 | if (First) { |
| 5325 | // -- it is implicitly considered to be constexpr if the implicit |
| 5326 | // definition would be, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5327 | MD->setConstexpr(Constexpr); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5328 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5329 | // -- it is implicitly considered to have the same exception-specification |
| 5330 | // as if it had been implicitly declared, |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5331 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5332 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 5333 | EPI.ExceptionSpec.SourceDecl = MD; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 5334 | MD->setType(Context.getFunctionType(ReturnType, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5335 | llvm::makeArrayRef(&ArgType, |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 5336 | ExpectedParams), |
| 5337 | EPI)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 5338 | } |
| 5339 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5340 | if (ShouldDeleteSpecialMember(MD, CSM)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 5341 | if (First) { |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 5342 | SetDeclDeleted(MD, MD->getLocation()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 5343 | } else { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5344 | // C++11 [dcl.fct.def.default]p4: |
| 5345 | // [For a] user-provided explicitly-defaulted function [...] if such a |
| 5346 | // function is implicitly defined as deleted, the program is ill-formed. |
| 5347 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5348 | ShouldDeleteSpecialMember(MD, CSM, /*Diagnose*/true); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5349 | HadError = true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 5350 | } |
| 5351 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 5352 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 5353 | if (HadError) |
| 5354 | MD->setInvalidDecl(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 5355 | } |
| 5356 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5357 | /// Check whether the exception specification provided for an |
| 5358 | /// explicitly-defaulted special member matches the exception specification |
| 5359 | /// that would have been generated for an implicit special member, per |
| 5360 | /// C++11 [dcl.fct.def.default]p2. |
| 5361 | void Sema::CheckExplicitlyDefaultedMemberExceptionSpec( |
| 5362 | CXXMethodDecl *MD, const FunctionProtoType *SpecifiedType) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5363 | // If the exception specification was explicitly specified but hadn't been |
| 5364 | // parsed when the method was defaulted, grab it now. |
| 5365 | if (SpecifiedType->getExceptionSpecType() == EST_Unparsed) |
| 5366 | SpecifiedType = |
| 5367 | MD->getTypeSourceInfo()->getType()->castAs<FunctionProtoType>(); |
| 5368 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5369 | // Compute the implicit exception specification. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 5370 | CallingConv CC = Context.getDefaultCallingConvention(/*IsVariadic=*/false, |
| 5371 | /*IsCXXMethod=*/true); |
| 5372 | FunctionProtoType::ExtProtoInfo EPI(CC); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5373 | EPI.ExceptionSpec = computeImplicitExceptionSpec(*this, MD->getLocation(), MD) |
| 5374 | .getExceptionSpec(); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5375 | const FunctionProtoType *ImplicitType = cast<FunctionProtoType>( |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 5376 | Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5377 | |
| 5378 | // Ensure that it matches. |
| 5379 | CheckEquivalentExceptionSpec( |
| 5380 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 5381 | << getSpecialMember(MD), PDiag(), |
| 5382 | ImplicitType, SourceLocation(), |
| 5383 | SpecifiedType, MD->getLocation()); |
| 5384 | } |
| 5385 | |
Alp Toker | 0823566 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 5386 | void Sema::CheckDelayedMemberExceptionSpecs() { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5387 | decltype(DelayedExceptionSpecChecks) Checks; |
| 5388 | decltype(DelayedDefaultedMemberExceptionSpecs) Specs; |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5389 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5390 | std::swap(Checks, DelayedExceptionSpecChecks); |
Alp Toker | 0823566 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 5391 | std::swap(Specs, DelayedDefaultedMemberExceptionSpecs); |
| 5392 | |
| 5393 | // Perform any deferred checking of exception specifications for virtual |
| 5394 | // destructors. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5395 | for (auto &Check : Checks) |
| 5396 | CheckOverridingFunctionExceptionSpec(Check.first, Check.second); |
Alp Toker | 0823566 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 5397 | |
| 5398 | // Check that any explicitly-defaulted methods have exception specifications |
| 5399 | // compatible with their implicit exception specifications. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5400 | for (auto &Spec : Specs) |
| 5401 | CheckExplicitlyDefaultedMemberExceptionSpec(Spec.first, Spec.second); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5404 | namespace { |
| 5405 | struct SpecialMemberDeletionInfo { |
| 5406 | Sema &S; |
| 5407 | CXXMethodDecl *MD; |
| 5408 | Sema::CXXSpecialMember CSM; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5409 | bool Diagnose; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5410 | |
| 5411 | // Properties of the special member, computed for convenience. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5412 | bool IsConstructor, IsAssignment, IsMove, ConstArg; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5413 | SourceLocation Loc; |
| 5414 | |
| 5415 | bool AllFieldsAreConst; |
| 5416 | |
| 5417 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5418 | Sema::CXXSpecialMember CSM, bool Diagnose) |
| 5419 | : S(S), MD(MD), CSM(CSM), Diagnose(Diagnose), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5420 | IsConstructor(false), IsAssignment(false), IsMove(false), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5421 | ConstArg(false), Loc(MD->getLocation()), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5422 | AllFieldsAreConst(true) { |
| 5423 | switch (CSM) { |
| 5424 | case Sema::CXXDefaultConstructor: |
| 5425 | case Sema::CXXCopyConstructor: |
| 5426 | IsConstructor = true; |
| 5427 | break; |
| 5428 | case Sema::CXXMoveConstructor: |
| 5429 | IsConstructor = true; |
| 5430 | IsMove = true; |
| 5431 | break; |
| 5432 | case Sema::CXXCopyAssignment: |
| 5433 | IsAssignment = true; |
| 5434 | break; |
| 5435 | case Sema::CXXMoveAssignment: |
| 5436 | IsAssignment = true; |
| 5437 | IsMove = true; |
| 5438 | break; |
| 5439 | case Sema::CXXDestructor: |
| 5440 | break; |
| 5441 | case Sema::CXXInvalid: |
| 5442 | llvm_unreachable("invalid special member kind"); |
| 5443 | } |
| 5444 | |
| 5445 | if (MD->getNumParams()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5446 | if (const ReferenceType *RT = |
| 5447 | MD->getParamDecl(0)->getType()->getAs<ReferenceType>()) |
| 5448 | ConstArg = RT->getPointeeType().isConstQualified(); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5449 | } |
| 5450 | } |
| 5451 | |
| 5452 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 5453 | |
| 5454 | /// Look up the corresponding special member in the given class. |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5455 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5456 | unsigned Quals, bool IsMutable) { |
| 5457 | return lookupCallFromSpecialMember(S, Class, CSM, Quals, |
| 5458 | ConstArg && !IsMutable); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5461 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5462 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5463 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5464 | bool shouldDeleteForField(FieldDecl *FD); |
| 5465 | bool shouldDeleteForAllConstMembers(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5466 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5467 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 5468 | unsigned Quals); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5469 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 5470 | Sema::SpecialMemberOverloadResult *SMOR, |
| 5471 | bool IsDtorCallInCtor); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5472 | |
| 5473 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5474 | }; |
| 5475 | } |
| 5476 | |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5477 | /// Is the given special member inaccessible when used on the given |
| 5478 | /// sub-object. |
| 5479 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 5480 | CXXMethodDecl *target) { |
| 5481 | /// If we're operating on a base class, the object type is the |
| 5482 | /// type of this special member. |
| 5483 | QualType objectTy; |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 5484 | AccessSpecifier access = target->getAccess(); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5485 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 5486 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 5487 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 5488 | |
| 5489 | // If we're operating on a field, the object type is the type of the field. |
| 5490 | } else { |
| 5491 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 5492 | } |
| 5493 | |
| 5494 | return S.isSpecialMemberAccessibleForDeletion(target, access, objectTy); |
| 5495 | } |
| 5496 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5497 | /// Check whether we should delete a special member due to the implicit |
| 5498 | /// definition containing a call to a special member of a subobject. |
| 5499 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 5500 | Subobject Subobj, Sema::SpecialMemberOverloadResult *SMOR, |
| 5501 | bool IsDtorCallInCtor) { |
| 5502 | CXXMethodDecl *Decl = SMOR->getMethod(); |
| 5503 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 5504 | |
| 5505 | int DiagKind = -1; |
| 5506 | |
| 5507 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 5508 | DiagKind = !Decl ? 0 : 1; |
| 5509 | else if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5510 | DiagKind = 2; |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5511 | else if (!isAccessible(Subobj, Decl)) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5512 | DiagKind = 3; |
| 5513 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 5514 | !Decl->isTrivial()) { |
| 5515 | // A member of a union must have a trivial corresponding special member. |
| 5516 | // As a weird special case, a destructor call from a union's constructor |
| 5517 | // must be accessible and non-deleted, but need not be trivial. Such a |
| 5518 | // destructor is never actually called, but is semantically checked as |
| 5519 | // if it were. |
| 5520 | DiagKind = 4; |
| 5521 | } |
| 5522 | |
| 5523 | if (DiagKind == -1) |
| 5524 | return false; |
| 5525 | |
| 5526 | if (Diagnose) { |
| 5527 | if (Field) { |
| 5528 | S.Diag(Field->getLocation(), |
| 5529 | diag::note_deleted_special_member_class_subobject) |
| 5530 | << CSM << MD->getParent() << /*IsField*/true |
| 5531 | << Field << DiagKind << IsDtorCallInCtor; |
| 5532 | } else { |
| 5533 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 5534 | S.Diag(Base->getLocStart(), |
| 5535 | diag::note_deleted_special_member_class_subobject) |
| 5536 | << CSM << MD->getParent() << /*IsField*/false |
| 5537 | << Base->getType() << DiagKind << IsDtorCallInCtor; |
| 5538 | } |
| 5539 | |
| 5540 | if (DiagKind == 1) |
| 5541 | S.NoteDeletedFunction(Decl); |
| 5542 | // FIXME: Explain inaccessibility if DiagKind == 3. |
| 5543 | } |
| 5544 | |
| 5545 | return true; |
| 5546 | } |
| 5547 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5548 | /// Check whether we should delete a special member function due to having a |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5549 | /// direct or virtual base class or non-static data member of class type M. |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5550 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5551 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5552 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5553 | bool IsMutable = Field && Field->isMutable(); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5554 | |
| 5555 | // C++11 [class.ctor]p5: |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5556 | // -- any direct or virtual base class, or non-static data member with no |
| 5557 | // brace-or-equal-initializer, has class type M (or array thereof) and |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5558 | // either M has no default constructor or overload resolution as applied |
| 5559 | // to M's default constructor results in an ambiguity or in a function |
| 5560 | // that is deleted or inaccessible |
| 5561 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 5562 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 5563 | // overload resolution, as applied to B's corresponding special member, |
| 5564 | // results in an ambiguity or a function that is deleted or inaccessible |
| 5565 | // from the defaulted special member |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5566 | // C++11 [class.dtor]p5: |
| 5567 | // -- any direct or virtual base class [...] has a type with a destructor |
| 5568 | // that is deleted or inaccessible |
| 5569 | if (!(CSM == Sema::CXXDefaultConstructor && |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5570 | Field && Field->hasInClassInitializer()) && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5571 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals, IsMutable), |
| 5572 | false)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5573 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5574 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5575 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 5576 | // -- any direct or virtual base class or non-static data member has a |
| 5577 | // type with a destructor that is deleted or inaccessible |
| 5578 | if (IsConstructor) { |
| 5579 | Sema::SpecialMemberOverloadResult *SMOR = |
| 5580 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 5581 | false, false, false, false, false); |
| 5582 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 5583 | return true; |
| 5584 | } |
| 5585 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5586 | return false; |
| 5587 | } |
| 5588 | |
| 5589 | /// Check whether we should delete a special member function due to the class |
| 5590 | /// having a particular direct or virtual base class. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5591 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5592 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5593 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | /// Check whether we should delete a special member function due to the class |
| 5597 | /// having a particular non-static data member. |
| 5598 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 5599 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 5600 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 5601 | |
| 5602 | if (CSM == Sema::CXXDefaultConstructor) { |
| 5603 | // For a default constructor, all references must be initialized in-class |
| 5604 | // and, if a union, it must have a non-const member. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5605 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 5606 | if (Diagnose) |
| 5607 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 5608 | << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5609 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5610 | } |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 5611 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 5612 | // const-qualified type (or array thereof) with no |
| 5613 | // brace-or-equal-initializer does not have a user-provided default |
| 5614 | // constructor. |
| 5615 | if (!inUnion() && FieldType.isConstQualified() && |
| 5616 | !FD->hasInClassInitializer() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5617 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 5618 | if (Diagnose) |
| 5619 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 5620 | << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 5621 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | if (inUnion() && !FieldType.isConstQualified()) |
| 5625 | AllFieldsAreConst = false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5626 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 5627 | // For a copy constructor, data members must not be of rvalue reference |
| 5628 | // type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5629 | if (FieldType->isRValueReferenceType()) { |
| 5630 | if (Diagnose) |
| 5631 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 5632 | << MD->getParent() << FD << FieldType; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5633 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5634 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5635 | } else if (IsAssignment) { |
| 5636 | // For an assignment operator, data members must not be of reference type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5637 | if (FieldType->isReferenceType()) { |
| 5638 | if (Diagnose) |
| 5639 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 5640 | << IsMove << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5641 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5642 | } |
| 5643 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 5644 | // C++11 [class.copy]p23: |
| 5645 | // -- a non-static data member of const non-class type (or array thereof) |
| 5646 | if (Diagnose) |
| 5647 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 5648 | << IsMove << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5649 | return true; |
| 5650 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5651 | } |
| 5652 | |
| 5653 | if (FieldRecord) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5654 | // Some additional restrictions exist on the variant members. |
| 5655 | if (!inUnion() && FieldRecord->isUnion() && |
| 5656 | FieldRecord->isAnonymousStructOrUnion()) { |
| 5657 | bool AllVariantFieldsAreConst = true; |
| 5658 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5659 | // FIXME: Handle anonymous unions declared within anonymous unions. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5660 | for (auto *UI : FieldRecord->fields()) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5661 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5662 | |
| 5663 | if (!UnionFieldType.isConstQualified()) |
| 5664 | AllVariantFieldsAreConst = false; |
| 5665 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5666 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 5667 | if (UnionFieldRecord && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5668 | shouldDeleteForClassSubobject(UnionFieldRecord, UI, |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5669 | UnionFieldType.getCVRQualifiers())) |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5670 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
| 5673 | // At least one member in each anonymous union must be non-const |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 5674 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5675 | !FieldRecord->field_empty()) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5676 | if (Diagnose) |
| 5677 | S.Diag(FieldRecord->getLocation(), |
| 5678 | diag::note_deleted_default_ctor_all_const) |
| 5679 | << MD->getParent() << /*anonymous union*/1; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5680 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5681 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5682 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5683 | // Don't check the implicit member of the anonymous union type. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5684 | // This is technically non-conformant, but sanity demands it. |
| 5685 | return false; |
| 5686 | } |
| 5687 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5688 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 5689 | FieldType.getCVRQualifiers())) |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5690 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
| 5693 | return false; |
| 5694 | } |
| 5695 | |
| 5696 | /// C++11 [class.ctor] p5: |
| 5697 | /// A defaulted default constructor for a class X is defined as deleted if |
| 5698 | /// X is a union and all of its variant members are of const-qualified type. |
| 5699 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 5700 | // This is a silly definition, because it gives an empty union a deleted |
| 5701 | // default constructor. Don't do that. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5702 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5703 | !MD->getParent()->field_empty()) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5704 | if (Diagnose) |
| 5705 | S.Diag(MD->getParent()->getLocation(), |
| 5706 | diag::note_deleted_default_ctor_all_const) |
| 5707 | << MD->getParent() << /*not anonymous union*/0; |
| 5708 | return true; |
| 5709 | } |
| 5710 | return false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | /// Determine whether a defaulted special member function should be defined as |
| 5714 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 5715 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5716 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5717 | bool Diagnose) { |
Richard Smith | eef0029 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 5718 | if (MD->isInvalidDecl()) |
| 5719 | return false; |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5720 | CXXRecordDecl *RD = MD->getParent(); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5721 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5722 | if (!LangOpts.CPlusPlus11 || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5723 | return false; |
| 5724 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5725 | // C++11 [expr.lambda.prim]p19: |
| 5726 | // The closure type associated with a lambda-expression has a |
| 5727 | // deleted (8.4.3) default constructor and a deleted copy |
| 5728 | // assignment operator. |
| 5729 | if (RD->isLambda() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5730 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 5731 | if (Diagnose) |
| 5732 | Diag(RD->getLocation(), diag::note_lambda_decl); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5733 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5736 | // For an anonymous struct or union, the copy and assignment special members |
| 5737 | // will never be used, so skip the check. For an anonymous union declared at |
| 5738 | // namespace scope, the constructor and destructor are used. |
| 5739 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 5740 | RD->isAnonymousStructOrUnion()) |
| 5741 | return false; |
| 5742 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5743 | // C++11 [class.copy]p7, p18: |
| 5744 | // If the class definition declares a move constructor or move assignment |
| 5745 | // operator, an implicitly declared copy constructor or copy assignment |
| 5746 | // operator is defined as deleted. |
| 5747 | if (MD->isImplicit() && |
| 5748 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5749 | CXXMethodDecl *UserDeclaredMove = nullptr; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5750 | |
| 5751 | // In Microsoft mode, a user-declared move only causes the deletion of the |
| 5752 | // corresponding copy operation, not both copy operations. |
| 5753 | if (RD->hasUserDeclaredMoveConstructor() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5754 | (!getLangOpts().MSVCCompat || CSM == CXXCopyConstructor)) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5755 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5756 | |
| 5757 | // Find any user-declared move constructor. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5758 | for (auto *I : RD->ctors()) { |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5759 | if (I->isMoveConstructor()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5760 | UserDeclaredMove = I; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5761 | break; |
| 5762 | } |
| 5763 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5764 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5765 | } else if (RD->hasUserDeclaredMoveAssignment() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5766 | (!getLangOpts().MSVCCompat || CSM == CXXCopyAssignment)) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5767 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5768 | |
| 5769 | // Find any user-declared move assignment operator. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5770 | for (auto *I : RD->methods()) { |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5771 | if (I->isMoveAssignmentOperator()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5772 | UserDeclaredMove = I; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5773 | break; |
| 5774 | } |
| 5775 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5776 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | if (UserDeclaredMove) { |
| 5780 | Diag(UserDeclaredMove->getLocation(), |
| 5781 | diag::note_deleted_copy_user_declared_move) |
Richard Smith | e6af660 | 2012-04-02 21:07:48 +0000 | [diff] [blame] | 5782 | << (CSM == CXXCopyAssignment) << RD |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5783 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 5784 | return true; |
| 5785 | } |
| 5786 | } |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5787 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5788 | // Do access control from the special member function |
| 5789 | ContextRAII MethodContext(*this, MD); |
| 5790 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5791 | // C++11 [class.dtor]p5: |
| 5792 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 5793 | // results in an ambiguity or in a function that is deleted or inaccessible |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5794 | if (CSM == CXXDestructor && MD->isVirtual()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5795 | FunctionDecl *OperatorDelete = nullptr; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5796 | DeclarationName Name = |
| 5797 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 5798 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5799 | OperatorDelete, false)) { |
| 5800 | if (Diagnose) |
| 5801 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5802 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5803 | } |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5806 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, Diagnose); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5807 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5808 | for (auto &BI : RD->bases()) |
| 5809 | if (!BI.isVirtual() && |
| 5810 | SMI.shouldDeleteForBase(&BI)) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5811 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5812 | |
Richard Smith | e088360 | 2013-07-22 18:06:23 +0000 | [diff] [blame] | 5813 | // Per DR1611, do not consider virtual bases of constructors of abstract |
| 5814 | // classes, since we are not going to construct them. |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 5815 | if (!RD->isAbstract() || !SMI.IsConstructor) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5816 | for (auto &BI : RD->vbases()) |
| 5817 | if (SMI.shouldDeleteForBase(&BI)) |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 5818 | return true; |
| 5819 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5820 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5821 | for (auto *FI : RD->fields()) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5822 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5823 | SMI.shouldDeleteForField(FI)) |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 5824 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5825 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5826 | if (SMI.shouldDeleteForAllConstMembers()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5827 | return true; |
| 5828 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5829 | if (getLangOpts().CUDA) { |
| 5830 | // We should delete the special member in CUDA mode if target inference |
| 5831 | // failed. |
| 5832 | return inferCUDATargetForImplicitSpecialMember(RD, CSM, MD, SMI.ConstArg, |
| 5833 | Diagnose); |
| 5834 | } |
| 5835 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5836 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5839 | /// Perform lookup for a special member of the specified kind, and determine |
| 5840 | /// whether it is trivial. If the triviality can be determined without the |
| 5841 | /// lookup, skip it. This is intended for use when determining whether a |
| 5842 | /// special member of a containing object is trivial, and thus does not ever |
| 5843 | /// perform overload resolution for default constructors. |
| 5844 | /// |
| 5845 | /// If \p Selected is not \c NULL, \c *Selected will be filled in with the |
| 5846 | /// member that was most likely to be intended to be trivial, if any. |
| 5847 | static bool findTrivialSpecialMember(Sema &S, CXXRecordDecl *RD, |
| 5848 | Sema::CXXSpecialMember CSM, unsigned Quals, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5849 | bool ConstRHS, CXXMethodDecl **Selected) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5850 | if (Selected) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5851 | *Selected = nullptr; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5852 | |
| 5853 | switch (CSM) { |
| 5854 | case Sema::CXXInvalid: |
| 5855 | llvm_unreachable("not a special member"); |
| 5856 | |
| 5857 | case Sema::CXXDefaultConstructor: |
| 5858 | // C++11 [class.ctor]p5: |
| 5859 | // A default constructor is trivial if: |
| 5860 | // - all the [direct subobjects] have trivial default constructors |
| 5861 | // |
| 5862 | // Note, no overload resolution is performed in this case. |
| 5863 | if (RD->hasTrivialDefaultConstructor()) |
| 5864 | return true; |
| 5865 | |
| 5866 | if (Selected) { |
| 5867 | // If there's a default constructor which could have been trivial, dig it |
| 5868 | // out. Otherwise, if there's any user-provided default constructor, point |
| 5869 | // to that as an example of why there's not a trivial one. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5870 | CXXConstructorDecl *DefCtor = nullptr; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5871 | if (RD->needsImplicitDefaultConstructor()) |
| 5872 | S.DeclareImplicitDefaultConstructor(RD); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5873 | for (auto *CI : RD->ctors()) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5874 | if (!CI->isDefaultConstructor()) |
| 5875 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5876 | DefCtor = CI; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5877 | if (!DefCtor->isUserProvided()) |
| 5878 | break; |
| 5879 | } |
| 5880 | |
| 5881 | *Selected = DefCtor; |
| 5882 | } |
| 5883 | |
| 5884 | return false; |
| 5885 | |
| 5886 | case Sema::CXXDestructor: |
| 5887 | // C++11 [class.dtor]p5: |
| 5888 | // A destructor is trivial if: |
| 5889 | // - all the direct [subobjects] have trivial destructors |
| 5890 | if (RD->hasTrivialDestructor()) |
| 5891 | return true; |
| 5892 | |
| 5893 | if (Selected) { |
| 5894 | if (RD->needsImplicitDestructor()) |
| 5895 | S.DeclareImplicitDestructor(RD); |
| 5896 | *Selected = RD->getDestructor(); |
| 5897 | } |
| 5898 | |
| 5899 | return false; |
| 5900 | |
| 5901 | case Sema::CXXCopyConstructor: |
| 5902 | // C++11 [class.copy]p12: |
| 5903 | // A copy constructor is trivial if: |
| 5904 | // - the constructor selected to copy each direct [subobject] is trivial |
| 5905 | if (RD->hasTrivialCopyConstructor()) { |
| 5906 | if (Quals == Qualifiers::Const) |
| 5907 | // We must either select the trivial copy constructor or reach an |
| 5908 | // ambiguity; no need to actually perform overload resolution. |
| 5909 | return true; |
| 5910 | } else if (!Selected) { |
| 5911 | return false; |
| 5912 | } |
| 5913 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5914 | // treat that as a language defect, as suggested on cxx-abi-dev, to treat |
| 5915 | // cases like B as having a non-trivial copy constructor: |
| 5916 | // struct A { template<typename T> A(T&); }; |
| 5917 | // struct B { mutable A a; }; |
| 5918 | goto NeedOverloadResolution; |
| 5919 | |
| 5920 | case Sema::CXXCopyAssignment: |
| 5921 | // C++11 [class.copy]p25: |
| 5922 | // A copy assignment operator is trivial if: |
| 5923 | // - the assignment operator selected to copy each direct [subobject] is |
| 5924 | // trivial |
| 5925 | if (RD->hasTrivialCopyAssignment()) { |
| 5926 | if (Quals == Qualifiers::Const) |
| 5927 | return true; |
| 5928 | } else if (!Selected) { |
| 5929 | return false; |
| 5930 | } |
| 5931 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5932 | // treat that as a language defect. |
| 5933 | goto NeedOverloadResolution; |
| 5934 | |
| 5935 | case Sema::CXXMoveConstructor: |
| 5936 | case Sema::CXXMoveAssignment: |
| 5937 | NeedOverloadResolution: |
| 5938 | Sema::SpecialMemberOverloadResult *SMOR = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5939 | lookupCallFromSpecialMember(S, RD, CSM, Quals, ConstRHS); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5940 | |
| 5941 | // The standard doesn't describe how to behave if the lookup is ambiguous. |
| 5942 | // We treat it as not making the member non-trivial, just like the standard |
| 5943 | // mandates for the default constructor. This should rarely matter, because |
| 5944 | // the member will also be deleted. |
| 5945 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5946 | return true; |
| 5947 | |
| 5948 | if (!SMOR->getMethod()) { |
| 5949 | assert(SMOR->getKind() == |
| 5950 | Sema::SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 5951 | return false; |
| 5952 | } |
| 5953 | |
| 5954 | // We deliberately don't check if we found a deleted special member. We're |
| 5955 | // not supposed to! |
| 5956 | if (Selected) |
| 5957 | *Selected = SMOR->getMethod(); |
| 5958 | return SMOR->getMethod()->isTrivial(); |
| 5959 | } |
| 5960 | |
| 5961 | llvm_unreachable("unknown special method kind"); |
| 5962 | } |
| 5963 | |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5964 | static CXXConstructorDecl *findUserDeclaredCtor(CXXRecordDecl *RD) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5965 | for (auto *CI : RD->ctors()) |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5966 | if (!CI->isImplicit()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5967 | return CI; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5968 | |
| 5969 | // Look for constructor templates. |
| 5970 | typedef CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> tmpl_iter; |
| 5971 | for (tmpl_iter TI(RD->decls_begin()), TE(RD->decls_end()); TI != TE; ++TI) { |
| 5972 | if (CXXConstructorDecl *CD = |
| 5973 | dyn_cast<CXXConstructorDecl>(TI->getTemplatedDecl())) |
| 5974 | return CD; |
| 5975 | } |
| 5976 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5977 | return nullptr; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5978 | } |
| 5979 | |
| 5980 | /// The kind of subobject we are checking for triviality. The values of this |
| 5981 | /// enumeration are used in diagnostics. |
| 5982 | enum TrivialSubobjectKind { |
| 5983 | /// The subobject is a base class. |
| 5984 | TSK_BaseClass, |
| 5985 | /// The subobject is a non-static data member. |
| 5986 | TSK_Field, |
| 5987 | /// The object is actually the complete object. |
| 5988 | TSK_CompleteObject |
| 5989 | }; |
| 5990 | |
| 5991 | /// Check whether the special member selected for a given type would be trivial. |
| 5992 | static bool checkTrivialSubobjectCall(Sema &S, SourceLocation SubobjLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5993 | QualType SubType, bool ConstRHS, |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5994 | Sema::CXXSpecialMember CSM, |
| 5995 | TrivialSubobjectKind Kind, |
| 5996 | bool Diagnose) { |
| 5997 | CXXRecordDecl *SubRD = SubType->getAsCXXRecordDecl(); |
| 5998 | if (!SubRD) |
| 5999 | return true; |
| 6000 | |
| 6001 | CXXMethodDecl *Selected; |
| 6002 | if (findTrivialSpecialMember(S, SubRD, CSM, SubType.getCVRQualifiers(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6003 | ConstRHS, Diagnose ? &Selected : nullptr)) |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6004 | return true; |
| 6005 | |
| 6006 | if (Diagnose) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6007 | if (ConstRHS) |
| 6008 | SubType.addConst(); |
| 6009 | |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6010 | if (!Selected && CSM == Sema::CXXDefaultConstructor) { |
| 6011 | S.Diag(SubobjLoc, diag::note_nontrivial_no_def_ctor) |
| 6012 | << Kind << SubType.getUnqualifiedType(); |
| 6013 | if (CXXConstructorDecl *CD = findUserDeclaredCtor(SubRD)) |
| 6014 | S.Diag(CD->getLocation(), diag::note_user_declared_ctor); |
| 6015 | } else if (!Selected) |
| 6016 | S.Diag(SubobjLoc, diag::note_nontrivial_no_copy) |
| 6017 | << Kind << SubType.getUnqualifiedType() << CSM << SubType; |
| 6018 | else if (Selected->isUserProvided()) { |
| 6019 | if (Kind == TSK_CompleteObject) |
| 6020 | S.Diag(Selected->getLocation(), diag::note_nontrivial_user_provided) |
| 6021 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 6022 | else { |
| 6023 | S.Diag(SubobjLoc, diag::note_nontrivial_user_provided) |
| 6024 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 6025 | S.Diag(Selected->getLocation(), diag::note_declared_at); |
| 6026 | } |
| 6027 | } else { |
| 6028 | if (Kind != TSK_CompleteObject) |
| 6029 | S.Diag(SubobjLoc, diag::note_nontrivial_subobject) |
| 6030 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 6031 | |
| 6032 | // Explain why the defaulted or deleted special member isn't trivial. |
| 6033 | S.SpecialMemberIsTrivial(Selected, CSM, Diagnose); |
| 6034 | } |
| 6035 | } |
| 6036 | |
| 6037 | return false; |
| 6038 | } |
| 6039 | |
| 6040 | /// Check whether the members of a class type allow a special member to be |
| 6041 | /// trivial. |
| 6042 | static bool checkTrivialClassMembers(Sema &S, CXXRecordDecl *RD, |
| 6043 | Sema::CXXSpecialMember CSM, |
| 6044 | bool ConstArg, bool Diagnose) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6045 | for (const auto *FI : RD->fields()) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6046 | if (FI->isInvalidDecl() || FI->isUnnamedBitfield()) |
| 6047 | continue; |
| 6048 | |
| 6049 | QualType FieldType = S.Context.getBaseElementType(FI->getType()); |
| 6050 | |
| 6051 | // Pretend anonymous struct or union members are members of this class. |
| 6052 | if (FI->isAnonymousStructOrUnion()) { |
| 6053 | if (!checkTrivialClassMembers(S, FieldType->getAsCXXRecordDecl(), |
| 6054 | CSM, ConstArg, Diagnose)) |
| 6055 | return false; |
| 6056 | continue; |
| 6057 | } |
| 6058 | |
| 6059 | // C++11 [class.ctor]p5: |
| 6060 | // A default constructor is trivial if [...] |
| 6061 | // -- no non-static data member of its class has a |
| 6062 | // brace-or-equal-initializer |
| 6063 | if (CSM == Sema::CXXDefaultConstructor && FI->hasInClassInitializer()) { |
| 6064 | if (Diagnose) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6065 | S.Diag(FI->getLocation(), diag::note_nontrivial_in_class_init) << FI; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6066 | return false; |
| 6067 | } |
| 6068 | |
| 6069 | // Objective C ARC 4.3.5: |
| 6070 | // [...] nontrivally ownership-qualified types are [...] not trivially |
| 6071 | // default constructible, copy constructible, move constructible, copy |
| 6072 | // assignable, move assignable, or destructible [...] |
| 6073 | if (S.getLangOpts().ObjCAutoRefCount && |
| 6074 | FieldType.hasNonTrivialObjCLifetime()) { |
| 6075 | if (Diagnose) |
| 6076 | S.Diag(FI->getLocation(), diag::note_nontrivial_objc_ownership) |
| 6077 | << RD << FieldType.getObjCLifetime(); |
| 6078 | return false; |
| 6079 | } |
| 6080 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6081 | bool ConstRHS = ConstArg && !FI->isMutable(); |
| 6082 | if (!checkTrivialSubobjectCall(S, FI->getLocation(), FieldType, ConstRHS, |
| 6083 | CSM, TSK_Field, Diagnose)) |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6084 | return false; |
| 6085 | } |
| 6086 | |
| 6087 | return true; |
| 6088 | } |
| 6089 | |
| 6090 | /// Diagnose why the specified class does not have a trivial special member of |
| 6091 | /// the given kind. |
| 6092 | void Sema::DiagnoseNontrivial(const CXXRecordDecl *RD, CXXSpecialMember CSM) { |
| 6093 | QualType Ty = Context.getRecordType(RD); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6094 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6095 | bool ConstArg = (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment); |
| 6096 | checkTrivialSubobjectCall(*this, RD->getLocation(), Ty, ConstArg, CSM, |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6097 | TSK_CompleteObject, /*Diagnose*/true); |
| 6098 | } |
| 6099 | |
| 6100 | /// Determine whether a defaulted or deleted special member function is trivial, |
| 6101 | /// as specified in C++11 [class.ctor]p5, C++11 [class.copy]p12, |
| 6102 | /// C++11 [class.copy]p25, and C++11 [class.dtor]p5. |
| 6103 | bool Sema::SpecialMemberIsTrivial(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 6104 | bool Diagnose) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6105 | assert(!MD->isUserProvided() && CSM != CXXInvalid && "not special enough"); |
| 6106 | |
| 6107 | CXXRecordDecl *RD = MD->getParent(); |
| 6108 | |
| 6109 | bool ConstArg = false; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6110 | |
Richard Smith | a8d478e | 2013-11-04 02:02:27 +0000 | [diff] [blame] | 6111 | // C++11 [class.copy]p12, p25: [DR1593] |
| 6112 | // A [special member] is trivial if [...] its parameter-type-list is |
| 6113 | // equivalent to the parameter-type-list of an implicit declaration [...] |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6114 | switch (CSM) { |
| 6115 | case CXXDefaultConstructor: |
| 6116 | case CXXDestructor: |
| 6117 | // Trivial default constructors and destructors cannot have parameters. |
| 6118 | break; |
| 6119 | |
| 6120 | case CXXCopyConstructor: |
| 6121 | case CXXCopyAssignment: { |
| 6122 | // Trivial copy operations always have const, non-volatile parameter types. |
| 6123 | ConstArg = true; |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 6124 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6125 | const ReferenceType *RT = Param0->getType()->getAs<ReferenceType>(); |
| 6126 | if (!RT || RT->getPointeeType().getCVRQualifiers() != Qualifiers::Const) { |
| 6127 | if (Diagnose) |
| 6128 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 6129 | << Param0->getSourceRange() << Param0->getType() |
| 6130 | << Context.getLValueReferenceType( |
| 6131 | Context.getRecordType(RD).withConst()); |
| 6132 | return false; |
| 6133 | } |
| 6134 | break; |
| 6135 | } |
| 6136 | |
| 6137 | case CXXMoveConstructor: |
| 6138 | case CXXMoveAssignment: { |
| 6139 | // Trivial move operations always have non-cv-qualified parameters. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 6140 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6141 | const RValueReferenceType *RT = |
| 6142 | Param0->getType()->getAs<RValueReferenceType>(); |
| 6143 | if (!RT || RT->getPointeeType().getCVRQualifiers()) { |
| 6144 | if (Diagnose) |
| 6145 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 6146 | << Param0->getSourceRange() << Param0->getType() |
| 6147 | << Context.getRValueReferenceType(Context.getRecordType(RD)); |
| 6148 | return false; |
| 6149 | } |
| 6150 | break; |
| 6151 | } |
| 6152 | |
| 6153 | case CXXInvalid: |
| 6154 | llvm_unreachable("not a special member"); |
| 6155 | } |
| 6156 | |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6157 | if (MD->getMinRequiredArguments() < MD->getNumParams()) { |
| 6158 | if (Diagnose) |
| 6159 | Diag(MD->getParamDecl(MD->getMinRequiredArguments())->getLocation(), |
| 6160 | diag::note_nontrivial_default_arg) |
| 6161 | << MD->getParamDecl(MD->getMinRequiredArguments())->getSourceRange(); |
| 6162 | return false; |
| 6163 | } |
| 6164 | if (MD->isVariadic()) { |
| 6165 | if (Diagnose) |
| 6166 | Diag(MD->getLocation(), diag::note_nontrivial_variadic); |
| 6167 | return false; |
| 6168 | } |
| 6169 | |
| 6170 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 6171 | // A copy/move [constructor or assignment operator] is trivial if |
| 6172 | // -- the [member] selected to copy/move each direct base class subobject |
| 6173 | // is trivial |
| 6174 | // |
| 6175 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 6176 | // A [default constructor or destructor] is trivial if |
| 6177 | // -- all the direct base classes have trivial [default constructors or |
| 6178 | // destructors] |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6179 | for (const auto &BI : RD->bases()) |
| 6180 | if (!checkTrivialSubobjectCall(*this, BI.getLocStart(), BI.getType(), |
| 6181 | ConstArg, CSM, TSK_BaseClass, Diagnose)) |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6182 | return false; |
| 6183 | |
| 6184 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 6185 | // A copy/move [constructor or assignment operator] for a class X is |
| 6186 | // trivial if |
| 6187 | // -- for each non-static data member of X that is of class type (or array |
| 6188 | // thereof), the constructor selected to copy/move that member is |
| 6189 | // trivial |
| 6190 | // |
| 6191 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 6192 | // A [default constructor or destructor] is trivial if |
| 6193 | // -- for all of the non-static data members of its class that are of class |
| 6194 | // type (or array thereof), each such class has a trivial [default |
| 6195 | // constructor or destructor] |
| 6196 | if (!checkTrivialClassMembers(*this, RD, CSM, ConstArg, Diagnose)) |
| 6197 | return false; |
| 6198 | |
| 6199 | // C++11 [class.dtor]p5: |
| 6200 | // A destructor is trivial if [...] |
| 6201 | // -- the destructor is not virtual |
| 6202 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 6203 | if (Diagnose) |
| 6204 | Diag(MD->getLocation(), diag::note_nontrivial_virtual_dtor) << RD; |
| 6205 | return false; |
| 6206 | } |
| 6207 | |
| 6208 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 6209 | // A [special member] for class X is trivial if [...] |
| 6210 | // -- class X has no virtual functions and no virtual base classes |
| 6211 | if (CSM != CXXDestructor && MD->getParent()->isDynamicClass()) { |
| 6212 | if (!Diagnose) |
| 6213 | return false; |
| 6214 | |
| 6215 | if (RD->getNumVBases()) { |
| 6216 | // Check for virtual bases. We already know that the corresponding |
| 6217 | // member in all bases is trivial, so vbases must all be direct. |
| 6218 | CXXBaseSpecifier &BS = *RD->vbases_begin(); |
| 6219 | assert(BS.isVirtual()); |
| 6220 | Diag(BS.getLocStart(), diag::note_nontrivial_has_virtual) << RD << 1; |
| 6221 | return false; |
| 6222 | } |
| 6223 | |
| 6224 | // Must have a virtual method. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6225 | for (const auto *MI : RD->methods()) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 6226 | if (MI->isVirtual()) { |
| 6227 | SourceLocation MLoc = MI->getLocStart(); |
| 6228 | Diag(MLoc, diag::note_nontrivial_has_virtual) << RD << 0; |
| 6229 | return false; |
| 6230 | } |
| 6231 | } |
| 6232 | |
| 6233 | llvm_unreachable("dynamic class with no vbases and no virtual functions"); |
| 6234 | } |
| 6235 | |
| 6236 | // Looks like it's trivial! |
| 6237 | return true; |
| 6238 | } |
| 6239 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6240 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 6241 | namespace { |
| 6242 | struct FindHiddenVirtualMethodData { |
| 6243 | Sema *S; |
| 6244 | CXXMethodDecl *Method; |
| 6245 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6246 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 6247 | }; |
| 6248 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6249 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 6250 | /// \brief Check whether any most overriden method from MD in Methods |
| 6251 | static bool CheckMostOverridenMethods(const CXXMethodDecl *MD, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6252 | const llvm::SmallPtrSetImpl<const CXXMethodDecl *>& Methods) { |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 6253 | if (MD->size_overridden_methods() == 0) |
| 6254 | return Methods.count(MD->getCanonicalDecl()); |
| 6255 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 6256 | E = MD->end_overridden_methods(); |
| 6257 | I != E; ++I) |
| 6258 | if (CheckMostOverridenMethods(*I, Methods)) |
| 6259 | return true; |
| 6260 | return false; |
| 6261 | } |
| 6262 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6263 | /// \brief Member lookup function that determines whether a given C++ |
| 6264 | /// method overloads virtual methods in a base class without overriding any, |
| 6265 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 6266 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 6267 | CXXBasePath &Path, |
| 6268 | void *UserData) { |
| 6269 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 6270 | |
| 6271 | FindHiddenVirtualMethodData &Data |
| 6272 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 6273 | |
| 6274 | DeclarationName Name = Data.Method->getDeclName(); |
| 6275 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 6276 | |
| 6277 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6278 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6279 | for (Path.Decls = BaseRecord->lookup(Name); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6280 | !Path.Decls.empty(); |
| 6281 | Path.Decls = Path.Decls.slice(1)) { |
| 6282 | NamedDecl *D = Path.Decls.front(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6283 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 6284 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6285 | foundSameNameMethod = true; |
| 6286 | // Interested only in hidden virtual methods. |
| 6287 | if (!MD->isVirtual()) |
| 6288 | continue; |
| 6289 | // If the method we are checking overrides a method from its base |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6290 | // don't warn about the other overloaded methods. Clang deviates from GCC |
| 6291 | // by only diagnosing overloads of inherited virtual functions that do not |
| 6292 | // override any other virtual functions in the base. GCC's |
| 6293 | // -Woverloaded-virtual diagnoses any derived function hiding a virtual |
| 6294 | // function from a base class. These cases may be better served by a |
| 6295 | // warning (not specific to virtual functions) on call sites when the call |
| 6296 | // would select a different function from the base class, were it visible. |
| 6297 | // See FIXME in test/SemaCXX/warn-overload-virtual.cpp for an example. |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6298 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 6299 | return true; |
| 6300 | // Collect the overload only if its hidden. |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 6301 | if (!CheckMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods)) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6302 | overloadedMethods.push_back(MD); |
| 6303 | } |
| 6304 | } |
| 6305 | |
| 6306 | if (foundSameNameMethod) |
| 6307 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 6308 | overloadedMethods.end()); |
| 6309 | return foundSameNameMethod; |
| 6310 | } |
| 6311 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 6312 | /// \brief Add the most overriden methods from MD to Methods |
| 6313 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6314 | llvm::SmallPtrSetImpl<const CXXMethodDecl *>& Methods) { |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 6315 | if (MD->size_overridden_methods() == 0) |
| 6316 | Methods.insert(MD->getCanonicalDecl()); |
| 6317 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 6318 | E = MD->end_overridden_methods(); |
| 6319 | I != E; ++I) |
| 6320 | AddMostOverridenMethods(*I, Methods); |
| 6321 | } |
| 6322 | |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 6323 | /// \brief Check if a method overloads virtual methods in a base class without |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6324 | /// overriding any. |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 6325 | void Sema::FindHiddenVirtualMethods(CXXMethodDecl *MD, |
| 6326 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
Benjamin Kramer | c470442 | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 6327 | if (!MD->getDeclName().isIdentifier()) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6328 | return; |
| 6329 | |
| 6330 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 6331 | /*bool RecordPaths=*/false, |
| 6332 | /*bool DetectVirtual=*/false); |
| 6333 | FindHiddenVirtualMethodData Data; |
| 6334 | Data.Method = MD; |
| 6335 | Data.S = this; |
| 6336 | |
| 6337 | // Keep the base methods that were overriden or introduced in the subclass |
| 6338 | // by 'using' in a set. A base method not in this set is hidden. |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 6339 | CXXRecordDecl *DC = MD->getParent(); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6340 | DeclContext::lookup_result R = DC->lookup(MD->getDeclName()); |
| 6341 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 6342 | NamedDecl *ND = *I; |
| 6343 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*I)) |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 6344 | ND = shad->getTargetDecl(); |
| 6345 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 6346 | AddMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 6349 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths)) |
| 6350 | OverloadedMethods = Data.OverloadedMethods; |
| 6351 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6352 | |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 6353 | void Sema::NoteHiddenVirtualMethods(CXXMethodDecl *MD, |
| 6354 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
| 6355 | for (unsigned i = 0, e = OverloadedMethods.size(); i != e; ++i) { |
| 6356 | CXXMethodDecl *overloadedMD = OverloadedMethods[i]; |
| 6357 | PartialDiagnostic PD = PDiag( |
| 6358 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 6359 | HandleFunctionTypeMismatch(PD, MD->getType(), overloadedMD->getType()); |
| 6360 | Diag(overloadedMD->getLocation(), PD); |
| 6361 | } |
| 6362 | } |
| 6363 | |
| 6364 | /// \brief Diagnose methods which overload virtual methods in a base class |
| 6365 | /// without overriding any. |
| 6366 | void Sema::DiagnoseHiddenVirtualMethods(CXXMethodDecl *MD) { |
| 6367 | if (MD->isInvalidDecl()) |
| 6368 | return; |
| 6369 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 6370 | if (Diags.isIgnored(diag::warn_overloaded_virtual, MD->getLocation())) |
Eli Friedman | dae9271 | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 6371 | return; |
| 6372 | |
| 6373 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 6374 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 6375 | if (!OverloadedMethods.empty()) { |
| 6376 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 6377 | << MD << (OverloadedMethods.size() > 1); |
| 6378 | |
| 6379 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 6380 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 6383 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6384 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 6385 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 6386 | SourceLocation RBrac, |
| 6387 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 6388 | if (!TagDecl) |
| 6389 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6390 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6391 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 6392 | |
Rafael Espindola | f729ce0 | 2012-07-12 04:32:30 +0000 | [diff] [blame] | 6393 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 6394 | if (l->getKind() != AttributeList::AT_Visibility) |
| 6395 | continue; |
| 6396 | l->setInvalid(); |
| 6397 | Diag(l->getLoc(), diag::warn_attribute_after_definition_ignored) << |
| 6398 | l->getName(); |
| 6399 | } |
| 6400 | |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 6401 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6402 | // strict aliasing violation! |
| 6403 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 6404 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 6405 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6406 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6407 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 6410 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 6411 | /// special functions, such as the default constructor, copy |
| 6412 | /// constructor, or destructor, to the given C++ class (C++ |
| 6413 | /// [special]p1). This routine can only be executed just before the |
| 6414 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6415 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6416 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6417 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 6418 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6419 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6420 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 6421 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6422 | // If the properties or semantics of the copy constructor couldn't be |
| 6423 | // determined while the class was being declared, force a declaration |
| 6424 | // of it now. |
| 6425 | if (ClassDecl->needsOverloadResolutionForCopyConstructor()) |
| 6426 | DeclareImplicitCopyConstructor(ClassDecl); |
| 6427 | } |
| 6428 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6429 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveConstructor()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 6430 | ++ASTContext::NumImplicitMoveConstructors; |
| 6431 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6432 | if (ClassDecl->needsOverloadResolutionForMoveConstructor()) |
| 6433 | DeclareImplicitMoveConstructor(ClassDecl); |
| 6434 | } |
| 6435 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6436 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 6437 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6438 | |
| 6439 | // If we have a dynamic class, then the copy assignment operator may be |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6440 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6441 | // it shows up in the right place in the vtable and that we diagnose |
| 6442 | // problems with the implicit exception specification. |
| 6443 | if (ClassDecl->isDynamicClass() || |
| 6444 | ClassDecl->needsOverloadResolutionForCopyAssignment()) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6445 | DeclareImplicitCopyAssignment(ClassDecl); |
| 6446 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 6447 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6448 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveAssignment()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 6449 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 6450 | |
| 6451 | // Likewise for the move assignment operator. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6452 | if (ClassDecl->isDynamicClass() || |
| 6453 | ClassDecl->needsOverloadResolutionForMoveAssignment()) |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 6454 | DeclareImplicitMoveAssignment(ClassDecl); |
| 6455 | } |
| 6456 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6457 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 6458 | ++ASTContext::NumImplicitDestructors; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6459 | |
| 6460 | // If we have a dynamic class, then the destructor may be virtual, so we |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6461 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 6462 | // shows up in the right place in the vtable and that we diagnose problems |
| 6463 | // with the implicit exception specification. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6464 | if (ClassDecl->isDynamicClass() || |
| 6465 | ClassDecl->needsOverloadResolutionForDestructor()) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6466 | DeclareImplicitDestructor(ClassDecl); |
| 6467 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6470 | unsigned Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6471 | if (!D) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6472 | return 0; |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6473 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6474 | // The order of template parameters is not important here. All names |
| 6475 | // get added to the same scope. |
| 6476 | SmallVector<TemplateParameterList *, 4> ParameterLists; |
| 6477 | |
| 6478 | if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) |
| 6479 | D = TD->getTemplatedDecl(); |
| 6480 | |
| 6481 | if (auto *PSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 6482 | ParameterLists.push_back(PSD->getTemplateParameters()); |
| 6483 | |
| 6484 | if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 6485 | for (unsigned i = 0; i < DD->getNumTemplateParameterLists(); ++i) |
| 6486 | ParameterLists.push_back(DD->getTemplateParameterList(i)); |
| 6487 | |
| 6488 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6489 | if (FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
| 6490 | ParameterLists.push_back(FTD->getTemplateParameters()); |
| 6491 | } |
| 6492 | } |
| 6493 | |
| 6494 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 6495 | for (unsigned i = 0; i < TD->getNumTemplateParameterLists(); ++i) |
| 6496 | ParameterLists.push_back(TD->getTemplateParameterList(i)); |
| 6497 | |
| 6498 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) { |
| 6499 | if (ClassTemplateDecl *CTD = RD->getDescribedClassTemplate()) |
| 6500 | ParameterLists.push_back(CTD->getTemplateParameters()); |
| 6501 | } |
| 6502 | } |
| 6503 | |
| 6504 | unsigned Count = 0; |
| 6505 | for (TemplateParameterList *Params : ParameterLists) { |
| 6506 | if (Params->size() > 0) |
| 6507 | // Ignore explicit specializations; they don't contribute to the template |
| 6508 | // depth. |
| 6509 | ++Count; |
| 6510 | for (NamedDecl *Param : *Params) { |
| 6511 | if (Param->getDeclName()) { |
| 6512 | S->AddDecl(Param); |
| 6513 | IdResolver.AddDecl(Param); |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6517 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6518 | return Count; |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 6519 | } |
| 6520 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6521 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6522 | if (!RecordD) return; |
| 6523 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6524 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6525 | PushDeclContext(S, Record); |
| 6526 | } |
| 6527 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6528 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6529 | if (!RecordD) return; |
| 6530 | PopDeclContext(); |
| 6531 | } |
| 6532 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6533 | /// This is used to implement the constant expression evaluation part of the |
| 6534 | /// attribute enable_if extension. There is nothing in standard C++ which would |
| 6535 | /// require reentering parameters. |
| 6536 | void Sema::ActOnReenterCXXMethodParameter(Scope *S, ParmVarDecl *Param) { |
| 6537 | if (!Param) |
| 6538 | return; |
| 6539 | |
| 6540 | S->AddDecl(Param); |
| 6541 | if (Param->getDeclName()) |
| 6542 | IdResolver.AddDecl(Param); |
| 6543 | } |
| 6544 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6545 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 6546 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 6547 | /// parsing those parts of the given Method declaration that could |
| 6548 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 6549 | /// arguments. This action should enter the scope of the given |
| 6550 | /// Method declaration as if we had just parsed the qualified method |
| 6551 | /// name. However, it should not bring the parameters into scope; |
| 6552 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6553 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6554 | } |
| 6555 | |
| 6556 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 6557 | /// C++ method declaration. We're (re-)introducing the given |
| 6558 | /// function parameter into scope for use in parsing later parts of |
| 6559 | /// the method declaration. For example, we could see an |
| 6560 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6561 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 6562 | if (!ParamD) |
| 6563 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6564 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6565 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6566 | |
| 6567 | // If this parameter has an unparsed default argument, clear it out |
| 6568 | // to make way for the parsed default argument. |
| 6569 | if (Param->hasUnparsedDefaultArg()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6570 | Param->setDefaultArg(nullptr); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6571 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6572 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6573 | if (Param->getDeclName()) |
| 6574 | IdResolver.AddDecl(Param); |
| 6575 | } |
| 6576 | |
| 6577 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 6578 | /// processing the delayed method declaration for Method. The method |
| 6579 | /// declaration is now considered finished. There may be a separate |
| 6580 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 6581 | /// immediately!) for this method, if it was also defined inside the |
| 6582 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6583 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 6584 | if (!MethodD) |
| 6585 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6586 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 6587 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6588 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6589 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6590 | |
| 6591 | // Now that we have our default arguments, check the constructor |
| 6592 | // again. It could produce additional diagnostics or affect whether |
| 6593 | // the class has implicitly-declared destructors, among other |
| 6594 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6595 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 6596 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6597 | |
| 6598 | // Check the default arguments, which we may have added. |
| 6599 | if (!Method->isInvalidDecl()) |
| 6600 | CheckCXXDefaultArguments(Method); |
| 6601 | } |
| 6602 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6603 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6604 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6605 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6606 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 6607 | /// will be updated to reflect a well-formed type for the constructor and |
| 6608 | /// returned. |
| 6609 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6610 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6611 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6612 | |
| 6613 | // C++ [class.ctor]p3: |
| 6614 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 6615 | // constructor can be invoked for a const, volatile or const |
| 6616 | // volatile object. A constructor shall not be declared const, |
| 6617 | // volatile, or const volatile (9.3.2). |
| 6618 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6619 | if (!D.isInvalidType()) |
| 6620 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 6621 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 6622 | << SourceRange(D.getIdentifierLoc()); |
| 6623 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6624 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6625 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6626 | if (!D.isInvalidType()) |
| 6627 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 6628 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6629 | << SourceRange(D.getIdentifierLoc()); |
| 6630 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6631 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6632 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6633 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 6634 | if (unsigned TypeQuals = D.getDeclSpec().getTypeQualifiers()) { |
| 6635 | diagnoseIgnoredQualifiers( |
| 6636 | diag::err_constructor_return_type, TypeQuals, SourceLocation(), |
| 6637 | D.getDeclSpec().getConstSpecLoc(), D.getDeclSpec().getVolatileSpecLoc(), |
| 6638 | D.getDeclSpec().getRestrictSpecLoc(), |
| 6639 | D.getDeclSpec().getAtomicSpecLoc()); |
| 6640 | D.setInvalidType(); |
| 6641 | } |
| 6642 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6643 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6644 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6645 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6646 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6647 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6648 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6649 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6650 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6651 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6652 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6653 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6654 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6655 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6656 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6657 | // C++0x [class.ctor]p4: |
| 6658 | // A constructor shall not be declared with a ref-qualifier. |
| 6659 | if (FTI.hasRefQualifier()) { |
| 6660 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 6661 | << FTI.RefQualifierIsLValueRef |
| 6662 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6663 | D.setInvalidType(); |
| 6664 | } |
| 6665 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6666 | // Rebuild the function type "R" without any type qualifiers (in |
| 6667 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6668 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6669 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6670 | if (Proto->getReturnType() == Context.VoidTy && !D.isInvalidType()) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6671 | return R; |
| 6672 | |
| 6673 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6674 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6675 | EPI.RefQualifier = RQ_None; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6676 | |
| 6677 | return Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6678 | } |
| 6679 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6680 | /// CheckConstructor - Checks a fully-formed constructor for |
| 6681 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 6682 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6683 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6684 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 6685 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 6686 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6687 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6688 | |
| 6689 | // C++ [class.copy]p3: |
| 6690 | // A declaration of a constructor for a class X is ill-formed if |
| 6691 | // its first parameter is of type (optionally cv-qualified) X and |
| 6692 | // either there are no other parameters or else all other |
| 6693 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 6694 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6695 | ((Constructor->getNumParams() == 1) || |
| 6696 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 6697 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 6698 | Constructor->getTemplateSpecializationKind() |
| 6699 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6700 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 6701 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 6702 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6703 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 6704 | const char *ConstRef |
| 6705 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 6706 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6707 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 6708 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 6709 | |
| 6710 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 6711 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6712 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6713 | } |
| 6714 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 6717 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 6718 | /// well-formedness, issuing any diagnostics required. Returns true |
| 6719 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6720 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6721 | CXXRecordDecl *RD = Destructor->getParent(); |
| 6722 | |
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 6723 | if (!Destructor->getOperatorDelete() && Destructor->isVirtual()) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6724 | SourceLocation Loc; |
| 6725 | |
| 6726 | if (!Destructor->isImplicit()) |
| 6727 | Loc = Destructor->getLocation(); |
| 6728 | else |
| 6729 | Loc = RD->getLocation(); |
| 6730 | |
| 6731 | // If we have a virtual destructor, look up the deallocation function |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6732 | FunctionDecl *OperatorDelete = nullptr; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6733 | DeclarationName Name = |
| 6734 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6735 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6736 | return true; |
Bill Wendling | c12b43a | 2013-12-07 23:53:50 +0000 | [diff] [blame] | 6737 | // If there's no class-specific operator delete, look up the global |
| 6738 | // non-array delete. |
| 6739 | if (!OperatorDelete) |
| 6740 | OperatorDelete = FindUsualDeallocationFunction(Loc, true, Name); |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 6741 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6742 | MarkFunctionReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6743 | |
| 6744 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6745 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6746 | |
| 6747 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6748 | } |
| 6749 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6750 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 6751 | /// the well-formednes of the destructor declarator @p D with type @p |
| 6752 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6753 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 6754 | /// will be updated to reflect a well-formed type for the destructor and |
| 6755 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6756 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6757 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6758 | // C++ [class.dtor]p1: |
| 6759 | // [...] A typedef-name that names a class is a class-name |
| 6760 | // (7.1.3); however, a typedef-name that names a class shall not |
| 6761 | // be used as the identifier in the declarator for a destructor |
| 6762 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6763 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6764 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6765 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6766 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6767 | else if (const TemplateSpecializationType *TST = |
| 6768 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 6769 | if (TST->isTypeAlias()) |
| 6770 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 6771 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6772 | |
| 6773 | // C++ [class.dtor]p2: |
| 6774 | // A destructor is used to destroy objects of its class type. A |
| 6775 | // destructor takes no parameters, and no return type can be |
| 6776 | // specified for it (not even void). The address of a destructor |
| 6777 | // shall not be taken. A destructor shall not be static. A |
| 6778 | // destructor can be invoked for a const, volatile or const |
| 6779 | // volatile object. A destructor shall not be declared const, |
| 6780 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6781 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6782 | if (!D.isInvalidType()) |
| 6783 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 6784 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6785 | << SourceRange(D.getIdentifierLoc()) |
| 6786 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6787 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6788 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6789 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 6790 | if (!D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6791 | // Destructors don't have return types, but the parser will |
| 6792 | // happily parse something like: |
| 6793 | // |
| 6794 | // class X { |
| 6795 | // float ~X(); |
| 6796 | // }; |
| 6797 | // |
| 6798 | // The return type will be eliminated later. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 6799 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 6800 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 6801 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6802 | << SourceRange(D.getIdentifierLoc()); |
| 6803 | else if (unsigned TypeQuals = D.getDeclSpec().getTypeQualifiers()) { |
| 6804 | diagnoseIgnoredQualifiers(diag::err_destructor_return_type, TypeQuals, |
| 6805 | SourceLocation(), |
| 6806 | D.getDeclSpec().getConstSpecLoc(), |
| 6807 | D.getDeclSpec().getVolatileSpecLoc(), |
| 6808 | D.getDeclSpec().getRestrictSpecLoc(), |
| 6809 | D.getDeclSpec().getAtomicSpecLoc()); |
| 6810 | D.setInvalidType(); |
| 6811 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6813 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6814 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6815 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6816 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6817 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6818 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6819 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6820 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6821 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6822 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6823 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6824 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6825 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6826 | } |
| 6827 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6828 | // C++0x [class.dtor]p2: |
| 6829 | // A destructor shall not be declared with a ref-qualifier. |
| 6830 | if (FTI.hasRefQualifier()) { |
| 6831 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 6832 | << FTI.RefQualifierIsLValueRef |
| 6833 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6834 | D.setInvalidType(); |
| 6835 | } |
| 6836 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6837 | // Make sure we don't have any parameters. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6838 | if (FTIHasNonVoidParameters(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6839 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 6840 | |
| 6841 | // Delete the parameters. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6842 | FTI.freeParams(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6843 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6844 | } |
| 6845 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6846 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6847 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6848 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6849 | D.setInvalidType(); |
| 6850 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6851 | |
| 6852 | // Rebuild the function type "R" without any type qualifiers or |
| 6853 | // parameters (in case any of the errors above fired) and with |
| 6854 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6855 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6856 | if (!D.isInvalidType()) |
| 6857 | return R; |
| 6858 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6859 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6860 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6861 | EPI.Variadic = false; |
| 6862 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6863 | EPI.RefQualifier = RQ_None; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6864 | return Context.getFunctionType(Context.VoidTy, None, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6867 | static void extendLeft(SourceRange &R, const SourceRange &Before) { |
| 6868 | if (Before.isInvalid()) |
| 6869 | return; |
| 6870 | R.setBegin(Before.getBegin()); |
| 6871 | if (R.getEnd().isInvalid()) |
| 6872 | R.setEnd(Before.getEnd()); |
| 6873 | } |
| 6874 | |
| 6875 | static void extendRight(SourceRange &R, const SourceRange &After) { |
| 6876 | if (After.isInvalid()) |
| 6877 | return; |
| 6878 | if (R.getBegin().isInvalid()) |
| 6879 | R.setBegin(After.getBegin()); |
| 6880 | R.setEnd(After.getEnd()); |
| 6881 | } |
| 6882 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6883 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 6884 | /// well-formednes of the conversion function declarator @p D with |
| 6885 | /// type @p R. If there are any errors in the declarator, this routine |
| 6886 | /// will emit diagnostics and return true. Otherwise, it will return |
| 6887 | /// false. Either way, the type @p R will be updated to reflect a |
| 6888 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6889 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6890 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6891 | // C++ [class.conv.fct]p1: |
| 6892 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6893 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6894 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6895 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6896 | if (!D.isInvalidType()) |
| 6897 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
Eli Friedman | 4cde94a | 2013-06-20 20:58:02 +0000 | [diff] [blame] | 6898 | << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6899 | << D.getName().getSourceRange(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6900 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6901 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6902 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6903 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6904 | TypeSourceInfo *ConvTSI = nullptr; |
| 6905 | QualType ConvType = |
| 6906 | GetTypeFromParser(D.getName().ConversionFunctionId, &ConvTSI); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6907 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6908 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6909 | // Conversion functions don't have return types, but the parser will |
| 6910 | // happily parse something like: |
| 6911 | // |
| 6912 | // class X { |
| 6913 | // float operator bool(); |
| 6914 | // }; |
| 6915 | // |
| 6916 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6917 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 6918 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6919 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6920 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6923 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 6924 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6925 | // Make sure we don't have any parameters. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6926 | if (Proto->getNumParams() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6927 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 6928 | |
| 6929 | // Delete the parameters. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6930 | D.getFunctionTypeInfo().freeParams(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6931 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6932 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6933 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6934 | D.setInvalidType(); |
| 6935 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6936 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6937 | // Diagnose "&operator bool()" and other such nonsense. This |
| 6938 | // is actually a gcc extension which we don't support. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6939 | if (Proto->getReturnType() != ConvType) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6940 | bool NeedsTypedef = false; |
| 6941 | SourceRange Before, After; |
| 6942 | |
| 6943 | // Walk the chunks and extract information on them for our diagnostic. |
| 6944 | bool PastFunctionChunk = false; |
| 6945 | for (auto &Chunk : D.type_objects()) { |
| 6946 | switch (Chunk.Kind) { |
| 6947 | case DeclaratorChunk::Function: |
| 6948 | if (!PastFunctionChunk) { |
| 6949 | if (Chunk.Fun.HasTrailingReturnType) { |
| 6950 | TypeSourceInfo *TRT = nullptr; |
| 6951 | GetTypeFromParser(Chunk.Fun.getTrailingReturnType(), &TRT); |
| 6952 | if (TRT) extendRight(After, TRT->getTypeLoc().getSourceRange()); |
| 6953 | } |
| 6954 | PastFunctionChunk = true; |
| 6955 | break; |
| 6956 | } |
| 6957 | // Fall through. |
| 6958 | case DeclaratorChunk::Array: |
| 6959 | NeedsTypedef = true; |
| 6960 | extendRight(After, Chunk.getSourceRange()); |
| 6961 | break; |
| 6962 | |
| 6963 | case DeclaratorChunk::Pointer: |
| 6964 | case DeclaratorChunk::BlockPointer: |
| 6965 | case DeclaratorChunk::Reference: |
| 6966 | case DeclaratorChunk::MemberPointer: |
| 6967 | extendLeft(Before, Chunk.getSourceRange()); |
| 6968 | break; |
| 6969 | |
| 6970 | case DeclaratorChunk::Paren: |
| 6971 | extendLeft(Before, Chunk.Loc); |
| 6972 | extendRight(After, Chunk.EndLoc); |
| 6973 | break; |
| 6974 | } |
| 6975 | } |
| 6976 | |
| 6977 | SourceLocation Loc = Before.isValid() ? Before.getBegin() : |
| 6978 | After.isValid() ? After.getBegin() : |
| 6979 | D.getIdentifierLoc(); |
| 6980 | auto &&DB = Diag(Loc, diag::err_conv_function_with_complex_decl); |
| 6981 | DB << Before << After; |
| 6982 | |
| 6983 | if (!NeedsTypedef) { |
| 6984 | DB << /*don't need a typedef*/0; |
| 6985 | |
| 6986 | // If we can provide a correct fix-it hint, do so. |
| 6987 | if (After.isInvalid() && ConvTSI) { |
| 6988 | SourceLocation InsertLoc = |
| 6989 | PP.getLocForEndOfToken(ConvTSI->getTypeLoc().getLocEnd()); |
| 6990 | DB << FixItHint::CreateInsertion(InsertLoc, " ") |
| 6991 | << FixItHint::CreateInsertionFromRange( |
| 6992 | InsertLoc, CharSourceRange::getTokenRange(Before)) |
| 6993 | << FixItHint::CreateRemoval(Before); |
| 6994 | } |
| 6995 | } else if (!Proto->getReturnType()->isDependentType()) { |
| 6996 | DB << /*typedef*/1 << Proto->getReturnType(); |
| 6997 | } else if (getLangOpts().CPlusPlus11) { |
| 6998 | DB << /*alias template*/2 << Proto->getReturnType(); |
| 6999 | } else { |
| 7000 | DB << /*might not be fixable*/3; |
| 7001 | } |
| 7002 | |
| 7003 | // Recover by incorporating the other type chunks into the result type. |
| 7004 | // Note, this does *not* change the name of the function. This is compatible |
| 7005 | // with the GCC extension: |
| 7006 | // struct S { &operator int(); } s; |
| 7007 | // int &r = s.operator int(); // ok in GCC |
| 7008 | // S::operator int&() {} // error in GCC, function name is 'operator int'. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7009 | ConvType = Proto->getReturnType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 7010 | } |
| 7011 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7012 | // C++ [class.conv.fct]p4: |
| 7013 | // The conversion-type-id shall not represent a function type nor |
| 7014 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7015 | if (ConvType->isArrayType()) { |
| 7016 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 7017 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 7018 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7019 | } else if (ConvType->isFunctionType()) { |
| 7020 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 7021 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 7022 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7023 | } |
| 7024 | |
| 7025 | // Rebuild the function type "R" without any parameters (in case any |
| 7026 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7027 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7028 | if (D.isInvalidType()) |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 7029 | R = Context.getFunctionType(ConvType, None, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7030 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7031 | // C++0x explicit conversion operators. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7032 | if (D.getDeclSpec().isExplicitSpecified()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7033 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7034 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7035 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 7036 | diag::ext_explicit_conversion_functions) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 7037 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7040 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 7041 | /// the declaration of the given C++ conversion function. This routine |
| 7042 | /// is responsible for recording the conversion function in the C++ |
| 7043 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7044 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7045 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 7046 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 7047 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7048 | |
| 7049 | // Make sure we aren't redeclaring the conversion function. |
| 7050 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7051 | |
| 7052 | // C++ [class.conv.fct]p1: |
| 7053 | // [...] A conversion function is never used to convert a |
| 7054 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 7055 | // same object type (or a reference to it), to a (possibly |
| 7056 | // cv-qualified) base class of that type (or a reference to it), |
| 7057 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7058 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 7059 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7060 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7061 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7062 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7063 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 7064 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 7065 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 7066 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 7067 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7068 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 7069 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 7070 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7071 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7072 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 7073 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7074 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7075 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 7076 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7077 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7078 | } |
| 7079 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 7080 | if (FunctionTemplateDecl *ConversionTemplate |
| 7081 | = Conversion->getDescribedFunctionTemplate()) |
| 7082 | return ConversionTemplate; |
| 7083 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7084 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 7085 | } |
| 7086 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7087 | //===----------------------------------------------------------------------===// |
| 7088 | // Namespace Handling |
| 7089 | //===----------------------------------------------------------------------===// |
| 7090 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 7091 | /// \brief Diagnose a mismatch in 'inline' qualifiers when a namespace is |
| 7092 | /// reopened. |
| 7093 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 7094 | SourceLocation Loc, |
| 7095 | IdentifierInfo *II, bool *IsInline, |
| 7096 | NamespaceDecl *PrevNS) { |
| 7097 | assert(*IsInline != PrevNS->isInline()); |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 7098 | |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 7099 | // HACK: Work around a bug in libstdc++4.6's <atomic>, where |
| 7100 | // std::__atomic[0,1,2] are defined as non-inline namespaces, then reopened as |
| 7101 | // inline namespaces, with the intention of bringing names into namespace std. |
| 7102 | // |
| 7103 | // We support this just well enough to get that case working; this is not |
| 7104 | // sufficient to support reopening namespaces as inline in general. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 7105 | if (*IsInline && II && II->getName().startswith("__atomic") && |
| 7106 | S.getSourceManager().isInSystemHeader(Loc)) { |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 7107 | // Mark all prior declarations of the namespace as inline. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 7108 | for (NamespaceDecl *NS = PrevNS->getMostRecentDecl(); NS; |
| 7109 | NS = NS->getPreviousDecl()) |
| 7110 | NS->setInline(*IsInline); |
| 7111 | // Patch up the lookup table for the containing namespace. This isn't really |
| 7112 | // correct, but it's good enough for this particular case. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7113 | for (auto *I : PrevNS->decls()) |
| 7114 | if (auto *ND = dyn_cast<NamedDecl>(I)) |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 7115 | PrevNS->getParent()->makeDeclVisibleInContext(ND); |
| 7116 | return; |
| 7117 | } |
| 7118 | |
| 7119 | if (PrevNS->isInline()) |
| 7120 | // The user probably just forgot the 'inline', so suggest that it |
| 7121 | // be added back. |
| 7122 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 7123 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 7124 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7125 | S.Diag(Loc, diag::err_inline_namespace_mismatch) << *IsInline; |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 7126 | |
| 7127 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 7128 | *IsInline = PrevNS->isInline(); |
| 7129 | } |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 7130 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7131 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 7132 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7133 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 7134 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 7135 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 7136 | SourceLocation IdentLoc, |
| 7137 | IdentifierInfo *II, |
| 7138 | SourceLocation LBrace, |
| 7139 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 7140 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 7141 | // For anonymous namespace, take the location of the left brace. |
| 7142 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7143 | bool IsInline = InlineLoc.isValid(); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 7144 | bool IsInvalid = false; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7145 | bool IsStd = false; |
| 7146 | bool AddToKnown = false; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7147 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 7148 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7149 | NamespaceDecl *PrevNS = nullptr; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7150 | if (II) { |
| 7151 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 7152 | // The identifier in an original-namespace-definition shall not |
| 7153 | // have been previously defined in the declarative region in |
| 7154 | // which the original-namespace-definition appears. The |
| 7155 | // identifier in an original-namespace-definition is the name of |
| 7156 | // the namespace. Subsequently in that declarative region, it is |
| 7157 | // treated as an original-namespace-name. |
| 7158 | // |
| 7159 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 7160 | // look through using directives, just look for any ordinary names. |
| 7161 | |
| 7162 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7163 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 7164 | Decl::IDNS_Namespace; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7165 | NamedDecl *PrevDecl = nullptr; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 7166 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 7167 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 7168 | ++I) { |
| 7169 | if ((*I)->getIdentifierNamespace() & IDNS) { |
| 7170 | PrevDecl = *I; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 7171 | break; |
| 7172 | } |
| 7173 | } |
| 7174 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7175 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 7176 | |
| 7177 | if (PrevNS) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7178 | // This is an extended namespace definition. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 7179 | if (IsInline != PrevNS->isInline()) |
| 7180 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 7181 | &IsInline, PrevNS); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7182 | } else if (PrevDecl) { |
| 7183 | // This is an invalid name redefinition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7184 | Diag(Loc, diag::err_redefinition_different_kind) |
| 7185 | << II; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7186 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 7187 | IsInvalid = true; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7188 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7189 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7190 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 7191 | // This is the first "real" definition of the namespace "std", so update |
| 7192 | // our cache of the "std" namespace to point at this definition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7193 | PrevNS = getStdNamespace(); |
| 7194 | IsStd = true; |
| 7195 | AddToKnown = !IsInline; |
| 7196 | } else { |
| 7197 | // We've seen this namespace for the first time. |
| 7198 | AddToKnown = !IsInline; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7199 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7200 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 7201 | // Anonymous namespaces. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7202 | |
| 7203 | // Determine whether the parent already has an anonymous namespace. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7204 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7205 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7206 | PrevNS = TU->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7207 | } else { |
| 7208 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7209 | PrevNS = ND->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7210 | } |
| 7211 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 7212 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 7213 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 7214 | &IsInline, PrevNS); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
| 7217 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 7218 | StartLoc, Loc, II, PrevNS); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 7219 | if (IsInvalid) |
| 7220 | Namespc->setInvalidDecl(); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7221 | |
| 7222 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 7223 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7224 | // FIXME: Should we be merging attributes? |
| 7225 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 7226 | PushNamespaceVisibilityAttr(Attr, Loc); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7227 | |
| 7228 | if (IsStd) |
| 7229 | StdNamespace = Namespc; |
| 7230 | if (AddToKnown) |
| 7231 | KnownNamespaces[Namespc] = false; |
| 7232 | |
| 7233 | if (II) { |
| 7234 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 7235 | } else { |
| 7236 | // Link the anonymous namespace into its parent. |
| 7237 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 7238 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 7239 | TU->setAnonymousNamespace(Namespc); |
| 7240 | } else { |
| 7241 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7242 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 7243 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 7244 | CurContext->addDecl(Namespc); |
| 7245 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 7246 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 7247 | // behaves as if it were replaced by |
| 7248 | // namespace unique { /* empty body */ } |
| 7249 | // using namespace unique; |
| 7250 | // namespace unique { namespace-body } |
| 7251 | // where all occurrences of 'unique' in a translation unit are |
| 7252 | // replaced by the same identifier and this identifier differs |
| 7253 | // from all other identifiers in the entire program. |
| 7254 | |
| 7255 | // We just create the namespace with an empty name and then add an |
| 7256 | // implicit using declaration, just like the standard suggests. |
| 7257 | // |
| 7258 | // CodeGen enforces the "universally unique" aspect by giving all |
| 7259 | // declarations semantically contained within an anonymous |
| 7260 | // namespace internal linkage. |
| 7261 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7262 | if (!PrevNS) { |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7263 | UsingDirectiveDecl* UD |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 7264 | = UsingDirectiveDecl::Create(Context, Parent, |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7265 | /* 'using' */ LBrace, |
| 7266 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 7267 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7268 | /* identifier */ SourceLocation(), |
| 7269 | Namespc, |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 7270 | /* Ancestor */ Parent); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7271 | UD->setImplicit(); |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 7272 | Parent->addDecl(UD); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 7273 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7274 | } |
| 7275 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 7276 | ActOnDocumentableDecl(Namespc); |
| 7277 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7278 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 7279 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7280 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 7281 | // for the namespace has the declarations that showed up in that particular |
| 7282 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7283 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7284 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7287 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 7288 | /// is a namespace alias, returns the namespace it points to. |
| 7289 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 7290 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 7291 | return AD->getNamespace(); |
| 7292 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 7293 | } |
| 7294 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7295 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 7296 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7297 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7298 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 7299 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 7300 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7301 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 7302 | if (Namespc->hasAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 7303 | PopPragmaVisibility(true, RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 7304 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7305 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 7306 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 7307 | return cast_or_null<CXXRecordDecl>( |
| 7308 | StdBadAlloc.get(Context.getExternalSource())); |
| 7309 | } |
| 7310 | |
| 7311 | NamespaceDecl *Sema::getStdNamespace() const { |
| 7312 | return cast_or_null<NamespaceDecl>( |
| 7313 | StdNamespace.get(Context.getExternalSource())); |
| 7314 | } |
| 7315 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7316 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 7317 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 7318 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7319 | if (!StdNamespace) { |
| 7320 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 7321 | StdNamespace = NamespaceDecl::Create(Context, |
| 7322 | Context.getTranslationUnitDecl(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7323 | /*Inline=*/false, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 7324 | SourceLocation(), SourceLocation(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 7325 | &PP.getIdentifierTable().get("std"), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7326 | /*PrevDecl=*/nullptr); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 7327 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7328 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7329 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 7330 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7331 | } |
| 7332 | |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7333 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7334 | assert(getLangOpts().CPlusPlus && |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7335 | "Looking for std::initializer_list outside of C++."); |
| 7336 | |
| 7337 | // We're looking for implicit instantiations of |
| 7338 | // template <typename E> class std::initializer_list. |
| 7339 | |
| 7340 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 7341 | return false; |
| 7342 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7343 | ClassTemplateDecl *Template = nullptr; |
| 7344 | const TemplateArgument *Arguments = nullptr; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7345 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 7346 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7347 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 7348 | ClassTemplateSpecializationDecl *Specialization = |
| 7349 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 7350 | if (!Specialization) |
| 7351 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7352 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 7353 | Template = Specialization->getSpecializedTemplate(); |
| 7354 | Arguments = Specialization->getTemplateArgs().data(); |
| 7355 | } else if (const TemplateSpecializationType *TST = |
| 7356 | Ty->getAs<TemplateSpecializationType>()) { |
| 7357 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 7358 | TST->getTemplateName().getAsTemplateDecl()); |
| 7359 | Arguments = TST->getArgs(); |
| 7360 | } |
| 7361 | if (!Template) |
| 7362 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7363 | |
| 7364 | if (!StdInitializerList) { |
| 7365 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 7366 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 7367 | if (TemplateClass->getIdentifier() != |
| 7368 | &PP.getIdentifierTable().get("initializer_list") || |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 7369 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 7370 | TemplateClass->getDeclContext())) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7371 | return false; |
| 7372 | // This is a template called std::initializer_list, but is it the right |
| 7373 | // template? |
| 7374 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 7375 | if (Params->getMinRequiredArguments() != 1) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7376 | return false; |
| 7377 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 7378 | return false; |
| 7379 | |
| 7380 | // It's the right template. |
| 7381 | StdInitializerList = Template; |
| 7382 | } |
| 7383 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 7384 | if (Template->getCanonicalDecl() != StdInitializerList->getCanonicalDecl()) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7385 | return false; |
| 7386 | |
| 7387 | // This is an instance of std::initializer_list. Find the argument type. |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 7388 | if (Element) |
| 7389 | *Element = Arguments[0].getAsType(); |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 7390 | return true; |
| 7391 | } |
| 7392 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 7393 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 7394 | NamespaceDecl *Std = S.getStdNamespace(); |
| 7395 | if (!Std) { |
| 7396 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7397 | return nullptr; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 7398 | } |
| 7399 | |
| 7400 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 7401 | Loc, Sema::LookupOrdinaryName); |
| 7402 | if (!S.LookupQualifiedName(Result, Std)) { |
| 7403 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7404 | return nullptr; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 7405 | } |
| 7406 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 7407 | if (!Template) { |
| 7408 | Result.suppressDiagnostics(); |
| 7409 | // We found something weird. Complain about the first thing we found. |
| 7410 | NamedDecl *Found = *Result.begin(); |
| 7411 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7412 | return nullptr; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 7413 | } |
| 7414 | |
| 7415 | // We found some template called std::initializer_list. Now verify that it's |
| 7416 | // correct. |
| 7417 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 7418 | if (Params->getMinRequiredArguments() != 1 || |
| 7419 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 7420 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7421 | return nullptr; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 7422 | } |
| 7423 | |
| 7424 | return Template; |
| 7425 | } |
| 7426 | |
| 7427 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 7428 | if (!StdInitializerList) { |
| 7429 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 7430 | if (!StdInitializerList) |
| 7431 | return QualType(); |
| 7432 | } |
| 7433 | |
| 7434 | TemplateArgumentListInfo Args(Loc, Loc); |
| 7435 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 7436 | Context.getTrivialTypeSourceInfo(Element, |
| 7437 | Loc))); |
| 7438 | return Context.getCanonicalType( |
| 7439 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 7440 | } |
| 7441 | |
Sebastian Redl | 98d3606 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 7442 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 7443 | // C++ [dcl.init.list]p2: |
| 7444 | // A constructor is an initializer-list constructor if its first parameter |
| 7445 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 7446 | // std::initializer_list<E> for some type E, and either there are no other |
| 7447 | // parameters or else all other parameters have default arguments. |
| 7448 | if (Ctor->getNumParams() < 1 || |
| 7449 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 7450 | return false; |
| 7451 | |
| 7452 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 7453 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 7454 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 7455 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7456 | return isStdInitializerList(ArgType, nullptr); |
Sebastian Redl | 98d3606 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 7457 | } |
| 7458 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 7459 | /// \brief Determine whether a using statement is in a context where it will be |
| 7460 | /// apply in all contexts. |
| 7461 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 7462 | switch (CurContext->getDeclKind()) { |
| 7463 | case Decl::TranslationUnit: |
| 7464 | return true; |
| 7465 | case Decl::LinkageSpec: |
| 7466 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 7467 | default: |
| 7468 | return false; |
| 7469 | } |
| 7470 | } |
| 7471 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 7472 | namespace { |
| 7473 | |
| 7474 | // Callback to only accept typo corrections that are namespaces. |
| 7475 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7476 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7477 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7478 | if (NamedDecl *ND = candidate.getCorrectionDecl()) |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 7479 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 7480 | return false; |
| 7481 | } |
| 7482 | }; |
| 7483 | |
| 7484 | } |
| 7485 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7486 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 7487 | CXXScopeSpec &SS, |
| 7488 | SourceLocation IdentLoc, |
| 7489 | IdentifierInfo *Ident) { |
| 7490 | R.clear(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7491 | if (TypoCorrection Corrected = |
| 7492 | S.CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), Sc, &SS, |
| 7493 | llvm::make_unique<NamespaceValidatorCCC>(), |
| 7494 | Sema::CTK_ErrorRecovery)) { |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 7495 | if (DeclContext *DC = S.computeDeclContext(SS, false)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7496 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 7497 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 7498 | Ident->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7499 | S.diagnoseTypo(Corrected, |
| 7500 | S.PDiag(diag::err_using_directive_member_suggest) |
| 7501 | << Ident << DC << DroppedSpecifier << SS.getRange(), |
| 7502 | S.PDiag(diag::note_namespace_defined_here)); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 7503 | } else { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7504 | S.diagnoseTypo(Corrected, |
| 7505 | S.PDiag(diag::err_using_directive_suggest) << Ident, |
| 7506 | S.PDiag(diag::note_namespace_defined_here)); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 7507 | } |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 7508 | R.addDecl(Corrected.getCorrectionDecl()); |
| 7509 | return true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7510 | } |
| 7511 | return false; |
| 7512 | } |
| 7513 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7514 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7515 | SourceLocation UsingLoc, |
| 7516 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7517 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7518 | SourceLocation IdentLoc, |
| 7519 | IdentifierInfo *NamespcName, |
| 7520 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7521 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 7522 | assert(NamespcName && "Invalid NamespcName."); |
| 7523 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 7524 | |
| 7525 | // This can only happen along a recovery path. |
| 7526 | while (S->getFlags() & Scope::TemplateParamScope) |
| 7527 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7528 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7529 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7530 | UsingDirectiveDecl *UDir = nullptr; |
| 7531 | NestedNameSpecifier *Qualifier = nullptr; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7532 | if (SS.isSet()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7533 | Qualifier = SS.getScopeRep(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7534 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 7535 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7536 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 7537 | LookupParsedName(R, S, &SS); |
| 7538 | if (R.isAmbiguous()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7539 | return nullptr; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7540 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7541 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7542 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7543 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 7544 | // "std" hasn't been defined yet, for GCC compatibility. |
| 7545 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 7546 | NamespcName->isStr("std")) { |
| 7547 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 7548 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7549 | R.resolveKind(); |
| 7550 | } |
| 7551 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7552 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7553 | } |
| 7554 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7555 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7556 | NamedDecl *Named = R.getFoundDecl(); |
| 7557 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 7558 | && "expected namespace decl"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7559 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 7560 | // The use of a nested name specifier may trigger deprecation warnings. |
| 7561 | DiagnoseUseOfDecl(Named, IdentLoc); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7562 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7563 | // C++ [namespace.udir]p1: |
| 7564 | // A using-directive specifies that the names in the nominated |
| 7565 | // namespace can be used in the scope in which the |
| 7566 | // using-directive appears after the using-directive. During |
| 7567 | // unqualified name lookup (3.4.1), the names appear as if they |
| 7568 | // were declared in the nearest enclosing namespace which |
| 7569 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 7570 | // namespace. [Note: in this context, "contains" means "contains |
| 7571 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7572 | |
| 7573 | // Find enclosing context containing both using-directive and |
| 7574 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7575 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7576 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 7577 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 7578 | CommonAncestor = CommonAncestor->getParent(); |
| 7579 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7580 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 7581 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7582 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 7583 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 7584 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Eli Friedman | 2414697 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 7585 | !SourceMgr.isInMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 7586 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 7587 | } |
| 7588 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7589 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7590 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 7591 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7592 | } |
| 7593 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7594 | if (UDir) |
| 7595 | ProcessDeclAttributeList(S, UDir, AttrList); |
| 7596 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7597 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7598 | } |
| 7599 | |
| 7600 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7601 | // If the scope has an associated entity and the using directive is at |
| 7602 | // namespace or translation unit scope, add the UsingDirectiveDecl into |
| 7603 | // its lookup structure so qualified name lookup can find it. |
Ted Kremenek | f0d5861 | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7604 | DeclContext *Ctx = S->getEntity(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7605 | if (Ctx && !Ctx->isFunctionOrMethod()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 7606 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7607 | else |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7608 | // Otherwise, it is at block scope. The using-directives will affect lookup |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7609 | // only to the end of the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7610 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7611 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7612 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7613 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7614 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 7615 | AccessSpecifier AS, |
| 7616 | bool HasUsingKeyword, |
| 7617 | SourceLocation UsingLoc, |
| 7618 | CXXScopeSpec &SS, |
| 7619 | UnqualifiedId &Name, |
| 7620 | AttributeList *AttrList, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7621 | bool HasTypenameKeyword, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 7622 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7623 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7624 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7625 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 7626 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7627 | case UnqualifiedId::IK_Identifier: |
| 7628 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 7629 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7630 | case UnqualifiedId::IK_ConversionFunctionId: |
| 7631 | break; |
| 7632 | |
| 7633 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 7634 | case UnqualifiedId::IK_ConstructorTemplateId: |
Richard Smith | a1366cb | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 7635 | // C++11 inheriting constructors. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7636 | Diag(Name.getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7637 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7638 | diag::warn_cxx98_compat_using_decl_constructor : |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7639 | diag::err_using_decl_constructor) |
| 7640 | << SS.getRange(); |
| 7641 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7642 | if (getLangOpts().CPlusPlus11) break; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7643 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7644 | return nullptr; |
| 7645 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7646 | case UnqualifiedId::IK_DestructorName: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7647 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7648 | << SS.getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7649 | return nullptr; |
| 7650 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7651 | case UnqualifiedId::IK_TemplateId: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7652 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7653 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7654 | return nullptr; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7655 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7656 | |
| 7657 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 7658 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7659 | if (!TargetName) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7660 | return nullptr; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7661 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7662 | // Warn about access declarations. |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 7663 | if (!HasUsingKeyword) { |
Enea Zaffanella | d4de59d | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 7664 | Diag(Name.getLocStart(), |
Richard Smith | 1b2209f | 2013-06-13 02:12:17 +0000 | [diff] [blame] | 7665 | getLangOpts().CPlusPlus11 ? diag::err_access_decl |
| 7666 | : diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7667 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 7668 | } |
| 7669 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 7670 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 7671 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7672 | return nullptr; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 7673 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7674 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7675 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7676 | /* IsInstantiation */ false, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7677 | HasTypenameKeyword, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7678 | if (UD) |
| 7679 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7680 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7681 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7682 | } |
| 7683 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7684 | /// \brief Determine whether a using declaration considers the given |
| 7685 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 7686 | /// the same entity or are both typedefs of the same type. |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7687 | static bool |
| 7688 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2) { |
| 7689 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7690 | return true; |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7691 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7692 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7693 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7694 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 7695 | TD2->getUnderlyingType()); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7696 | |
| 7697 | return false; |
| 7698 | } |
| 7699 | |
| 7700 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7701 | /// Determines whether to create a using shadow decl for a particular |
| 7702 | /// decl, given the set of decls existing prior to this using lookup. |
| 7703 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7704 | const LookupResult &Previous, |
| 7705 | UsingShadowDecl *&PrevShadow) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7706 | // Diagnose finding a decl which is not from a base class of the |
| 7707 | // current class. We do this now because there are cases where this |
| 7708 | // function will silently decide not to build a shadow decl, which |
| 7709 | // will pre-empt further diagnostics. |
| 7710 | // |
| 7711 | // We don't need to do this in C++0x because we do the check once on |
| 7712 | // the qualifier. |
| 7713 | // |
| 7714 | // FIXME: diagnose the following if we care enough: |
| 7715 | // struct A { int foo; }; |
| 7716 | // struct B : A { using A::foo; }; |
| 7717 | // template <class T> struct C : A {}; |
| 7718 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 7719 | // This is invalid (during instantiation) in C++03 because B::foo |
| 7720 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 7721 | // We can't diagnose it immediately because C<T> is an unknown |
| 7722 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 7723 | // to A::foo, which will look well-formed when we instantiate. |
| 7724 | // The right solution is to not collapse the shadow-decl chain. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7725 | if (!getLangOpts().CPlusPlus11 && CurContext->isRecord()) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7726 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 7727 | |
| 7728 | // Handle enums and anonymous structs. |
| 7729 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 7730 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 7731 | while (OrigRec->isAnonymousStructOrUnion()) |
| 7732 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 7733 | |
| 7734 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 7735 | if (OrigDC == CurContext) { |
| 7736 | Diag(Using->getLocation(), |
| 7737 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7738 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7739 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 7740 | return true; |
| 7741 | } |
| 7742 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7743 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7744 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7745 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7746 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7747 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7748 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 7749 | return true; |
| 7750 | } |
| 7751 | } |
| 7752 | |
| 7753 | if (Previous.empty()) return false; |
| 7754 | |
| 7755 | NamedDecl *Target = Orig; |
| 7756 | if (isa<UsingShadowDecl>(Target)) |
| 7757 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 7758 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7759 | // If the target happens to be one of the previous declarations, we |
| 7760 | // don't have a conflict. |
| 7761 | // |
| 7762 | // FIXME: but we might be increasing its access, in which case we |
| 7763 | // should redeclare it. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7764 | NamedDecl *NonTag = nullptr, *Tag = nullptr; |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7765 | bool FoundEquivalentDecl = false; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7766 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7767 | I != E; ++I) { |
| 7768 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7769 | if (IsEquivalentForUsingDecl(Context, D, Target)) { |
| 7770 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(*I)) |
| 7771 | PrevShadow = Shadow; |
| 7772 | FoundEquivalentDecl = true; |
| 7773 | } |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7774 | |
| 7775 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 7776 | } |
| 7777 | |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7778 | if (FoundEquivalentDecl) |
| 7779 | return false; |
| 7780 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7781 | if (FunctionDecl *FD = Target->getAsFunction()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7782 | NamedDecl *OldDecl = nullptr; |
| 7783 | switch (CheckOverload(nullptr, FD, Previous, OldDecl, |
| 7784 | /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7785 | case Ovl_Overload: |
| 7786 | return false; |
| 7787 | |
| 7788 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7789 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7790 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7791 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7792 | // We found a decl with the exact signature. |
| 7793 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7794 | // If we're in a record, we want to hide the target, so we |
| 7795 | // return true (without a diagnostic) to tell the caller not to |
| 7796 | // build a shadow decl. |
| 7797 | if (CurContext->isRecord()) |
| 7798 | return true; |
| 7799 | |
| 7800 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7801 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7802 | break; |
| 7803 | } |
| 7804 | |
| 7805 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7806 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 7807 | return true; |
| 7808 | } |
| 7809 | |
| 7810 | // Target is not a function. |
| 7811 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7812 | if (isa<TagDecl>(Target)) { |
| 7813 | // No conflict between a tag and a non-tag. |
| 7814 | if (!Tag) return false; |
| 7815 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7816 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7817 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7818 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 7819 | return true; |
| 7820 | } |
| 7821 | |
| 7822 | // No conflict between a tag and a non-tag. |
| 7823 | if (!NonTag) return false; |
| 7824 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7825 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7826 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7827 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 7828 | return true; |
| 7829 | } |
| 7830 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7831 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7832 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7833 | UsingDecl *UD, |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7834 | NamedDecl *Orig, |
| 7835 | UsingShadowDecl *PrevDecl) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7836 | |
| 7837 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7838 | NamedDecl *Target = Orig; |
| 7839 | if (isa<UsingShadowDecl>(Target)) { |
| 7840 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 7841 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7842 | } |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7843 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7844 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7845 | = UsingShadowDecl::Create(Context, CurContext, |
| 7846 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7847 | UD->addShadowDecl(Shadow); |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7848 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 7849 | Shadow->setAccess(UD->getAccess()); |
| 7850 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 7851 | Shadow->setInvalidDecl(); |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7852 | |
| 7853 | Shadow->setPreviousDecl(PrevDecl); |
| 7854 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7855 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7856 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7857 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7858 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7859 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7860 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7861 | return Shadow; |
| 7862 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7863 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7864 | /// Hides a using shadow declaration. This is required by the current |
| 7865 | /// using-decl implementation when a resolvable using declaration in a |
| 7866 | /// class is followed by a declaration which would hide or override |
| 7867 | /// one or more of the using decl's targets; for example: |
| 7868 | /// |
| 7869 | /// struct Base { void foo(int); }; |
| 7870 | /// struct Derived : Base { |
| 7871 | /// using Base::foo; |
| 7872 | /// void foo(int); |
| 7873 | /// }; |
| 7874 | /// |
| 7875 | /// The governing language is C++03 [namespace.udecl]p12: |
| 7876 | /// |
| 7877 | /// When a using-declaration brings names from a base class into a |
| 7878 | /// derived class scope, member functions in the derived class |
| 7879 | /// override and/or hide member functions with the same name and |
| 7880 | /// parameter types in a base class (rather than conflicting). |
| 7881 | /// |
| 7882 | /// There are two ways to implement this: |
| 7883 | /// (1) optimistically create shadow decls when they're not hidden |
| 7884 | /// by existing declarations, or |
| 7885 | /// (2) don't create any shadow decls (or at least don't make them |
| 7886 | /// visible) until we've fully parsed/instantiated the class. |
| 7887 | /// The problem with (1) is that we might have to retroactively remove |
| 7888 | /// a shadow decl, which requires several O(n) operations because the |
| 7889 | /// decl structures are (very reasonably) not designed for removal. |
| 7890 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 7891 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7892 | if (Shadow->getDeclName().getNameKind() == |
| 7893 | DeclarationName::CXXConversionFunctionName) |
| 7894 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 7895 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7896 | // Remove it from the DeclContext... |
| 7897 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7898 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7899 | // ...and the scope, if applicable... |
| 7900 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7901 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7902 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7903 | } |
| 7904 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7905 | // ...and the using decl. |
| 7906 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 7907 | |
| 7908 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7909 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7910 | } |
| 7911 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7912 | /// Find the base specifier for a base class with the given type. |
| 7913 | static CXXBaseSpecifier *findDirectBaseWithType(CXXRecordDecl *Derived, |
| 7914 | QualType DesiredBase, |
| 7915 | bool &AnyDependentBases) { |
| 7916 | // Check whether the named type is a direct base class. |
| 7917 | CanQualType CanonicalDesiredBase = DesiredBase->getCanonicalTypeUnqualified(); |
| 7918 | for (auto &Base : Derived->bases()) { |
| 7919 | CanQualType BaseType = Base.getType()->getCanonicalTypeUnqualified(); |
| 7920 | if (CanonicalDesiredBase == BaseType) |
| 7921 | return &Base; |
| 7922 | if (BaseType->isDependentType()) |
| 7923 | AnyDependentBases = true; |
| 7924 | } |
| 7925 | return nullptr; |
| 7926 | } |
| 7927 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7928 | namespace { |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7929 | class UsingValidatorCCC : public CorrectionCandidateCallback { |
| 7930 | public: |
Kaelyn Uhrain | b5c7768 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7931 | UsingValidatorCCC(bool HasTypenameKeyword, bool IsInstantiation, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7932 | NestedNameSpecifier *NNS, CXXRecordDecl *RequireMemberOf) |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7933 | : HasTypenameKeyword(HasTypenameKeyword), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7934 | IsInstantiation(IsInstantiation), OldNNS(NNS), |
| 7935 | RequireMemberOf(RequireMemberOf) {} |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7936 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7937 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7938 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 7939 | |
| 7940 | // Keywords are not valid here. |
| 7941 | if (!ND || isa<NamespaceDecl>(ND)) |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7942 | return false; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7943 | |
| 7944 | // Completely unqualified names are invalid for a 'using' declaration. |
| 7945 | if (Candidate.WillReplaceSpecifier() && !Candidate.getCorrectionSpecifier()) |
| 7946 | return false; |
| 7947 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7948 | if (RequireMemberOf) { |
| 7949 | auto *FoundRecord = dyn_cast<CXXRecordDecl>(ND); |
| 7950 | if (FoundRecord && FoundRecord->isInjectedClassName()) { |
| 7951 | // No-one ever wants a using-declaration to name an injected-class-name |
| 7952 | // of a base class, unless they're declaring an inheriting constructor. |
| 7953 | ASTContext &Ctx = ND->getASTContext(); |
| 7954 | if (!Ctx.getLangOpts().CPlusPlus11) |
| 7955 | return false; |
| 7956 | QualType FoundType = Ctx.getRecordType(FoundRecord); |
| 7957 | |
| 7958 | // Check that the injected-class-name is named as a member of its own |
| 7959 | // type; we don't want to suggest 'using Derived::Base;', since that |
| 7960 | // means something else. |
| 7961 | NestedNameSpecifier *Specifier = |
| 7962 | Candidate.WillReplaceSpecifier() |
| 7963 | ? Candidate.getCorrectionSpecifier() |
| 7964 | : OldNNS; |
| 7965 | if (!Specifier->getAsType() || |
| 7966 | !Ctx.hasSameType(QualType(Specifier->getAsType(), 0), FoundType)) |
| 7967 | return false; |
| 7968 | |
| 7969 | // Check that this inheriting constructor declaration actually names a |
| 7970 | // direct base class of the current class. |
| 7971 | bool AnyDependentBases = false; |
| 7972 | if (!findDirectBaseWithType(RequireMemberOf, |
| 7973 | Ctx.getRecordType(FoundRecord), |
| 7974 | AnyDependentBases) && |
| 7975 | !AnyDependentBases) |
| 7976 | return false; |
| 7977 | } else { |
| 7978 | auto *RD = dyn_cast<CXXRecordDecl>(ND->getDeclContext()); |
| 7979 | if (!RD || RequireMemberOf->isProvablyNotDerivedFrom(RD)) |
| 7980 | return false; |
| 7981 | |
| 7982 | // FIXME: Check that the base class member is accessible? |
| 7983 | } |
| 7984 | } |
| 7985 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7986 | if (isa<TypeDecl>(ND)) |
| 7987 | return HasTypenameKeyword || !IsInstantiation; |
| 7988 | |
| 7989 | return !HasTypenameKeyword; |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7990 | } |
| 7991 | |
| 7992 | private: |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7993 | bool HasTypenameKeyword; |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7994 | bool IsInstantiation; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7995 | NestedNameSpecifier *OldNNS; |
| 7996 | CXXRecordDecl *RequireMemberOf; |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7997 | }; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7998 | } // end anonymous namespace |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7999 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8000 | /// Builds a using declaration. |
| 8001 | /// |
| 8002 | /// \param IsInstantiation - Whether this call arises from an |
| 8003 | /// instantiation of an unresolved using declaration. We treat |
| 8004 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 8005 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 8006 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 8007 | CXXScopeSpec &SS, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8008 | DeclarationNameInfo NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 8009 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8010 | bool IsInstantiation, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8011 | bool HasTypenameKeyword, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8012 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 8013 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 8014 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 8015 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 8016 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 8017 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8018 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 8019 | if (SS.isEmpty()) { |
| 8020 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8021 | return nullptr; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 8022 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8023 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8024 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 8025 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8026 | ForRedeclaration); |
| 8027 | Previous.setHideTags(false); |
| 8028 | if (S) { |
| 8029 | LookupName(Previous, S); |
| 8030 | |
| 8031 | // It is really dumb that we have to do this. |
| 8032 | LookupResult::Filter F = Previous.makeFilter(); |
| 8033 | while (F.hasNext()) { |
| 8034 | NamedDecl *D = F.next(); |
| 8035 | if (!isDeclInScope(D, CurContext, S)) |
| 8036 | F.erase(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8037 | // If we found a local extern declaration that's not ordinarily visible, |
| 8038 | // and this declaration is being added to a non-block scope, ignore it. |
| 8039 | // We're only checking for scope conflicts here, not also for violations |
| 8040 | // of the linkage rules. |
| 8041 | else if (!CurContext->isFunctionOrMethod() && D->isLocalExternDecl() && |
| 8042 | !(D->getIdentifierNamespace() & Decl::IDNS_Ordinary)) |
| 8043 | F.erase(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8044 | } |
| 8045 | F.done(); |
| 8046 | } else { |
| 8047 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 8048 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 8049 | LookupQualifiedName(Previous, CurContext); |
| 8050 | } |
| 8051 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8052 | // Check for invalid redeclarations. |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8053 | if (CheckUsingDeclRedeclaration(UsingLoc, HasTypenameKeyword, |
| 8054 | SS, IdentLoc, Previous)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8055 | return nullptr; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8056 | |
| 8057 | // Check for bad qualifiers. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8058 | if (CheckUsingDeclQualifier(UsingLoc, SS, NameInfo, IdentLoc)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8059 | return nullptr; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8060 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 8061 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8062 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8063 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 8064 | if (!LookupContext) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8065 | if (HasTypenameKeyword) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8066 | // FIXME: not all declaration name kinds are legal here |
| 8067 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 8068 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8069 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 8070 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8071 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8072 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 8073 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8074 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8075 | D->setAccess(AS); |
| 8076 | CurContext->addDecl(D); |
| 8077 | return D; |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 8078 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8079 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8080 | auto Build = [&](bool Invalid) { |
| 8081 | UsingDecl *UD = |
| 8082 | UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, NameInfo, |
| 8083 | HasTypenameKeyword); |
| 8084 | UD->setAccess(AS); |
| 8085 | CurContext->addDecl(UD); |
| 8086 | UD->setInvalidDecl(Invalid); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8087 | return UD; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8088 | }; |
| 8089 | auto BuildInvalid = [&]{ return Build(true); }; |
| 8090 | auto BuildValid = [&]{ return Build(false); }; |
| 8091 | |
| 8092 | if (RequireCompleteDeclContext(SS, LookupContext)) |
| 8093 | return BuildInvalid(); |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 8094 | |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 8095 | // Look up the target name. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 8096 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8097 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8098 | // Unlike most lookups, we don't always want to hide tag |
| 8099 | // declarations: tag names are visible through the using declaration |
| 8100 | // even if hidden by ordinary names, *except* in a dependent context |
| 8101 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8102 | if (!IsInstantiation) |
| 8103 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 8104 | |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 8105 | // For the purposes of this lookup, we have a base object type |
| 8106 | // equal to that of the current context. |
| 8107 | if (CurContext->isRecord()) { |
| 8108 | R.setBaseObjectType( |
| 8109 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 8110 | } |
| 8111 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8112 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8113 | |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 8114 | // Try to correct typos if possible. If constructor name lookup finds no |
| 8115 | // results, that means the named class has no explicit constructors, and we |
| 8116 | // suppressed declaring implicit ones (probably because it's dependent or |
| 8117 | // invalid). |
| 8118 | if (R.empty() && |
| 8119 | NameInfo.getName().getNameKind() != DeclarationName::CXXConstructorName) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8120 | if (TypoCorrection Corrected = CorrectTypo( |
| 8121 | R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
| 8122 | llvm::make_unique<UsingValidatorCCC>( |
| 8123 | HasTypenameKeyword, IsInstantiation, SS.getScopeRep(), |
| 8124 | dyn_cast<CXXRecordDecl>(CurContext)), |
| 8125 | CTK_ErrorRecovery)) { |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 8126 | // We reject any correction for which ND would be NULL. |
| 8127 | NamedDecl *ND = Corrected.getCorrectionDecl(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8128 | |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 8129 | // We reject candidates where DroppedSpecifier == true, hence the |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 8130 | // literal '0' below. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 8131 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) |
| 8132 | << NameInfo.getName() << LookupContext << 0 |
| 8133 | << SS.getRange()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8134 | |
| 8135 | // If we corrected to an inheriting constructor, handle it as one. |
| 8136 | auto *RD = dyn_cast<CXXRecordDecl>(ND); |
| 8137 | if (RD && RD->isInjectedClassName()) { |
| 8138 | // Fix up the information we'll use to build the using declaration. |
| 8139 | if (Corrected.WillReplaceSpecifier()) { |
| 8140 | NestedNameSpecifierLocBuilder Builder; |
| 8141 | Builder.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), |
| 8142 | QualifierLoc.getSourceRange()); |
| 8143 | QualifierLoc = Builder.getWithLocInContext(Context); |
| 8144 | } |
| 8145 | |
| 8146 | NameInfo.setName(Context.DeclarationNames.getCXXConstructorName( |
| 8147 | Context.getCanonicalType(Context.getRecordType(RD)))); |
| 8148 | NameInfo.setNamedTypeInfo(nullptr); |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 8149 | for (auto *Ctor : LookupConstructors(RD)) |
| 8150 | R.addDecl(Ctor); |
| 8151 | } else { |
| 8152 | // FIXME: Pick up all the declarations if we found an overloaded function. |
| 8153 | R.addDecl(ND); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8154 | } |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 8155 | } else { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 8156 | Diag(IdentLoc, diag::err_no_member) |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 8157 | << NameInfo.getName() << LookupContext << SS.getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8158 | return BuildInvalid(); |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 8159 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 8160 | } |
| 8161 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8162 | if (R.isAmbiguous()) |
| 8163 | return BuildInvalid(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8164 | |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8165 | if (HasTypenameKeyword) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8166 | // 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] | 8167 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8168 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 8169 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 8170 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 8171 | diag::note_using_decl_target); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8172 | return BuildInvalid(); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8173 | } |
| 8174 | } else { |
| 8175 | // If we asked for a non-typename and we got a type, error out, |
| 8176 | // but only if this is an instantiation of an unresolved using |
| 8177 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8178 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8179 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 8180 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8181 | return BuildInvalid(); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 8182 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 8183 | } |
| 8184 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 8185 | // C++0x N2914 [namespace.udecl]p6: |
| 8186 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8187 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 8188 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 8189 | << SS.getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8190 | return BuildInvalid(); |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 8191 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8192 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8193 | UsingDecl *UD = BuildValid(); |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame^] | 8194 | |
| 8195 | // The normal rules do not apply to inheriting constructor declarations. |
| 8196 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
| 8197 | // Suppress access diagnostics; the access check is instead performed at the |
| 8198 | // point of use for an inheriting constructor. |
| 8199 | R.suppressDiagnostics(); |
| 8200 | CheckInheritingConstructorUsingDecl(UD); |
| 8201 | return UD; |
| 8202 | } |
| 8203 | |
| 8204 | // Otherwise, look up the target name. |
| 8205 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8206 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8207 | UsingShadowDecl *PrevDecl = nullptr; |
Richard Smith | f06a2893 | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 8208 | if (!CheckUsingShadowDecl(UD, *I, Previous, PrevDecl)) |
| 8209 | BuildUsingShadowDecl(S, UD, *I, PrevDecl); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8210 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 8211 | |
| 8212 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 8213 | } |
| 8214 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8215 | /// Additional checks for a using declaration referring to a constructor name. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 8216 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8217 | assert(!UD->hasTypename() && "expecting a constructor name"); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8218 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8219 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8220 | assert(SourceType && |
| 8221 | "Using decl naming constructor doesn't have type in scope spec."); |
| 8222 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 8223 | |
| 8224 | // Check whether the named type is a direct base class. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8225 | bool AnyDependentBases = false; |
| 8226 | auto *Base = findDirectBaseWithType(TargetClass, QualType(SourceType, 0), |
| 8227 | AnyDependentBases); |
| 8228 | if (!Base && !AnyDependentBases) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8229 | Diag(UD->getUsingLoc(), |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8230 | diag::err_using_decl_constructor_not_in_direct_base) |
| 8231 | << UD->getNameInfo().getSourceRange() |
| 8232 | << QualType(SourceType, 0) << TargetClass; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8233 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8234 | return true; |
| 8235 | } |
| 8236 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8237 | if (Base) |
| 8238 | Base->setInheritConstructors(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8239 | |
| 8240 | return false; |
| 8241 | } |
| 8242 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8243 | /// Checks that the given using declaration is not an invalid |
| 8244 | /// redeclaration. Note that this is checking only for the using decl |
| 8245 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 8246 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8247 | bool HasTypenameKeyword, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8248 | const CXXScopeSpec &SS, |
| 8249 | SourceLocation NameLoc, |
| 8250 | const LookupResult &Prev) { |
| 8251 | // C++03 [namespace.udecl]p8: |
| 8252 | // C++0x [namespace.udecl]p10: |
| 8253 | // A using-declaration is a declaration and can therefore be used |
| 8254 | // repeatedly where (and only where) multiple declarations are |
| 8255 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 8256 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 8257 | // That's in non-member contexts. |
| 8258 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8259 | return false; |
| 8260 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8261 | NestedNameSpecifier *Qual = SS.getScopeRep(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8262 | |
| 8263 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 8264 | NamedDecl *D = *I; |
| 8265 | |
| 8266 | bool DTypename; |
| 8267 | NestedNameSpecifier *DQual; |
| 8268 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8269 | DTypename = UD->hasTypename(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8270 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8271 | } else if (UnresolvedUsingValueDecl *UD |
| 8272 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 8273 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8274 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8275 | } else if (UnresolvedUsingTypenameDecl *UD |
| 8276 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 8277 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8278 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8279 | } else continue; |
| 8280 | |
| 8281 | // using decls differ if one says 'typename' and the other doesn't. |
| 8282 | // FIXME: non-dependent using decls? |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 8283 | if (HasTypenameKeyword != DTypename) continue; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8284 | |
| 8285 | // using decls differ if they name different scopes (but note that |
| 8286 | // template instantiation can cause this check to trigger when it |
| 8287 | // didn't before instantiation). |
| 8288 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 8289 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 8290 | continue; |
| 8291 | |
| 8292 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 8293 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 8294 | return true; |
| 8295 | } |
| 8296 | |
| 8297 | return false; |
| 8298 | } |
| 8299 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8300 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8301 | /// Checks that the given nested-name qualifier used in a using decl |
| 8302 | /// in the current context is appropriately related to the current |
| 8303 | /// scope. If an error is found, diagnoses it and returns true. |
| 8304 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 8305 | const CXXScopeSpec &SS, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8306 | const DeclarationNameInfo &NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8307 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8308 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8309 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8310 | if (!CurContext->isRecord()) { |
| 8311 | // C++03 [namespace.udecl]p3: |
| 8312 | // C++0x [namespace.udecl]p8: |
| 8313 | // A using-declaration for a class member shall be a member-declaration. |
| 8314 | |
| 8315 | // If we weren't able to compute a valid scope, it must be a |
| 8316 | // dependent class scope. |
| 8317 | if (!NamedContext || NamedContext->isRecord()) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8318 | auto *RD = dyn_cast_or_null<CXXRecordDecl>(NamedContext); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8319 | if (RD && RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), RD)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8320 | RD = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8321 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8322 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 8323 | << SS.getRange(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8324 | |
| 8325 | // If we have a complete, non-dependent source type, try to suggest a |
| 8326 | // way to get the same effect. |
| 8327 | if (!RD) |
| 8328 | return true; |
| 8329 | |
| 8330 | // Find what this using-declaration was referring to. |
| 8331 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
| 8332 | R.setHideTags(false); |
| 8333 | R.suppressDiagnostics(); |
| 8334 | LookupQualifiedName(R, RD); |
| 8335 | |
| 8336 | if (R.getAsSingle<TypeDecl>()) { |
| 8337 | if (getLangOpts().CPlusPlus11) { |
| 8338 | // Convert 'using X::Y;' to 'using Y = X::Y;'. |
| 8339 | Diag(SS.getBeginLoc(), diag::note_using_decl_class_member_workaround) |
| 8340 | << 0 // alias declaration |
| 8341 | << FixItHint::CreateInsertion(SS.getBeginLoc(), |
| 8342 | NameInfo.getName().getAsString() + |
| 8343 | " = "); |
| 8344 | } else { |
| 8345 | // Convert 'using X::Y;' to 'typedef X::Y Y;'. |
| 8346 | SourceLocation InsertLoc = |
| 8347 | PP.getLocForEndOfToken(NameInfo.getLocEnd()); |
| 8348 | Diag(InsertLoc, diag::note_using_decl_class_member_workaround) |
| 8349 | << 1 // typedef declaration |
| 8350 | << FixItHint::CreateReplacement(UsingLoc, "typedef") |
| 8351 | << FixItHint::CreateInsertion( |
| 8352 | InsertLoc, " " + NameInfo.getName().getAsString()); |
| 8353 | } |
| 8354 | } else if (R.getAsSingle<VarDecl>()) { |
| 8355 | // Don't provide a fixit outside C++11 mode; we don't want to suggest |
| 8356 | // repeating the type of the static data member here. |
| 8357 | FixItHint FixIt; |
| 8358 | if (getLangOpts().CPlusPlus11) { |
| 8359 | // Convert 'using X::Y;' to 'auto &Y = X::Y;'. |
| 8360 | FixIt = FixItHint::CreateReplacement( |
| 8361 | UsingLoc, "auto &" + NameInfo.getName().getAsString() + " = "); |
| 8362 | } |
| 8363 | |
| 8364 | Diag(UsingLoc, diag::note_using_decl_class_member_workaround) |
| 8365 | << 2 // reference declaration |
| 8366 | << FixIt; |
| 8367 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8368 | return true; |
| 8369 | } |
| 8370 | |
| 8371 | // Otherwise, everything is known to be fine. |
| 8372 | return false; |
| 8373 | } |
| 8374 | |
| 8375 | // The current scope is a record. |
| 8376 | |
| 8377 | // If the named context is dependent, we can't decide much. |
| 8378 | if (!NamedContext) { |
| 8379 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 8380 | // nested-name-specifier does not refer to a base class, which is |
| 8381 | // still possible in some cases. |
| 8382 | |
| 8383 | // Otherwise we have to conservatively report that things might be |
| 8384 | // okay. |
| 8385 | return false; |
| 8386 | } |
| 8387 | |
| 8388 | if (!NamedContext->isRecord()) { |
| 8389 | // Ideally this would point at the last name in the specifier, |
| 8390 | // but we don't have that level of source info. |
| 8391 | Diag(SS.getRange().getBegin(), |
| 8392 | diag::err_using_decl_nested_name_specifier_is_not_class) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8393 | << SS.getScopeRep() << SS.getRange(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8394 | return true; |
| 8395 | } |
| 8396 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 8397 | if (!NamedContext->isDependentContext() && |
| 8398 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 8399 | return true; |
| 8400 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8401 | if (getLangOpts().CPlusPlus11) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8402 | // C++0x [namespace.udecl]p3: |
| 8403 | // In a using-declaration used as a member-declaration, the |
| 8404 | // nested-name-specifier shall name a base class of the class |
| 8405 | // being defined. |
| 8406 | |
| 8407 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 8408 | cast<CXXRecordDecl>(NamedContext))) { |
| 8409 | if (CurContext == NamedContext) { |
| 8410 | Diag(NameLoc, |
| 8411 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 8412 | << SS.getRange(); |
| 8413 | return true; |
| 8414 | } |
| 8415 | |
| 8416 | Diag(SS.getRange().getBegin(), |
| 8417 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8418 | << SS.getScopeRep() |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8419 | << cast<CXXRecordDecl>(CurContext) |
| 8420 | << SS.getRange(); |
| 8421 | return true; |
| 8422 | } |
| 8423 | |
| 8424 | return false; |
| 8425 | } |
| 8426 | |
| 8427 | // C++03 [namespace.udecl]p4: |
| 8428 | // A using-declaration used as a member-declaration shall refer |
| 8429 | // to a member of a base class of the class being defined [etc.]. |
| 8430 | |
| 8431 | // Salient point: SS doesn't have to name a base class as long as |
| 8432 | // lookup only finds members from base classes. Therefore we can |
| 8433 | // diagnose here only if we can prove that that can't happen, |
| 8434 | // i.e. if the class hierarchies provably don't intersect. |
| 8435 | |
| 8436 | // TODO: it would be nice if "definitely valid" results were cached |
| 8437 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 8438 | // need to be repeated. |
| 8439 | |
| 8440 | struct UserData { |
Benjamin Kramer | 8c43dcc | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 8441 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8442 | |
| 8443 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 8444 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 8445 | Data->Bases.insert(Base); |
| 8446 | return true; |
| 8447 | } |
| 8448 | |
| 8449 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 8450 | return !Class->forallBases(collect, this); |
| 8451 | } |
| 8452 | |
| 8453 | /// Returns true if the base is dependent or is one of the |
| 8454 | /// accumulated base classes. |
| 8455 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 8456 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 8457 | return !Data->Bases.count(Base); |
| 8458 | } |
| 8459 | |
| 8460 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 8461 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 8462 | } |
| 8463 | }; |
| 8464 | |
| 8465 | UserData Data; |
| 8466 | |
| 8467 | // Returns false if we find a dependent base. |
| 8468 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 8469 | return false; |
| 8470 | |
| 8471 | // Returns false if the class has a dependent base or if it or one |
| 8472 | // of its bases is present in the base set of the current context. |
| 8473 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 8474 | return false; |
| 8475 | |
| 8476 | Diag(SS.getRange().getBegin(), |
| 8477 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8478 | << SS.getScopeRep() |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 8479 | << cast<CXXRecordDecl>(CurContext) |
| 8480 | << SS.getRange(); |
| 8481 | |
| 8482 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8485 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 8486 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8487 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8488 | SourceLocation UsingLoc, |
| 8489 | UnqualifiedId &Name, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 8490 | AttributeList *AttrList, |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 8491 | TypeResult Type, |
| 8492 | Decl *DeclFromDeclSpec) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8493 | // Skip up to the relevant declaration scope. |
| 8494 | while (S->getFlags() & Scope::TemplateParamScope) |
| 8495 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8496 | assert((S->getFlags() & Scope::DeclScope) && |
| 8497 | "got alias-declaration outside of declaration scope"); |
| 8498 | |
| 8499 | if (Type.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8500 | return nullptr; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8501 | |
| 8502 | bool Invalid = false; |
| 8503 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8504 | TypeSourceInfo *TInfo = nullptr; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 8505 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8506 | |
| 8507 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8508 | return nullptr; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8509 | |
| 8510 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8511 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8512 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8513 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 8514 | TInfo->getTypeLoc().getBeginLoc()); |
| 8515 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8516 | |
| 8517 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 8518 | LookupName(Previous, S); |
| 8519 | |
| 8520 | // Warn about shadowing the name of a template parameter. |
| 8521 | if (Previous.isSingleResult() && |
| 8522 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 8523 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8524 | Previous.clear(); |
| 8525 | } |
| 8526 | |
| 8527 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 8528 | "name in alias declaration must be an identifier"); |
| 8529 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 8530 | Name.StartLocation, |
| 8531 | Name.Identifier, TInfo); |
| 8532 | |
| 8533 | NewTD->setAccess(AS); |
| 8534 | |
| 8535 | if (Invalid) |
| 8536 | NewTD->setInvalidDecl(); |
| 8537 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 8538 | ProcessDeclAttributeList(S, NewTD, AttrList); |
| 8539 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8540 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 8541 | Invalid |= NewTD->isInvalidDecl(); |
| 8542 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8543 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8544 | |
| 8545 | NamedDecl *NewND; |
| 8546 | if (TemplateParamLists.size()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8547 | TypeAliasTemplateDecl *OldDecl = nullptr; |
| 8548 | TemplateParameterList *OldTemplateParams = nullptr; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8549 | |
| 8550 | if (TemplateParamLists.size() != 1) { |
| 8551 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8552 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 8553 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8554 | } |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8555 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8556 | |
| 8557 | // Only consider previous declarations in the same scope. |
| 8558 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 8559 | /*ExplicitInstantiationOrSpecialization*/false); |
| 8560 | if (!Previous.empty()) { |
| 8561 | Redeclaration = true; |
| 8562 | |
| 8563 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 8564 | if (!OldDecl && !Invalid) { |
| 8565 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 8566 | << Name.Identifier; |
| 8567 | |
| 8568 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 8569 | if (OldD->getLocation().isValid()) |
| 8570 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 8571 | |
| 8572 | Invalid = true; |
| 8573 | } |
| 8574 | |
| 8575 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 8576 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 8577 | OldDecl->getTemplateParameters(), |
| 8578 | /*Complain=*/true, |
| 8579 | TPL_TemplateMatch)) |
| 8580 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 8581 | else |
| 8582 | Invalid = true; |
| 8583 | |
| 8584 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 8585 | if (!Invalid && |
| 8586 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 8587 | NewTD->getUnderlyingType())) { |
| 8588 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 8589 | // but we can't reasonably accept it. |
| 8590 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 8591 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 8592 | if (OldTD->getLocation().isValid()) |
| 8593 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 8594 | Invalid = true; |
| 8595 | } |
| 8596 | } |
| 8597 | } |
| 8598 | |
| 8599 | // Merge any previous default template arguments into our parameters, |
| 8600 | // and check the parameter list. |
| 8601 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 8602 | TPC_TypeAliasTemplate)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8603 | return nullptr; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8604 | |
| 8605 | TypeAliasTemplateDecl *NewDecl = |
| 8606 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 8607 | Name.Identifier, TemplateParams, |
| 8608 | NewTD); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8609 | NewTD->setDescribedAliasTemplate(NewDecl); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8610 | |
| 8611 | NewDecl->setAccess(AS); |
| 8612 | |
| 8613 | if (Invalid) |
| 8614 | NewDecl->setInvalidDecl(); |
| 8615 | else if (OldDecl) |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 8616 | NewDecl->setPreviousDecl(OldDecl); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8617 | |
| 8618 | NewND = NewDecl; |
| 8619 | } else { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 8620 | if (auto *TD = dyn_cast_or_null<TagDecl>(DeclFromDeclSpec)) { |
| 8621 | setTagNameForLinkagePurposes(TD, NewTD); |
| 8622 | handleTagNumbering(TD, S); |
| 8623 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8624 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 8625 | NewND = NewTD; |
| 8626 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8627 | |
| 8628 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8629 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8630 | |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 8631 | ActOnDocumentableDecl(NewND); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 8632 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 8633 | } |
| 8634 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8635 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, SourceLocation NamespaceLoc, |
| 8636 | SourceLocation AliasLoc, |
| 8637 | IdentifierInfo *Alias, CXXScopeSpec &SS, |
| 8638 | SourceLocation IdentLoc, |
| 8639 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8640 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 8641 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8642 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 8643 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 8644 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8645 | if (R.isAmbiguous()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8646 | return nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8647 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8648 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 8649 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Richard Smith | bf9658c | 2012-04-05 23:13:23 +0000 | [diff] [blame] | 8650 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8651 | return nullptr; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 8652 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 8653 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8654 | assert(!R.isAmbiguous() && !R.empty()); |
| 8655 | |
| 8656 | // Check if we have a previous declaration with the same name. |
| 8657 | NamedDecl *PrevDecl = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 8658 | ForRedeclaration); |
| 8659 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 8660 | PrevDecl = nullptr; |
| 8661 | |
| 8662 | NamedDecl *ND = R.getFoundDecl(); |
| 8663 | |
| 8664 | if (PrevDecl) { |
| 8665 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
| 8666 | // We already have an alias with the same name that points to the same |
| 8667 | // namespace; check that it matches. |
| 8668 | if (!AD->getNamespace()->Equals(getNamespaceDecl(ND))) { |
| 8669 | Diag(AliasLoc, diag::err_redefinition_different_namespace_alias) |
| 8670 | << Alias; |
| 8671 | Diag(PrevDecl->getLocation(), diag::note_previous_namespace_alias) |
| 8672 | << AD->getNamespace(); |
| 8673 | return nullptr; |
| 8674 | } |
| 8675 | } else { |
| 8676 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) |
| 8677 | ? diag::err_redefinition |
| 8678 | : diag::err_redefinition_different_kind; |
| 8679 | Diag(AliasLoc, DiagID) << Alias; |
| 8680 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 8681 | return nullptr; |
| 8682 | } |
| 8683 | } |
| 8684 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8685 | // The use of a nested name specifier may trigger deprecation warnings. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8686 | DiagnoseUseOfDecl(ND, IdentLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8687 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8688 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8689 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 8690 | Alias, SS.getWithLocInContext(Context), |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8691 | IdentLoc, ND); |
| 8692 | if (PrevDecl) |
| 8693 | AliasDecl->setPreviousDecl(cast<NamespaceAliasDecl>(PrevDecl)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8694 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 8695 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8696 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8699 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8700 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(SourceLocation Loc, |
| 8701 | CXXMethodDecl *MD) { |
| 8702 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8703 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8704 | // C++ [except.spec]p14: |
| 8705 | // An implicitly declared special member function (Clause 12) shall have an |
| 8706 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8707 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8708 | if (ClassDecl->isInvalidDecl()) |
| 8709 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8710 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8711 | // Direct base-class constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8712 | for (const auto &B : ClassDecl->bases()) { |
| 8713 | if (B.isVirtual()) // Handled below. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8714 | continue; |
| 8715 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8716 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8717 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8718 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8719 | // If this is a deleted function, add it anyway. This might be conformant |
| 8720 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8721 | if (Constructor) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8722 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8723 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8724 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8725 | |
| 8726 | // Virtual base-class constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8727 | for (const auto &B : ClassDecl->vbases()) { |
| 8728 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8729 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8730 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8731 | // If this is a deleted function, add it anyway. This might be conformant |
| 8732 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8733 | if (Constructor) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8734 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8735 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8736 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8737 | |
| 8738 | // Field constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8739 | for (const auto *F : ClassDecl->fields()) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8740 | if (F->hasInClassInitializer()) { |
| 8741 | if (Expr *E = F->getInClassInitializer()) |
| 8742 | ExceptSpec.CalledExpr(E); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8743 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8744 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8745 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8746 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 8747 | // If this is a deleted function, add it anyway. This might be conformant |
| 8748 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8749 | // In particular, the problem is that this function never gets called. It |
| 8750 | // might just be ill-formed because this function attempts to refer to |
| 8751 | // a deleted function here. |
| 8752 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8753 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8754 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8755 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8756 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8757 | return ExceptSpec; |
| 8758 | } |
| 8759 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8760 | Sema::ImplicitExceptionSpecification |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8761 | Sema::ComputeInheritingCtorExceptionSpec(CXXConstructorDecl *CD) { |
| 8762 | CXXRecordDecl *ClassDecl = CD->getParent(); |
| 8763 | |
| 8764 | // C++ [except.spec]p14: |
| 8765 | // An inheriting constructor [...] shall have an exception-specification. [...] |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8766 | ImplicitExceptionSpecification ExceptSpec(*this); |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8767 | if (ClassDecl->isInvalidDecl()) |
| 8768 | return ExceptSpec; |
| 8769 | |
| 8770 | // Inherited constructor. |
| 8771 | const CXXConstructorDecl *InheritedCD = CD->getInheritedConstructor(); |
| 8772 | const CXXRecordDecl *InheritedDecl = InheritedCD->getParent(); |
| 8773 | // FIXME: Copying or moving the parameters could add extra exceptions to the |
| 8774 | // set, as could the default arguments for the inherited constructor. This |
| 8775 | // will be addressed when we implement the resolution of core issue 1351. |
| 8776 | ExceptSpec.CalledDecl(CD->getLocStart(), InheritedCD); |
| 8777 | |
| 8778 | // Direct base-class constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8779 | for (const auto &B : ClassDecl->bases()) { |
| 8780 | if (B.isVirtual()) // Handled below. |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8781 | continue; |
| 8782 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8783 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8784 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8785 | if (BaseClassDecl == InheritedDecl) |
| 8786 | continue; |
| 8787 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8788 | if (Constructor) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8789 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8790 | } |
| 8791 | } |
| 8792 | |
| 8793 | // Virtual base-class constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8794 | for (const auto &B : ClassDecl->vbases()) { |
| 8795 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8796 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8797 | if (BaseClassDecl == InheritedDecl) |
| 8798 | continue; |
| 8799 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8800 | if (Constructor) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8801 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8802 | } |
| 8803 | } |
| 8804 | |
| 8805 | // Field constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 8806 | for (const auto *F : ClassDecl->fields()) { |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8807 | if (F->hasInClassInitializer()) { |
| 8808 | if (Expr *E = F->getInClassInitializer()) |
| 8809 | ExceptSpec.CalledExpr(E); |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8810 | } else if (const RecordType *RecordTy |
| 8811 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 8812 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8813 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 8814 | if (Constructor) |
| 8815 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
| 8816 | } |
| 8817 | } |
| 8818 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8819 | return ExceptSpec; |
| 8820 | } |
| 8821 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8822 | namespace { |
| 8823 | /// RAII object to register a special member as being currently declared. |
| 8824 | struct DeclaringSpecialMember { |
| 8825 | Sema &S; |
| 8826 | Sema::SpecialMemberDecl D; |
| 8827 | bool WasAlreadyBeingDeclared; |
| 8828 | |
| 8829 | DeclaringSpecialMember(Sema &S, CXXRecordDecl *RD, Sema::CXXSpecialMember CSM) |
| 8830 | : S(S), D(RD, CSM) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8831 | WasAlreadyBeingDeclared = !S.SpecialMembersBeingDeclared.insert(D).second; |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8832 | if (WasAlreadyBeingDeclared) |
| 8833 | // This almost never happens, but if it does, ensure that our cache |
| 8834 | // doesn't contain a stale result. |
| 8835 | S.SpecialMemberCache.clear(); |
| 8836 | |
| 8837 | // FIXME: Register a note to be produced if we encounter an error while |
| 8838 | // declaring the special member. |
| 8839 | } |
| 8840 | ~DeclaringSpecialMember() { |
| 8841 | if (!WasAlreadyBeingDeclared) |
| 8842 | S.SpecialMembersBeingDeclared.erase(D); |
| 8843 | } |
| 8844 | |
| 8845 | /// \brief Are we already trying to declare this special member? |
| 8846 | bool isAlreadyBeingDeclared() const { |
| 8847 | return WasAlreadyBeingDeclared; |
| 8848 | } |
| 8849 | }; |
| 8850 | } |
| 8851 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8852 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 8853 | CXXRecordDecl *ClassDecl) { |
| 8854 | // C++ [class.ctor]p5: |
| 8855 | // A default constructor for a class X is a constructor of class X |
| 8856 | // that can be called without an argument. If there is no |
| 8857 | // user-declared constructor for class X, a default constructor is |
| 8858 | // implicitly declared. An implicitly-declared default constructor |
| 8859 | // is an inline public member of its class. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8860 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8861 | "Should not build implicit default constructor!"); |
| 8862 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8863 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDefaultConstructor); |
| 8864 | if (DSM.isAlreadyBeingDeclared()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8865 | return nullptr; |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8866 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8867 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 8868 | CXXDefaultConstructor, |
| 8869 | false); |
| 8870 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8871 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8872 | CanQualType ClassType |
| 8873 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8874 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8875 | DeclarationName Name |
| 8876 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8877 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8878 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8879 | Context, ClassDecl, ClassLoc, NameInfo, /*Type*/QualType(), |
| 8880 | /*TInfo=*/nullptr, /*isExplicit=*/false, /*isInline=*/true, |
| 8881 | /*isImplicitlyDeclared=*/true, Constexpr); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8882 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 8883 | DefaultCon->setDefaulted(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8884 | |
| 8885 | if (getLangOpts().CUDA) { |
| 8886 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXDefaultConstructor, |
| 8887 | DefaultCon, |
| 8888 | /* ConstRHS */ false, |
| 8889 | /* Diagnose */ false); |
| 8890 | } |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8891 | |
| 8892 | // Build an exception specification pointing back at this constructor. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8893 | FunctionProtoType::ExtProtoInfo EPI = getImplicitMethodEPI(*this, DefaultCon); |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8894 | DefaultCon->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8895 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8896 | // We don't need to use SpecialMemberIsTrivial here; triviality for default |
| 8897 | // constructors is easy to compute. |
| 8898 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
| 8899 | |
| 8900 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8901 | SetDeclDeleted(DefaultCon, ClassLoc); |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8902 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8903 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8904 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8905 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8906 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8907 | PushOnScopeChains(DefaultCon, S, false); |
| 8908 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8909 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8910 | return DefaultCon; |
| 8911 | } |
| 8912 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8913 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 8914 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 8915 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8916 | !Constructor->doesThisDeclarationHaveABody() && |
| 8917 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 8918 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8919 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 8920 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8921 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 8922 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8923 | SynthesizedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8924 | DiagnosticErrorTrap Trap(Diags); |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 8925 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8926 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 8927 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 8928 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8929 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8930 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8931 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8932 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8933 | // The exception specification is needed because we are defining the |
| 8934 | // function. |
| 8935 | ResolveExceptionSpec(CurrentLocation, |
| 8936 | Constructor->getType()->castAs<FunctionProtoType>()); |
| 8937 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 8938 | SourceLocation Loc = Constructor->getLocEnd().isValid() |
| 8939 | ? Constructor->getLocEnd() |
| 8940 | : Constructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8941 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8942 | |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 8943 | Constructor->markUsed(Context); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8944 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8945 | |
| 8946 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8947 | L->CompletedImplicitDefinition(Constructor); |
| 8948 | } |
Richard Trieu | 858d2ba | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 8949 | |
| 8950 | DiagnoseUninitializedFields(*this, Constructor); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8953 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
Alp Toker | 0823566 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 8954 | // Perform any delayed checks on exception specifications. |
| 8955 | CheckDelayedMemberExceptionSpecs(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8956 | } |
| 8957 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8958 | namespace { |
| 8959 | /// Information on inheriting constructors to declare. |
| 8960 | class InheritingConstructorInfo { |
| 8961 | public: |
| 8962 | InheritingConstructorInfo(Sema &SemaRef, CXXRecordDecl *Derived) |
| 8963 | : SemaRef(SemaRef), Derived(Derived) { |
| 8964 | // Mark the constructors that we already have in the derived class. |
| 8965 | // |
| 8966 | // C++11 [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 8967 | // unless there is a user-declared constructor with the same signature in |
| 8968 | // the class where the using-declaration appears. |
| 8969 | visitAll(Derived, &InheritingConstructorInfo::noteDeclaredInDerived); |
| 8970 | } |
| 8971 | |
| 8972 | void inheritAll(CXXRecordDecl *RD) { |
| 8973 | visitAll(RD, &InheritingConstructorInfo::inherit); |
| 8974 | } |
| 8975 | |
| 8976 | private: |
| 8977 | /// Information about an inheriting constructor. |
| 8978 | struct InheritingConstructor { |
| 8979 | InheritingConstructor() |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 8980 | : DeclaredInDerived(false), BaseCtor(nullptr), DerivedCtor(nullptr) {} |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8981 | |
| 8982 | /// If \c true, a constructor with this signature is already declared |
| 8983 | /// in the derived class. |
| 8984 | bool DeclaredInDerived; |
| 8985 | |
| 8986 | /// The constructor which is inherited. |
| 8987 | const CXXConstructorDecl *BaseCtor; |
| 8988 | |
| 8989 | /// The derived constructor we declared. |
| 8990 | CXXConstructorDecl *DerivedCtor; |
| 8991 | }; |
| 8992 | |
| 8993 | /// Inheriting constructors with a given canonical type. There can be at |
| 8994 | /// most one such non-template constructor, and any number of templated |
| 8995 | /// constructors. |
| 8996 | struct InheritingConstructorsForType { |
| 8997 | InheritingConstructor NonTemplate; |
Robert Wilhelm | e7205c0 | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8998 | SmallVector<std::pair<TemplateParameterList *, InheritingConstructor>, 4> |
| 8999 | Templates; |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9000 | |
| 9001 | InheritingConstructor &getEntry(Sema &S, const CXXConstructorDecl *Ctor) { |
| 9002 | if (FunctionTemplateDecl *FTD = Ctor->getDescribedFunctionTemplate()) { |
| 9003 | TemplateParameterList *ParamList = FTD->getTemplateParameters(); |
| 9004 | for (unsigned I = 0, N = Templates.size(); I != N; ++I) |
| 9005 | if (S.TemplateParameterListsAreEqual(ParamList, Templates[I].first, |
| 9006 | false, S.TPL_TemplateMatch)) |
| 9007 | return Templates[I].second; |
| 9008 | Templates.push_back(std::make_pair(ParamList, InheritingConstructor())); |
| 9009 | return Templates.back().second; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9010 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9011 | |
| 9012 | return NonTemplate; |
| 9013 | } |
| 9014 | }; |
| 9015 | |
| 9016 | /// Get or create the inheriting constructor record for a constructor. |
| 9017 | InheritingConstructor &getEntry(const CXXConstructorDecl *Ctor, |
| 9018 | QualType CtorType) { |
| 9019 | return Map[CtorType.getCanonicalType()->castAs<FunctionProtoType>()] |
| 9020 | .getEntry(SemaRef, Ctor); |
| 9021 | } |
| 9022 | |
| 9023 | typedef void (InheritingConstructorInfo::*VisitFn)(const CXXConstructorDecl*); |
| 9024 | |
| 9025 | /// Process all constructors for a class. |
| 9026 | void visitAll(const CXXRecordDecl *RD, VisitFn Callback) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9027 | for (const auto *Ctor : RD->ctors()) |
| 9028 | (this->*Callback)(Ctor); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9029 | for (CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> |
| 9030 | I(RD->decls_begin()), E(RD->decls_end()); |
| 9031 | I != E; ++I) { |
| 9032 | const FunctionDecl *FD = (*I)->getTemplatedDecl(); |
| 9033 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 9034 | (this->*Callback)(CD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9035 | } |
| 9036 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9037 | |
| 9038 | /// Note that a constructor (or constructor template) was declared in Derived. |
| 9039 | void noteDeclaredInDerived(const CXXConstructorDecl *Ctor) { |
| 9040 | getEntry(Ctor, Ctor->getType()).DeclaredInDerived = true; |
| 9041 | } |
| 9042 | |
| 9043 | /// Inherit a single constructor. |
| 9044 | void inherit(const CXXConstructorDecl *Ctor) { |
| 9045 | const FunctionProtoType *CtorType = |
| 9046 | Ctor->getType()->castAs<FunctionProtoType>(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9047 | ArrayRef<QualType> ArgTypes = CtorType->getParamTypes(); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9048 | FunctionProtoType::ExtProtoInfo EPI = CtorType->getExtProtoInfo(); |
| 9049 | |
| 9050 | SourceLocation UsingLoc = getUsingLoc(Ctor->getParent()); |
| 9051 | |
| 9052 | // Core issue (no number yet): the ellipsis is always discarded. |
| 9053 | if (EPI.Variadic) { |
| 9054 | SemaRef.Diag(UsingLoc, diag::warn_using_decl_constructor_ellipsis); |
| 9055 | SemaRef.Diag(Ctor->getLocation(), |
| 9056 | diag::note_using_decl_constructor_ellipsis); |
| 9057 | EPI.Variadic = false; |
| 9058 | } |
| 9059 | |
| 9060 | // Declare a constructor for each number of parameters. |
| 9061 | // |
| 9062 | // C++11 [class.inhctor]p1: |
| 9063 | // The candidate set of inherited constructors from the class X named in |
| 9064 | // the using-declaration consists of [... modulo defects ...] for each |
| 9065 | // constructor or constructor template of X, the set of constructors or |
| 9066 | // constructor templates that results from omitting any ellipsis parameter |
| 9067 | // specification and successively omitting parameters with a default |
| 9068 | // argument from the end of the parameter-type-list |
Richard Smith | 987c030 | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 9069 | unsigned MinParams = minParamsToInherit(Ctor); |
| 9070 | unsigned Params = Ctor->getNumParams(); |
| 9071 | if (Params >= MinParams) { |
| 9072 | do |
| 9073 | declareCtor(UsingLoc, Ctor, |
| 9074 | SemaRef.Context.getFunctionType( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9075 | Ctor->getReturnType(), ArgTypes.slice(0, Params), EPI)); |
Richard Smith | 987c030 | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 9076 | while (Params > MinParams && |
| 9077 | Ctor->getParamDecl(--Params)->hasDefaultArg()); |
| 9078 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9079 | } |
| 9080 | |
| 9081 | /// Find the using-declaration which specified that we should inherit the |
| 9082 | /// constructors of \p Base. |
| 9083 | SourceLocation getUsingLoc(const CXXRecordDecl *Base) { |
| 9084 | // No fancy lookup required; just look for the base constructor name |
| 9085 | // directly within the derived class. |
| 9086 | ASTContext &Context = SemaRef.Context; |
| 9087 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 9088 | Context.getCanonicalType(Context.getRecordType(Base))); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 9089 | DeclContext::lookup_result Decls = Derived->lookup(Name); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9090 | return Decls.empty() ? Derived->getLocation() : Decls[0]->getLocation(); |
| 9091 | } |
| 9092 | |
| 9093 | unsigned minParamsToInherit(const CXXConstructorDecl *Ctor) { |
| 9094 | // C++11 [class.inhctor]p3: |
| 9095 | // [F]or each constructor template in the candidate set of inherited |
| 9096 | // constructors, a constructor template is implicitly declared |
| 9097 | if (Ctor->getDescribedFunctionTemplate()) |
| 9098 | return 0; |
| 9099 | |
| 9100 | // For each non-template constructor in the candidate set of inherited |
| 9101 | // constructors other than a constructor having no parameters or a |
| 9102 | // copy/move constructor having a single parameter, a constructor is |
| 9103 | // implicitly declared [...] |
| 9104 | if (Ctor->getNumParams() == 0) |
| 9105 | return 1; |
| 9106 | if (Ctor->isCopyOrMoveConstructor()) |
| 9107 | return 2; |
| 9108 | |
| 9109 | // Per discussion on core reflector, never inherit a constructor which |
| 9110 | // would become a default, copy, or move constructor of Derived either. |
| 9111 | const ParmVarDecl *PD = Ctor->getParamDecl(0); |
| 9112 | const ReferenceType *RT = PD->getType()->getAs<ReferenceType>(); |
| 9113 | return (RT && RT->getPointeeCXXRecordDecl() == Derived) ? 2 : 1; |
| 9114 | } |
| 9115 | |
| 9116 | /// Declare a single inheriting constructor, inheriting the specified |
| 9117 | /// constructor, with the given type. |
| 9118 | void declareCtor(SourceLocation UsingLoc, const CXXConstructorDecl *BaseCtor, |
| 9119 | QualType DerivedType) { |
| 9120 | InheritingConstructor &Entry = getEntry(BaseCtor, DerivedType); |
| 9121 | |
| 9122 | // C++11 [class.inhctor]p3: |
| 9123 | // ... a constructor is implicitly declared with the same constructor |
| 9124 | // characteristics unless there is a user-declared constructor with |
| 9125 | // the same signature in the class where the using-declaration appears |
| 9126 | if (Entry.DeclaredInDerived) |
| 9127 | return; |
| 9128 | |
| 9129 | // C++11 [class.inhctor]p7: |
| 9130 | // If two using-declarations declare inheriting constructors with the |
| 9131 | // same signature, the program is ill-formed |
| 9132 | if (Entry.DerivedCtor) { |
| 9133 | if (BaseCtor->getParent() != Entry.BaseCtor->getParent()) { |
| 9134 | // Only diagnose this once per constructor. |
| 9135 | if (Entry.DerivedCtor->isInvalidDecl()) |
| 9136 | return; |
| 9137 | Entry.DerivedCtor->setInvalidDecl(); |
| 9138 | |
| 9139 | SemaRef.Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 9140 | SemaRef.Diag(BaseCtor->getLocation(), |
| 9141 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 9142 | SemaRef.Diag(Entry.BaseCtor->getLocation(), |
| 9143 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 9144 | SemaRef.Diag(Entry.DerivedCtor->getLocation(), |
| 9145 | diag::note_using_decl_constructor_conflict_previous_using); |
| 9146 | } else { |
| 9147 | // Core issue (no number): if the same inheriting constructor is |
| 9148 | // produced by multiple base class constructors from the same base |
| 9149 | // class, the inheriting constructor is defined as deleted. |
| 9150 | SemaRef.SetDeclDeleted(Entry.DerivedCtor, UsingLoc); |
| 9151 | } |
| 9152 | |
| 9153 | return; |
| 9154 | } |
| 9155 | |
| 9156 | ASTContext &Context = SemaRef.Context; |
| 9157 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 9158 | Context.getCanonicalType(Context.getRecordType(Derived))); |
| 9159 | DeclarationNameInfo NameInfo(Name, UsingLoc); |
| 9160 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9161 | TemplateParameterList *TemplateParams = nullptr; |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9162 | if (const FunctionTemplateDecl *FTD = |
| 9163 | BaseCtor->getDescribedFunctionTemplate()) { |
| 9164 | TemplateParams = FTD->getTemplateParameters(); |
| 9165 | // We're reusing template parameters from a different DeclContext. This |
| 9166 | // is questionable at best, but works out because the template depth in |
| 9167 | // both places is guaranteed to be 0. |
| 9168 | // FIXME: Rebuild the template parameters in the new context, and |
| 9169 | // transform the function type to refer to them. |
| 9170 | } |
| 9171 | |
| 9172 | // Build type source info pointing at the using-declaration. This is |
| 9173 | // required by template instantiation. |
| 9174 | TypeSourceInfo *TInfo = |
| 9175 | Context.getTrivialTypeSourceInfo(DerivedType, UsingLoc); |
| 9176 | FunctionProtoTypeLoc ProtoLoc = |
| 9177 | TInfo->getTypeLoc().IgnoreParens().castAs<FunctionProtoTypeLoc>(); |
| 9178 | |
| 9179 | CXXConstructorDecl *DerivedCtor = CXXConstructorDecl::Create( |
| 9180 | Context, Derived, UsingLoc, NameInfo, DerivedType, |
| 9181 | TInfo, BaseCtor->isExplicit(), /*Inline=*/true, |
| 9182 | /*ImplicitlyDeclared=*/true, /*Constexpr=*/BaseCtor->isConstexpr()); |
| 9183 | |
| 9184 | // Build an unevaluated exception specification for this constructor. |
| 9185 | const FunctionProtoType *FPT = DerivedType->castAs<FunctionProtoType>(); |
| 9186 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9187 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 9188 | EPI.ExceptionSpec.SourceDecl = DerivedCtor; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9189 | DerivedCtor->setType(Context.getFunctionType(FPT->getReturnType(), |
| 9190 | FPT->getParamTypes(), EPI)); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9191 | |
| 9192 | // Build the parameter declarations. |
| 9193 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9194 | for (unsigned I = 0, N = FPT->getNumParams(); I != N; ++I) { |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9195 | TypeSourceInfo *TInfo = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9196 | Context.getTrivialTypeSourceInfo(FPT->getParamType(I), UsingLoc); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9197 | ParmVarDecl *PD = ParmVarDecl::Create( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9198 | Context, DerivedCtor, UsingLoc, UsingLoc, /*IdentifierInfo=*/nullptr, |
| 9199 | FPT->getParamType(I), TInfo, SC_None, /*DefaultArg=*/nullptr); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9200 | PD->setScopeInfo(0, I); |
| 9201 | PD->setImplicit(); |
| 9202 | ParamDecls.push_back(PD); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9203 | ProtoLoc.setParam(I, PD); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9204 | } |
| 9205 | |
| 9206 | // Set up the new constructor. |
| 9207 | DerivedCtor->setAccess(BaseCtor->getAccess()); |
| 9208 | DerivedCtor->setParams(ParamDecls); |
| 9209 | DerivedCtor->setInheritedConstructor(BaseCtor); |
| 9210 | if (BaseCtor->isDeleted()) |
| 9211 | SemaRef.SetDeclDeleted(DerivedCtor, UsingLoc); |
| 9212 | |
| 9213 | // If this is a constructor template, build the template declaration. |
| 9214 | if (TemplateParams) { |
| 9215 | FunctionTemplateDecl *DerivedTemplate = |
| 9216 | FunctionTemplateDecl::Create(SemaRef.Context, Derived, UsingLoc, Name, |
| 9217 | TemplateParams, DerivedCtor); |
| 9218 | DerivedTemplate->setAccess(BaseCtor->getAccess()); |
| 9219 | DerivedCtor->setDescribedFunctionTemplate(DerivedTemplate); |
| 9220 | Derived->addDecl(DerivedTemplate); |
| 9221 | } else { |
| 9222 | Derived->addDecl(DerivedCtor); |
| 9223 | } |
| 9224 | |
| 9225 | Entry.BaseCtor = BaseCtor; |
| 9226 | Entry.DerivedCtor = DerivedCtor; |
| 9227 | } |
| 9228 | |
| 9229 | Sema &SemaRef; |
| 9230 | CXXRecordDecl *Derived; |
| 9231 | typedef llvm::DenseMap<const Type *, InheritingConstructorsForType> MapType; |
| 9232 | MapType Map; |
| 9233 | }; |
| 9234 | } |
| 9235 | |
| 9236 | void Sema::DeclareInheritingConstructors(CXXRecordDecl *ClassDecl) { |
| 9237 | // Defer declaring the inheriting constructors until the class is |
| 9238 | // instantiated. |
| 9239 | if (ClassDecl->isDependentContext()) |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9240 | return; |
| 9241 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9242 | // Find base classes from which we might inherit constructors. |
| 9243 | SmallVector<CXXRecordDecl*, 4> InheritedBases; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9244 | for (const auto &BaseIt : ClassDecl->bases()) |
| 9245 | if (BaseIt.getInheritConstructors()) |
| 9246 | InheritedBases.push_back(BaseIt.getType()->getAsCXXRecordDecl()); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 9247 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9248 | // Go no further if we're not inheriting any constructors. |
| 9249 | if (InheritedBases.empty()) |
| 9250 | return; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9251 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 9252 | // Declare the inherited constructors. |
| 9253 | InheritingConstructorInfo ICI(*this, ClassDecl); |
| 9254 | for (unsigned I = 0, N = InheritedBases.size(); I != N; ++I) |
| 9255 | ICI.inheritAll(InheritedBases[I]); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 9256 | } |
| 9257 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 9258 | void Sema::DefineInheritingConstructor(SourceLocation CurrentLocation, |
| 9259 | CXXConstructorDecl *Constructor) { |
| 9260 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 9261 | assert(Constructor->getInheritedConstructor() && |
| 9262 | !Constructor->doesThisDeclarationHaveABody() && |
| 9263 | !Constructor->isDeleted()); |
| 9264 | |
| 9265 | SynthesizedFunctionScope Scope(*this, Constructor); |
| 9266 | DiagnosticErrorTrap Trap(Diags); |
| 9267 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
| 9268 | Trap.hasErrorOccurred()) { |
| 9269 | Diag(CurrentLocation, diag::note_inhctor_synthesized_at) |
| 9270 | << Context.getTagDeclType(ClassDecl); |
| 9271 | Constructor->setInvalidDecl(); |
| 9272 | return; |
| 9273 | } |
| 9274 | |
| 9275 | SourceLocation Loc = Constructor->getLocation(); |
| 9276 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
| 9277 | |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 9278 | Constructor->markUsed(Context); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 9279 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 9280 | |
| 9281 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9282 | L->CompletedImplicitDefinition(Constructor); |
| 9283 | } |
| 9284 | } |
| 9285 | |
| 9286 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9287 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9288 | Sema::ComputeDefaultedDtorExceptionSpec(CXXMethodDecl *MD) { |
| 9289 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9290 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9291 | // C++ [except.spec]p14: |
| 9292 | // An implicitly declared special member function (Clause 12) shall have |
| 9293 | // an exception-specification. |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9294 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 9295 | if (ClassDecl->isInvalidDecl()) |
| 9296 | return ExceptSpec; |
| 9297 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9298 | // Direct base-class destructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9299 | for (const auto &B : ClassDecl->bases()) { |
| 9300 | if (B.isVirtual()) // Handled below. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9301 | continue; |
| 9302 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9303 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) |
| 9304 | ExceptSpec.CalledDecl(B.getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9305 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9306 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9307 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9308 | // Virtual base-class destructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9309 | for (const auto &B : ClassDecl->vbases()) { |
| 9310 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) |
| 9311 | ExceptSpec.CalledDecl(B.getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9312 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9313 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9314 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9315 | // Field destructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9316 | for (const auto *F : ClassDecl->fields()) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9317 | if (const RecordType *RecordTy |
| 9318 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9319 | ExceptSpec.CalledDecl(F->getLocation(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9320 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9321 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9322 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9323 | return ExceptSpec; |
| 9324 | } |
| 9325 | |
| 9326 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 9327 | // C++ [class.dtor]p2: |
| 9328 | // If a class has no user-declared destructor, a destructor is |
| 9329 | // declared implicitly. An implicitly-declared destructor is an |
| 9330 | // inline public member of its class. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9331 | assert(ClassDecl->needsImplicitDestructor()); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9332 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9333 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDestructor); |
| 9334 | if (DSM.isAlreadyBeingDeclared()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9335 | return nullptr; |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9336 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 9337 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9338 | CanQualType ClassType |
| 9339 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9340 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9341 | DeclarationName Name |
| 9342 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9343 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9344 | CXXDestructorDecl *Destructor |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9345 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9346 | QualType(), nullptr, /*isInline=*/true, |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9347 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9348 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9349 | Destructor->setDefaulted(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9350 | |
| 9351 | if (getLangOpts().CUDA) { |
| 9352 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXDestructor, |
| 9353 | Destructor, |
| 9354 | /* ConstRHS */ false, |
| 9355 | /* Diagnose */ false); |
| 9356 | } |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9357 | |
| 9358 | // Build an exception specification pointing back at this destructor. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9359 | FunctionProtoType::ExtProtoInfo EPI = getImplicitMethodEPI(*this, Destructor); |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 9360 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9361 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9362 | AddOverriddenMethods(ClassDecl, Destructor); |
| 9363 | |
| 9364 | // We don't need to use SpecialMemberIsTrivial here; triviality for |
| 9365 | // destructors is easy to compute. |
| 9366 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
| 9367 | |
| 9368 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 9369 | SetDeclDeleted(Destructor, ClassLoc); |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9370 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 9371 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 9372 | ++ASTContext::NumImplicitDestructorsDeclared; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9373 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 9374 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 9375 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 9376 | PushOnScopeChains(Destructor, S, false); |
| 9377 | ClassDecl->addDecl(Destructor); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9378 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 9379 | return Destructor; |
| 9380 | } |
| 9381 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9382 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 9383 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 9384 | assert((Destructor->isDefaulted() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9385 | !Destructor->doesThisDeclarationHaveABody() && |
| 9386 | !Destructor->isDeleted()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9387 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 9388 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9389 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9390 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9391 | if (Destructor->isInvalidDecl()) |
| 9392 | return; |
| 9393 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9394 | SynthesizedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9395 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 9396 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 9397 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 9398 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9399 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9400 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 9401 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9402 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 9403 | |
| 9404 | Destructor->setInvalidDecl(); |
| 9405 | return; |
| 9406 | } |
| 9407 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9408 | // The exception specification is needed because we are defining the |
| 9409 | // function. |
| 9410 | ResolveExceptionSpec(CurrentLocation, |
| 9411 | Destructor->getType()->castAs<FunctionProtoType>()); |
| 9412 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9413 | SourceLocation Loc = Destructor->getLocEnd().isValid() |
| 9414 | ? Destructor->getLocEnd() |
| 9415 | : Destructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 9416 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 9417 | Destructor->markUsed(Context); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9418 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9419 | |
| 9420 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9421 | L->CompletedImplicitDefinition(Destructor); |
| 9422 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9423 | } |
| 9424 | |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 9425 | /// \brief Perform any semantic analysis which needs to be delayed until all |
| 9426 | /// pending class member declarations have been parsed. |
| 9427 | void Sema::ActOnFinishCXXMemberDecls() { |
Douglas Gregor | 1031884 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 9428 | // If the context is an invalid C++ class, just suppress these checks. |
| 9429 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 9430 | if (Record->isInvalidDecl()) { |
Alp Toker | 0823566 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 9431 | DelayedDefaultedMemberExceptionSpecs.clear(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 9432 | DelayedExceptionSpecChecks.clear(); |
Douglas Gregor | 1031884 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 9433 | return; |
| 9434 | } |
| 9435 | } |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 9436 | } |
| 9437 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 9438 | static void getDefaultArgExprsForConstructors(Sema &S, CXXRecordDecl *Class) { |
| 9439 | // Don't do anything for template patterns. |
| 9440 | if (Class->getDescribedClassTemplate()) |
| 9441 | return; |
| 9442 | |
| 9443 | for (Decl *Member : Class->decls()) { |
| 9444 | auto *CD = dyn_cast<CXXConstructorDecl>(Member); |
| 9445 | if (!CD) { |
| 9446 | // Recurse on nested classes. |
| 9447 | if (auto *NestedRD = dyn_cast<CXXRecordDecl>(Member)) |
| 9448 | getDefaultArgExprsForConstructors(S, NestedRD); |
| 9449 | continue; |
| 9450 | } else if (!CD->isDefaultConstructor() || !CD->hasAttr<DLLExportAttr>()) { |
| 9451 | continue; |
| 9452 | } |
| 9453 | |
| 9454 | for (unsigned I = 0, E = CD->getNumParams(); I != E; ++I) { |
| 9455 | // Skip any default arguments that we've already instantiated. |
| 9456 | if (S.Context.getDefaultArgExprForConstructor(CD, I)) |
| 9457 | continue; |
| 9458 | |
| 9459 | Expr *DefaultArg = S.BuildCXXDefaultArgExpr(Class->getLocation(), CD, |
| 9460 | CD->getParamDecl(I)).get(); |
| 9461 | S.Context.addDefaultArgExprForConstructor(CD, I, DefaultArg); |
| 9462 | } |
| 9463 | } |
| 9464 | } |
| 9465 | |
| 9466 | void Sema::ActOnFinishCXXMemberDefaultArgs(Decl *D) { |
| 9467 | auto *RD = dyn_cast<CXXRecordDecl>(D); |
| 9468 | |
| 9469 | // Default constructors that are annotated with __declspec(dllexport) which |
| 9470 | // have default arguments or don't use the standard calling convention are |
| 9471 | // wrapped with a thunk called the default constructor closure. |
| 9472 | if (RD && Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 9473 | getDefaultArgExprsForConstructors(*this, RD); |
| 9474 | } |
| 9475 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9476 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *ClassDecl, |
| 9477 | CXXDestructorDecl *Destructor) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9478 | assert(getLangOpts().CPlusPlus11 && |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9479 | "adjusting dtor exception specs was introduced in c++11"); |
| 9480 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9481 | // C++11 [class.dtor]p3: |
| 9482 | // A declaration of a destructor that does not have an exception- |
| 9483 | // specification is implicitly considered to have the same exception- |
| 9484 | // specification as an implicit declaration. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9485 | const FunctionProtoType *DtorType = Destructor->getType()-> |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9486 | getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9487 | if (DtorType->hasExceptionSpec()) |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9488 | return; |
| 9489 | |
Chandler Carruth | 3f224b2 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 9490 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 9491 | // the only thing of interest in the destructor type is its extended info. |
| 9492 | // The return and arguments are fixed. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9493 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9494 | EPI.ExceptionSpec.Type = EST_Unevaluated; |
| 9495 | EPI.ExceptionSpec.SourceDecl = Destructor; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 9496 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 9497 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9498 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 9499 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 9500 | // change in behavior can break conforming C++03 programs at runtime. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9501 | // However, we don't have a body or an exception specification yet, so it |
| 9502 | // needs to be done somewhere else. |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 9503 | } |
| 9504 | |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9505 | namespace { |
| 9506 | /// \brief An abstract base class for all helper classes used in building the |
| 9507 | // copy/move operators. These classes serve as factory functions and help us |
| 9508 | // avoid using the same Expr* in the AST twice. |
| 9509 | class ExprBuilder { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 9510 | ExprBuilder(const ExprBuilder&) = delete; |
| 9511 | ExprBuilder &operator=(const ExprBuilder&) = delete; |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9512 | |
| 9513 | protected: |
| 9514 | static Expr *assertNotNull(Expr *E) { |
| 9515 | assert(E && "Expression construction must not fail."); |
| 9516 | return E; |
| 9517 | } |
| 9518 | |
| 9519 | public: |
| 9520 | ExprBuilder() {} |
| 9521 | virtual ~ExprBuilder() {} |
| 9522 | |
| 9523 | virtual Expr *build(Sema &S, SourceLocation Loc) const = 0; |
| 9524 | }; |
| 9525 | |
| 9526 | class RefBuilder: public ExprBuilder { |
| 9527 | VarDecl *Var; |
| 9528 | QualType VarType; |
| 9529 | |
| 9530 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9531 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9532 | return assertNotNull(S.BuildDeclRefExpr(Var, VarType, VK_LValue, Loc).get()); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9533 | } |
| 9534 | |
| 9535 | RefBuilder(VarDecl *Var, QualType VarType) |
| 9536 | : Var(Var), VarType(VarType) {} |
| 9537 | }; |
| 9538 | |
| 9539 | class ThisBuilder: public ExprBuilder { |
| 9540 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9541 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9542 | return assertNotNull(S.ActOnCXXThis(Loc).getAs<Expr>()); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9543 | } |
| 9544 | }; |
| 9545 | |
| 9546 | class CastBuilder: public ExprBuilder { |
| 9547 | const ExprBuilder &Builder; |
| 9548 | QualType Type; |
| 9549 | ExprValueKind Kind; |
| 9550 | const CXXCastPath &Path; |
| 9551 | |
| 9552 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9553 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9554 | return assertNotNull(S.ImpCastExprToType(Builder.build(S, Loc), Type, |
| 9555 | CK_UncheckedDerivedToBase, Kind, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9556 | &Path).get()); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9557 | } |
| 9558 | |
| 9559 | CastBuilder(const ExprBuilder &Builder, QualType Type, ExprValueKind Kind, |
| 9560 | const CXXCastPath &Path) |
| 9561 | : Builder(Builder), Type(Type), Kind(Kind), Path(Path) {} |
| 9562 | }; |
| 9563 | |
| 9564 | class DerefBuilder: public ExprBuilder { |
| 9565 | const ExprBuilder &Builder; |
| 9566 | |
| 9567 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9568 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9569 | return assertNotNull( |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9570 | S.CreateBuiltinUnaryOp(Loc, UO_Deref, Builder.build(S, Loc)).get()); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
| 9573 | DerefBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 9574 | }; |
| 9575 | |
| 9576 | class MemberBuilder: public ExprBuilder { |
| 9577 | const ExprBuilder &Builder; |
| 9578 | QualType Type; |
| 9579 | CXXScopeSpec SS; |
| 9580 | bool IsArrow; |
| 9581 | LookupResult &MemberLookup; |
| 9582 | |
| 9583 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9584 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9585 | return assertNotNull(S.BuildMemberReferenceExpr( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9586 | Builder.build(S, Loc), Type, Loc, IsArrow, SS, SourceLocation(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9587 | nullptr, MemberLookup, nullptr).get()); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9588 | } |
| 9589 | |
| 9590 | MemberBuilder(const ExprBuilder &Builder, QualType Type, bool IsArrow, |
| 9591 | LookupResult &MemberLookup) |
| 9592 | : Builder(Builder), Type(Type), IsArrow(IsArrow), |
| 9593 | MemberLookup(MemberLookup) {} |
| 9594 | }; |
| 9595 | |
| 9596 | class MoveCastBuilder: public ExprBuilder { |
| 9597 | const ExprBuilder &Builder; |
| 9598 | |
| 9599 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9600 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9601 | return assertNotNull(CastForMoving(S, Builder.build(S, Loc))); |
| 9602 | } |
| 9603 | |
| 9604 | MoveCastBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 9605 | }; |
| 9606 | |
| 9607 | class LvalueConvBuilder: public ExprBuilder { |
| 9608 | const ExprBuilder &Builder; |
| 9609 | |
| 9610 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9611 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9612 | return assertNotNull( |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9613 | S.DefaultLvalueConversion(Builder.build(S, Loc)).get()); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9614 | } |
| 9615 | |
| 9616 | LvalueConvBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 9617 | }; |
| 9618 | |
| 9619 | class SubscriptBuilder: public ExprBuilder { |
| 9620 | const ExprBuilder &Base; |
| 9621 | const ExprBuilder &Index; |
| 9622 | |
| 9623 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 9624 | Expr *build(Sema &S, SourceLocation Loc) const override { |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9625 | return assertNotNull(S.CreateBuiltinArraySubscriptExpr( |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9626 | Base.build(S, Loc), Loc, Index.build(S, Loc), Loc).get()); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9627 | } |
| 9628 | |
| 9629 | SubscriptBuilder(const ExprBuilder &Base, const ExprBuilder &Index) |
| 9630 | : Base(Base), Index(Index) {} |
| 9631 | }; |
| 9632 | |
| 9633 | } // end anonymous namespace |
| 9634 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9635 | /// When generating a defaulted copy or move assignment operator, if a field |
| 9636 | /// should be copied with __builtin_memcpy rather than via explicit assignments, |
| 9637 | /// do so. This optimization only applies for arrays of scalars, and for arrays |
| 9638 | /// of class type where the selected copy/move-assignment operator is trivial. |
| 9639 | static StmtResult |
| 9640 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9641 | const ExprBuilder &ToB, const ExprBuilder &FromB) { |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9642 | // Compute the size of the memory buffer to be copied. |
| 9643 | QualType SizeType = S.Context.getSizeType(); |
| 9644 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 9645 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 9646 | |
| 9647 | // Take the address of the field references for "from" and "to". We |
| 9648 | // directly construct UnaryOperators here because semantic analysis |
| 9649 | // does not permit us to take the address of an xvalue. |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9650 | Expr *From = FromB.build(S, Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9651 | From = new (S.Context) UnaryOperator(From, UO_AddrOf, |
| 9652 | S.Context.getPointerType(From->getType()), |
| 9653 | VK_RValue, OK_Ordinary, Loc); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9654 | Expr *To = ToB.build(S, Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9655 | To = new (S.Context) UnaryOperator(To, UO_AddrOf, |
| 9656 | S.Context.getPointerType(To->getType()), |
| 9657 | VK_RValue, OK_Ordinary, Loc); |
| 9658 | |
| 9659 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 9660 | bool NeedsCollectableMemCpy = |
| 9661 | E->isRecordType() && E->getAs<RecordType>()->getDecl()->hasObjectMember(); |
| 9662 | |
| 9663 | // Create a reference to the __builtin_objc_memmove_collectable function |
| 9664 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 9665 | "__builtin_objc_memmove_collectable" : |
| 9666 | "__builtin_memcpy"; |
| 9667 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 9668 | Sema::LookupOrdinaryName); |
| 9669 | S.LookupName(R, S.TUScope, true); |
| 9670 | |
| 9671 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 9672 | if (!MemCpy) |
| 9673 | // Something went horribly wrong earlier, and we will have complained |
| 9674 | // about it. |
| 9675 | return StmtError(); |
| 9676 | |
| 9677 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9678 | VK_RValue, Loc, nullptr); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9679 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 9680 | |
| 9681 | Expr *CallArgs[] = { |
| 9682 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 9683 | }; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9684 | ExprResult Call = S.ActOnCallExpr(/*Scope=*/nullptr, MemCpyRef.get(), |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9685 | Loc, CallArgs, Loc); |
| 9686 | |
| 9687 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9688 | return Call.getAs<Stmt>(); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9689 | } |
| 9690 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9691 | /// \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] | 9692 | /// \c To. |
| 9693 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9694 | /// This routine is used to copy/move the members of a class with an |
| 9695 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9696 | /// copied are arrays, this routine builds for loops to copy them. |
| 9697 | /// |
| 9698 | /// \param S The Sema object used for type-checking. |
| 9699 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9700 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9701 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9702 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 9703 | /// must have this type. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9704 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9705 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9706 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9707 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9708 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9709 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9710 | /// Otherwise, it's a non-static member subobject. |
| 9711 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9712 | /// \param Copying Whether we're copying or moving. |
| 9713 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9714 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 9715 | /// |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9716 | /// \returns A statement or a loop that copies the expressions, or StmtResult(0) |
| 9717 | /// if a memcpy should be used instead. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9718 | static StmtResult |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9719 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9720 | const ExprBuilder &To, const ExprBuilder &From, |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9721 | bool CopyingBaseSubobject, bool Copying, |
| 9722 | unsigned Depth = 0) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9723 | // C++11 [class.copy]p28: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9724 | // Each subobject is assigned in the manner appropriate to its type: |
| 9725 | // |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9726 | // - if the subobject is of class type, as if by a call to operator= with |
| 9727 | // the subobject as the object expression and the corresponding |
| 9728 | // subobject of x as a single function argument (as if by explicit |
| 9729 | // qualification; that is, ignoring any possible virtual overriding |
| 9730 | // functions in more derived classes); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9731 | // |
| 9732 | // C++03 [class.copy]p13: |
| 9733 | // - if the subobject is of class type, the copy assignment operator for |
| 9734 | // the class is used (as if by explicit qualification; that is, |
| 9735 | // ignoring any possible virtual overriding functions in more derived |
| 9736 | // classes); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9737 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 9738 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9739 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9740 | // Look for operator=. |
| 9741 | DeclarationName Name |
| 9742 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 9743 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 9744 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9745 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9746 | // Prior to C++11, filter out any result that isn't a copy/move-assignment |
| 9747 | // operator. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9748 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9749 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 9750 | while (F.hasNext()) { |
| 9751 | NamedDecl *D = F.next(); |
| 9752 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 9753 | if (Method->isCopyAssignmentOperator() || |
| 9754 | (!Copying && Method->isMoveAssignmentOperator())) |
| 9755 | continue; |
| 9756 | |
| 9757 | F.erase(); |
| 9758 | } |
| 9759 | F.done(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 9760 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9761 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9762 | // Suppress the protected check (C++ [class.protected]) for each of the |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9763 | // assignment operators we found. This strange dance is required when |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9764 | // we're assigning via a base classes's copy-assignment operator. To |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9765 | // ensure that we're getting the right base class subobject (without |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9766 | // ambiguities), we need to cast "this" to that subobject type; to |
| 9767 | // ensure that we don't go through the virtual call mechanism, we need |
| 9768 | // to qualify the operator= name with the base class (see below). However, |
| 9769 | // this means that if the base class has a protected copy assignment |
| 9770 | // operator, the protected member access check will fail. So, we |
| 9771 | // rewrite "protected" access to "public" access in this case, since we |
| 9772 | // know by construction that we're calling from a derived class. |
| 9773 | if (CopyingBaseSubobject) { |
| 9774 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 9775 | L != LEnd; ++L) { |
| 9776 | if (L.getAccess() == AS_protected) |
| 9777 | L.setAccess(AS_public); |
| 9778 | } |
| 9779 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9780 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9781 | // Create the nested-name-specifier that will be used to qualify the |
| 9782 | // reference to operator=; this is required to suppress the virtual |
| 9783 | // call mechanism. |
| 9784 | CXXScopeSpec SS; |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 9785 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9786 | SS.MakeTrivial(S.Context, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9787 | NestedNameSpecifier::Create(S.Context, nullptr, false, |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 9788 | CanonicalT), |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 9789 | Loc); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9790 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9791 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9792 | ExprResult OpEqualRef |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9793 | = S.BuildMemberReferenceExpr(To.build(S, Loc), T, Loc, /*isArrow=*/false, |
| 9794 | SS, /*TemplateKWLoc=*/SourceLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9795 | /*FirstQualifierInScope=*/nullptr, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9796 | OpLookup, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9797 | /*TemplateArgs=*/nullptr, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9798 | /*SuppressQualifierCheck=*/true); |
| 9799 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9800 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9801 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9802 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9803 | |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9804 | Expr *FromInst = From.build(S, Loc); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9805 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/nullptr, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9806 | OpEqualRef.getAs<Expr>(), |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9807 | Loc, FromInst, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9808 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9809 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9810 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9811 | // If we built a call to a trivial 'operator=' while copying an array, |
| 9812 | // bail out. We'll replace the whole shebang with a memcpy. |
| 9813 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 9814 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9815 | return StmtResult((Stmt*)nullptr); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9816 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9817 | // Convert to an expression-statement, and clean up any produced |
| 9818 | // temporaries. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9819 | return S.ActOnExprStmt(Call); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9820 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 9821 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9822 | // - if the subobject is of scalar type, the built-in assignment |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9823 | // operator is used. |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9824 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9825 | if (!ArrayTy) { |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9826 | ExprResult Assignment = S.CreateBuiltinBinOp( |
| 9827 | Loc, BO_Assign, To.build(S, Loc), From.build(S, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9828 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9829 | return StmtError(); |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9830 | return S.ActOnExprStmt(Assignment); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9831 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9832 | |
| 9833 | // - if the subobject is an array, each element is assigned, in the |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9834 | // manner appropriate to the element type; |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9835 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9836 | // Construct a loop over the array bounds, e.g., |
| 9837 | // |
| 9838 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 9839 | // |
| 9840 | // that will copy each of the array elements. |
| 9841 | QualType SizeType = S.Context.getSizeType(); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9842 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9843 | // Create the iteration variable. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9844 | IdentifierInfo *IterationVarName = nullptr; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9845 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9846 | SmallString<8> Str; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9847 | llvm::raw_svector_ostream OS(Str); |
| 9848 | OS << "__i" << Depth; |
| 9849 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 9850 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9851 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9852 | IterationVarName, SizeType, |
| 9853 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9854 | SC_None); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9855 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9856 | // Initialize the iteration variable to zero. |
| 9857 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 9858 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9859 | |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9860 | // Creates a reference to the iteration variable. |
| 9861 | RefBuilder IterationVarRef(IterationVar, SizeType); |
| 9862 | LvalueConvBuilder IterationVarRefRVal(IterationVarRef); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 9863 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9864 | // Create the DeclStmt that holds the iteration variable. |
| 9865 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9866 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9867 | // Subscript the "from" and "to" expressions with the iteration variable. |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9868 | SubscriptBuilder FromIndexCopy(From, IterationVarRefRVal); |
| 9869 | MoveCastBuilder FromIndexMove(FromIndexCopy); |
| 9870 | const ExprBuilder *FromIndex; |
| 9871 | if (Copying) |
| 9872 | FromIndex = &FromIndexCopy; |
| 9873 | else |
| 9874 | FromIndex = &FromIndexMove; |
| 9875 | |
| 9876 | SubscriptBuilder ToIndex(To, IterationVarRefRVal); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9877 | |
| 9878 | // Build the copy/move for an individual element of the array. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9879 | StmtResult Copy = |
| 9880 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9881 | ToIndex, *FromIndex, CopyingBaseSubobject, |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9882 | Copying, Depth + 1); |
| 9883 | // Bail out if copying fails or if we determined that we should use memcpy. |
| 9884 | if (Copy.isInvalid() || !Copy.get()) |
| 9885 | return Copy; |
| 9886 | |
| 9887 | // Create the comparison against the array bound. |
| 9888 | llvm::APInt Upper |
| 9889 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 9890 | Expr *Comparison |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9891 | = new (S.Context) BinaryOperator(IterationVarRefRVal.build(S, Loc), |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9892 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 9893 | BO_NE, S.Context.BoolTy, |
| 9894 | VK_RValue, OK_Ordinary, Loc, false); |
| 9895 | |
| 9896 | // Create the pre-increment of the iteration variable. |
| 9897 | Expr *Increment |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9898 | = new (S.Context) UnaryOperator(IterationVarRef.build(S, Loc), UO_PreInc, |
| 9899 | SizeType, VK_LValue, OK_Ordinary, Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9900 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9901 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9902 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9903 | S.MakeFullExpr(Comparison), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9904 | nullptr, S.MakeFullDiscardedValueExpr(Increment), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 9905 | Loc, Copy.get()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9906 | } |
| 9907 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9908 | static StmtResult |
| 9909 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9910 | const ExprBuilder &To, const ExprBuilder &From, |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9911 | bool CopyingBaseSubobject, bool Copying) { |
| 9912 | // Maybe we should use a memcpy? |
| 9913 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 9914 | T.isTriviallyCopyableType(S.Context)) |
| 9915 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 9916 | |
| 9917 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 9918 | CopyingBaseSubobject, |
| 9919 | Copying, 0)); |
| 9920 | |
| 9921 | // If we ended up picking a trivial assignment operator for an array of a |
| 9922 | // non-trivially-copyable class type, just emit a memcpy. |
| 9923 | if (!Result.isInvalid() && !Result.get()) |
| 9924 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 9925 | |
| 9926 | return Result; |
| 9927 | } |
| 9928 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9929 | Sema::ImplicitExceptionSpecification |
| 9930 | Sema::ComputeDefaultedCopyAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 9931 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9932 | |
| 9933 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 9934 | if (ClassDecl->isInvalidDecl()) |
| 9935 | return ExceptSpec; |
| 9936 | |
| 9937 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9938 | assert(T->getNumParams() == 1 && "not a copy assignment op"); |
| 9939 | unsigned ArgQuals = |
| 9940 | T->getParamType(0).getNonReferenceType().getCVRQualifiers(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9941 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9942 | // C++ [except.spec]p14: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9943 | // An implicitly declared special member function (Clause 12) shall have an |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9944 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9945 | |
| 9946 | // It is unspecified whether or not an implicit copy assignment operator |
| 9947 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 9948 | // made. As such, this exception specification is effectively unspecified. |
| 9949 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 9950 | // the side of assuming such calls to be made regardless of whether they |
| 9951 | // actually happen. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9952 | for (const auto &Base : ClassDecl->bases()) { |
| 9953 | if (Base.isVirtual()) |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9954 | continue; |
| 9955 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 9956 | CXXRecordDecl *BaseClassDecl |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9957 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9958 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 9959 | ArgQuals, false, 0)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9960 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyAssign); |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9961 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9962 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9963 | for (const auto &Base : ClassDecl->vbases()) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9964 | CXXRecordDecl *BaseClassDecl |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9965 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9966 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 9967 | ArgQuals, false, 0)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9968 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyAssign); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9969 | } |
| 9970 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 9971 | for (const auto *Field : ClassDecl->fields()) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9972 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9973 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 9974 | if (CXXMethodDecl *CopyAssign = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9975 | LookupCopyingAssignment(FieldClassDecl, |
| 9976 | ArgQuals | FieldType.getCVRQualifiers(), |
| 9977 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9978 | ExceptSpec.CalledDecl(Field->getLocation(), CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 9979 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9980 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9981 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9982 | return ExceptSpec; |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9983 | } |
| 9984 | |
| 9985 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 9986 | // Note: The following rules are largely analoguous to the copy |
| 9987 | // constructor rules. Note that virtual bases are not taken into account |
| 9988 | // for determining the argument type of the operator. Note also that |
| 9989 | // operators taking an object instead of a reference are allowed. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9990 | assert(ClassDecl->needsImplicitCopyAssignment()); |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9991 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9992 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyAssignment); |
| 9993 | if (DSM.isAlreadyBeingDeclared()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 9994 | return nullptr; |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9995 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9996 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 9997 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9998 | bool Const = ClassDecl->implicitCopyAssignmentHasConstParam(); |
| 9999 | if (Const) |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 10000 | ArgType = ArgType.withConst(); |
| 10001 | ArgType = Context.getLValueReferenceType(ArgType); |
| 10002 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 10003 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 10004 | CXXCopyAssignment, |
| 10005 | Const); |
| 10006 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 10007 | // An implicitly-declared copy assignment operator is an inline public |
| 10008 | // member of its class. |
| 10009 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10010 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 10011 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 10012 | CXXMethodDecl *CopyAssignment = |
| 10013 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10014 | /*TInfo=*/nullptr, /*StorageClass=*/SC_None, |
| 10015 | /*isInline=*/true, Constexpr, SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 10016 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 10017 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 10018 | CopyAssignment->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10019 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10020 | if (getLangOpts().CUDA) { |
| 10021 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXCopyAssignment, |
| 10022 | CopyAssignment, |
| 10023 | /* ConstRHS */ Const, |
| 10024 | /* Diagnose */ false); |
| 10025 | } |
| 10026 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10027 | // Build an exception specification pointing back at this member. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10028 | FunctionProtoType::ExtProtoInfo EPI = |
| 10029 | getImplicitMethodEPI(*this, CopyAssignment); |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 10030 | CopyAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10031 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 10032 | // Add the parameter to the operator. |
| 10033 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10034 | ClassLoc, ClassLoc, |
| 10035 | /*Id=*/nullptr, ArgType, |
| 10036 | /*TInfo=*/nullptr, SC_None, |
| 10037 | nullptr); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 10038 | CopyAssignment->setParams(FromParam); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 10039 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10040 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 10041 | |
| 10042 | CopyAssignment->setTrivial( |
| 10043 | ClassDecl->needsOverloadResolutionForCopyAssignment() |
| 10044 | ? SpecialMemberIsTrivial(CopyAssignment, CXXCopyAssignment) |
| 10045 | : ClassDecl->hasTrivialCopyAssignment()); |
| 10046 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10047 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 10048 | SetDeclDeleted(CopyAssignment, ClassLoc); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10049 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10050 | // Note that we have added this copy-assignment operator. |
| 10051 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 10052 | |
| 10053 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 10054 | PushOnScopeChains(CopyAssignment, S, false); |
| 10055 | ClassDecl->addDecl(CopyAssignment); |
| 10056 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 10057 | return CopyAssignment; |
| 10058 | } |
| 10059 | |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10060 | /// Diagnose an implicit copy operation for a class which is odr-used, but |
| 10061 | /// which is deprecated because the class has a user-declared copy constructor, |
| 10062 | /// copy assignment operator, or destructor. |
| 10063 | static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp, |
| 10064 | SourceLocation UseLoc) { |
| 10065 | assert(CopyOp->isImplicit()); |
| 10066 | |
| 10067 | CXXRecordDecl *RD = CopyOp->getParent(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10068 | CXXMethodDecl *UserDeclaredOperation = nullptr; |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10069 | |
| 10070 | // In Microsoft mode, assignment operations don't affect constructors and |
| 10071 | // vice versa. |
| 10072 | if (RD->hasUserDeclaredDestructor()) { |
| 10073 | UserDeclaredOperation = RD->getDestructor(); |
| 10074 | } else if (!isa<CXXConstructorDecl>(CopyOp) && |
| 10075 | RD->hasUserDeclaredCopyConstructor() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10076 | !S.getLangOpts().MSVCCompat) { |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10077 | // Find any user-declared copy constructor. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10078 | for (auto *I : RD->ctors()) { |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10079 | if (I->isCopyConstructor()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10080 | UserDeclaredOperation = I; |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10081 | break; |
| 10082 | } |
| 10083 | } |
| 10084 | assert(UserDeclaredOperation); |
| 10085 | } else if (isa<CXXConstructorDecl>(CopyOp) && |
| 10086 | RD->hasUserDeclaredCopyAssignment() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10087 | !S.getLangOpts().MSVCCompat) { |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10088 | // Find any user-declared move assignment operator. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10089 | for (auto *I : RD->methods()) { |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10090 | if (I->isCopyAssignmentOperator()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10091 | UserDeclaredOperation = I; |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10092 | break; |
| 10093 | } |
| 10094 | } |
| 10095 | assert(UserDeclaredOperation); |
| 10096 | } |
| 10097 | |
| 10098 | if (UserDeclaredOperation) { |
| 10099 | S.Diag(UserDeclaredOperation->getLocation(), |
| 10100 | diag::warn_deprecated_copy_operation) |
| 10101 | << RD << /*copy assignment*/!isa<CXXConstructorDecl>(CopyOp) |
| 10102 | << /*destructor*/isa<CXXDestructorDecl>(UserDeclaredOperation); |
| 10103 | S.Diag(UseLoc, diag::note_member_synthesized_at) |
| 10104 | << (isa<CXXConstructorDecl>(CopyOp) ? Sema::CXXCopyConstructor |
| 10105 | : Sema::CXXCopyAssignment) |
| 10106 | << RD; |
| 10107 | } |
| 10108 | } |
| 10109 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10110 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 10111 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 10112 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10113 | CopyAssignOperator->isOverloadedOperator() && |
| 10114 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10115 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 10116 | !CopyAssignOperator->isDeleted()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10117 | "DefineImplicitCopyAssignment called for wrong function"); |
| 10118 | |
| 10119 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 10120 | |
| 10121 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 10122 | CopyAssignOperator->setInvalidDecl(); |
| 10123 | return; |
| 10124 | } |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10125 | |
| 10126 | // C++11 [class.copy]p18: |
| 10127 | // The [definition of an implicitly declared copy assignment operator] is |
| 10128 | // deprecated if the class has a user-declared copy constructor or a |
| 10129 | // user-declared destructor. |
| 10130 | if (getLangOpts().CPlusPlus11 && CopyAssignOperator->isImplicit()) |
| 10131 | diagnoseDeprecatedCopyOperation(*this, CopyAssignOperator, CurrentLocation); |
| 10132 | |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10133 | CopyAssignOperator->markUsed(Context); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10134 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10135 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 10136 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10137 | |
| 10138 | // C++0x [class.copy]p30: |
| 10139 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 10140 | // for a non-union class X performs memberwise copy assignment of its |
| 10141 | // subobjects. The direct base classes of X are assigned first, in the |
| 10142 | // order of their declaration in the base-specifier-list, and then the |
| 10143 | // immediate non-static data members of X are assigned, in the order in |
| 10144 | // which they were declared in the class definition. |
| 10145 | |
| 10146 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 10147 | SmallVector<Stmt*, 8> Statements; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10148 | |
| 10149 | // The parameter for the "other" object, which we are copying from. |
| 10150 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 10151 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 10152 | QualType OtherRefType = Other->getType(); |
| 10153 | if (const LValueReferenceType *OtherRef |
| 10154 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 10155 | OtherRefType = OtherRef->getPointeeType(); |
| 10156 | OtherQuals = OtherRefType.getQualifiers(); |
| 10157 | } |
| 10158 | |
| 10159 | // Our location for everything implicitly-generated. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10160 | SourceLocation Loc = CopyAssignOperator->getLocEnd().isValid() |
| 10161 | ? CopyAssignOperator->getLocEnd() |
| 10162 | : CopyAssignOperator->getLocation(); |
| 10163 | |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10164 | // Builds a DeclRefExpr for the "other" object. |
| 10165 | RefBuilder OtherRef(Other, OtherRefType); |
| 10166 | |
| 10167 | // Builds the "this" pointer. |
| 10168 | ThisBuilder This; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10169 | |
| 10170 | // Assign base classes. |
| 10171 | bool Invalid = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10172 | for (auto &Base : ClassDecl->bases()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10173 | // Form the assignment: |
| 10174 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10175 | QualType BaseType = Base.getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 10176 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10177 | Invalid = true; |
| 10178 | continue; |
| 10179 | } |
| 10180 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 10181 | CXXCastPath BasePath; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10182 | BasePath.push_back(&Base); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 10183 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10184 | // Construct the "from" expression, which is an implicit cast to the |
| 10185 | // appropriately-qualified base type. |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10186 | CastBuilder From(OtherRef, Context.getQualifiedType(BaseType, OtherQuals), |
| 10187 | VK_LValue, BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10188 | |
| 10189 | // Dereference "this". |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10190 | DerefBuilder DerefThis(This); |
| 10191 | CastBuilder To(DerefThis, |
| 10192 | Context.getCVRQualifiedType( |
| 10193 | BaseType, CopyAssignOperator->getTypeQualifiers()), |
| 10194 | VK_LValue, BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10195 | |
| 10196 | // Build the copy. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 10197 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10198 | To, From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10199 | /*CopyingBaseSubobject=*/true, |
| 10200 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10201 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 10202 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10203 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 10204 | CopyAssignOperator->setInvalidDecl(); |
| 10205 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10206 | } |
| 10207 | |
| 10208 | // Success! Record the copy. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10209 | Statements.push_back(Copy.getAs<Expr>()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10210 | } |
| 10211 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10212 | // Assign non-static members. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10213 | for (auto *Field : ClassDecl->fields()) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 10214 | if (Field->isUnnamedBitfield()) |
| 10215 | continue; |
Eli Friedman | 8150da3 | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 10216 | |
| 10217 | if (Field->isInvalidDecl()) { |
| 10218 | Invalid = true; |
| 10219 | continue; |
| 10220 | } |
| 10221 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10222 | // Check for members of reference type; we can't copy those. |
| 10223 | if (Field->getType()->isReferenceType()) { |
| 10224 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 10225 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 10226 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 10227 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10228 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10229 | Invalid = true; |
| 10230 | continue; |
| 10231 | } |
| 10232 | |
| 10233 | // Check for members of const-qualified, non-class type. |
| 10234 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 10235 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 10236 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 10237 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 10238 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 10239 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10240 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10241 | Invalid = true; |
| 10242 | continue; |
| 10243 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 10244 | |
| 10245 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 10246 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 10247 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10248 | |
| 10249 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 10250 | if (FieldType->isIncompleteArrayType()) { |
| 10251 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 10252 | "Incomplete array type is not valid"); |
| 10253 | continue; |
| 10254 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10255 | |
| 10256 | // Build references to the field in the object we're copying from and to. |
| 10257 | CXXScopeSpec SS; // Intentionally empty |
| 10258 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 10259 | LookupMemberName); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10260 | MemberLookup.addDecl(Field); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10261 | MemberLookup.resolveKind(); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10262 | |
| 10263 | MemberBuilder From(OtherRef, OtherRefType, /*IsArrow=*/false, MemberLookup); |
| 10264 | |
| 10265 | MemberBuilder To(This, getCurrentThisType(), /*IsArrow=*/true, MemberLookup); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10266 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10267 | // Build the copy of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 10268 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10269 | To, From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10270 | /*CopyingBaseSubobject=*/false, |
| 10271 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10272 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 10273 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10274 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 10275 | CopyAssignOperator->setInvalidDecl(); |
| 10276 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10277 | } |
| 10278 | |
| 10279 | // Success! Record the copy. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10280 | Statements.push_back(Copy.getAs<Stmt>()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10281 | } |
| 10282 | |
| 10283 | if (!Invalid) { |
| 10284 | // Add a "return *this;" |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10285 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10286 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10287 | StmtResult Return = BuildReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10288 | if (Return.isInvalid()) |
| 10289 | Invalid = true; |
| 10290 | else { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10291 | Statements.push_back(Return.getAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 10292 | |
| 10293 | if (Trap.hasErrorOccurred()) { |
| 10294 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10295 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 10296 | Invalid = true; |
| 10297 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10298 | } |
| 10299 | } |
| 10300 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10301 | // The exception specification is needed because we are defining the |
| 10302 | // function. |
| 10303 | ResolveExceptionSpec(CurrentLocation, |
| 10304 | CopyAssignOperator->getType()->castAs<FunctionProtoType>()); |
| 10305 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 10306 | if (Invalid) { |
| 10307 | CopyAssignOperator->setInvalidDecl(); |
| 10308 | return; |
| 10309 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10310 | |
| 10311 | StmtResult Body; |
| 10312 | { |
| 10313 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10314 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10315 | /*isStmtExpr=*/false); |
| 10316 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 10317 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10318 | CopyAssignOperator->setBody(Body.getAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 10319 | |
| 10320 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10321 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 10322 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10323 | } |
| 10324 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10325 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10326 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 10327 | CXXRecordDecl *ClassDecl = MD->getParent(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10328 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10329 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10330 | if (ClassDecl->isInvalidDecl()) |
| 10331 | return ExceptSpec; |
| 10332 | |
| 10333 | // C++0x [except.spec]p14: |
| 10334 | // An implicitly declared special member function (Clause 12) shall have an |
| 10335 | // exception-specification. [...] |
| 10336 | |
| 10337 | // It is unspecified whether or not an implicit move assignment operator |
| 10338 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 10339 | // made. As such, this exception specification is effectively unspecified. |
| 10340 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 10341 | // the side of assuming such calls to be made regardless of whether they |
| 10342 | // actually happen. |
| 10343 | // Note that a move constructor is not implicitly declared when there are |
| 10344 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10345 | for (const auto &Base : ClassDecl->bases()) { |
| 10346 | if (Base.isVirtual()) |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10347 | continue; |
| 10348 | |
| 10349 | CXXRecordDecl *BaseClassDecl |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10350 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10351 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10352 | 0, false, 0)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10353 | ExceptSpec.CalledDecl(Base.getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10354 | } |
| 10355 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10356 | for (const auto &Base : ClassDecl->vbases()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10357 | CXXRecordDecl *BaseClassDecl |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10358 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10359 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10360 | 0, false, 0)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10361 | ExceptSpec.CalledDecl(Base.getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10362 | } |
| 10363 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10364 | for (const auto *Field : ClassDecl->fields()) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 10365 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10366 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10367 | if (CXXMethodDecl *MoveAssign = |
| 10368 | LookupMovingAssignment(FieldClassDecl, |
| 10369 | FieldType.getCVRQualifiers(), |
| 10370 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10371 | ExceptSpec.CalledDecl(Field->getLocation(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10372 | } |
| 10373 | } |
| 10374 | |
| 10375 | return ExceptSpec; |
| 10376 | } |
| 10377 | |
| 10378 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 10379 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 10380 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10381 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveAssignment); |
| 10382 | if (DSM.isAlreadyBeingDeclared()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10383 | return nullptr; |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10384 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10385 | // Note: The following rules are largely analoguous to the move |
| 10386 | // constructor rules. |
| 10387 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10388 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 10389 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 10390 | ArgType = Context.getRValueReferenceType(ArgType); |
| 10391 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 10392 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 10393 | CXXMoveAssignment, |
| 10394 | false); |
| 10395 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10396 | // An implicitly-declared move assignment operator is an inline public |
| 10397 | // member of its class. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10398 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 10399 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 10400 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 10401 | CXXMethodDecl *MoveAssignment = |
| 10402 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10403 | /*TInfo=*/nullptr, /*StorageClass=*/SC_None, |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 10404 | /*isInline=*/true, Constexpr, SourceLocation()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10405 | MoveAssignment->setAccess(AS_public); |
| 10406 | MoveAssignment->setDefaulted(); |
| 10407 | MoveAssignment->setImplicit(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10408 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10409 | if (getLangOpts().CUDA) { |
| 10410 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXMoveAssignment, |
| 10411 | MoveAssignment, |
| 10412 | /* ConstRHS */ false, |
| 10413 | /* Diagnose */ false); |
| 10414 | } |
| 10415 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10416 | // Build an exception specification pointing back at this member. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10417 | FunctionProtoType::ExtProtoInfo EPI = |
| 10418 | getImplicitMethodEPI(*this, MoveAssignment); |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 10419 | MoveAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10420 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10421 | // Add the parameter to the operator. |
| 10422 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10423 | ClassLoc, ClassLoc, |
| 10424 | /*Id=*/nullptr, ArgType, |
| 10425 | /*TInfo=*/nullptr, SC_None, |
| 10426 | nullptr); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 10427 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10428 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10429 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 10430 | |
| 10431 | MoveAssignment->setTrivial( |
| 10432 | ClassDecl->needsOverloadResolutionForMoveAssignment() |
| 10433 | ? SpecialMemberIsTrivial(MoveAssignment, CXXMoveAssignment) |
| 10434 | : ClassDecl->hasTrivialMoveAssignment()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10435 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 10436 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 10437 | ClassDecl->setImplicitMoveAssignmentIsDeleted(); |
| 10438 | SetDeclDeleted(MoveAssignment, ClassLoc); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10439 | } |
| 10440 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10441 | // Note that we have added this copy-assignment operator. |
| 10442 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 10443 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10444 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 10445 | PushOnScopeChains(MoveAssignment, S, false); |
| 10446 | ClassDecl->addDecl(MoveAssignment); |
| 10447 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10448 | return MoveAssignment; |
| 10449 | } |
| 10450 | |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10451 | /// Check if we're implicitly defining a move assignment operator for a class |
| 10452 | /// with virtual bases. Such a move assignment might move-assign the virtual |
| 10453 | /// base multiple times. |
| 10454 | static void checkMoveAssignmentForRepeatedMove(Sema &S, CXXRecordDecl *Class, |
| 10455 | SourceLocation CurrentLocation) { |
| 10456 | assert(!Class->isDependentContext() && "should not define dependent move"); |
| 10457 | |
| 10458 | // Only a virtual base could get implicitly move-assigned multiple times. |
| 10459 | // Only a non-trivial move assignment can observe this. We only want to |
| 10460 | // diagnose if we implicitly define an assignment operator that assigns |
| 10461 | // two base classes, both of which move-assign the same virtual base. |
| 10462 | if (Class->getNumVBases() == 0 || Class->hasTrivialMoveAssignment() || |
| 10463 | Class->getNumBases() < 2) |
| 10464 | return; |
| 10465 | |
| 10466 | llvm::SmallVector<CXXBaseSpecifier *, 16> Worklist; |
| 10467 | typedef llvm::DenseMap<CXXRecordDecl*, CXXBaseSpecifier*> VBaseMap; |
| 10468 | VBaseMap VBases; |
| 10469 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10470 | for (auto &BI : Class->bases()) { |
| 10471 | Worklist.push_back(&BI); |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10472 | while (!Worklist.empty()) { |
| 10473 | CXXBaseSpecifier *BaseSpec = Worklist.pop_back_val(); |
| 10474 | CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 10475 | |
| 10476 | // If the base has no non-trivial move assignment operators, |
| 10477 | // we don't care about moves from it. |
| 10478 | if (!Base->hasNonTrivialMoveAssignment()) |
| 10479 | continue; |
| 10480 | |
| 10481 | // If there's nothing virtual here, skip it. |
| 10482 | if (!BaseSpec->isVirtual() && !Base->getNumVBases()) |
| 10483 | continue; |
| 10484 | |
| 10485 | // If we're not actually going to call a move assignment for this base, |
| 10486 | // or the selected move assignment is trivial, skip it. |
| 10487 | Sema::SpecialMemberOverloadResult *SMOR = |
| 10488 | S.LookupSpecialMember(Base, Sema::CXXMoveAssignment, |
| 10489 | /*ConstArg*/false, /*VolatileArg*/false, |
| 10490 | /*RValueThis*/true, /*ConstThis*/false, |
| 10491 | /*VolatileThis*/false); |
| 10492 | if (!SMOR->getMethod() || SMOR->getMethod()->isTrivial() || |
| 10493 | !SMOR->getMethod()->isMoveAssignmentOperator()) |
| 10494 | continue; |
| 10495 | |
| 10496 | if (BaseSpec->isVirtual()) { |
| 10497 | // We're going to move-assign this virtual base, and its move |
| 10498 | // assignment operator is not trivial. If this can happen for |
| 10499 | // multiple distinct direct bases of Class, diagnose it. (If it |
| 10500 | // only happens in one base, we'll diagnose it when synthesizing |
| 10501 | // that base class's move assignment operator.) |
| 10502 | CXXBaseSpecifier *&Existing = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10503 | VBases.insert(std::make_pair(Base->getCanonicalDecl(), &BI)) |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10504 | .first->second; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10505 | if (Existing && Existing != &BI) { |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10506 | S.Diag(CurrentLocation, diag::warn_vbase_moved_multiple_times) |
| 10507 | << Class << Base; |
| 10508 | S.Diag(Existing->getLocStart(), diag::note_vbase_moved_here) |
| 10509 | << (Base->getCanonicalDecl() == |
| 10510 | Existing->getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 10511 | << Base << Existing->getType() << Existing->getSourceRange(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10512 | S.Diag(BI.getLocStart(), diag::note_vbase_moved_here) |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10513 | << (Base->getCanonicalDecl() == |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10514 | BI.getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 10515 | << Base << BI.getType() << BaseSpec->getSourceRange(); |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10516 | |
| 10517 | // Only diagnose each vbase once. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10518 | Existing = nullptr; |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10519 | } |
| 10520 | } else { |
| 10521 | // Only walk over bases that have defaulted move assignment operators. |
| 10522 | // We assume that any user-provided move assignment operator handles |
| 10523 | // the multiple-moves-of-vbase case itself somehow. |
| 10524 | if (!SMOR->getMethod()->isDefaulted()) |
| 10525 | continue; |
| 10526 | |
| 10527 | // We're going to move the base classes of Base. Add them to the list. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10528 | for (auto &BI : Base->bases()) |
| 10529 | Worklist.push_back(&BI); |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10530 | } |
| 10531 | } |
| 10532 | } |
| 10533 | } |
| 10534 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10535 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 10536 | CXXMethodDecl *MoveAssignOperator) { |
| 10537 | assert((MoveAssignOperator->isDefaulted() && |
| 10538 | MoveAssignOperator->isOverloadedOperator() && |
| 10539 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10540 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 10541 | !MoveAssignOperator->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10542 | "DefineImplicitMoveAssignment called for wrong function"); |
| 10543 | |
| 10544 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 10545 | |
| 10546 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 10547 | MoveAssignOperator->setInvalidDecl(); |
| 10548 | return; |
| 10549 | } |
| 10550 | |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10551 | MoveAssignOperator->markUsed(Context); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10552 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10553 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10554 | DiagnosticErrorTrap Trap(Diags); |
| 10555 | |
| 10556 | // C++0x [class.copy]p28: |
| 10557 | // The implicitly-defined or move assignment operator for a non-union class |
| 10558 | // X performs memberwise move assignment of its subobjects. The direct base |
| 10559 | // classes of X are assigned first, in the order of their declaration in the |
| 10560 | // base-specifier-list, and then the immediate non-static data members of X |
| 10561 | // are assigned, in the order in which they were declared in the class |
| 10562 | // definition. |
| 10563 | |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10564 | // Issue a warning if our implicit move assignment operator will move |
| 10565 | // from a virtual base more than once. |
| 10566 | checkMoveAssignmentForRepeatedMove(*this, ClassDecl, CurrentLocation); |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 10567 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10568 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 10569 | SmallVector<Stmt*, 8> Statements; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10570 | |
| 10571 | // The parameter for the "other" object, which we are move from. |
| 10572 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 10573 | QualType OtherRefType = Other->getType()-> |
| 10574 | getAs<RValueReferenceType>()->getPointeeType(); |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 10575 | assert(!OtherRefType.getQualifiers() && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10576 | "Bad argument type of defaulted move assignment"); |
| 10577 | |
| 10578 | // Our location for everything implicitly-generated. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10579 | SourceLocation Loc = MoveAssignOperator->getLocEnd().isValid() |
| 10580 | ? MoveAssignOperator->getLocEnd() |
| 10581 | : MoveAssignOperator->getLocation(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10582 | |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10583 | // Builds a reference to the "other" object. |
| 10584 | RefBuilder OtherRef(Other, OtherRefType); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10585 | // Cast to rvalue. |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10586 | MoveCastBuilder MoveOther(OtherRef); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10587 | |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10588 | // Builds the "this" pointer. |
| 10589 | ThisBuilder This; |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 10590 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10591 | // Assign base classes. |
| 10592 | bool Invalid = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10593 | for (auto &Base : ClassDecl->bases()) { |
Richard Smith | 33b1f63 | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 10594 | // C++11 [class.copy]p28: |
| 10595 | // It is unspecified whether subobjects representing virtual base classes |
| 10596 | // are assigned more than once by the implicitly-defined copy assignment |
| 10597 | // operator. |
| 10598 | // FIXME: Do not assign to a vbase that will be assigned by some other base |
| 10599 | // class. For a move-assignment, this can result in the vbase being moved |
| 10600 | // multiple times. |
| 10601 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10602 | // Form the assignment: |
| 10603 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10604 | QualType BaseType = Base.getType().getUnqualifiedType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10605 | if (!BaseType->isRecordType()) { |
| 10606 | Invalid = true; |
| 10607 | continue; |
| 10608 | } |
| 10609 | |
| 10610 | CXXCastPath BasePath; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10611 | BasePath.push_back(&Base); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10612 | |
| 10613 | // Construct the "from" expression, which is an implicit cast to the |
| 10614 | // appropriately-qualified base type. |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10615 | CastBuilder From(OtherRef, BaseType, VK_XValue, BasePath); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10616 | |
| 10617 | // Dereference "this". |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10618 | DerefBuilder DerefThis(This); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10619 | |
| 10620 | // Implicitly cast "this" to the appropriately-qualified base type. |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10621 | CastBuilder To(DerefThis, |
| 10622 | Context.getCVRQualifiedType( |
| 10623 | BaseType, MoveAssignOperator->getTypeQualifiers()), |
| 10624 | VK_LValue, BasePath); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10625 | |
| 10626 | // Build the move. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 10627 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10628 | To, From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10629 | /*CopyingBaseSubobject=*/true, |
| 10630 | /*Copying=*/false); |
| 10631 | if (Move.isInvalid()) { |
| 10632 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10633 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 10634 | MoveAssignOperator->setInvalidDecl(); |
| 10635 | return; |
| 10636 | } |
| 10637 | |
| 10638 | // Success! Record the move. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10639 | Statements.push_back(Move.getAs<Expr>()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10640 | } |
| 10641 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10642 | // Assign non-static members. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10643 | for (auto *Field : ClassDecl->fields()) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 10644 | if (Field->isUnnamedBitfield()) |
| 10645 | continue; |
| 10646 | |
Eli Friedman | 8150da3 | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 10647 | if (Field->isInvalidDecl()) { |
| 10648 | Invalid = true; |
| 10649 | continue; |
| 10650 | } |
| 10651 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10652 | // Check for members of reference type; we can't move those. |
| 10653 | if (Field->getType()->isReferenceType()) { |
| 10654 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 10655 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 10656 | Diag(Field->getLocation(), diag::note_declared_at); |
| 10657 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10658 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 10659 | Invalid = true; |
| 10660 | continue; |
| 10661 | } |
| 10662 | |
| 10663 | // Check for members of const-qualified, non-class type. |
| 10664 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 10665 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 10666 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 10667 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 10668 | Diag(Field->getLocation(), diag::note_declared_at); |
| 10669 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10670 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 10671 | Invalid = true; |
| 10672 | continue; |
| 10673 | } |
| 10674 | |
| 10675 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 10676 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 10677 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10678 | |
| 10679 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 10680 | if (FieldType->isIncompleteArrayType()) { |
| 10681 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 10682 | "Incomplete array type is not valid"); |
| 10683 | continue; |
| 10684 | } |
| 10685 | |
| 10686 | // Build references to the field in the object we're copying from and to. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10687 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 10688 | LookupMemberName); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10689 | MemberLookup.addDecl(Field); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10690 | MemberLookup.resolveKind(); |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10691 | MemberBuilder From(MoveOther, OtherRefType, |
| 10692 | /*IsArrow=*/false, MemberLookup); |
| 10693 | MemberBuilder To(This, getCurrentThisType(), |
| 10694 | /*IsArrow=*/true, MemberLookup); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10695 | |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10696 | assert(!From.build(*this, Loc)->isLValue() && // could be xvalue or prvalue |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10697 | "Member reference with rvalue base must be rvalue except for reference " |
| 10698 | "members, which aren't allowed for move assignment."); |
| 10699 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10700 | // Build the move of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 10701 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
Pavel Labath | 66ea35d | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10702 | To, From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10703 | /*CopyingBaseSubobject=*/false, |
| 10704 | /*Copying=*/false); |
| 10705 | if (Move.isInvalid()) { |
| 10706 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10707 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 10708 | MoveAssignOperator->setInvalidDecl(); |
| 10709 | return; |
| 10710 | } |
Richard Smith | e7ce709 | 2012-11-12 23:33:00 +0000 | [diff] [blame] | 10711 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10712 | // Success! Record the copy. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10713 | Statements.push_back(Move.getAs<Stmt>()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10714 | } |
| 10715 | |
| 10716 | if (!Invalid) { |
| 10717 | // Add a "return *this;" |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10718 | ExprResult ThisObj = |
| 10719 | CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
| 10720 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10721 | StmtResult Return = BuildReturnStmt(Loc, ThisObj.get()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10722 | if (Return.isInvalid()) |
| 10723 | Invalid = true; |
| 10724 | else { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10725 | Statements.push_back(Return.getAs<Stmt>()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10726 | |
| 10727 | if (Trap.hasErrorOccurred()) { |
| 10728 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10729 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 10730 | Invalid = true; |
| 10731 | } |
| 10732 | } |
| 10733 | } |
| 10734 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10735 | // The exception specification is needed because we are defining the |
| 10736 | // function. |
| 10737 | ResolveExceptionSpec(CurrentLocation, |
| 10738 | MoveAssignOperator->getType()->castAs<FunctionProtoType>()); |
| 10739 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10740 | if (Invalid) { |
| 10741 | MoveAssignOperator->setInvalidDecl(); |
| 10742 | return; |
| 10743 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10744 | |
| 10745 | StmtResult Body; |
| 10746 | { |
| 10747 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10748 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10749 | /*isStmtExpr=*/false); |
| 10750 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 10751 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10752 | MoveAssignOperator->setBody(Body.getAs<Stmt>()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10753 | |
| 10754 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10755 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 10756 | } |
| 10757 | } |
| 10758 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10759 | Sema::ImplicitExceptionSpecification |
| 10760 | Sema::ComputeDefaultedCopyCtorExceptionSpec(CXXMethodDecl *MD) { |
| 10761 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 10762 | |
| 10763 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 10764 | if (ClassDecl->isInvalidDecl()) |
| 10765 | return ExceptSpec; |
| 10766 | |
| 10767 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10768 | assert(T->getNumParams() >= 1 && "not a copy ctor"); |
| 10769 | unsigned Quals = T->getParamType(0).getNonReferenceType().getCVRQualifiers(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10770 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10771 | // C++ [except.spec]p14: |
| 10772 | // An implicitly declared special member function (Clause 12) shall have an |
| 10773 | // exception-specification. [...] |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10774 | for (const auto &Base : ClassDecl->bases()) { |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10775 | // Virtual bases are handled below. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10776 | if (Base.isVirtual()) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10777 | continue; |
| 10778 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 10779 | CXXRecordDecl *BaseClassDecl |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10780 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10781 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 10782 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10783 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10784 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10785 | for (const auto &Base : ClassDecl->vbases()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 10786 | CXXRecordDecl *BaseClassDecl |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10787 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10788 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 10789 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10790 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10791 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10792 | for (const auto *Field : ClassDecl->fields()) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 10793 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10794 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 10795 | if (CXXConstructorDecl *CopyConstructor = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10796 | LookupCopyingConstructor(FieldClassDecl, |
| 10797 | Quals | FieldType.getCVRQualifiers())) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10798 | ExceptSpec.CalledDecl(Field->getLocation(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10799 | } |
| 10800 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 10801 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10802 | return ExceptSpec; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10803 | } |
| 10804 | |
| 10805 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 10806 | CXXRecordDecl *ClassDecl) { |
| 10807 | // C++ [class.copy]p4: |
| 10808 | // If the class definition does not explicitly declare a copy |
| 10809 | // constructor, one is declared implicitly. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 10810 | assert(ClassDecl->needsImplicitCopyConstructor()); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10811 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10812 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyConstructor); |
| 10813 | if (DSM.isAlreadyBeingDeclared()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10814 | return nullptr; |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10815 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10816 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 10817 | QualType ArgType = ClassType; |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 10818 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10819 | if (Const) |
| 10820 | ArgType = ArgType.withConst(); |
| 10821 | ArgType = Context.getLValueReferenceType(ArgType); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10822 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10823 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 10824 | CXXCopyConstructor, |
| 10825 | Const); |
| 10826 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10827 | DeclarationName Name |
| 10828 | = Context.DeclarationNames.getCXXConstructorName( |
| 10829 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10830 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 10831 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10832 | |
| 10833 | // An implicitly-declared copy constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10834 | // member of its class. |
| 10835 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10836 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/nullptr, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10837 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10838 | Constexpr); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10839 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10840 | CopyConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10841 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10842 | if (getLangOpts().CUDA) { |
| 10843 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXCopyConstructor, |
| 10844 | CopyConstructor, |
| 10845 | /* ConstRHS */ Const, |
| 10846 | /* Diagnose */ false); |
| 10847 | } |
| 10848 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10849 | // Build an exception specification pointing back at this member. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10850 | FunctionProtoType::ExtProtoInfo EPI = |
| 10851 | getImplicitMethodEPI(*this, CopyConstructor); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10852 | CopyConstructor->setType( |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 10853 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10854 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10855 | // Add the parameter to the constructor. |
| 10856 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10857 | ClassLoc, ClassLoc, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10858 | /*IdentifierInfo=*/nullptr, |
| 10859 | ArgType, /*TInfo=*/nullptr, |
| 10860 | SC_None, nullptr); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 10861 | CopyConstructor->setParams(FromParam); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10862 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10863 | CopyConstructor->setTrivial( |
| 10864 | ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 10865 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor) |
| 10866 | : ClassDecl->hasTrivialCopyConstructor()); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 10867 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10868 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 10869 | SetDeclDeleted(CopyConstructor, ClassLoc); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10870 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10871 | // Note that we have declared this constructor. |
| 10872 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 10873 | |
| 10874 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 10875 | PushOnScopeChains(CopyConstructor, S, false); |
| 10876 | ClassDecl->addDecl(CopyConstructor); |
| 10877 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10878 | return CopyConstructor; |
| 10879 | } |
| 10880 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10881 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10882 | CXXConstructorDecl *CopyConstructor) { |
| 10883 | assert((CopyConstructor->isDefaulted() && |
| 10884 | CopyConstructor->isCopyConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10885 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 10886 | !CopyConstructor->isDeleted()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10887 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10888 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 10889 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10890 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10891 | |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10892 | // C++11 [class.copy]p7: |
Benjamin Kramer | e575359 | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 10893 | // The [definition of an implicitly declared copy constructor] is |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10894 | // deprecated if the class has a user-declared copy assignment operator |
| 10895 | // or a user-declared destructor. |
| 10896 | if (getLangOpts().CPlusPlus11 && CopyConstructor->isImplicit()) |
| 10897 | diagnoseDeprecatedCopyOperation(*this, CopyConstructor, CurrentLocation); |
| 10898 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10899 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 10900 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10901 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 10902 | if (SetCtorInitializers(CopyConstructor, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 10903 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 10904 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 10905 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 10906 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 10907 | } else { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10908 | SourceLocation Loc = CopyConstructor->getLocEnd().isValid() |
| 10909 | ? CopyConstructor->getLocEnd() |
| 10910 | : CopyConstructor->getLocation(); |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10911 | Sema::CompoundScopeRAII CompoundScope(*this); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 10912 | CopyConstructor->setBody( |
| 10913 | ActOnCompoundStmt(Loc, Loc, None, /*isStmtExpr=*/false).getAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 10914 | } |
Robert Wilhelm | c895f4d | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 10915 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10916 | // The exception specification is needed because we are defining the |
| 10917 | // function. |
| 10918 | ResolveExceptionSpec(CurrentLocation, |
| 10919 | CopyConstructor->getType()->castAs<FunctionProtoType>()); |
| 10920 | |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10921 | CopyConstructor->markUsed(Context); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10922 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 10923 | |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 10924 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10925 | L->CompletedImplicitDefinition(CopyConstructor); |
| 10926 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10927 | } |
| 10928 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10929 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10930 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXMethodDecl *MD) { |
| 10931 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 10932 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10933 | // C++ [except.spec]p14: |
| 10934 | // An implicitly declared special member function (Clause 12) shall have an |
| 10935 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10936 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10937 | if (ClassDecl->isInvalidDecl()) |
| 10938 | return ExceptSpec; |
| 10939 | |
| 10940 | // Direct base-class constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10941 | for (const auto &B : ClassDecl->bases()) { |
| 10942 | if (B.isVirtual()) // Handled below. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10943 | continue; |
| 10944 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10945 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10946 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10947 | CXXConstructorDecl *Constructor = |
| 10948 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10949 | // If this is a deleted function, add it anyway. This might be conformant |
| 10950 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10951 | if (Constructor) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10952 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10953 | } |
| 10954 | } |
| 10955 | |
| 10956 | // Virtual base-class constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10957 | for (const auto &B : ClassDecl->vbases()) { |
| 10958 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10959 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10960 | CXXConstructorDecl *Constructor = |
| 10961 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10962 | // If this is a deleted function, add it anyway. This might be conformant |
| 10963 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10964 | if (Constructor) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10965 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10966 | } |
| 10967 | } |
| 10968 | |
| 10969 | // Field constructors. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10970 | for (const auto *F : ClassDecl->fields()) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10971 | QualType FieldType = Context.getBaseElementType(F->getType()); |
| 10972 | if (CXXRecordDecl *FieldRecDecl = FieldType->getAsCXXRecordDecl()) { |
| 10973 | CXXConstructorDecl *Constructor = |
| 10974 | LookupMovingConstructor(FieldRecDecl, FieldType.getCVRQualifiers()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10975 | // If this is a deleted function, add it anyway. This might be conformant |
| 10976 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10977 | // In particular, the problem is that this function never gets called. It |
| 10978 | // might just be ill-formed because this function attempts to refer to |
| 10979 | // a deleted function here. |
| 10980 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10981 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10982 | } |
| 10983 | } |
| 10984 | |
| 10985 | return ExceptSpec; |
| 10986 | } |
| 10987 | |
| 10988 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 10989 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 10990 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 10991 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10992 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveConstructor); |
| 10993 | if (DSM.isAlreadyBeingDeclared()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 10994 | return nullptr; |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10995 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10996 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 10997 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10998 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10999 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 11000 | CXXMoveConstructor, |
| 11001 | false); |
| 11002 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11003 | DeclarationName Name |
| 11004 | = Context.DeclarationNames.getCXXConstructorName( |
| 11005 | Context.getCanonicalType(ClassType)); |
| 11006 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 11007 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 11008 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 11009 | // C++11 [class.copy]p11: |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11010 | // An implicitly-declared copy/move constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 11011 | // member of its class. |
| 11012 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11013 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/nullptr, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 11014 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 11015 | Constexpr); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11016 | MoveConstructor->setAccess(AS_public); |
| 11017 | MoveConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 11018 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11019 | if (getLangOpts().CUDA) { |
| 11020 | inferCUDATargetForImplicitSpecialMember(ClassDecl, CXXMoveConstructor, |
| 11021 | MoveConstructor, |
| 11022 | /* ConstRHS */ false, |
| 11023 | /* Diagnose */ false); |
| 11024 | } |
| 11025 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11026 | // Build an exception specification pointing back at this member. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 11027 | FunctionProtoType::ExtProtoInfo EPI = |
| 11028 | getImplicitMethodEPI(*this, MoveConstructor); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11029 | MoveConstructor->setType( |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 11030 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11031 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11032 | // Add the parameter to the constructor. |
| 11033 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 11034 | ClassLoc, ClassLoc, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11035 | /*IdentifierInfo=*/nullptr, |
| 11036 | ArgType, /*TInfo=*/nullptr, |
| 11037 | SC_None, nullptr); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 11038 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11039 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 11040 | MoveConstructor->setTrivial( |
| 11041 | ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 11042 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor) |
| 11043 | : ClassDecl->hasTrivialMoveConstructor()); |
| 11044 | |
Sean Hunt | 769bb2d | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 11045 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
Richard Smith | 743cbb9 | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 11046 | ClassDecl->setImplicitMoveConstructorIsDeleted(); |
| 11047 | SetDeclDeleted(MoveConstructor, ClassLoc); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11048 | } |
| 11049 | |
| 11050 | // Note that we have declared this constructor. |
| 11051 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 11052 | |
| 11053 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 11054 | PushOnScopeChains(MoveConstructor, S, false); |
| 11055 | ClassDecl->addDecl(MoveConstructor); |
| 11056 | |
| 11057 | return MoveConstructor; |
| 11058 | } |
| 11059 | |
| 11060 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 11061 | CXXConstructorDecl *MoveConstructor) { |
| 11062 | assert((MoveConstructor->isDefaulted() && |
| 11063 | MoveConstructor->isMoveConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 11064 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 11065 | !MoveConstructor->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11066 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 11067 | |
| 11068 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 11069 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 11070 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 11071 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11072 | DiagnosticErrorTrap Trap(Diags); |
| 11073 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 11074 | if (SetCtorInitializers(MoveConstructor, /*AnyErrors=*/false) || |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11075 | Trap.hasErrorOccurred()) { |
| 11076 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 11077 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 11078 | MoveConstructor->setInvalidDecl(); |
| 11079 | } else { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11080 | SourceLocation Loc = MoveConstructor->getLocEnd().isValid() |
| 11081 | ? MoveConstructor->getLocEnd() |
| 11082 | : MoveConstructor->getLocation(); |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 11083 | Sema::CompoundScopeRAII CompoundScope(*this); |
Robert Wilhelm | c895f4d | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 11084 | MoveConstructor->setBody(ActOnCompoundStmt( |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11085 | Loc, Loc, None, /*isStmtExpr=*/ false).getAs<Stmt>()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11086 | } |
| 11087 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11088 | // The exception specification is needed because we are defining the |
| 11089 | // function. |
| 11090 | ResolveExceptionSpec(CurrentLocation, |
| 11091 | MoveConstructor->getType()->castAs<FunctionProtoType>()); |
| 11092 | |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 11093 | MoveConstructor->markUsed(Context); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11094 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11095 | |
| 11096 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 11097 | L->CompletedImplicitDefinition(MoveConstructor); |
| 11098 | } |
| 11099 | } |
| 11100 | |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 11101 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
Eli Friedman | c4ef948 | 2013-07-18 23:29:14 +0000 | [diff] [blame] | 11102 | return FD->isDeleted() && FD->isDefaulted() && isa<CXXMethodDecl>(FD); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 11103 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11104 | |
| 11105 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11106 | SourceLocation CurrentLocation, |
| 11107 | CXXConversionDecl *Conv) { |
| 11108 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 11109 | CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 11110 | // If we are defining a specialization of a conversion to function-ptr |
| 11111 | // cache the deduced template arguments for this specialization |
| 11112 | // so that we can use them to retrieve the corresponding call-operator |
| 11113 | // and static-invoker. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11114 | const TemplateArgumentList *DeducedTemplateArgs = nullptr; |
| 11115 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11116 | // Retrieve the corresponding call-operator specialization. |
| 11117 | if (Lambda->isGenericLambda()) { |
| 11118 | assert(Conv->isFunctionTemplateSpecialization()); |
| 11119 | FunctionTemplateDecl *CallOpTemplate = |
| 11120 | CallOp->getDescribedFunctionTemplate(); |
| 11121 | DeducedTemplateArgs = Conv->getTemplateSpecializationArgs(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11122 | void *InsertPos = nullptr; |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11123 | FunctionDecl *CallOpSpec = CallOpTemplate->findSpecialization( |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11124 | DeducedTemplateArgs->asArray(), |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11125 | InsertPos); |
| 11126 | assert(CallOpSpec && |
| 11127 | "Conversion operator must have a corresponding call operator"); |
| 11128 | CallOp = cast<CXXMethodDecl>(CallOpSpec); |
| 11129 | } |
| 11130 | // Mark the call operator referenced (and add to pending instantiations |
| 11131 | // if necessary). |
| 11132 | // For both the conversion and static-invoker template specializations |
| 11133 | // we construct their body's in this function, so no need to add them |
| 11134 | // to the PendingInstantiations. |
| 11135 | MarkFunctionReferenced(CurrentLocation, CallOp); |
| 11136 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 11137 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11138 | DiagnosticErrorTrap Trap(Diags); |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11139 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11140 | // Retrieve the static invoker... |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11141 | CXXMethodDecl *Invoker = Lambda->getLambdaStaticInvoker(); |
| 11142 | // ... and get the corresponding specialization for a generic lambda. |
| 11143 | if (Lambda->isGenericLambda()) { |
| 11144 | assert(DeducedTemplateArgs && |
| 11145 | "Must have deduced template arguments from Conversion Operator"); |
| 11146 | FunctionTemplateDecl *InvokeTemplate = |
| 11147 | Invoker->getDescribedFunctionTemplate(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11148 | void *InsertPos = nullptr; |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11149 | FunctionDecl *InvokeSpec = InvokeTemplate->findSpecialization( |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11150 | DeducedTemplateArgs->asArray(), |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11151 | InsertPos); |
| 11152 | assert(InvokeSpec && |
| 11153 | "Must have a corresponding static invoker specialization"); |
| 11154 | Invoker = cast<CXXMethodDecl>(InvokeSpec); |
| 11155 | } |
| 11156 | // Construct the body of the conversion function { return __invoke; }. |
| 11157 | Expr *FunctionRef = BuildDeclRefExpr(Invoker, Invoker->getType(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11158 | VK_LValue, Conv->getLocation()).get(); |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11159 | assert(FunctionRef && "Can't refer to __invoke function?"); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11160 | Stmt *Return = BuildReturnStmt(Conv->getLocation(), FunctionRef).get(); |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11161 | Conv->setBody(new (Context) CompoundStmt(Context, Return, |
| 11162 | Conv->getLocation(), |
| 11163 | Conv->getLocation())); |
| 11164 | |
| 11165 | Conv->markUsed(Context); |
| 11166 | Conv->setReferenced(); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11167 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11168 | // Fill in the __invoke function with a dummy implementation. IR generation |
| 11169 | // will fill in the actual details. |
| 11170 | Invoker->markUsed(Context); |
| 11171 | Invoker->setReferenced(); |
| 11172 | Invoker->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
| 11173 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11174 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 11175 | L->CompletedImplicitDefinition(Conv); |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11176 | L->CompletedImplicitDefinition(Invoker); |
| 11177 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11178 | } |
| 11179 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11180 | |
| 11181 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11182 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 11183 | SourceLocation CurrentLocation, |
| 11184 | CXXConversionDecl *Conv) |
| 11185 | { |
Faisal Vali | 56fe35b | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 11186 | assert(!Conv->getParent()->isGenericLambda()); |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 11187 | |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 11188 | Conv->markUsed(Context); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11189 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 11190 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11191 | DiagnosticErrorTrap Trap(Diags); |
| 11192 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 11193 | // Copy-initialize the lambda object as needed to capture it. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11194 | Expr *This = ActOnCXXThis(CurrentLocation).get(); |
| 11195 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).get(); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11196 | |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 11197 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 11198 | Conv->getLocation(), |
| 11199 | Conv, DerefThis); |
| 11200 | |
| 11201 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 11202 | // behavior. Note that only the general conversion function does this |
| 11203 | // (since it's unusable otherwise); in the case where we inline the |
| 11204 | // block literal, it has block literal lifetime semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11205 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 11206 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 11207 | CK_CopyAndAutoreleaseBlockObject, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11208 | BuildBlock.get(), nullptr, VK_RValue); |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 11209 | |
| 11210 | if (BuildBlock.isInvalid()) { |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11211 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 11212 | Conv->setInvalidDecl(); |
| 11213 | return; |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11214 | } |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 11215 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 11216 | // Create the return statement that returns the block from the conversion |
| 11217 | // function. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11218 | StmtResult Return = BuildReturnStmt(Conv->getLocation(), BuildBlock.get()); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 11219 | if (Return.isInvalid()) { |
| 11220 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 11221 | Conv->setInvalidDecl(); |
| 11222 | return; |
| 11223 | } |
| 11224 | |
| 11225 | // Set the body of the conversion function. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11226 | Stmt *ReturnS = Return.get(); |
Nico Weber | d36aa35 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 11227 | Conv->setBody(new (Context) CompoundStmt(Context, ReturnS, |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 11228 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11229 | Conv->getLocation())); |
| 11230 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 11231 | // We're done; notify the mutation listener, if any. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 11232 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 11233 | L->CompletedImplicitDefinition(Conv); |
| 11234 | } |
| 11235 | } |
| 11236 | |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 11237 | /// \brief Determine whether the given list arguments contains exactly one |
| 11238 | /// "real" (non-default) argument. |
| 11239 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 11240 | switch (Args.size()) { |
| 11241 | case 0: |
| 11242 | return false; |
| 11243 | |
| 11244 | default: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11245 | if (!Args[1]->isDefaultArgument()) |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 11246 | return false; |
| 11247 | |
| 11248 | // fall through |
| 11249 | case 1: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11250 | return !Args[0]->isDefaultArgument(); |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 11251 | } |
| 11252 | |
| 11253 | return false; |
| 11254 | } |
| 11255 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11256 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 11257 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11258 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 11259 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11260 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 11261 | bool IsListInitialization, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11262 | bool IsStdInitListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 11263 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 11264 | unsigned ConstructKind, |
| 11265 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 11266 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11267 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 11268 | // C++0x [class.copy]p34: |
| 11269 | // When certain criteria are met, an implementation is allowed to |
| 11270 | // omit the copy/move construction of a class object, even if the |
| 11271 | // copy/move constructor and/or destructor for the object have |
| 11272 | // side effects. [...] |
| 11273 | // - when a temporary class object that has not been bound to a |
| 11274 | // reference (12.2) would be copied/moved to a class object |
| 11275 | // with the same cv-unqualified type, the copy/move operation |
| 11276 | // can be omitted by constructing the temporary object |
| 11277 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 11278 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 11279 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11280 | Expr *SubExpr = ExprArgs[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 11281 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 11282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11283 | |
| 11284 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11285 | Elidable, ExprArgs, HadMultipleCandidates, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11286 | IsListInitialization, |
| 11287 | IsStdInitListInitialization, RequiresZeroInit, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 11288 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 11289 | } |
| 11290 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 11291 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 11292 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11293 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 11294 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 11295 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 11296 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 11297 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 11298 | bool IsListInitialization, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11299 | bool IsStdInitListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 11300 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 11301 | unsigned ConstructKind, |
| 11302 | SourceRange ParenRange) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11303 | MarkFunctionReferenced(ConstructLoc, Constructor); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11304 | return CXXConstructExpr::Create( |
| 11305 | Context, DeclInitType, ConstructLoc, Constructor, Elidable, ExprArgs, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11306 | HadMultipleCandidates, IsListInitialization, IsStdInitListInitialization, |
| 11307 | RequiresZeroInit, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 11308 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 11309 | ParenRange); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 11310 | } |
| 11311 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11312 | ExprResult Sema::BuildCXXDefaultInitExpr(SourceLocation Loc, FieldDecl *Field) { |
| 11313 | assert(Field->hasInClassInitializer()); |
| 11314 | |
| 11315 | // If we already have the in-class initializer nothing needs to be done. |
| 11316 | if (Field->getInClassInitializer()) |
| 11317 | return CXXDefaultInitExpr::Create(Context, Loc, Field); |
| 11318 | |
| 11319 | // Maybe we haven't instantiated the in-class initializer. Go check the |
| 11320 | // pattern FieldDecl to see if it has one. |
| 11321 | CXXRecordDecl *ParentRD = cast<CXXRecordDecl>(Field->getParent()); |
| 11322 | |
| 11323 | if (isTemplateInstantiation(ParentRD->getTemplateSpecializationKind())) { |
| 11324 | CXXRecordDecl *ClassPattern = ParentRD->getTemplateInstantiationPattern(); |
| 11325 | DeclContext::lookup_result Lookup = |
| 11326 | ClassPattern->lookup(Field->getDeclName()); |
| 11327 | assert(Lookup.size() == 1); |
| 11328 | FieldDecl *Pattern = cast<FieldDecl>(Lookup[0]); |
| 11329 | if (InstantiateInClassInitializer(Loc, Field, Pattern, |
| 11330 | getTemplateInstantiationArgs(Field))) |
| 11331 | return ExprError(); |
| 11332 | return CXXDefaultInitExpr::Create(Context, Loc, Field); |
| 11333 | } |
| 11334 | |
| 11335 | // DR1351: |
| 11336 | // If the brace-or-equal-initializer of a non-static data member |
| 11337 | // invokes a defaulted default constructor of its class or of an |
| 11338 | // enclosing class in a potentially evaluated subexpression, the |
| 11339 | // program is ill-formed. |
| 11340 | // |
| 11341 | // This resolution is unworkable: the exception specification of the |
| 11342 | // default constructor can be needed in an unevaluated context, in |
| 11343 | // particular, in the operand of a noexcept-expression, and we can be |
| 11344 | // unable to compute an exception specification for an enclosed class. |
| 11345 | // |
| 11346 | // Any attempt to resolve the exception specification of a defaulted default |
| 11347 | // constructor before the initializer is lexically complete will ultimately |
| 11348 | // come here at which point we can diagnose it. |
| 11349 | RecordDecl *OutermostClass = ParentRD->getOuterLexicalRecordContext(); |
| 11350 | if (OutermostClass == ParentRD) { |
| 11351 | Diag(Field->getLocEnd(), diag::err_in_class_initializer_not_yet_parsed) |
| 11352 | << ParentRD << Field; |
| 11353 | } else { |
| 11354 | Diag(Field->getLocEnd(), |
| 11355 | diag::err_in_class_initializer_not_yet_parsed_outer_class) |
| 11356 | << ParentRD << OutermostClass << Field; |
| 11357 | } |
| 11358 | |
| 11359 | return ExprError(); |
| 11360 | } |
| 11361 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 11362 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 11363 | if (VD->isInvalidDecl()) return; |
| 11364 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 11365 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 11366 | if (ClassDecl->isInvalidDecl()) return; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 11367 | if (ClassDecl->hasIrrelevantDestructor()) return; |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 11368 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 11369 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 11370 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11371 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 11372 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 11373 | PDiag(diag::err_access_dtor_var) |
| 11374 | << VD->getDeclName() |
| 11375 | << VD->getType()); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 11376 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 11377 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11378 | if (Destructor->isTrivial()) return; |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 11379 | if (!VD->hasGlobalStorage()) return; |
| 11380 | |
| 11381 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 11382 | // class-static, function-static). |
| 11383 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 11384 | |
| 11385 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 11386 | if (!VD->isStaticLocal()) |
| 11387 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 11388 | } |
| 11389 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 11390 | /// \brief Given a constructor and the set of arguments provided for the |
| 11391 | /// constructor, convert the arguments and add any required default arguments |
| 11392 | /// to form a proper call to this constructor. |
| 11393 | /// |
| 11394 | /// \returns true if an error occurred, false otherwise. |
| 11395 | bool |
| 11396 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 11397 | MultiExprArg ArgsPtr, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11398 | SourceLocation Loc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 11399 | SmallVectorImpl<Expr*> &ConvertedArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 11400 | bool AllowExplicit, |
| 11401 | bool IsListInitialization) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 11402 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 11403 | unsigned NumArgs = ArgsPtr.size(); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11404 | Expr **Args = ArgsPtr.data(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 11405 | |
| 11406 | const FunctionProtoType *Proto |
| 11407 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 11408 | assert(Proto && "Constructor without a prototype?"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11409 | unsigned NumParams = Proto->getNumParams(); |
| 11410 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 11411 | // If too few arguments are available, we'll fill in the rest with defaults. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11412 | if (NumArgs < NumParams) |
| 11413 | ConvertedArgs.reserve(NumParams); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 11414 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 11415 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 11416 | |
| 11417 | VariadicCallType CallType = |
| 11418 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11419 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 11420 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11421 | Proto, 0, |
| 11422 | llvm::makeArrayRef(Args, NumArgs), |
| 11423 | AllArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 11424 | CallType, AllowExplicit, |
| 11425 | IsListInitialization); |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 11426 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11427 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 11428 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11429 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 11430 | CheckConstructorCall(Constructor, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 11431 | llvm::makeArrayRef(AllArgs.data(), AllArgs.size()), |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 11432 | Proto, Loc); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 11433 | |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 11434 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 11435 | } |
| 11436 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 11437 | static inline bool |
| 11438 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 11439 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11440 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 11441 | if (isa<NamespaceDecl>(DC)) { |
| 11442 | return SemaRef.Diag(FnDecl->getLocation(), |
| 11443 | diag::err_operator_new_delete_declared_in_namespace) |
| 11444 | << FnDecl->getDeclName(); |
| 11445 | } |
| 11446 | |
| 11447 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 11448 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 11449 | return SemaRef.Diag(FnDecl->getLocation(), |
| 11450 | diag::err_operator_new_delete_declared_static) |
| 11451 | << FnDecl->getDeclName(); |
| 11452 | } |
| 11453 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 11454 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 11455 | } |
| 11456 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11457 | static inline bool |
| 11458 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 11459 | CanQualType ExpectedResultType, |
| 11460 | CanQualType ExpectedFirstParamType, |
| 11461 | unsigned DependentParamTypeDiag, |
| 11462 | unsigned InvalidParamTypeDiag) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11463 | QualType ResultType = |
| 11464 | FnDecl->getType()->getAs<FunctionType>()->getReturnType(); |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11465 | |
| 11466 | // Check that the result type is not dependent. |
| 11467 | if (ResultType->isDependentType()) |
| 11468 | return SemaRef.Diag(FnDecl->getLocation(), |
| 11469 | diag::err_operator_new_delete_dependent_result_type) |
| 11470 | << FnDecl->getDeclName() << ExpectedResultType; |
| 11471 | |
| 11472 | // Check that the result type is what we expect. |
| 11473 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 11474 | return SemaRef.Diag(FnDecl->getLocation(), |
| 11475 | diag::err_operator_new_delete_invalid_result_type) |
| 11476 | << FnDecl->getDeclName() << ExpectedResultType; |
| 11477 | |
| 11478 | // A function template must have at least 2 parameters. |
| 11479 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 11480 | return SemaRef.Diag(FnDecl->getLocation(), |
| 11481 | diag::err_operator_new_delete_template_too_few_parameters) |
| 11482 | << FnDecl->getDeclName(); |
| 11483 | |
| 11484 | // The function decl must have at least 1 parameter. |
| 11485 | if (FnDecl->getNumParams() == 0) |
| 11486 | return SemaRef.Diag(FnDecl->getLocation(), |
| 11487 | diag::err_operator_new_delete_too_few_parameters) |
| 11488 | << FnDecl->getDeclName(); |
| 11489 | |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 11490 | // Check the first parameter type is not dependent. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11491 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 11492 | if (FirstParamType->isDependentType()) |
| 11493 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 11494 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 11495 | |
| 11496 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 11497 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11498 | ExpectedFirstParamType) |
| 11499 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 11500 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 11501 | |
| 11502 | return false; |
| 11503 | } |
| 11504 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 11505 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11506 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 11507 | // C++ [basic.stc.dynamic.allocation]p1: |
| 11508 | // A program is ill-formed if an allocation function is declared in a |
| 11509 | // namespace scope other than global scope or declared static in global |
| 11510 | // scope. |
| 11511 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 11512 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11513 | |
| 11514 | CanQualType SizeTy = |
| 11515 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 11516 | |
| 11517 | // C++ [basic.stc.dynamic.allocation]p1: |
| 11518 | // The return type shall be void*. The first parameter shall have type |
| 11519 | // std::size_t. |
| 11520 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 11521 | SizeTy, |
| 11522 | diag::err_operator_new_dependent_param_type, |
| 11523 | diag::err_operator_new_param_type)) |
| 11524 | return true; |
| 11525 | |
| 11526 | // C++ [basic.stc.dynamic.allocation]p1: |
| 11527 | // The first parameter shall not have an associated default argument. |
| 11528 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 11529 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11530 | diag::err_operator_new_default_arg) |
| 11531 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 11532 | |
| 11533 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 11534 | } |
| 11535 | |
| 11536 | static bool |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 11537 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 11538 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 11539 | // A program is ill-formed if deallocation functions are declared in a |
| 11540 | // namespace scope other than global scope or declared static in global |
| 11541 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 11542 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 11543 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 11544 | |
| 11545 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 11546 | // Each deallocation function shall return void and its first parameter |
| 11547 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 11548 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 11549 | SemaRef.Context.VoidPtrTy, |
| 11550 | diag::err_operator_delete_dependent_param_type, |
| 11551 | diag::err_operator_delete_param_type)) |
| 11552 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 11553 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 11554 | return false; |
| 11555 | } |
| 11556 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11557 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 11558 | /// of this overloaded operator is well-formed. If so, returns false; |
| 11559 | /// otherwise, emits appropriate diagnostics and returns true. |
| 11560 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 11561 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11562 | "Expected an overloaded operator declaration"); |
| 11563 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11564 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 11565 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11566 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11567 | // The allocation and deallocation functions, operator new, |
| 11568 | // operator new[], operator delete and operator delete[], are |
| 11569 | // described completely in 3.7.3. The attributes and restrictions |
| 11570 | // found in the rest of this subclause do not apply to them unless |
| 11571 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 11572 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 11573 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 11574 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 11575 | if (Op == OO_New || Op == OO_Array_New) |
| 11576 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11577 | |
| 11578 | // C++ [over.oper]p6: |
| 11579 | // An operator function shall either be a non-static member |
| 11580 | // function or be a non-member function and have at least one |
| 11581 | // parameter whose type is a class, a reference to a class, an |
| 11582 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 11583 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 11584 | if (MethodDecl->isStatic()) |
| 11585 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11586 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11587 | } else { |
| 11588 | bool ClassOrEnumParam = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11589 | for (auto Param : FnDecl->params()) { |
| 11590 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 11591 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 11592 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11593 | ClassOrEnumParam = true; |
| 11594 | break; |
| 11595 | } |
| 11596 | } |
| 11597 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 11598 | if (!ClassOrEnumParam) |
| 11599 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11600 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11601 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11602 | } |
| 11603 | |
| 11604 | // C++ [over.oper]p8: |
| 11605 | // An operator function cannot have default arguments (8.3.6), |
| 11606 | // except where explicitly stated below. |
| 11607 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11608 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11609 | // (C++ [over.call]p1). |
| 11610 | if (Op != OO_Call) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11611 | for (auto Param : FnDecl->params()) { |
| 11612 | if (Param->hasDefaultArg()) |
| 11613 | return Diag(Param->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 11614 | diag::err_operator_overload_default_arg) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11615 | << FnDecl->getDeclName() << Param->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11616 | } |
| 11617 | } |
| 11618 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 11619 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 11620 | { false, false, false } |
| 11621 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 11622 | , { Unary, Binary, MemberOnly } |
| 11623 | #include "clang/Basic/OperatorKinds.def" |
| 11624 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11625 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 11626 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 11627 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 11628 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11629 | |
| 11630 | // C++ [over.oper]p8: |
| 11631 | // [...] Operator functions cannot have more or fewer parameters |
| 11632 | // than the number required for the corresponding operator, as |
| 11633 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11634 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 11635 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11636 | if (Op != OO_Call && |
| 11637 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 11638 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 11639 | (NumParams < 1) || (NumParams > 2))) { |
| 11640 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 11641 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 11642 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 11643 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 11644 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 11645 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 11646 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 11647 | assert(CanBeBinaryOperator && |
| 11648 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 11649 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 11650 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11651 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 11652 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11653 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11654 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 11655 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 11656 | // Overloaded operators other than operator() cannot be variadic. |
| 11657 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 11658 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11659 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11660 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11661 | } |
| 11662 | |
| 11663 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 11664 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 11665 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 11666 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 11667 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11668 | } |
| 11669 | |
| 11670 | // C++ [over.inc]p1: |
| 11671 | // The user-defined function called operator++ implements the |
| 11672 | // prefix and postfix ++ operator. If this function is a member |
| 11673 | // function with no parameters, or a non-member function with one |
| 11674 | // parameter of class or enumeration type, it defines the prefix |
| 11675 | // increment operator ++ for objects of that type. If the function |
| 11676 | // is a member function with one parameter (which shall be of type |
| 11677 | // int) or a non-member function with two parameters (the second |
| 11678 | // of which shall be of type int), it defines the postfix |
| 11679 | // increment operator ++ for objects of that type. |
| 11680 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 11681 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11682 | QualType ParamType = LastParam->getType(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11683 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11684 | if (!ParamType->isSpecificBuiltinType(BuiltinType::Int) && |
| 11685 | !ParamType->isDependentType()) |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 11686 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11687 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 11688 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11689 | } |
| 11690 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 11691 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 11692 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 11693 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11694 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 11695 | /// of this literal operator function is well-formed. If so, returns |
| 11696 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 11697 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
Richard Smith | e5658f0 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 11698 | if (isa<CXXMethodDecl>(FnDecl)) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11699 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 11700 | << FnDecl->getDeclName(); |
| 11701 | return true; |
| 11702 | } |
| 11703 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 11704 | if (FnDecl->isExternC()) { |
| 11705 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 11706 | return true; |
| 11707 | } |
| 11708 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11709 | bool Valid = false; |
| 11710 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11711 | // This might be the definition of a literal operator template. |
| 11712 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 11713 | // This might be a specialization of a literal operator template. |
| 11714 | if (!TpDecl) |
| 11715 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 11716 | |
Richard Smith | b328e29 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 11717 | // template <char...> type operator "" name() and |
| 11718 | // template <class T, T...> type operator "" name() are the only valid |
| 11719 | // template signatures, and the only valid signatures with no parameters. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 11720 | if (TpDecl) { |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 11721 | if (FnDecl->param_size() == 0) { |
Richard Smith | b328e29 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 11722 | // Must have one or two template parameters |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 11723 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 11724 | if (Params->size() == 1) { |
| 11725 | NonTypeTemplateParmDecl *PmDecl = |
Richard Smith | 5295b97 | 2012-08-03 21:14:57 +0000 | [diff] [blame] | 11726 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11727 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 11728 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 11729 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 11730 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 11731 | Valid = true; |
Richard Smith | b328e29 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 11732 | } else if (Params->size() == 2) { |
| 11733 | TemplateTypeParmDecl *PmType = |
| 11734 | dyn_cast<TemplateTypeParmDecl>(Params->getParam(0)); |
| 11735 | NonTypeTemplateParmDecl *PmArgs = |
| 11736 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(1)); |
| 11737 | |
| 11738 | // The second template parameter must be a parameter pack with the |
| 11739 | // first template parameter as its type. |
| 11740 | if (PmType && PmArgs && |
| 11741 | !PmType->isTemplateParameterPack() && |
| 11742 | PmArgs->isTemplateParameterPack()) { |
| 11743 | const TemplateTypeParmType *TArgs = |
| 11744 | PmArgs->getType()->getAs<TemplateTypeParmType>(); |
| 11745 | if (TArgs && TArgs->getDepth() == PmType->getDepth() && |
| 11746 | TArgs->getIndex() == PmType->getIndex()) { |
| 11747 | Valid = true; |
| 11748 | if (ActiveTemplateInstantiations.empty()) |
| 11749 | Diag(FnDecl->getLocation(), |
| 11750 | diag::ext_string_literal_operator_template); |
| 11751 | } |
| 11752 | } |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 11753 | } |
| 11754 | } |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 11755 | } else if (FnDecl->param_size()) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11756 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 11757 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 11758 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 11759 | QualType T = (*Param)->getType().getUnqualifiedType(); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11760 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 11761 | // unsigned long long int, long double, and any character type are allowed |
| 11762 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11763 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 11764 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 11765 | Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 11766 | Context.hasSameType(T, Context.WideCharTy) || |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11767 | Context.hasSameType(T, Context.Char16Ty) || |
| 11768 | Context.hasSameType(T, Context.Char32Ty)) { |
| 11769 | if (++Param == FnDecl->param_end()) |
| 11770 | Valid = true; |
| 11771 | goto FinishedParams; |
| 11772 | } |
| 11773 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 11774 | // 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] | 11775 | const PointerType *PT = T->getAs<PointerType>(); |
| 11776 | if (!PT) |
| 11777 | goto FinishedParams; |
| 11778 | T = PT->getPointeeType(); |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 11779 | if (!T.isConstQualified() || T.isVolatileQualified()) |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11780 | goto FinishedParams; |
| 11781 | T = T.getUnqualifiedType(); |
| 11782 | |
| 11783 | // Move on to the second parameter; |
| 11784 | ++Param; |
| 11785 | |
| 11786 | // If there is no second parameter, the first must be a const char * |
| 11787 | if (Param == FnDecl->param_end()) { |
| 11788 | if (Context.hasSameType(T, Context.CharTy)) |
| 11789 | Valid = true; |
| 11790 | goto FinishedParams; |
| 11791 | } |
| 11792 | |
| 11793 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 11794 | // are allowed as the first parameter to a two-parameter function |
| 11795 | if (!(Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 11796 | Context.hasSameType(T, Context.WideCharTy) || |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11797 | Context.hasSameType(T, Context.Char16Ty) || |
| 11798 | Context.hasSameType(T, Context.Char32Ty))) |
| 11799 | goto FinishedParams; |
| 11800 | |
| 11801 | // The second and final parameter must be an std::size_t |
| 11802 | T = (*Param)->getType().getUnqualifiedType(); |
| 11803 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 11804 | ++Param == FnDecl->param_end()) |
| 11805 | Valid = true; |
| 11806 | } |
| 11807 | |
| 11808 | // FIXME: This diagnostic is absolutely terrible. |
| 11809 | FinishedParams: |
| 11810 | if (!Valid) { |
| 11811 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 11812 | << FnDecl->getDeclName(); |
| 11813 | return true; |
| 11814 | } |
| 11815 | |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 11816 | // A parameter-declaration-clause containing a default argument is not |
| 11817 | // equivalent to any of the permitted forms. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11818 | for (auto Param : FnDecl->params()) { |
| 11819 | if (Param->hasDefaultArg()) { |
| 11820 | Diag(Param->getDefaultArgRange().getBegin(), |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 11821 | diag::err_literal_operator_default_argument) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11822 | << Param->getDefaultArgRange(); |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 11823 | break; |
| 11824 | } |
| 11825 | } |
| 11826 | |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 11827 | StringRef LiteralName |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 11828 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 11829 | if (LiteralName[0] != '_') { |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 11830 | // C++11 [usrlit.suffix]p1: |
| 11831 | // Literal suffix identifiers that do not start with an underscore |
| 11832 | // are reserved for future standardization. |
Richard Smith | 4ac537b | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 11833 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved) |
| 11834 | << NumericLiteralParser::isValidUDSuffix(getLangOpts(), LiteralName); |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 11835 | } |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 11836 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11837 | return false; |
| 11838 | } |
| 11839 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11840 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 11841 | /// linkage specification, including the language and (if present) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11842 | /// the '{'. ExternLoc is the location of the 'extern', Lang is the |
| 11843 | /// language string literal. LBraceLoc, if valid, provides the location of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11844 | /// the '{' brace. Otherwise, this linkage specification does not |
| 11845 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 11846 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11847 | Expr *LangStr, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 11848 | SourceLocation LBraceLoc) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11849 | StringLiteral *Lit = cast<StringLiteral>(LangStr); |
| 11850 | if (!Lit->isAscii()) { |
| 11851 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_not_ascii) |
| 11852 | << LangStr->getSourceRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11853 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11854 | } |
| 11855 | |
| 11856 | StringRef Lang = Lit->getString(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11857 | LinkageSpecDecl::LanguageIDs Language; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11858 | if (Lang == "C") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11859 | Language = LinkageSpecDecl::lang_c; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11860 | else if (Lang == "C++") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11861 | Language = LinkageSpecDecl::lang_cxx; |
| 11862 | else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11863 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_unknown) |
| 11864 | << LangStr->getSourceRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 11865 | return nullptr; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11867 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11868 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11869 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11870 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, ExternLoc, |
| 11871 | LangStr->getExprLoc(), Language, |
Rafael Espindola | e5e575d | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 11872 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11873 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11874 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11875 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11876 | } |
| 11877 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 11878 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11879 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 11880 | /// valid, it's the position of the closing '}' brace in a linkage |
| 11881 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11882 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 11883 | Decl *LinkageSpec, |
| 11884 | SourceLocation RBraceLoc) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11885 | if (RBraceLoc.isValid()) { |
| 11886 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 11887 | LSDecl->setRBraceLoc(RBraceLoc); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 11888 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 11889 | PopDeclContext(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11890 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 11891 | } |
| 11892 | |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 11893 | Decl *Sema::ActOnEmptyDeclaration(Scope *S, |
| 11894 | AttributeList *AttrList, |
| 11895 | SourceLocation SemiLoc) { |
| 11896 | Decl *ED = EmptyDecl::Create(Context, CurContext, SemiLoc); |
| 11897 | // Attribute declarations appertain to empty declaration so we handle |
| 11898 | // them here. |
| 11899 | if (AttrList) |
| 11900 | ProcessDeclAttributeList(S, ED, AttrList); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 11901 | |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 11902 | CurContext->addDecl(ED); |
| 11903 | return ED; |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 11904 | } |
| 11905 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11906 | /// \brief Perform semantic analysis for the variable declaration that |
| 11907 | /// occurs within a C++ catch clause, returning the newly-created |
| 11908 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11909 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11910 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11911 | SourceLocation StartLoc, |
| 11912 | SourceLocation Loc, |
| 11913 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11914 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11915 | QualType ExDeclType = TInfo->getType(); |
| 11916 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11917 | // Arrays and functions decay. |
| 11918 | if (ExDeclType->isArrayType()) |
| 11919 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 11920 | else if (ExDeclType->isFunctionType()) |
| 11921 | ExDeclType = Context.getPointerType(ExDeclType); |
| 11922 | |
| 11923 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 11924 | // The exception-declaration shall not denote a pointer or reference to an |
| 11925 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11926 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11927 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11928 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11929 | Invalid = true; |
| 11930 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11931 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11932 | QualType BaseType = ExDeclType; |
| 11933 | 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] | 11934 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11935 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11936 | BaseType = Ptr->getPointeeType(); |
| 11937 | Mode = 1; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11938 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11939 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11940 | // 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] | 11941 | BaseType = Ref->getPointeeType(); |
| 11942 | Mode = 2; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11943 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11944 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11945 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11946 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11947 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11948 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11949 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11950 | RequireNonAbstractType(Loc, ExDeclType, |
| 11951 | diag::err_abstract_type_in_decl, |
| 11952 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 11953 | Invalid = true; |
| 11954 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11955 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 11956 | // of ObjC types, and no runtime supports catching ObjC types by value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11957 | if (!Invalid && getLangOpts().ObjC1) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11958 | QualType T = ExDeclType; |
| 11959 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 11960 | T = RT->getPointeeType(); |
| 11961 | |
| 11962 | if (T->isObjCObjectType()) { |
| 11963 | Diag(Loc, diag::err_objc_object_catch); |
| 11964 | Invalid = true; |
| 11965 | } else if (T->isObjCObjectPointerType()) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11966 | // FIXME: should this be a test for macosx-fragile specifically? |
| 11967 | if (getLangOpts().ObjCRuntime.isFragile()) |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 11968 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11969 | } |
| 11970 | } |
| 11971 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11972 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 11973 | ExDeclType, TInfo, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 11974 | ExDecl->setExceptionVariable(true); |
| 11975 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 11976 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11977 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 11978 | Invalid = true; |
| 11979 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 11980 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11981 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
John McCall | b760f11 | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 11982 | // Insulate this from anything else we might currently be parsing. |
| 11983 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
| 11984 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11985 | // C++ [except.handle]p16: |
Nick Lewycky | ee0bc3b | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11986 | // The object declared in an exception-declaration or, if the |
| 11987 | // exception-declaration does not specify a name, a temporary (12.2) is |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11988 | // copy-initialized (8.5) from the exception object. [...] |
| 11989 | // The object is destroyed when the handler exits, after the destruction |
| 11990 | // of any automatic objects initialized within the handler. |
| 11991 | // |
Nick Lewycky | ee0bc3b | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11992 | // We just pretend to initialize the object with itself, then make sure |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11993 | // it can be destroyed later. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 11994 | QualType initType = Context.getExceptionObjectType(ExDeclType); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11995 | |
| 11996 | InitializedEntity entity = |
| 11997 | InitializedEntity::InitializeVariable(ExDecl); |
| 11998 | InitializationKind initKind = |
| 11999 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 12000 | |
| 12001 | Expr *opaqueValue = |
| 12002 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 12003 | InitializationSequence sequence(*this, entity, initKind, opaqueValue); |
| 12004 | ExprResult result = sequence.Perform(*this, entity, initKind, opaqueValue); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 12005 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 12006 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 12007 | else { |
| 12008 | // If the constructor used was non-trivial, set this as the |
| 12009 | // "initializer". |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12010 | CXXConstructExpr *construct = result.getAs<CXXConstructExpr>(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 12011 | if (!construct->getConstructor()->isTrivial()) { |
| 12012 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 12013 | ExDecl->setInit(init); |
| 12014 | } |
| 12015 | |
| 12016 | // And make sure it's destructable. |
| 12017 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 12018 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 12019 | } |
| 12020 | } |
| 12021 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 12022 | if (Invalid) |
| 12023 | ExDecl->setInvalidDecl(); |
| 12024 | |
| 12025 | return ExDecl; |
| 12026 | } |
| 12027 | |
| 12028 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 12029 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12030 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 12031 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 12032 | bool Invalid = D.isInvalidType(); |
| 12033 | |
| 12034 | // Check for unexpanded parameter packs. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 12035 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 12036 | UPPC_ExceptionType)) { |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 12037 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 12038 | D.getIdentifierLoc()); |
| 12039 | Invalid = true; |
| 12040 | } |
| 12041 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12042 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 12043 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 12044 | LookupOrdinaryName, |
| 12045 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12046 | // The scope should be freshly made just for us. There is just no way |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12047 | // it contains any previous declaration, except for function parameters in |
| 12048 | // a function-try-block's catch statement. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12049 | assert(!S->isDeclScope(PrevDecl)); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12050 | if (isDeclInScope(PrevDecl, CurContext, S)) { |
| 12051 | Diag(D.getIdentifierLoc(), diag::err_redefinition) |
| 12052 | << D.getIdentifier(); |
| 12053 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 12054 | Invalid = true; |
| 12055 | } else if (PrevDecl->isTemplateParameter()) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12056 | // Maybe we will complain about the shadowed template parameter. |
| 12057 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12058 | } |
| 12059 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12060 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12061 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 12062 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12063 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12064 | } |
| 12065 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 12066 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 12067 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 12068 | D.getIdentifierLoc(), |
| 12069 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 12070 | if (Invalid) |
| 12071 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12072 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12073 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12074 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 12075 | PushOnScopeChains(ExDecl, S); |
| 12076 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12077 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12078 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 12079 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12080 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 12081 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 12082 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 12083 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 12084 | Expr *AssertExpr, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12085 | Expr *AssertMessageExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 12086 | SourceLocation RParenLoc) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12087 | StringLiteral *AssertMessage = |
| 12088 | AssertMessageExpr ? cast<StringLiteral>(AssertMessageExpr) : nullptr; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 12089 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12090 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12091 | return nullptr; |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12092 | |
| 12093 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 12094 | AssertMessage, RParenLoc, false); |
| 12095 | } |
| 12096 | |
| 12097 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 12098 | Expr *AssertExpr, |
| 12099 | StringLiteral *AssertMessage, |
| 12100 | SourceLocation RParenLoc, |
| 12101 | bool Failed) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12102 | assert(AssertExpr != nullptr && "Expected non-null condition"); |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12103 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 12104 | !Failed) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12105 | // In a static_assert-declaration, the constant-expression shall be a |
| 12106 | // constant expression that can be contextually converted to bool. |
| 12107 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 12108 | if (Converted.isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12109 | Failed = true; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12110 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 12111 | llvm::APSInt Cond; |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12112 | if (!Failed && VerifyIntegerConstantExpression(Converted.get(), &Cond, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 12113 | diag::err_static_assert_expression_is_not_constant, |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 12114 | /*AllowFold=*/false).isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12115 | Failed = true; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 12116 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12117 | if (!Failed && !Cond) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12118 | SmallString<256> MsgBuffer; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 12119 | llvm::raw_svector_ostream Msg(MsgBuffer); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12120 | if (AssertMessage) |
| 12121 | AssertMessage->printPretty(Msg, nullptr, getPrintingPolicy()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 12122 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12123 | << !AssertMessage << Msg.str() << AssertExpr->getSourceRange(); |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12124 | Failed = true; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 12125 | } |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 12126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12127 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 12128 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 12129 | AssertExpr, AssertMessage, RParenLoc, |
| 12130 | Failed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12131 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 12132 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12133 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 12134 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 12135 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12136 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 12137 | /// |
| 12138 | /// \returns A friend declaration that. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 12139 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 12140 | SourceLocation FriendLoc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12141 | TypeSourceInfo *TSInfo) { |
| 12142 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 12143 | |
| 12144 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 12145 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12146 | |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 12147 | // C++03 [class.friend]p2: |
| 12148 | // An elaborated-type-specifier shall be used in a friend declaration |
| 12149 | // for a class.* |
| 12150 | // |
| 12151 | // * The class-key of the elaborated-type-specifier is required. |
| 12152 | if (!ActiveTemplateInstantiations.empty()) { |
| 12153 | // Do not complain about the form of friend template types during |
| 12154 | // template instantiation; we will already have complained when the |
| 12155 | // template was declared. |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 12156 | } else { |
| 12157 | if (!T->isElaboratedTypeSpecifier()) { |
| 12158 | // If we evaluated the type to a record type, suggest putting |
| 12159 | // a tag in front. |
| 12160 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 12161 | RecordDecl *RD = RT->getDecl(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12162 | |
| 12163 | SmallString<16> InsertionText(" "); |
| 12164 | InsertionText += RD->getKindName(); |
| 12165 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 12166 | Diag(TypeRange.getBegin(), |
| 12167 | getLangOpts().CPlusPlus11 ? |
| 12168 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 12169 | diag::ext_unelaborated_friend_type) |
| 12170 | << (unsigned) RD->getTagKind() |
| 12171 | << T |
| 12172 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 12173 | InsertionText); |
| 12174 | } else { |
| 12175 | Diag(FriendLoc, |
| 12176 | getLangOpts().CPlusPlus11 ? |
| 12177 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 12178 | diag::ext_nonclass_type_friend) |
| 12179 | << T |
| 12180 | << TypeRange; |
| 12181 | } |
| 12182 | } else if (T->getAs<EnumType>()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 12183 | Diag(FriendLoc, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12184 | getLangOpts().CPlusPlus11 ? |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 12185 | diag::warn_cxx98_compat_enum_friend : |
| 12186 | diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12187 | << T |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 12188 | << TypeRange; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12189 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12190 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 12191 | // C++11 [class.friend]p3: |
| 12192 | // A friend declaration that does not declare a function shall have one |
| 12193 | // of the following forms: |
| 12194 | // friend elaborated-type-specifier ; |
| 12195 | // friend simple-type-specifier ; |
| 12196 | // friend typename-specifier ; |
| 12197 | if (getLangOpts().CPlusPlus11 && LocStart != FriendLoc) |
| 12198 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 12199 | } |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 12200 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 12201 | // If the type specifier in a friend declaration designates a (possibly |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 12202 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 12203 | // the friend declaration is ignored. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12204 | return FriendDecl::Create(Context, CurContext, |
| 12205 | TSInfo->getTypeLoc().getLocStart(), TSInfo, |
| 12206 | FriendLoc); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12207 | } |
| 12208 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12209 | /// Handle a friend tag declaration where the scope specifier was |
| 12210 | /// templated. |
| 12211 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 12212 | unsigned TagSpec, SourceLocation TagLoc, |
| 12213 | CXXScopeSpec &SS, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 12214 | IdentifierInfo *Name, |
| 12215 | SourceLocation NameLoc, |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12216 | AttributeList *Attr, |
| 12217 | MultiTemplateParamsArg TempParamLists) { |
| 12218 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 12219 | |
| 12220 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12221 | bool Invalid = false; |
| 12222 | |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 12223 | if (TemplateParameterList *TemplateParams = |
| 12224 | MatchTemplateParametersToScopeSpecifier( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12225 | TagLoc, NameLoc, SS, nullptr, TempParamLists, /*friend*/ true, |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 12226 | isExplicitSpecialization, Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12227 | if (TemplateParams->size() > 0) { |
| 12228 | // This is a declaration of a class template. |
| 12229 | if (Invalid) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12230 | return nullptr; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 12231 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 12232 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, SS, Name, |
| 12233 | NameLoc, Attr, TemplateParams, AS_public, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 12234 | /*ModulePrivateLoc=*/SourceLocation(), |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 12235 | FriendLoc, TempParamLists.size() - 1, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12236 | TempParamLists.data()).get(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12237 | } else { |
| 12238 | // The "template<>" header is extraneous. |
| 12239 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 12240 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 12241 | isExplicitSpecialization = true; |
| 12242 | } |
| 12243 | } |
| 12244 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12245 | if (Invalid) return nullptr; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12246 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12247 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 12248 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 12249 | if (TempParamLists[I]->size()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12250 | isAllExplicitSpecializations = false; |
| 12251 | break; |
| 12252 | } |
| 12253 | } |
| 12254 | |
| 12255 | // FIXME: don't ignore attributes. |
| 12256 | |
| 12257 | // If it's explicit specializations all the way down, just forget |
| 12258 | // about the template header and build an appropriate non-templated |
| 12259 | // friend. TODO: for source fidelity, remember the headers. |
| 12260 | if (isAllExplicitSpecializations) { |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 12261 | if (SS.isEmpty()) { |
| 12262 | bool Owned = false; |
| 12263 | bool IsDependent = false; |
| 12264 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12265 | Attr, AS_public, |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 12266 | /*ModulePrivateLoc=*/SourceLocation(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12267 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 12268 | /*ScopedEnumKWLoc=*/SourceLocation(), |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 12269 | /*ScopedEnumUsesClassTag=*/false, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12270 | /*UnderlyingType=*/TypeResult(), |
| 12271 | /*IsTypeSpecifier=*/false); |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 12272 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12273 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 12274 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12275 | ElaboratedTypeKeyword Keyword |
| 12276 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 12277 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 12278 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12279 | if (T.isNull()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12280 | return nullptr; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12281 | |
| 12282 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 12283 | if (isa<DependentNameType>(T)) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12284 | DependentNameTypeLoc TL = |
| 12285 | TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 12286 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 12287 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12288 | TL.setNameLoc(NameLoc); |
| 12289 | } else { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12290 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 12291 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 12292 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12293 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12294 | } |
| 12295 | |
| 12296 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 12297 | TSI, FriendLoc, TempParamLists); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12298 | Friend->setAccess(AS_public); |
| 12299 | CurContext->addDecl(Friend); |
| 12300 | return Friend; |
| 12301 | } |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 12302 | |
| 12303 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 12304 | |
| 12305 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12306 | |
| 12307 | // Handle the case of a templated-scope friend class. e.g. |
| 12308 | // template <class T> class A<T>::B; |
| 12309 | // FIXME: we don't support these right now. |
Richard Smith | ce6426f | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 12310 | Diag(NameLoc, diag::warn_template_qualified_friend_unsupported) |
| 12311 | << SS.getScopeRep() << SS.getRange() << cast<CXXRecordDecl>(CurContext); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12312 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 12313 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 12314 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12315 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 12316 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 12317 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12318 | TL.setNameLoc(NameLoc); |
| 12319 | |
| 12320 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 12321 | TSI, FriendLoc, TempParamLists); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 12322 | Friend->setAccess(AS_public); |
| 12323 | Friend->setUnsupportedFriend(true); |
| 12324 | CurContext->addDecl(Friend); |
| 12325 | return Friend; |
| 12326 | } |
| 12327 | |
| 12328 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12329 | /// Handle a friend type declaration. This works in tandem with |
| 12330 | /// ActOnTag. |
| 12331 | /// |
| 12332 | /// Notes on friend class templates: |
| 12333 | /// |
| 12334 | /// We generally treat friend class declarations as if they were |
| 12335 | /// declaring a class. So, for example, the elaborated type specifier |
| 12336 | /// in a friend declaration is required to obey the restrictions of a |
| 12337 | /// class-head (i.e. no typedefs in the scope chain), template |
| 12338 | /// parameters are required to match up with simple template-ids, &c. |
| 12339 | /// However, unlike when declaring a template specialization, it's |
| 12340 | /// okay to refer to a template specialization without an empty |
| 12341 | /// template parameter declaration, e.g. |
| 12342 | /// friend class A<T>::B<unsigned>; |
| 12343 | /// We permit this as a special case; if there are any template |
| 12344 | /// parameters present at all, require proper matching, i.e. |
James Dennett | ef2b5b3 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 12345 | /// template <> template \<class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12346 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 12347 | MultiTemplateParamsArg TempParams) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 12348 | SourceLocation Loc = DS.getLocStart(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12349 | |
| 12350 | assert(DS.isFriendSpecified()); |
| 12351 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 12352 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12353 | // Try to convert the decl specifier to a type. This works for |
| 12354 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 12355 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 12356 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 12357 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 12358 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 12359 | if (TheDeclarator.isInvalidType()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12360 | return nullptr; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12361 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 12362 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12363 | return nullptr; |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 12364 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12365 | // This is definitely an error in C++98. It's probably meant to |
| 12366 | // be forbidden in C++0x, too, but the specification is just |
| 12367 | // poorly written. |
| 12368 | // |
| 12369 | // The problem is with declarations like the following: |
| 12370 | // template <T> friend A<T>::foo; |
| 12371 | // where deciding whether a class C is a friend or not now hinges |
| 12372 | // on whether there exists an instantiation of A that causes |
| 12373 | // 'foo' to equal C. There are restrictions on class-heads |
| 12374 | // (which we declare (by fiat) elaborated friend declarations to |
| 12375 | // be) that makes this tractable. |
| 12376 | // |
| 12377 | // FIXME: handle "template <> friend class A<T>;", which |
| 12378 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 12379 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12380 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 12381 | << DS.getSourceRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12382 | return nullptr; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12383 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12384 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12385 | // C++98 [class.friend]p1: A friend of a class is a function |
| 12386 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 12387 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 12388 | // deadline. It's also a very silly restriction that seriously |
| 12389 | // affects inner classes and which nobody else seems to implement; |
| 12390 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 12391 | // |
| 12392 | // But note that we could warn about it: it's always useless to |
| 12393 | // friend one of your own members (it's not, however, worthless to |
| 12394 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12395 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12396 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12397 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12398 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12399 | NumTempParamLists, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 12400 | TempParams.data(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 12401 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12402 | DS.getFriendSpecLoc()); |
| 12403 | else |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 12404 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 12405 | |
| 12406 | if (!D) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12407 | return nullptr; |
| 12408 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 12409 | D->setAccess(AS_public); |
| 12410 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12411 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12412 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12413 | } |
| 12414 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 12415 | NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
| 12416 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12417 | const DeclSpec &DS = D.getDeclSpec(); |
| 12418 | |
| 12419 | assert(DS.isFriendSpecified()); |
| 12420 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 12421 | |
| 12422 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 12423 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12424 | |
| 12425 | // C++ [class.friend]p1 |
| 12426 | // A friend of a class is a function or class.... |
| 12427 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12428 | // It *doesn't* see through dependent types, which is correct |
| 12429 | // according to [temp.arg.type]p3: |
| 12430 | // If a declaration acquires a function type through a |
| 12431 | // type dependent on a template-parameter and this causes |
| 12432 | // a declaration that does not use the syntactic form of a |
| 12433 | // function declarator to have a function type, the program |
| 12434 | // is ill-formed. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 12435 | if (!TInfo->getType()->isFunctionType()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12436 | Diag(Loc, diag::err_unexpected_friend); |
| 12437 | |
| 12438 | // It might be worthwhile to try to recover by creating an |
| 12439 | // appropriate declaration. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12440 | return nullptr; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12441 | } |
| 12442 | |
| 12443 | // C++ [namespace.memdef]p3 |
| 12444 | // - If a friend declaration in a non-local class first declares a |
| 12445 | // class or function, the friend class or function is a member |
| 12446 | // of the innermost enclosing namespace. |
| 12447 | // - The name of the friend is not found by simple name lookup |
| 12448 | // until a matching declaration is provided in that namespace |
| 12449 | // scope (either before or after the class declaration granting |
| 12450 | // friendship). |
| 12451 | // - If a friend function is called, its name may be found by the |
| 12452 | // name lookup that considers functions from namespaces and |
| 12453 | // classes associated with the types of the function arguments. |
| 12454 | // - When looking for a prior declaration of a class or a function |
| 12455 | // declared as a friend, scopes outside the innermost enclosing |
| 12456 | // namespace scope are not considered. |
| 12457 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12458 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 12459 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 12460 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12461 | assert(Name); |
| 12462 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 12463 | // Check for unexpanded parameter packs. |
| 12464 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 12465 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 12466 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12467 | return nullptr; |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 12468 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12469 | // The context we found the declaration in, or in which we should |
| 12470 | // create the declaration. |
| 12471 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 12472 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 12473 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 12474 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12475 | |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 12476 | // There are five cases here. |
| 12477 | // - There's no scope specifier and we're in a local class. Only look |
| 12478 | // for functions declared in the immediately-enclosing block scope. |
| 12479 | // We recover from invalid scope qualifiers as if they just weren't there. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12480 | FunctionDecl *FunctionContainingLocalClass = nullptr; |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 12481 | if ((SS.isInvalid() || !SS.isSet()) && |
| 12482 | (FunctionContainingLocalClass = |
| 12483 | cast<CXXRecordDecl>(CurContext)->isLocalClass())) { |
| 12484 | // C++11 [class.friend]p11: |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 12485 | // If a friend declaration appears in a local class and the name |
| 12486 | // specified is an unqualified name, a prior declaration is |
| 12487 | // looked up without considering scopes that are outside the |
| 12488 | // innermost enclosing non-class scope. For a friend function |
| 12489 | // declaration, if there is no prior declaration, the program is |
| 12490 | // ill-formed. |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 12491 | |
| 12492 | // Find the innermost enclosing non-class scope. This is the block |
| 12493 | // scope containing the local class definition (or for a nested class, |
| 12494 | // the outer local class). |
| 12495 | DCScope = S->getFnParent(); |
| 12496 | |
| 12497 | // Look up the function name in the scope. |
| 12498 | Previous.clear(LookupLocalFriendName); |
| 12499 | LookupName(Previous, S, /*AllowBuiltinCreation*/false); |
| 12500 | |
| 12501 | if (!Previous.empty()) { |
| 12502 | // All possible previous declarations must have the same context: |
| 12503 | // either they were declared at block scope or they are members of |
| 12504 | // one of the enclosing local classes. |
| 12505 | DC = Previous.getRepresentativeDecl()->getDeclContext(); |
| 12506 | } else { |
| 12507 | // This is ill-formed, but provide the context that we would have |
| 12508 | // declared the function in, if we were permitted to, for error recovery. |
| 12509 | DC = FunctionContainingLocalClass; |
| 12510 | } |
Richard Smith | a41c97a | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 12511 | adjustContextForLocalExternDecl(DC); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 12512 | |
| 12513 | // C++ [class.friend]p6: |
| 12514 | // A function can be defined in a friend declaration of a class if and |
| 12515 | // only if the class is a non-local class (9.8), the function name is |
| 12516 | // unqualified, and the function has namespace scope. |
| 12517 | if (D.isFunctionDefinition()) { |
| 12518 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 12519 | } |
| 12520 | |
| 12521 | // - There's no scope specifier, in which case we just go to the |
| 12522 | // appropriate scope and look for a function or function template |
| 12523 | // there as appropriate. |
| 12524 | } else if (SS.isInvalid() || !SS.isSet()) { |
| 12525 | // C++11 [namespace.memdef]p3: |
| 12526 | // If the name in a friend declaration is neither qualified nor |
| 12527 | // a template-id and the declaration is a function or an |
| 12528 | // elaborated-type-specifier, the lookup to determine whether |
| 12529 | // the entity has been previously declared shall not consider |
| 12530 | // any scopes outside the innermost enclosing namespace. |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 12531 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12532 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 12533 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12534 | DC = CurContext; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12535 | |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 12536 | // Skip class contexts. If someone can cite chapter and verse |
| 12537 | // for this behavior, that would be nice --- it's what GCC and |
| 12538 | // EDG do, and it seems like a reasonable intent, but the spec |
| 12539 | // really only says that checks for unqualified existing |
| 12540 | // declarations should stop at the nearest enclosing namespace, |
| 12541 | // not that they should only consider the nearest enclosing |
| 12542 | // namespace. |
| 12543 | while (DC->isRecord()) |
| 12544 | DC = DC->getParent(); |
| 12545 | |
| 12546 | DeclContext *LookupDC = DC; |
| 12547 | while (LookupDC->isTransparentContext()) |
| 12548 | LookupDC = LookupDC->getParent(); |
| 12549 | |
| 12550 | while (true) { |
| 12551 | LookupQualifiedName(Previous, LookupDC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12552 | |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 12553 | if (!Previous.empty()) { |
| 12554 | DC = LookupDC; |
| 12555 | break; |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 12556 | } |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 12557 | |
| 12558 | if (isTemplateId) { |
| 12559 | if (isa<TranslationUnitDecl>(LookupDC)) break; |
| 12560 | } else { |
| 12561 | if (LookupDC->isFileContext()) break; |
| 12562 | } |
| 12563 | LookupDC = LookupDC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12564 | } |
| 12565 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 12566 | DCScope = getScopeForDeclContext(S, DC); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 12567 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12568 | // - There's a non-dependent scope specifier, in which case we |
| 12569 | // compute it and do a previous lookup there for a function |
| 12570 | // or function template. |
| 12571 | } else if (!SS.getScopeRep()->isDependent()) { |
| 12572 | DC = computeDeclContext(SS); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12573 | if (!DC) return nullptr; |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12574 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12575 | if (RequireCompleteDeclContext(SS, DC)) return nullptr; |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12576 | |
| 12577 | LookupQualifiedName(Previous, DC); |
| 12578 | |
| 12579 | // Ignore things found implicitly in the wrong scope. |
| 12580 | // TODO: better diagnostics for this case. Suggesting the right |
| 12581 | // qualified scope would be nice... |
| 12582 | LookupResult::Filter F = Previous.makeFilter(); |
| 12583 | while (F.hasNext()) { |
| 12584 | NamedDecl *D = F.next(); |
| 12585 | if (!DC->InEnclosingNamespaceSetOf( |
| 12586 | D->getDeclContext()->getRedeclContext())) |
| 12587 | F.erase(); |
| 12588 | } |
| 12589 | F.done(); |
| 12590 | |
| 12591 | if (Previous.empty()) { |
| 12592 | D.setInvalidType(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 12593 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 12594 | << Name << TInfo->getType(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12595 | return nullptr; |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12596 | } |
| 12597 | |
| 12598 | // C++ [class.friend]p1: A friend of a class is a function or |
| 12599 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 12600 | if (DC->Equals(CurContext)) |
| 12601 | Diag(DS.getFriendSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 12602 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 12603 | diag::warn_cxx98_compat_friend_is_member : |
| 12604 | diag::err_friend_is_member); |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 12605 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 12606 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 12607 | // C++ [class.friend]p6: |
| 12608 | // A function can be defined in a friend declaration of a class if and |
| 12609 | // only if the class is a non-local class (9.8), the function name is |
| 12610 | // unqualified, and the function has namespace scope. |
| 12611 | SemaDiagnosticBuilder DB |
| 12612 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 12613 | |
| 12614 | DB << SS.getScopeRep(); |
| 12615 | if (DC->isFileContext()) |
| 12616 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 12617 | SS.clear(); |
| 12618 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12619 | |
| 12620 | // - There's a scope specifier that does not match any template |
| 12621 | // parameter lists, in which case we use some arbitrary context, |
| 12622 | // create a method or method template, and wait for instantiation. |
| 12623 | // - There's a scope specifier that does match some template |
| 12624 | // parameter lists, which we don't handle right now. |
| 12625 | } else { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 12626 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 12627 | // C++ [class.friend]p6: |
| 12628 | // A function can be defined in a friend declaration of a class if and |
| 12629 | // only if the class is a non-local class (9.8), the function name is |
| 12630 | // unqualified, and the function has namespace scope. |
| 12631 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 12632 | << SS.getScopeRep(); |
| 12633 | } |
| 12634 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12635 | DC = CurContext; |
| 12636 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12637 | } |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 12638 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 12639 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12640 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 12641 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 12642 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 12643 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12644 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 12645 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 12646 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12647 | return nullptr; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12648 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12649 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 12650 | |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 12651 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 12652 | // does not contain the declaration context, i.e., in an out-of-line |
| 12653 | // definition of a class. |
| 12654 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 12655 | if (!DCScope) { |
| 12656 | FakeDCScope.setEntity(DC); |
| 12657 | DCScope = &FakeDCScope; |
| 12658 | } |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 12659 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 12660 | bool AddToScope = true; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 12661 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 12662 | TemplateParams, AddToScope); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 12663 | if (!ND) return nullptr; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 12664 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 12665 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 12666 | |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 12667 | // If we performed typo correction, we might have added a scope specifier |
| 12668 | // and changed the decl context. |
| 12669 | DC = ND->getDeclContext(); |
| 12670 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 12671 | // Add the function declaration to the appropriate lookup tables, |
| 12672 | // adjusting the redeclarations list as necessary. We don't |
| 12673 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12674 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 12675 | // Also update the scope-based lookup if the target context's |
| 12676 | // lookup context is in lexical scope. |
| 12677 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 12678 | DC = DC->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 12679 | DC->makeDeclVisibleInContext(ND); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 12680 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 12681 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 12682 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12683 | |
| 12684 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 12685 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12686 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 12687 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 12688 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 12689 | |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 12690 | if (ND->isInvalidDecl()) { |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12691 | FrD->setInvalidDecl(); |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 12692 | } else { |
| 12693 | if (DC->isRecord()) CheckFriendAccess(ND); |
| 12694 | |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 12695 | FunctionDecl *FD; |
| 12696 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 12697 | FD = FTD->getTemplatedDecl(); |
| 12698 | else |
| 12699 | FD = cast<FunctionDecl>(ND); |
| 12700 | |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 12701 | // C++11 [dcl.fct.default]p4: If a friend declaration specifies a |
| 12702 | // default argument expression, that declaration shall be a definition |
| 12703 | // and shall be the only declaration of the function or function |
| 12704 | // template in the translation unit. |
| 12705 | if (functionDeclHasDefaultArgument(FD)) { |
| 12706 | if (FunctionDecl *OldFD = FD->getPreviousDecl()) { |
| 12707 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 12708 | Diag(OldFD->getLocation(), diag::note_previous_declaration); |
| 12709 | } else if (!D.isFunctionDefinition()) |
| 12710 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_must_be_def); |
| 12711 | } |
| 12712 | |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 12713 | // Mark templated-scope function declarations as unsupported. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 12714 | if (FD->getNumTemplateParameterLists() && SS.isValid()) { |
| 12715 | Diag(FD->getLocation(), diag::warn_template_qualified_friend_unsupported) |
| 12716 | << SS.getScopeRep() << SS.getRange() |
| 12717 | << cast<CXXRecordDecl>(CurContext); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 12718 | FrD->setUnsupportedFriend(true); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 12719 | } |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 12720 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 12721 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12722 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 12723 | } |
| 12724 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12725 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 12726 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12727 | |
Aaron Ballman | afb7ce3 | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 12728 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(Dcl); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 12729 | if (!Fn) { |
| 12730 | Diag(DelLoc, diag::err_deleted_non_function); |
| 12731 | return; |
| 12732 | } |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 12733 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 12734 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 12735 | // Don't consider the implicit declaration we generate for explicit |
| 12736 | // specializations. FIXME: Do not generate these implicit declarations. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12737 | if ((Prev->getTemplateSpecializationKind() != TSK_ExplicitSpecialization || |
| 12738 | Prev->getPreviousDecl()) && |
| 12739 | !Prev->isDefined()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 12740 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12741 | Diag(Prev->getLocation().isInvalid() ? DelLoc : Prev->getLocation(), |
| 12742 | Prev->isImplicit() ? diag::note_previous_implicit_declaration |
| 12743 | : diag::note_previous_declaration); |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 12744 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 12745 | // If the declaration wasn't the first, we delete the function anyway for |
| 12746 | // recovery. |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 12747 | Fn = Fn->getCanonicalDecl(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 12748 | } |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 12749 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12750 | // dllimport/dllexport cannot be deleted. |
| 12751 | if (const InheritableAttr *DLLAttr = getDLLAttr(Fn)) { |
| 12752 | Diag(Fn->getLocation(), diag::err_attribute_dll_deleted) << DLLAttr; |
| 12753 | Fn->setInvalidDecl(); |
| 12754 | } |
| 12755 | |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 12756 | if (Fn->isDeleted()) |
| 12757 | return; |
| 12758 | |
| 12759 | // See if we're deleting a function which is already known to override a |
| 12760 | // non-deleted virtual function. |
| 12761 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn)) { |
| 12762 | bool IssuedDiagnostic = false; |
| 12763 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 12764 | E = MD->end_overridden_methods(); |
| 12765 | I != E; ++I) { |
| 12766 | if (!(*MD->begin_overridden_methods())->isDeleted()) { |
| 12767 | if (!IssuedDiagnostic) { |
| 12768 | Diag(DelLoc, diag::err_deleted_override) << MD->getDeclName(); |
| 12769 | IssuedDiagnostic = true; |
| 12770 | } |
| 12771 | Diag((*I)->getLocation(), diag::note_overridden_virtual_function); |
| 12772 | } |
| 12773 | } |
| 12774 | } |
| 12775 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12776 | // C++11 [basic.start.main]p3: |
| 12777 | // A program that defines main as deleted [...] is ill-formed. |
| 12778 | if (Fn->isMain()) |
| 12779 | Diag(DelLoc, diag::err_deleted_main); |
| 12780 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 12781 | Fn->setDeletedAsWritten(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 12782 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12783 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12784 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
Aaron Ballman | afb7ce3 | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 12785 | CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Dcl); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12786 | |
| 12787 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 12788 | if (MD->getParent()->isDependentType()) { |
| 12789 | MD->setDefaulted(); |
| 12790 | MD->setExplicitlyDefaulted(); |
| 12791 | return; |
| 12792 | } |
| 12793 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12794 | CXXSpecialMember Member = getSpecialMember(MD); |
| 12795 | if (Member == CXXInvalid) { |
Eli Friedman | fcb5a25 | 2013-07-11 23:55:07 +0000 | [diff] [blame] | 12796 | if (!MD->isInvalidDecl()) |
| 12797 | Diag(DefaultLoc, diag::err_default_special_members); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12798 | return; |
| 12799 | } |
| 12800 | |
| 12801 | MD->setDefaulted(); |
| 12802 | MD->setExplicitlyDefaulted(); |
| 12803 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12804 | // If this definition appears within the record, do the checking when |
| 12805 | // the record is complete. |
| 12806 | const FunctionDecl *Primary = MD; |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12807 | if (const FunctionDecl *Pattern = MD->getTemplateInstantiationPattern()) |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12808 | // Find the uninstantiated declaration that actually had the '= default' |
| 12809 | // on it. |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12810 | Pattern->isDefined(Primary); |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12811 | |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 12812 | // If the method was defaulted on its first declaration, we will have |
| 12813 | // already performed the checking in CheckCompletedCXXClass. Such a |
| 12814 | // declaration doesn't trigger an implicit definition. |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12815 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12816 | return; |
| 12817 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12818 | CheckExplicitlyDefaultedSpecialMember(MD); |
| 12819 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12820 | if (MD->isInvalidDecl()) |
| 12821 | return; |
| 12822 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12823 | switch (Member) { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12824 | case CXXDefaultConstructor: |
| 12825 | DefineImplicitDefaultConstructor(DefaultLoc, |
| 12826 | cast<CXXConstructorDecl>(MD)); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 12827 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12828 | case CXXCopyConstructor: |
| 12829 | DefineImplicitCopyConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12830 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12831 | case CXXCopyAssignment: |
| 12832 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 12833 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12834 | case CXXDestructor: |
| 12835 | DefineImplicitDestructor(DefaultLoc, cast<CXXDestructorDecl>(MD)); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 12836 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12837 | case CXXMoveConstructor: |
| 12838 | DefineImplicitMoveConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 12839 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12840 | case CXXMoveAssignment: |
| 12841 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 12842 | break; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 12843 | case CXXInvalid: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 12844 | llvm_unreachable("Invalid special member."); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12845 | } |
| 12846 | } else { |
| 12847 | Diag(DefaultLoc, diag::err_default_special_members); |
| 12848 | } |
| 12849 | } |
| 12850 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12851 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 12852 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12853 | Stmt *SubStmt = *CI; |
| 12854 | if (!SubStmt) |
| 12855 | continue; |
| 12856 | if (isa<ReturnStmt>(SubStmt)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 12857 | Self.Diag(SubStmt->getLocStart(), |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12858 | diag::err_return_in_constructor_handler); |
| 12859 | if (!isa<Expr>(SubStmt)) |
| 12860 | SearchForReturnInStmt(Self, SubStmt); |
| 12861 | } |
| 12862 | } |
| 12863 | |
| 12864 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 12865 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 12866 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 12867 | SearchForReturnInStmt(*this, Handler); |
| 12868 | } |
| 12869 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12870 | |
David Blaikie | 299adab | 2013-01-18 23:03:15 +0000 | [diff] [blame] | 12871 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 12872 | const CXXMethodDecl *Old) { |
| 12873 | const FunctionType *NewFT = New->getType()->getAs<FunctionType>(); |
| 12874 | const FunctionType *OldFT = Old->getType()->getAs<FunctionType>(); |
| 12875 | |
| 12876 | CallingConv NewCC = NewFT->getCallConv(), OldCC = OldFT->getCallConv(); |
| 12877 | |
| 12878 | // If the calling conventions match, everything is fine |
| 12879 | if (NewCC == OldCC) |
| 12880 | return false; |
| 12881 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12882 | // If the calling conventions mismatch because the new function is static, |
| 12883 | // suppress the calling convention mismatch error; the error about static |
| 12884 | // function override (err_static_overrides_virtual from |
| 12885 | // Sema::CheckFunctionDeclaration) is more clear. |
| 12886 | if (New->getStorageClass() == SC_Static) |
| 12887 | return false; |
| 12888 | |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 12889 | Diag(New->getLocation(), |
| 12890 | diag::err_conflicting_overriding_cc_attributes) |
| 12891 | << New->getDeclName() << New->getType() << Old->getType(); |
| 12892 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12893 | return true; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 12894 | } |
| 12895 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12896 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12897 | const CXXMethodDecl *Old) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 12898 | QualType NewTy = New->getType()->getAs<FunctionType>()->getReturnType(); |
| 12899 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getReturnType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12900 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 12901 | if (Context.hasSameType(NewTy, OldTy) || |
| 12902 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12903 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12904 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12905 | // Check if the return types are covariant |
| 12906 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12907 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12908 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12909 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 12910 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12911 | NewClassTy = NewPT->getPointeeType(); |
| 12912 | OldClassTy = OldPT->getPointeeType(); |
| 12913 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12914 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 12915 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 12916 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 12917 | NewClassTy = NewRT->getPointeeType(); |
| 12918 | OldClassTy = OldRT->getPointeeType(); |
| 12919 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12920 | } |
| 12921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12922 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12923 | // The return types aren't either both pointers or references to a class type. |
| 12924 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12925 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12926 | diag::err_different_return_type_for_overriding_virtual_function) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12927 | << New->getDeclName() << NewTy << OldTy |
| 12928 | << New->getReturnTypeSourceRange(); |
| 12929 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 12930 | << Old->getReturnTypeSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12931 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12932 | return true; |
| 12933 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12934 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12935 | // C++ [class.virtual]p6: |
| 12936 | // If the return type of D::f differs from the return type of B::f, the |
| 12937 | // class type in the return type of D::f shall be complete at the point of |
| 12938 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 12939 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 12940 | if (!RT->isBeingDefined() && |
| 12941 | RequireCompleteType(New->getLocation(), NewClassTy, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 12942 | diag::err_covariant_return_incomplete, |
| 12943 | New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12944 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 12945 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12946 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 12947 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12948 | // Check if the new class derives from the old class. |
| 12949 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12950 | Diag(New->getLocation(), diag::err_covariant_return_not_derived) |
| 12951 | << New->getDeclName() << NewTy << OldTy |
| 12952 | << New->getReturnTypeSourceRange(); |
| 12953 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 12954 | << Old->getReturnTypeSourceRange(); |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12955 | return true; |
| 12956 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12957 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12958 | // Check if we the conversion from derived to base is valid. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12959 | if (CheckDerivedToBaseConversion( |
| 12960 | NewClassTy, OldClassTy, |
| 12961 | diag::err_covariant_return_inaccessible_base, |
| 12962 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 12963 | New->getLocation(), New->getReturnTypeSourceRange(), |
| 12964 | New->getDeclName(), nullptr)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 12965 | // FIXME: this note won't trigger for delayed access control |
| 12966 | // diagnostics, and it's impossible to get an undelayed error |
| 12967 | // here from access control during the original parse because |
| 12968 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12969 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 12970 | << Old->getReturnTypeSourceRange(); |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12971 | return true; |
| 12972 | } |
| 12973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12974 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12975 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12976 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12977 | Diag(New->getLocation(), |
| 12978 | diag::err_covariant_return_type_different_qualifications) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12979 | << New->getDeclName() << NewTy << OldTy |
| 12980 | << New->getReturnTypeSourceRange(); |
| 12981 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 12982 | << Old->getReturnTypeSourceRange(); |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12983 | return true; |
| 12984 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12985 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12986 | |
| 12987 | // The new class type must have the same or less qualifiers as the old type. |
| 12988 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 12989 | Diag(New->getLocation(), |
| 12990 | diag::err_covariant_return_type_class_type_more_qualified) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 12991 | << New->getDeclName() << NewTy << OldTy |
| 12992 | << New->getReturnTypeSourceRange(); |
| 12993 | Diag(Old->getLocation(), diag::note_overridden_virtual_function) |
| 12994 | << Old->getReturnTypeSourceRange(); |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12995 | return true; |
| 12996 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12997 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12998 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12999 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13000 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 13001 | /// \brief Mark the given method pure. |
| 13002 | /// |
| 13003 | /// \param Method the method to be marked pure. |
| 13004 | /// |
| 13005 | /// \param InitRange the source range that covers the "0" initializer. |
| 13006 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 13007 | SourceLocation EndLoc = InitRange.getEnd(); |
| 13008 | if (EndLoc.isValid()) |
| 13009 | Method->setRangeEnd(EndLoc); |
| 13010 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 13011 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 13012 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 13013 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 13014 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 13015 | |
| 13016 | if (!Method->isInvalidDecl()) |
| 13017 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 13018 | << Method->getDeclName() << InitRange; |
| 13019 | return true; |
| 13020 | } |
| 13021 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 13022 | /// \brief Determine whether the given declaration is a static data member. |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 13023 | static bool isStaticDataMember(const Decl *D) { |
| 13024 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(D)) |
| 13025 | return Var->isStaticDataMember(); |
| 13026 | |
| 13027 | return false; |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 13028 | } |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 13029 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 13030 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 13031 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 13032 | /// is a fresh scope pushed for just this purpose. |
| 13033 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13034 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 13035 | /// static data member of class X, names should be looked up in the scope of |
| 13036 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13037 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13038 | // If there is no declaration, there was an error parsing it. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13039 | if (!D || D->isInvalidDecl()) |
| 13040 | return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13041 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13042 | // We will always have a nested name specifier here, but this declaration |
| 13043 | // might not be out of line if the specifier names the current namespace: |
| 13044 | // extern int n; |
| 13045 | // int ::n = 0; |
| 13046 | if (D->isOutOfLine()) |
| 13047 | EnterDeclaratorContext(S, D->getDeclContext()); |
| 13048 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 13049 | // If we are parsing the initializer for a static data member, push a |
| 13050 | // new expression evaluation context that is associated with this static |
| 13051 | // data member. |
| 13052 | if (isStaticDataMember(D)) |
| 13053 | PushExpressionEvaluationContext(PotentiallyEvaluated, D); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13054 | } |
| 13055 | |
| 13056 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13057 | /// initializer for the out-of-line declaration 'D'. |
| 13058 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13059 | // If there is no declaration, there was an error parsing it. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13060 | if (!D || D->isInvalidDecl()) |
| 13061 | return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13062 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 13063 | if (isStaticDataMember(D)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13064 | PopExpressionEvaluationContext(); |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 13065 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13066 | if (D->isOutOfLine()) |
| 13067 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 13068 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 13069 | |
| 13070 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 13071 | /// C++ if/switch/while/for statement. |
| 13072 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 13073 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 13074 | // C++ 6.4p2: |
| 13075 | // The declarator shall not specify a function or an array. |
| 13076 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 13077 | // new class or enumeration. |
| 13078 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 13079 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 13080 | |
| 13081 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 13082 | if (!Dcl) |
| 13083 | return true; |
| 13084 | |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 13085 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 13086 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 13087 | << D.getSourceRange(); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 13088 | return true; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 13089 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 13090 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 13091 | return Dcl; |
| 13092 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 13093 | |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 13094 | void Sema::LoadExternalVTableUses() { |
| 13095 | if (!ExternalSource) |
| 13096 | return; |
| 13097 | |
| 13098 | SmallVector<ExternalVTableUse, 4> VTables; |
| 13099 | ExternalSource->ReadUsedVTables(VTables); |
| 13100 | SmallVector<VTableUse, 4> NewUses; |
| 13101 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 13102 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 13103 | = VTablesUsed.find(VTables[I].Record); |
| 13104 | // Even if a definition wasn't required before, it may be required now. |
| 13105 | if (Pos != VTablesUsed.end()) { |
| 13106 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 13107 | Pos->second = true; |
| 13108 | continue; |
| 13109 | } |
| 13110 | |
| 13111 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 13112 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 13113 | } |
| 13114 | |
| 13115 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 13116 | } |
| 13117 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13118 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 13119 | bool DefinitionRequired) { |
| 13120 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 13121 | // not have a vtable. |
| 13122 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
John McCall | aeeacf7 | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 13123 | CurContext->isDependentContext() || isUnevaluatedContext()) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 13124 | return; |
| 13125 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13126 | // Try to insert this class into the map. |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 13127 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13128 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 13129 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 13130 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 13131 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 13132 | // If we already had an entry, check to see if we are promoting this vtable |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 13133 | // to require a definition. If so, we need to reappend to the VTableUses |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 13134 | // list, since we may have already processed the first entry. |
| 13135 | if (DefinitionRequired && !Pos.first->second) { |
| 13136 | Pos.first->second = true; |
| 13137 | } else { |
| 13138 | // Otherwise, we can early exit. |
| 13139 | return; |
| 13140 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13141 | } else { |
| 13142 | // The Microsoft ABI requires that we perform the destructor body |
| 13143 | // checks (i.e. operator delete() lookup) when the vtable is marked used, as |
| 13144 | // the deleting destructor is emitted with the vtable, not with the |
| 13145 | // destructor definition as in the Itanium ABI. |
| 13146 | // If it has a definition, we do the check at that point instead. |
| 13147 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 13148 | Class->hasUserDeclaredDestructor() && |
| 13149 | !Class->getDestructor()->isDefined() && |
| 13150 | !Class->getDestructor()->isDeleted()) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 13151 | CXXDestructorDecl *DD = Class->getDestructor(); |
| 13152 | ContextRAII SavedContext(*this, DD); |
| 13153 | CheckDestructor(DD); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13154 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13155 | } |
| 13156 | |
| 13157 | // Local classes need to have their virtual members marked |
| 13158 | // immediately. For all other classes, we mark their virtual members |
| 13159 | // at the end of the translation unit. |
| 13160 | if (Class->isLocalClass()) |
| 13161 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 13162 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13163 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 13164 | } |
| 13165 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13166 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 13167 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13168 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 13169 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 13170 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13171 | // Note: The VTableUses vector could grow as a result of marking |
| 13172 | // the members of a class as "used", so we check the size each |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 13173 | // time through the loop and prefer indices (which are stable) to |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13174 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 13175 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13176 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 13177 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13178 | if (!Class) |
| 13179 | continue; |
| 13180 | |
| 13181 | SourceLocation Loc = VTableUses[I].second; |
| 13182 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 13183 | bool DefineVTable = true; |
| 13184 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13185 | // If this class has a key function, but that key function is |
| 13186 | // defined in another translation unit, we don't need to emit the |
| 13187 | // vtable even though we're using it. |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 13188 | const CXXMethodDecl *KeyFunction = Context.getCurrentKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 13189 | if (KeyFunction && !KeyFunction->hasBody()) { |
Rafael Espindola | fc21813 | 2013-08-26 23:23:21 +0000 | [diff] [blame] | 13190 | // The key function is in another translation unit. |
| 13191 | DefineVTable = false; |
| 13192 | TemplateSpecializationKind TSK = |
| 13193 | KeyFunction->getTemplateSpecializationKind(); |
| 13194 | assert(TSK != TSK_ExplicitInstantiationDefinition && |
| 13195 | TSK != TSK_ImplicitInstantiation && |
| 13196 | "Instantiations don't have key functions"); |
| 13197 | (void)TSK; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13198 | } else if (!KeyFunction) { |
| 13199 | // If we have a class with no key function that is the subject |
| 13200 | // of an explicit instantiation declaration, suppress the |
| 13201 | // vtable; it will live with the explicit instantiation |
| 13202 | // definition. |
| 13203 | bool IsExplicitInstantiationDeclaration |
| 13204 | = Class->getTemplateSpecializationKind() |
| 13205 | == TSK_ExplicitInstantiationDeclaration; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13206 | for (auto R : Class->redecls()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13207 | TemplateSpecializationKind TSK |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13208 | = cast<CXXRecordDecl>(R)->getTemplateSpecializationKind(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13209 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 13210 | IsExplicitInstantiationDeclaration = true; |
| 13211 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 13212 | IsExplicitInstantiationDeclaration = false; |
| 13213 | break; |
| 13214 | } |
| 13215 | } |
| 13216 | |
| 13217 | if (IsExplicitInstantiationDeclaration) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 13218 | DefineVTable = false; |
| 13219 | } |
| 13220 | |
| 13221 | // The exception specifications for all virtual members may be needed even |
| 13222 | // if we are not providing an authoritative form of the vtable in this TU. |
| 13223 | // We may choose to emit it available_externally anyway. |
| 13224 | if (!DefineVTable) { |
| 13225 | MarkVirtualMemberExceptionSpecsNeeded(Loc, Class); |
| 13226 | continue; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13227 | } |
| 13228 | |
| 13229 | // Mark all of the virtual members of this class as referenced, so |
| 13230 | // that we can build a vtable. Then, tell the AST consumer that a |
| 13231 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 13232 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13233 | MarkVirtualMembersReferenced(Loc, Class); |
| 13234 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 13235 | if (VTablesUsed[Canonical]) |
| 13236 | Consumer.HandleVTable(Class); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13237 | |
| 13238 | // Optionally warn if we're emitting a weak vtable. |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 13239 | if (Class->isExternallyVisible() && |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13240 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13241 | const FunctionDecl *KeyFunctionDef = nullptr; |
Douglas Gregor | a120d01 | 2011-09-23 19:04:03 +0000 | [diff] [blame] | 13242 | if (!KeyFunction || |
| 13243 | (KeyFunction->hasBody(KeyFunctionDef) && |
| 13244 | KeyFunctionDef->isInlined())) |
David Blaikie | 44d95b5 | 2011-12-09 18:32:50 +0000 | [diff] [blame] | 13245 | Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == |
| 13246 | TSK_ExplicitInstantiationDefinition |
| 13247 | ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) |
| 13248 | << Class; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13249 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 13250 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 13251 | VTableUses.clear(); |
| 13252 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 13253 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 13254 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 13255 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 13256 | void Sema::MarkVirtualMemberExceptionSpecsNeeded(SourceLocation Loc, |
| 13257 | const CXXRecordDecl *RD) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13258 | for (const auto *I : RD->methods()) |
| 13259 | if (I->isVirtual() && !I->isPure()) |
| 13260 | ResolveExceptionSpec(Loc, I->getType()->castAs<FunctionProtoType>()); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 13261 | } |
| 13262 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 13263 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 13264 | const CXXRecordDecl *RD) { |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 13265 | // Mark all functions which will appear in RD's vtable as used. |
| 13266 | CXXFinalOverriderMap FinalOverriders; |
| 13267 | RD->getFinalOverriders(FinalOverriders); |
| 13268 | for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), |
| 13269 | E = FinalOverriders.end(); |
| 13270 | I != E; ++I) { |
| 13271 | for (OverridingMethods::const_iterator OI = I->second.begin(), |
| 13272 | OE = I->second.end(); |
| 13273 | OI != OE; ++OI) { |
| 13274 | assert(OI->second.size() > 0 && "no final overrider"); |
| 13275 | CXXMethodDecl *Overrider = OI->second.front().Method; |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 13276 | |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 13277 | // C++ [basic.def.odr]p2: |
| 13278 | // [...] A virtual member function is used if it is not pure. [...] |
| 13279 | if (!Overrider->isPure()) |
| 13280 | MarkFunctionReferenced(Loc, Overrider); |
| 13281 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 13282 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 13283 | |
| 13284 | // Only classes that have virtual bases need a VTT. |
| 13285 | if (RD->getNumVBases() == 0) |
| 13286 | return; |
| 13287 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13288 | for (const auto &I : RD->bases()) { |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 13289 | const CXXRecordDecl *Base = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13290 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 13291 | if (Base->getNumVBases() == 0) |
| 13292 | continue; |
| 13293 | MarkVirtualMembersReferenced(Loc, Base); |
| 13294 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 13295 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13296 | |
| 13297 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 13298 | /// Objective-C implementation whose ivars need be initialized. |
| 13299 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13300 | if (!getLangOpts().CPlusPlus) |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13301 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 13302 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 13303 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13304 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 13305 | if (ivars.empty()) |
| 13306 | return; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 13307 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13308 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 13309 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 13310 | if (Field->isInvalidDecl()) |
| 13311 | continue; |
| 13312 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 13313 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13314 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 13315 | InitializationKind InitKind = |
| 13316 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 13317 | |
| 13318 | InitializationSequence InitSeq(*this, InitEntity, InitKind, None); |
| 13319 | ExprResult MemberInit = |
| 13320 | InitSeq.Perform(*this, InitEntity, InitKind, None); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 13321 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13322 | // Note, MemberInit could actually come back empty if no initialization |
| 13323 | // is required (e.g., because it would call a trivial default constructor) |
| 13324 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 13325 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 13326 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13327 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 13328 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 13329 | SourceLocation(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 13330 | MemberInit.getAs<Expr>(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 13331 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13332 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 13333 | |
| 13334 | // Be sure that the destructor is accessible and is marked as referenced. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13335 | if (const RecordType *RecordTy = |
| 13336 | Context.getBaseElementType(Field->getType()) |
| 13337 | ->getAs<RecordType>()) { |
| 13338 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 13339 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 13340 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 13341 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 13342 | PDiag(diag::err_access_dtor_ivar) |
| 13343 | << Context.getBaseElementType(Field->getType())); |
| 13344 | } |
| 13345 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 13346 | } |
| 13347 | ObjCImplementation->setIvarInitializers(Context, |
| 13348 | AllToInit.data(), AllToInit.size()); |
| 13349 | } |
| 13350 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 13351 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13352 | static |
| 13353 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 13354 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 13355 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 13356 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 13357 | Sema &S) { |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13358 | if (Ctor->isInvalidDecl()) |
| 13359 | return; |
| 13360 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 13361 | CXXConstructorDecl *Target = Ctor->getTargetConstructor(); |
| 13362 | |
| 13363 | // Target may not be determinable yet, for instance if this is a dependent |
| 13364 | // call in an uninstantiated template. |
| 13365 | if (Target) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13366 | const FunctionDecl *FNTarget = nullptr; |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 13367 | (void)Target->hasBody(FNTarget); |
| 13368 | Target = const_cast<CXXConstructorDecl*>( |
| 13369 | cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 13370 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13371 | |
| 13372 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 13373 | // Avoid dereferencing a null pointer here. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13374 | *TCanonical = Target? Target->getCanonicalDecl() : nullptr; |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13375 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13376 | if (!Current.insert(Canonical).second) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13377 | return; |
| 13378 | |
| 13379 | // We know that beyond here, we aren't chaining into a cycle. |
| 13380 | if (!Target || !Target->isDelegatingConstructor() || |
| 13381 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 13382 | Valid.insert(Current.begin(), Current.end()); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13383 | Current.clear(); |
| 13384 | // We've hit a cycle. |
| 13385 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 13386 | Current.count(TCanonical)) { |
| 13387 | // If we haven't diagnosed this cycle yet, do so now. |
| 13388 | if (!Invalid.count(TCanonical)) { |
| 13389 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 13390 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13391 | << Ctor; |
| 13392 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 13393 | // Don't add a note for a function delegating directly to itself. |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13394 | if (TCanonical != Canonical) |
| 13395 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 13396 | |
| 13397 | CXXConstructorDecl *C = Target; |
| 13398 | while (C->getCanonicalDecl() != Canonical) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13399 | const FunctionDecl *FNTarget = nullptr; |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13400 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 13401 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 13402 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 13403 | C = const_cast<CXXConstructorDecl*>( |
| 13404 | cast<CXXConstructorDecl>(FNTarget)); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13405 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 13406 | } |
| 13407 | } |
| 13408 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 13409 | Invalid.insert(Current.begin(), Current.end()); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13410 | Current.clear(); |
| 13411 | } else { |
| 13412 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 13413 | } |
| 13414 | } |
| 13415 | |
| 13416 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 13417 | void Sema::CheckDelegatingCtorCycles() { |
| 13418 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 13419 | |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 13420 | for (DelegatingCtorDeclsType::iterator |
| 13421 | I = DelegatingCtorDecls.begin(ExternalSource), |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13422 | E = DelegatingCtorDecls.end(); |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 13423 | I != E; ++I) |
| 13424 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13425 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 13426 | for (llvm::SmallSet<CXXConstructorDecl *, 4>::iterator CI = Invalid.begin(), |
| 13427 | CE = Invalid.end(); |
| 13428 | CI != CE; ++CI) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 13429 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 13430 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 13431 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13432 | namespace { |
| 13433 | /// \brief AST visitor that finds references to the 'this' expression. |
| 13434 | class FindCXXThisExpr : public RecursiveASTVisitor<FindCXXThisExpr> { |
| 13435 | Sema &S; |
| 13436 | |
| 13437 | public: |
| 13438 | explicit FindCXXThisExpr(Sema &S) : S(S) { } |
| 13439 | |
| 13440 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 13441 | S.Diag(E->getLocation(), diag::err_this_static_member_func) |
| 13442 | << E->isImplicit(); |
| 13443 | return false; |
| 13444 | } |
| 13445 | }; |
| 13446 | } |
| 13447 | |
| 13448 | bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { |
| 13449 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 13450 | if (!TSInfo) |
| 13451 | return false; |
| 13452 | |
| 13453 | TypeLoc TL = TSInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 13454 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13455 | if (!ProtoTL) |
| 13456 | return false; |
| 13457 | |
| 13458 | // C++11 [expr.prim.general]p3: |
| 13459 | // [The expression this] shall not appear before the optional |
| 13460 | // cv-qualifier-seq and it shall not appear within the declaration of a |
| 13461 | // static member function (although its type and value category are defined |
| 13462 | // within a static member function as they are within a non-static member |
| 13463 | // function). [ Note: this is because declaration matching does not occur |
NAKAMURA Takumi | c86d1fd | 2012-04-21 09:40:04 +0000 | [diff] [blame] | 13464 | // until the complete declarator is known. - end note ] |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 13465 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13466 | FindCXXThisExpr Finder(*this); |
| 13467 | |
| 13468 | // If the return type came after the cv-qualifier-seq, check it now. |
| 13469 | if (Proto->hasTrailingReturn() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13470 | !Finder.TraverseTypeLoc(ProtoTL.getReturnLoc())) |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13471 | return true; |
| 13472 | |
| 13473 | // Check the exception specification. |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13474 | if (checkThisInStaticMemberFunctionExceptionSpec(Method)) |
| 13475 | return true; |
| 13476 | |
| 13477 | return checkThisInStaticMemberFunctionAttributes(Method); |
| 13478 | } |
| 13479 | |
| 13480 | bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { |
| 13481 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 13482 | if (!TSInfo) |
| 13483 | return false; |
| 13484 | |
| 13485 | TypeLoc TL = TSInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 13486 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13487 | if (!ProtoTL) |
| 13488 | return false; |
| 13489 | |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 13490 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13491 | FindCXXThisExpr Finder(*this); |
| 13492 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13493 | switch (Proto->getExceptionSpecType()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13494 | case EST_Unparsed: |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 13495 | case EST_Uninstantiated: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 13496 | case EST_Unevaluated: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13497 | case EST_BasicNoexcept: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13498 | case EST_DynamicNone: |
| 13499 | case EST_MSAny: |
| 13500 | case EST_None: |
| 13501 | break; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13502 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13503 | case EST_ComputedNoexcept: |
| 13504 | if (!Finder.TraverseStmt(Proto->getNoexceptExpr())) |
| 13505 | return true; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13506 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13507 | case EST_Dynamic: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13508 | for (const auto &E : Proto->exceptions()) { |
| 13509 | if (!Finder.TraverseType(E)) |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13510 | return true; |
| 13511 | } |
| 13512 | break; |
| 13513 | } |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13514 | |
| 13515 | return false; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13516 | } |
| 13517 | |
| 13518 | bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) { |
| 13519 | FindCXXThisExpr Finder(*this); |
| 13520 | |
| 13521 | // Check attributes. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13522 | for (const auto *A : Method->attrs()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13523 | // FIXME: This should be emitted by tblgen. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13524 | Expr *Arg = nullptr; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13525 | ArrayRef<Expr *> Args; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13526 | if (const auto *G = dyn_cast<GuardedByAttr>(A)) |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13527 | Arg = G->getArg(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13528 | else if (const auto *G = dyn_cast<PtGuardedByAttr>(A)) |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13529 | Arg = G->getArg(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13530 | else if (const auto *AA = dyn_cast<AcquiredAfterAttr>(A)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13531 | Args = llvm::makeArrayRef(AA->args_begin(), AA->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13532 | else if (const auto *AB = dyn_cast<AcquiredBeforeAttr>(A)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13533 | Args = llvm::makeArrayRef(AB->args_begin(), AB->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13534 | else if (const auto *ETLF = dyn_cast<ExclusiveTrylockFunctionAttr>(A)) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13535 | Arg = ETLF->getSuccessValue(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13536 | Args = llvm::makeArrayRef(ETLF->args_begin(), ETLF->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13537 | } else if (const auto *STLF = dyn_cast<SharedTrylockFunctionAttr>(A)) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13538 | Arg = STLF->getSuccessValue(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13539 | Args = llvm::makeArrayRef(STLF->args_begin(), STLF->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13540 | } else if (const auto *LR = dyn_cast<LockReturnedAttr>(A)) |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13541 | Arg = LR->getArg(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13542 | else if (const auto *LE = dyn_cast<LocksExcludedAttr>(A)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13543 | Args = llvm::makeArrayRef(LE->args_begin(), LE->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13544 | else if (const auto *RC = dyn_cast<RequiresCapabilityAttr>(A)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13545 | Args = llvm::makeArrayRef(RC->args_begin(), RC->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13546 | else if (const auto *AC = dyn_cast<AcquireCapabilityAttr>(A)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13547 | Args = llvm::makeArrayRef(AC->args_begin(), AC->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13548 | else if (const auto *AC = dyn_cast<TryAcquireCapabilityAttr>(A)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13549 | Args = llvm::makeArrayRef(AC->args_begin(), AC->args_size()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13550 | else if (const auto *RC = dyn_cast<ReleaseCapabilityAttr>(A)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13551 | Args = llvm::makeArrayRef(RC->args_begin(), RC->args_size()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 13552 | |
| 13553 | if (Arg && !Finder.TraverseStmt(Arg)) |
| 13554 | return true; |
| 13555 | |
| 13556 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 13557 | if (!Finder.TraverseStmt(Args[I])) |
| 13558 | return true; |
| 13559 | } |
| 13560 | } |
| 13561 | |
| 13562 | return false; |
| 13563 | } |
| 13564 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13565 | void Sema::checkExceptionSpecification( |
| 13566 | bool IsTopLevel, ExceptionSpecificationType EST, |
| 13567 | ArrayRef<ParsedType> DynamicExceptions, |
| 13568 | ArrayRef<SourceRange> DynamicExceptionRanges, Expr *NoexceptExpr, |
| 13569 | SmallVectorImpl<QualType> &Exceptions, |
| 13570 | FunctionProtoType::ExceptionSpecInfo &ESI) { |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13571 | Exceptions.clear(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13572 | ESI.Type = EST; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13573 | if (EST == EST_Dynamic) { |
| 13574 | Exceptions.reserve(DynamicExceptions.size()); |
| 13575 | for (unsigned ei = 0, ee = DynamicExceptions.size(); ei != ee; ++ei) { |
| 13576 | // FIXME: Preserve type source info. |
| 13577 | QualType ET = GetTypeFromParser(DynamicExceptions[ei]); |
| 13578 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13579 | if (IsTopLevel) { |
| 13580 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 13581 | collectUnexpandedParameterPacks(ET, Unexpanded); |
| 13582 | if (!Unexpanded.empty()) { |
| 13583 | DiagnoseUnexpandedParameterPacks( |
| 13584 | DynamicExceptionRanges[ei].getBegin(), UPPC_ExceptionType, |
| 13585 | Unexpanded); |
| 13586 | continue; |
| 13587 | } |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13588 | } |
| 13589 | |
| 13590 | // Check that the type is valid for an exception spec, and |
| 13591 | // drop it if not. |
| 13592 | if (!CheckSpecifiedExceptionType(ET, DynamicExceptionRanges[ei])) |
| 13593 | Exceptions.push_back(ET); |
| 13594 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13595 | ESI.Exceptions = Exceptions; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13596 | return; |
| 13597 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13598 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13599 | if (EST == EST_ComputedNoexcept) { |
| 13600 | // If an error occurred, there's no expression here. |
| 13601 | if (NoexceptExpr) { |
| 13602 | assert((NoexceptExpr->isTypeDependent() || |
| 13603 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 13604 | Context.BoolTy) && |
| 13605 | "Parser should have made sure that the expression is boolean"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13606 | if (IsTopLevel && NoexceptExpr && |
| 13607 | DiagnoseUnexpandedParameterPack(NoexceptExpr)) { |
| 13608 | ESI.Type = EST_BasicNoexcept; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13609 | return; |
| 13610 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13611 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13612 | if (!NoexceptExpr->isValueDependent()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13613 | NoexceptExpr = VerifyIntegerConstantExpression(NoexceptExpr, nullptr, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 13614 | diag::err_noexcept_needs_constant_expression, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 13615 | /*AllowFold*/ false).get(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13616 | ESI.NoexceptExpr = NoexceptExpr; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 13617 | } |
| 13618 | return; |
| 13619 | } |
| 13620 | } |
| 13621 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13622 | void Sema::actOnDelayedExceptionSpecification(Decl *MethodD, |
| 13623 | ExceptionSpecificationType EST, |
| 13624 | SourceRange SpecificationRange, |
| 13625 | ArrayRef<ParsedType> DynamicExceptions, |
| 13626 | ArrayRef<SourceRange> DynamicExceptionRanges, |
| 13627 | Expr *NoexceptExpr) { |
| 13628 | if (!MethodD) |
| 13629 | return; |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 13630 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13631 | // Dig out the method we're referring to. |
| 13632 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(MethodD)) |
| 13633 | MethodD = FunTmpl->getTemplatedDecl(); |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 13634 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 13635 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(MethodD); |
| 13636 | if (!Method) |
| 13637 | return; |
| 13638 | |
| 13639 | // Check the exception specification. |
| 13640 | llvm::SmallVector<QualType, 4> Exceptions; |
| 13641 | FunctionProtoType::ExceptionSpecInfo ESI; |
| 13642 | checkExceptionSpecification(/*IsTopLevel*/true, EST, DynamicExceptions, |
| 13643 | DynamicExceptionRanges, NoexceptExpr, Exceptions, |
| 13644 | ESI); |
| 13645 | |
| 13646 | // Update the exception specification on the function type. |
| 13647 | Context.adjustExceptionSpec(Method, ESI, /*AsWritten*/true); |
| 13648 | |
| 13649 | if (Method->isStatic()) |
| 13650 | checkThisInStaticMemberFunctionExceptionSpec(Method); |
| 13651 | |
| 13652 | if (Method->isVirtual()) { |
| 13653 | // Check overrides, which we previously had to delay. |
| 13654 | for (CXXMethodDecl::method_iterator O = Method->begin_overridden_methods(), |
| 13655 | OEnd = Method->end_overridden_methods(); |
| 13656 | O != OEnd; ++O) |
| 13657 | CheckOverridingFunctionExceptionSpec(Method, *O); |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 13658 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 13659 | } |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13660 | |
| 13661 | /// HandleMSProperty - Analyze a __delcspec(property) field of a C++ class. |
| 13662 | /// |
| 13663 | MSPropertyDecl *Sema::HandleMSProperty(Scope *S, RecordDecl *Record, |
| 13664 | SourceLocation DeclStart, |
| 13665 | Declarator &D, Expr *BitWidth, |
| 13666 | InClassInitStyle InitStyle, |
| 13667 | AccessSpecifier AS, |
| 13668 | AttributeList *MSPropertyAttr) { |
| 13669 | IdentifierInfo *II = D.getIdentifier(); |
| 13670 | if (!II) { |
| 13671 | Diag(DeclStart, diag::err_anonymous_property); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13672 | return nullptr; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13673 | } |
| 13674 | SourceLocation Loc = D.getIdentifierLoc(); |
| 13675 | |
| 13676 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 13677 | QualType T = TInfo->getType(); |
| 13678 | if (getLangOpts().CPlusPlus) { |
| 13679 | CheckExtraCXXDefaultArguments(D); |
| 13680 | |
| 13681 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 13682 | UPPC_DataMemberType)) { |
| 13683 | D.setInvalidType(); |
| 13684 | T = Context.IntTy; |
| 13685 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 13686 | } |
| 13687 | } |
| 13688 | |
| 13689 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| 13690 | |
| 13691 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 13692 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 13693 | diag::err_invalid_thread) |
| 13694 | << DeclSpec::getSpecifierName(TSCS); |
| 13695 | |
| 13696 | // Check to see if this name was declared as a member previously |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13697 | NamedDecl *PrevDecl = nullptr; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13698 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
| 13699 | LookupName(Previous, S); |
| 13700 | switch (Previous.getResultKind()) { |
| 13701 | case LookupResult::Found: |
| 13702 | case LookupResult::FoundUnresolvedValue: |
| 13703 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 13704 | break; |
| 13705 | |
| 13706 | case LookupResult::FoundOverloaded: |
| 13707 | PrevDecl = Previous.getRepresentativeDecl(); |
| 13708 | break; |
| 13709 | |
| 13710 | case LookupResult::NotFound: |
| 13711 | case LookupResult::NotFoundInCurrentInstantiation: |
| 13712 | case LookupResult::Ambiguous: |
| 13713 | break; |
| 13714 | } |
| 13715 | |
| 13716 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 13717 | // Maybe we will complain about the shadowed template parameter. |
| 13718 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 13719 | // Just pretend that we didn't see the previous declaration. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13720 | PrevDecl = nullptr; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13721 | } |
| 13722 | |
| 13723 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 13724 | PrevDecl = nullptr; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13725 | |
| 13726 | SourceLocation TSSL = D.getLocStart(); |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13727 | const AttributeList::PropertyData &Data = MSPropertyAttr->getPropertyData(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 13728 | MSPropertyDecl *NewPD = MSPropertyDecl::Create( |
| 13729 | Context, Record, Loc, II, T, TInfo, TSSL, Data.GetterId, Data.SetterId); |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13730 | ProcessDeclAttributes(TUScope, NewPD, D); |
| 13731 | NewPD->setAccess(AS); |
| 13732 | |
| 13733 | if (NewPD->isInvalidDecl()) |
| 13734 | Record->setInvalidDecl(); |
| 13735 | |
| 13736 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 13737 | NewPD->setModulePrivate(); |
| 13738 | |
| 13739 | if (NewPD->isInvalidDecl() && PrevDecl) { |
| 13740 | // Don't introduce NewFD into scope; there's already something |
| 13741 | // with the same name in the same scope. |
| 13742 | } else if (II) { |
| 13743 | PushOnScopeChains(NewPD, S); |
| 13744 | } else |
| 13745 | Record->addDecl(NewPD); |
| 13746 | |
| 13747 | return NewPD; |
| 13748 | } |