| Chris Lattner | 199abbc | 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 | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
| Argyrios Kyrtzidis | 2f67f37 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
| Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTLambda.h" |
| Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTMutationListener.h" |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
| Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclVisitor.h" |
| Richard Trieu | 4fc8536 | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 22 | #include "clang/AST/EvaluatedExprVisitor.h" |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 25 | #include "clang/AST/RecursiveASTVisitor.h" |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtVisitor.h" |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeOrdering.h" |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
| Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
| Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 31 | #include "clang/Lex/LiteralSupport.h" |
| Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Preprocessor.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 33 | #include "clang/Sema/CXXFieldCollector.h" |
| 34 | #include "clang/Sema/DeclSpec.h" |
| 35 | #include "clang/Sema/Initialization.h" |
| 36 | #include "clang/Sema/Lookup.h" |
| 37 | #include "clang/Sema/ParsedTemplate.h" |
| 38 | #include "clang/Sema/Scope.h" |
| 39 | #include "clang/Sema/ScopeInfo.h" |
| Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/STLExtras.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/SmallString.h" |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 42 | #include <map> |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 43 | #include <set> |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace clang; |
| 46 | |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // CheckDefaultArgumentVisitor |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| Chris Lattner | b0d3844 | 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 | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 57 | class CheckDefaultArgumentVisitor |
| Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 58 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | Expr *DefaultArg; |
| 60 | Sema *S; |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 61 | |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 62 | public: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 64 | : DefaultArg(defarg), S(s) {} |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 65 | |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 66 | bool VisitExpr(Expr *Node); |
| 67 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| Douglas Gregor | 97a9c81 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 68 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
| Douglas Gregor | f0d4951 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 69 | bool VisitLambdaExpr(LambdaExpr *Lambda); |
| John McCall | 7353c86 | 2013-04-09 01:56:28 +0000 | [diff] [blame] | 70 | bool VisitPseudoObjectExpr(PseudoObjectExpr *POE); |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 71 | }; |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 72 | |
| Chris Lattner | b0d3844 | 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 | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 76 | for (Stmt::child_range I = Node->children(); I; ++I) |
| Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 77 | IsInvalid |= Visit(*I); |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 78 | return IsInvalid; |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| Chris Lattner | b0d3844 | 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 | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 85 | NamedDecl *Decl = DRE->getDecl(); |
| Chris Lattner | b0d3844 | 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 | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 95 | return S->Diag(DRE->getLocStart(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 96 | diag::err_param_default_argument_references_param) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 97 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
| Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 98 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
| Chris Lattner | b0d3844 | 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 | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 102 | if (VDecl->isLocalVarDecl()) |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 103 | return S->Diag(DRE->getLocStart(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 104 | diag::err_param_default_argument_references_local) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 105 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 106 | } |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 107 | |
| Douglas Gregor | 8e12c38 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 108 | return false; |
| 109 | } |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 110 | |
| Douglas Gregor | 97a9c81 | 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 | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 116 | return S->Diag(ThisE->getLocStart(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 117 | diag::err_param_default_argument_references_this) |
| 118 | << ThisE->getSourceRange(); |
| Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 119 | } |
| Douglas Gregor | f0d4951 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 120 | |
| John McCall | 7353c86 | 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 | f0d4951 | 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 | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 150 | void |
| 151 | Sema::ImplicitExceptionSpecification::CalledDecl(SourceLocation CallLoc, |
| 152 | const CXXMethodDecl *Method) { |
| Richard Smith | d3b5c908 | 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) |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
| 157 | const FunctionProtoType *Proto |
| 158 | = Method->getType()->getAs<FunctionProtoType>(); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 159 | Proto = Self->ResolveExceptionSpec(CallLoc, Proto); |
| 160 | if (!Proto) |
| 161 | return; |
| Alexis Hunt | 6d5b96c | 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 | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 166 | if (EST == EST_MSAny || EST == EST_None) { |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 167 | ClearExceptions(); |
| 168 | ComputedEST = EST; |
| 169 | return; |
| 170 | } |
| 171 | |
| Richard Smith | 938f40b | 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 | |
| Alexis Hunt | 6d5b96c | 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 | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 193 | FunctionProtoType::NoexceptResult NR = |
| 194 | Proto->getNoexceptSpec(Self->Context); |
| Alexis Hunt | 6d5b96c | 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. |
| Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 215 | for (const auto &E : Proto->exceptions()) |
| 216 | if (ExceptionsSeen.insert(Self->Context.getCanonicalType(E))) |
| 217 | Exceptions.push_back(E); |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 220 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 221 | if (!E || ComputedEST == EST_MSAny) |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 222 | return; |
| 223 | |
| 224 | // FIXME: |
| 225 | // |
| 226 | // C++0x [except.spec]p14: |
| NAKAMURA Takumi | 5364847 | 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 | 938f40b | 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 | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 245 | if (Self->canThrow(E)) |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 246 | ComputedEST = EST_None; |
| 247 | } |
| 248 | |
| Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 249 | bool |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 250 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | SourceLocation EqualLoc) { |
| Anders Carlsson | 114056f | 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 | c80a127 | 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 | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 264 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 265 | Param); |
| Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 266 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 267 | EqualLoc); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 268 | InitializationSequence InitSeq(*this, Entity, Kind, Arg); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 269 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Arg); |
| Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 270 | if (Result.isInvalid()) |
| Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 271 | return true; |
| Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 272 | Arg = Result.takeAs<Expr>(); |
| Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 273 | |
| Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 274 | CheckCompletedExpr(Arg, EqualLoc); |
| John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 275 | Arg = MaybeCreateExprWithCleanups(Arg); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
| Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 277 | // Okay: add the default argument to the parameter |
| 278 | Param->setDefaultArg(Arg); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
| Douglas Gregor | 758cb67 | 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 | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 292 | return false; |
| Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| Chris Lattner | 5825824 | 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 | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 298 | void |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 299 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 300 | Expr *DefaultArg) { |
| 301 | if (!param || !DefaultArg) |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 302 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 304 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 305 | UnparsedDefaultArgLocs.erase(Param); |
| 306 | |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 307 | // Default arguments are only permitted in C++ |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 308 | if (!getLangOpts().CPlusPlus) { |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 309 | Diag(EqualLoc, diag::err_param_default_argument) |
| 310 | << DefaultArg->getSourceRange(); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 311 | Param->setInvalidDecl(); |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 312 | return; |
| 313 | } |
| 314 | |
| Douglas Gregor | 6ff1fbf | 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; |
| 319 | } |
| 320 | |
| Anders Carlsson | f1c2695 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 321 | // Check that the default argument is well-formed |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 322 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 323 | if (DefaultArgChecker.Visit(DefaultArg)) { |
| Anders Carlsson | f1c2695 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 324 | Param->setInvalidDecl(); |
| 325 | return; |
| 326 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 328 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 331 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 332 | /// argument for a function parameter, but we can't parse it yet |
| 333 | /// because we're inside a class definition. Note that this default |
| 334 | /// argument will be parsed later. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 335 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 336 | SourceLocation EqualLoc, |
| 337 | SourceLocation ArgLoc) { |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 338 | if (!param) |
| 339 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 341 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
| Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 342 | Param->setUnparsedDefaultArg(); |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 343 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
| Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 346 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 347 | /// the default argument for the parameter param failed. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 348 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 349 | if (!param) |
| 350 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 352 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 353 | Param->setInvalidDecl(); |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 354 | UnparsedDefaultArgLocs.erase(Param); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 357 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 358 | /// arguments in the declarator, which is not a function declaration |
| 359 | /// or definition and therefore is not permitted to have default |
| 360 | /// arguments. This routine should be invoked for every declarator |
| 361 | /// that is not a function declaration or definition. |
| 362 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 363 | // C++ [dcl.fct.default]p3 |
| 364 | // A default argument expression shall be specified only in the |
| 365 | // parameter-declaration-clause of a function declaration or in a |
| 366 | // template-parameter (14.1). It shall not be specified for a |
| 367 | // parameter pack. If it is specified in a |
| 368 | // parameter-declaration-clause, it shall not occur within a |
| 369 | // declarator or abstract-declarator of a parameter-declaration. |
| Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 370 | bool MightBeFunction = D.isFunctionDeclarationContext(); |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 371 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 372 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 373 | if (chunk.Kind == DeclaratorChunk::Function) { |
| Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 374 | if (MightBeFunction) { |
| 375 | // This is a function declaration. It can have default arguments, but |
| 376 | // keep looking in case its return type is a function type with default |
| 377 | // arguments. |
| 378 | MightBeFunction = false; |
| 379 | continue; |
| 380 | } |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 381 | for (unsigned argIdx = 0, e = chunk.Fun.NumParams; argIdx != e; |
| 382 | ++argIdx) { |
| 383 | ParmVarDecl *Param = cast<ParmVarDecl>(chunk.Fun.Params[argIdx].Param); |
| Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 384 | if (Param->hasUnparsedDefaultArg()) { |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 385 | CachedTokens *Toks = chunk.Fun.Params[argIdx].DefaultArgTokens; |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 386 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 387 | << SourceRange((*Toks)[1].getLocation(), |
| 388 | Toks->back().getLocation()); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 389 | delete Toks; |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 390 | chunk.Fun.Params[argIdx].DefaultArgTokens = 0; |
| Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 391 | } else if (Param->getDefaultArg()) { |
| 392 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 393 | << Param->getDefaultArg()->getSourceRange(); |
| 394 | Param->setDefaultArg(0); |
| Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 397 | } else if (chunk.Kind != DeclaratorChunk::Paren) { |
| 398 | MightBeFunction = false; |
| Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 403 | static bool functionDeclHasDefaultArgument(const FunctionDecl *FD) { |
| 404 | for (unsigned NumParams = FD->getNumParams(); NumParams > 0; --NumParams) { |
| 405 | const ParmVarDecl *PVD = FD->getParamDecl(NumParams-1); |
| 406 | if (!PVD->hasDefaultArg()) |
| 407 | return false; |
| 408 | if (!PVD->hasInheritedDefaultArg()) |
| 409 | return true; |
| 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | |
| Craig Topper | e479428 | 2012-09-21 04:33:26 +0000 | [diff] [blame] | 414 | /// MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 415 | /// function, once we already know that they have the same |
| 416 | /// type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 417 | /// error, false otherwise. |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 418 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, |
| 419 | Scope *S) { |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 420 | bool Invalid = false; |
| 421 | |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 422 | // C++ [dcl.fct.default]p4: |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 423 | // For non-template functions, default arguments can be added in |
| 424 | // later declarations of a function in the same |
| 425 | // scope. Declarations in different scopes have completely |
| 426 | // distinct sets of default arguments. That is, declarations in |
| 427 | // inner scopes do not acquire default arguments from |
| 428 | // declarations in outer scopes, and vice versa. In a given |
| 429 | // function declaration, all parameters subsequent to a |
| 430 | // parameter with a default argument shall have default |
| 431 | // arguments supplied in this or previous declarations. A |
| 432 | // default argument shall not be redefined by a later |
| 433 | // declaration (not even to the same value). |
| Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 434 | // |
| 435 | // C++ [dcl.fct.default]p6: |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 436 | // Except for member functions of class templates, the default arguments |
| 437 | // in a member function definition that appears outside of the class |
| 438 | // definition are added to the set of default arguments provided by the |
| Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 439 | // member function declaration in the class definition. |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 440 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 441 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 442 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 443 | |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 444 | bool OldParamHasDfl = OldParam->hasDefaultArg(); |
| 445 | bool NewParamHasDfl = NewParam->hasDefaultArg(); |
| 446 | |
| 447 | NamedDecl *ND = Old; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 448 | |
| 449 | // The declaration context corresponding to the scope is the semantic |
| 450 | // parent, unless this is a local function declaration, in which case |
| 451 | // it is that surrounding function. |
| 452 | DeclContext *ScopeDC = New->getLexicalDeclContext(); |
| 453 | if (!ScopeDC->isFunctionOrMethod()) |
| 454 | ScopeDC = New->getDeclContext(); |
| 455 | if (S && !isDeclInScope(ND, ScopeDC, S) && |
| 456 | !New->getDeclContext()->isRecord()) |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 457 | // Ignore default parameters of old decl if they are not in |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 458 | // the same scope and this is not an out-of-line definition of |
| 459 | // a member function. |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 460 | OldParamHasDfl = false; |
| 461 | |
| 462 | if (OldParamHasDfl && NewParamHasDfl) { |
| Francois Pichet | 8cb243a | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 463 | |
| Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 464 | unsigned DiagDefaultParamID = |
| 465 | diag::err_param_default_argument_redefinition; |
| 466 | |
| 467 | // MSVC accepts that default parameters be redefined for member functions |
| 468 | // of template class. The new default parameter's value is ignored. |
| 469 | Invalid = true; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 470 | if (getLangOpts().MicrosoftExt) { |
| Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 471 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 472 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
| Francois Pichet | 8cb243a | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 473 | // Merge the old default argument into the new parameter. |
| 474 | NewParam->setHasInheritedDefaultArg(); |
| 475 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 476 | NewParam->setUninstantiatedDefaultArg( |
| 477 | OldParam->getUninstantiatedDefaultArg()); |
| 478 | else |
| 479 | NewParam->setDefaultArg(OldParam->getInit()); |
| Francois Pichet | 9392165 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 480 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
| Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 481 | Invalid = false; |
| 482 | } |
| 483 | } |
| Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 484 | |
| Francois Pichet | 8cb243a | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 485 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 486 | // hint here. Alternatively, we could walk the type-source information |
| 487 | // for NewParam to find the last source location in the type... but it |
| 488 | // isn't worth the effort right now. This is the kind of test case that |
| 489 | // is hard to get right: |
| Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 490 | // int f(int); |
| 491 | // void g(int (*fp)(int) = f); |
| 492 | // void g(int (*fp)(int) = &f); |
| Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 493 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
| Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 494 | << NewParam->getDefaultArgRange(); |
| Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 495 | |
| 496 | // Look for the function declaration where the default argument was |
| 497 | // actually written, which may be a declaration prior to Old. |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 498 | for (FunctionDecl *Older = Old->getPreviousDecl(); |
| 499 | Older; Older = Older->getPreviousDecl()) { |
| Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 500 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 501 | break; |
| 502 | |
| 503 | OldParam = Older->getParamDecl(p); |
| 504 | } |
| 505 | |
| 506 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 507 | << OldParam->getDefaultArgRange(); |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 508 | } else if (OldParamHasDfl) { |
| John McCall | e61b02b | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 509 | // Merge the old default argument into the new parameter. |
| 510 | // It's important to use getInit() here; getDefaultArg() |
| John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 511 | // strips off any top-level ExprWithCleanups. |
| John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 512 | NewParam->setHasInheritedDefaultArg(); |
| Douglas Gregor | 4f15f4d | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 513 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 514 | NewParam->setUninstantiatedDefaultArg( |
| 515 | OldParam->getUninstantiatedDefaultArg()); |
| 516 | else |
| John McCall | e61b02b | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 517 | NewParam->setDefaultArg(OldParam->getInit()); |
| James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 518 | } else if (NewParamHasDfl) { |
| Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 519 | if (New->getDescribedFunctionTemplate()) { |
| 520 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 521 | Diag(NewParam->getLocation(), |
| 522 | diag::err_param_default_argument_template_redecl) |
| 523 | << NewParam->getDefaultArgRange(); |
| 524 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 525 | << false; |
| Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 526 | } else if (New->getTemplateSpecializationKind() |
| 527 | != TSK_ImplicitInstantiation && |
| 528 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 529 | // C++ [temp.expr.spec]p21: |
| 530 | // Default function arguments shall not be specified in a declaration |
| 531 | // or a definition for one of the following explicit specializations: |
| 532 | // - the explicit specialization of a function template; |
| Douglas Gregor | 3362bde | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 533 | // - the explicit specialization of a member function template; |
| 534 | // - the explicit specialization of a member function of a class |
| Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 535 | // template where the class template specialization to which the |
| 536 | // member function specialization belongs is implicitly |
| 537 | // instantiated. |
| 538 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 539 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 540 | << New->getDeclName() |
| 541 | << NewParam->getDefaultArgRange(); |
| Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 542 | } else if (New->getDeclContext()->isDependentContext()) { |
| 543 | // C++ [dcl.fct.default]p6 (DR217): |
| 544 | // Default arguments for a member function of a class template shall |
| 545 | // be specified on the initial declaration of the member function |
| 546 | // within the class template. |
| 547 | // |
| 548 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 549 | // leads me to the conclusion that one cannot add default function |
| 550 | // arguments for an out-of-line definition of a member function of a |
| 551 | // dependent type. |
| 552 | int WhichKind = 2; |
| 553 | if (CXXRecordDecl *Record |
| 554 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 555 | if (Record->getDescribedClassTemplate()) |
| 556 | WhichKind = 0; |
| 557 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 558 | WhichKind = 1; |
| 559 | else |
| 560 | WhichKind = 2; |
| 561 | } |
| 562 | |
| 563 | Diag(NewParam->getLocation(), |
| 564 | diag::err_param_default_argument_member_template_redecl) |
| 565 | << WhichKind |
| 566 | << NewParam->getDefaultArgRange(); |
| 567 | } |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| Richard Smith | 58c3cc1 | 2012-11-28 03:45:24 +0000 | [diff] [blame] | 571 | // DR1344: If a default argument is added outside a class definition and that |
| 572 | // default argument makes the function a special member function, the program |
| 573 | // is ill-formed. This can only happen for constructors. |
| 574 | if (isa<CXXConstructorDecl>(New) && |
| 575 | New->getMinRequiredArguments() < Old->getMinRequiredArguments()) { |
| 576 | CXXSpecialMember NewSM = getSpecialMember(cast<CXXMethodDecl>(New)), |
| 577 | OldSM = getSpecialMember(cast<CXXMethodDecl>(Old)); |
| 578 | if (NewSM != OldSM) { |
| 579 | ParmVarDecl *NewParam = New->getParamDecl(New->getMinRequiredArguments()); |
| 580 | assert(NewParam->hasDefaultArg()); |
| 581 | Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special) |
| 582 | << NewParam->getDefaultArgRange() << NewSM; |
| 583 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 584 | } |
| 585 | } |
| 586 | |
| David Majnemer | ee4f402 | 2014-03-30 06:44:54 +0000 | [diff] [blame] | 587 | const FunctionDecl *Def; |
| Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 588 | // C++11 [dcl.constexpr]p1: If any declaration of a function or function |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 589 | // template has a constexpr specifier then all its declarations shall |
| Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 590 | // contain the constexpr specifier. |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 591 | if (New->isConstexpr() != Old->isConstexpr()) { |
| 592 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 593 | << New << New->isConstexpr(); |
| 594 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 595 | Invalid = true; |
| David Majnemer | ee4f402 | 2014-03-30 06:44:54 +0000 | [diff] [blame] | 596 | } else if (!Old->isInlined() && New->isInlined() && Old->isDefined(Def)) { |
| 597 | // C++11 [dcl.fcn.spec]p4: |
| 598 | // If the definition of a function appears in a translation unit before its |
| 599 | // first declaration as inline, the program is ill-formed. |
| 600 | Diag(New->getLocation(), diag::err_inline_decl_follows_def) << New; |
| 601 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 602 | Invalid = true; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 605 | // C++11 [dcl.fct.default]p4: If a friend declaration specifies a default |
| NAKAMURA Takumi | 3e7db84 | 2013-07-17 17:57:52 +0000 | [diff] [blame] | 606 | // argument expression, that declaration shall be a definition and shall be |
| David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 607 | // the only declaration of the function or function template in the |
| 608 | // translation unit. |
| 609 | if (Old->getFriendObjectKind() == Decl::FOK_Undeclared && |
| 610 | functionDeclHasDefaultArgument(Old)) { |
| 611 | Diag(New->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 612 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 613 | Invalid = true; |
| 614 | } |
| 615 | |
| Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 616 | if (CheckEquivalentExceptionSpec(Old, New)) |
| Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 617 | Invalid = true; |
| Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 618 | |
| Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 619 | return Invalid; |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 622 | /// \brief Merge the exception specifications of two variable declarations. |
| 623 | /// |
| 624 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 625 | /// checks if the redeclaration might have an exception specification and |
| 626 | /// validates compatibility and merges the specs if necessary. |
| 627 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 628 | // Shortcut if exceptions are disabled. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 629 | if (!getLangOpts().CXXExceptions) |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 630 | return; |
| 631 | |
| 632 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 633 | "Should only be called if types are otherwise the same."); |
| 634 | |
| 635 | QualType NewType = New->getType(); |
| 636 | QualType OldType = Old->getType(); |
| 637 | |
| 638 | // We're only interested in pointers and references to functions, as well |
| 639 | // as pointers to member functions. |
| 640 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 641 | NewType = R->getPointeeType(); |
| 642 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 643 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 644 | NewType = P->getPointeeType(); |
| 645 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 646 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 647 | NewType = M->getPointeeType(); |
| 648 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 649 | } |
| 650 | |
| 651 | if (!NewType->isFunctionProtoType()) |
| 652 | return; |
| 653 | |
| 654 | // There's lots of special cases for functions. For function pointers, system |
| 655 | // libraries are hopefully not as broken so that we don't need these |
| 656 | // workarounds. |
| 657 | if (CheckEquivalentExceptionSpec( |
| 658 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 659 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 660 | New->setInvalidDecl(); |
| 661 | } |
| 662 | } |
| 663 | |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 664 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 665 | /// function declaration are well-formed according to C++ |
| 666 | /// [dcl.fct.default]. |
| 667 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 668 | unsigned NumParams = FD->getNumParams(); |
| 669 | unsigned p; |
| 670 | |
| 671 | // Find first parameter with a default argument |
| 672 | for (p = 0; p < NumParams; ++p) { |
| 673 | ParmVarDecl *Param = FD->getParamDecl(p); |
| Richard Smith | 3cb4c63 | 2013-04-17 16:25:20 +0000 | [diff] [blame] | 674 | if (Param->hasDefaultArg()) |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | |
| 678 | // C++ [dcl.fct.default]p4: |
| 679 | // In a given function declaration, all parameters |
| 680 | // subsequent to a parameter with a default argument shall |
| 681 | // have default arguments supplied in this or previous |
| 682 | // declarations. A default argument shall not be redefined |
| 683 | // by a later declaration (not even to the same value). |
| 684 | unsigned LastMissingDefaultArg = 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | for (; p < NumParams; ++p) { |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 686 | ParmVarDecl *Param = FD->getParamDecl(p); |
| Anders Carlsson | 5a53238 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 687 | if (!Param->hasDefaultArg()) { |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 688 | if (Param->isInvalidDecl()) |
| 689 | /* We already complained about this parameter. */; |
| 690 | else if (Param->getIdentifier()) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | Diag(Param->getLocation(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 692 | diag::err_param_default_argument_missing_name) |
| Chris Lattner | b91fd17 | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 693 | << Param->getIdentifier(); |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 694 | else |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | Diag(Param->getLocation(), |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 696 | diag::err_param_default_argument_missing); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 698 | LastMissingDefaultArg = p; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (LastMissingDefaultArg > 0) { |
| 703 | // Some default arguments were missing. Clear out all of the |
| 704 | // default arguments up to (and including) the last missing |
| 705 | // default argument, so that we leave the function parameters |
| 706 | // in a semantically valid state. |
| 707 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 708 | ParmVarDecl *Param = FD->getParamDecl(p); |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 709 | if (Param->hasDefaultArg()) { |
| Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 710 | Param->setDefaultArg(0); |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | } |
| Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 715 | |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 716 | // CheckConstexprParameterTypes - Check whether a function's parameter types |
| 717 | // are all literal types. If so, return true. If not, produce a suitable |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 718 | // diagnostic and return false. |
| 719 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 720 | const FunctionDecl *FD) { |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 721 | unsigned ArgIndex = 0; |
| 722 | const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 723 | for (FunctionProtoType::param_type_iterator i = FT->param_type_begin(), |
| 724 | e = FT->param_type_end(); |
| 725 | i != e; ++i, ++ArgIndex) { |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 726 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 727 | SourceLocation ParamLoc = PD->getLocation(); |
| 728 | if (!(*i)->isDependentType() && |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 729 | SemaRef.RequireLiteralType(ParamLoc, *i, |
| Douglas Gregor | a6c5abb | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 730 | diag::err_constexpr_non_literal_param, |
| 731 | ArgIndex+1, PD->getSourceRange(), |
| 732 | isa<CXXConstructorDecl>(FD))) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 733 | return false; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 734 | } |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 735 | return true; |
| 736 | } |
| 737 | |
| 738 | /// \brief Get diagnostic %select index for tag kind for |
| 739 | /// record diagnostic message. |
| 740 | /// WARNING: Indexes apply to particular diagnostics only! |
| 741 | /// |
| 742 | /// \returns diagnostic %select index. |
| Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 743 | static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) { |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 744 | switch (Tag) { |
| Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 745 | case TTK_Struct: return 0; |
| 746 | case TTK_Interface: return 1; |
| 747 | case TTK_Class: return 2; |
| 748 | default: llvm_unreachable("Invalid tag kind for record diagnostic!"); |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 749 | } |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // CheckConstexprFunctionDecl - Check whether a function declaration satisfies |
| 753 | // the requirements of a constexpr function definition or a constexpr |
| 754 | // constructor definition. If so, return true. If not, produce appropriate |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 755 | // diagnostics and return false. |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 756 | // |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 757 | // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. |
| 758 | bool Sema::CheckConstexprFunctionDecl(const FunctionDecl *NewFD) { |
| Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 759 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 760 | if (MD && MD->isInstance()) { |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 761 | // C++11 [dcl.constexpr]p4: |
| 762 | // The definition of a constexpr constructor shall satisfy the following |
| 763 | // constraints: |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 764 | // - the class shall not have any virtual base classes; |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 765 | const CXXRecordDecl *RD = MD->getParent(); |
| 766 | if (RD->getNumVBases()) { |
| 767 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 768 | << isa<CXXConstructorDecl>(NewFD) |
| 769 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 770 | for (const auto &I : RD->vbases()) |
| 771 | Diag(I.getLocStart(), |
| 772 | diag::note_constexpr_virtual_base_here) << I.getSourceRange(); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 773 | return false; |
| 774 | } |
| Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 778 | // C++11 [dcl.constexpr]p3: |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 779 | // The definition of a constexpr function shall satisfy the following |
| 780 | // constraints: |
| 781 | // - it shall not be virtual; |
| 782 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 783 | if (Method && Method->isVirtual()) { |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 784 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 785 | |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 786 | // If it's not obvious why this function is virtual, find an overridden |
| 787 | // function which uses the 'virtual' keyword. |
| 788 | const CXXMethodDecl *WrittenVirtual = Method; |
| 789 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 790 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 791 | if (WrittenVirtual != Method) |
| 792 | Diag(WrittenVirtual->getLocation(), |
| 793 | diag::note_overridden_virtual_function); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 794 | return false; |
| 795 | } |
| 796 | |
| 797 | // - its return type shall be a literal type; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 798 | QualType RT = NewFD->getReturnType(); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 799 | if (!RT->isDependentType() && |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 800 | RequireLiteralType(NewFD->getLocation(), RT, |
| Douglas Gregor | a6c5abb | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 801 | diag::err_constexpr_non_literal_return)) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 802 | return false; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 805 | // - each of its parameter types shall be a literal type; |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 806 | if (!CheckConstexprParameterTypes(*this, NewFD)) |
| Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 807 | return false; |
| 808 | |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 809 | return true; |
| 810 | } |
| 811 | |
| 812 | /// Check the given declaration statement is legal within a constexpr function |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 813 | /// body. C++11 [dcl.constexpr]p3,p4, and C++1y [dcl.constexpr]p3. |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 814 | /// |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 815 | /// \return true if the body is OK (maybe only as an extension), false if we |
| 816 | /// have diagnosed a problem. |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 817 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 818 | DeclStmt *DS, SourceLocation &Cxx1yLoc) { |
| 819 | // C++11 [dcl.constexpr]p3 and p4: |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 820 | // The definition of a constexpr function(p3) or constructor(p4) [...] shall |
| 821 | // contain only |
| Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 822 | for (const auto *DclIt : DS->decls()) { |
| 823 | switch (DclIt->getKind()) { |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 824 | case Decl::StaticAssert: |
| 825 | case Decl::Using: |
| 826 | case Decl::UsingShadow: |
| 827 | case Decl::UsingDirective: |
| 828 | case Decl::UnresolvedUsingTypename: |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 829 | case Decl::UnresolvedUsingValue: |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 830 | // - static_assert-declarations |
| 831 | // - using-declarations, |
| 832 | // - using-directives, |
| 833 | continue; |
| 834 | |
| 835 | case Decl::Typedef: |
| 836 | case Decl::TypeAlias: { |
| 837 | // - typedef declarations and alias-declarations that do not define |
| 838 | // classes or enumerations, |
| Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 839 | const auto *TN = cast<TypedefNameDecl>(DclIt); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 840 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 841 | // Don't allow variably-modified types in constexpr functions. |
| 842 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 843 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 844 | << TL.getSourceRange() << TL.getType() |
| 845 | << isa<CXXConstructorDecl>(Dcl); |
| 846 | return false; |
| 847 | } |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | case Decl::Enum: |
| 852 | case Decl::CXXRecord: |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 853 | // C++1y allows types to be defined, not just declared. |
| Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 854 | if (cast<TagDecl>(DclIt)->isThisDeclarationADefinition()) |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 855 | SemaRef.Diag(DS->getLocStart(), |
| 856 | SemaRef.getLangOpts().CPlusPlus1y |
| 857 | ? diag::warn_cxx11_compat_constexpr_type_definition |
| 858 | : diag::ext_constexpr_type_definition) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 859 | << isa<CXXConstructorDecl>(Dcl); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 860 | continue; |
| 861 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 862 | case Decl::EnumConstant: |
| 863 | case Decl::IndirectField: |
| 864 | case Decl::ParmVar: |
| 865 | // These can only appear with other declarations which are banned in |
| 866 | // C++11 and permitted in C++1y, so ignore them. |
| 867 | continue; |
| 868 | |
| 869 | case Decl::Var: { |
| 870 | // C++1y [dcl.constexpr]p3 allows anything except: |
| 871 | // a definition of a variable of non-literal type or of static or |
| 872 | // thread storage duration or for which no initialization is performed. |
| Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 873 | const auto *VD = cast<VarDecl>(DclIt); |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 874 | if (VD->isThisDeclarationADefinition()) { |
| 875 | if (VD->isStaticLocal()) { |
| 876 | SemaRef.Diag(VD->getLocation(), |
| 877 | diag::err_constexpr_local_var_static) |
| 878 | << isa<CXXConstructorDecl>(Dcl) |
| 879 | << (VD->getTLSKind() == VarDecl::TLS_Dynamic); |
| 880 | return false; |
| 881 | } |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 882 | if (!VD->getType()->isDependentType() && |
| 883 | SemaRef.RequireLiteralType( |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 884 | VD->getLocation(), VD->getType(), |
| 885 | diag::err_constexpr_local_var_non_literal_type, |
| 886 | isa<CXXConstructorDecl>(Dcl))) |
| 887 | return false; |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 888 | if (!VD->getType()->isDependentType() && |
| 889 | !VD->hasInit() && !VD->isCXXForRangeDecl()) { |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 890 | SemaRef.Diag(VD->getLocation(), |
| 891 | diag::err_constexpr_local_var_no_init) |
| 892 | << isa<CXXConstructorDecl>(Dcl); |
| 893 | return false; |
| 894 | } |
| 895 | } |
| 896 | SemaRef.Diag(VD->getLocation(), |
| 897 | SemaRef.getLangOpts().CPlusPlus1y |
| 898 | ? diag::warn_cxx11_compat_constexpr_local_var |
| 899 | : diag::ext_constexpr_local_var) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 900 | << isa<CXXConstructorDecl>(Dcl); |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 901 | continue; |
| 902 | } |
| 903 | |
| 904 | case Decl::NamespaceAlias: |
| 905 | case Decl::Function: |
| 906 | // These are disallowed in C++11 and permitted in C++1y. Allow them |
| 907 | // everywhere as an extension. |
| 908 | if (!Cxx1yLoc.isValid()) |
| 909 | Cxx1yLoc = DS->getLocStart(); |
| 910 | continue; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 911 | |
| 912 | default: |
| 913 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 914 | << isa<CXXConstructorDecl>(Dcl); |
| 915 | return false; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | /// Check that the given field is initialized within a constexpr constructor. |
| 923 | /// |
| 924 | /// \param Dcl The constexpr constructor being checked. |
| 925 | /// \param Field The field being checked. This may be a member of an anonymous |
| 926 | /// struct or union nested within the class being checked. |
| 927 | /// \param Inits All declarations, including anonymous struct/union members and |
| 928 | /// indirect members, for which any initialization was provided. |
| 929 | /// \param Diagnosed Set to true if an error is produced. |
| 930 | static void CheckConstexprCtorInitializer(Sema &SemaRef, |
| 931 | const FunctionDecl *Dcl, |
| 932 | FieldDecl *Field, |
| 933 | llvm::SmallSet<Decl*, 16> &Inits, |
| 934 | bool &Diagnosed) { |
| Eli Friedman | b13e64e | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 935 | if (Field->isInvalidDecl()) |
| 936 | return; |
| 937 | |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 938 | if (Field->isUnnamedBitfield()) |
| 939 | return; |
| Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 940 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 941 | // Anonymous unions with no variant members and empty anonymous structs do not |
| 942 | // need to be explicitly initialized. FIXME: Anonymous structs that contain no |
| 943 | // indirect fields don't need initializing. |
| Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 944 | if (Field->isAnonymousStructOrUnion() && |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 945 | (Field->getType()->isUnionType() |
| 946 | ? !Field->getType()->getAsCXXRecordDecl()->hasVariantMembers() |
| 947 | : Field->getType()->getAsCXXRecordDecl()->isEmpty())) |
| Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 948 | return; |
| 949 | |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 950 | if (!Inits.count(Field)) { |
| 951 | if (!Diagnosed) { |
| 952 | SemaRef.Diag(Dcl->getLocation(), diag::err_constexpr_ctor_missing_init); |
| 953 | Diagnosed = true; |
| 954 | } |
| 955 | SemaRef.Diag(Field->getLocation(), diag::note_constexpr_ctor_missing_init); |
| 956 | } else if (Field->isAnonymousStructOrUnion()) { |
| 957 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 958 | for (auto *I : RD->fields()) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 959 | // If an anonymous union contains an anonymous struct of which any member |
| 960 | // is initialized, all members must be initialized. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 961 | if (!RD->isUnion() || Inits.count(I)) |
| 962 | CheckConstexprCtorInitializer(SemaRef, Dcl, I, Inits, Diagnosed); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 966 | /// Check the provided statement is allowed in a constexpr function |
| 967 | /// definition. |
| 968 | static bool |
| 969 | CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S, |
| Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 970 | SmallVectorImpl<SourceLocation> &ReturnStmts, |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 971 | SourceLocation &Cxx1yLoc) { |
| 972 | // - its function-body shall be [...] a compound-statement that contains only |
| 973 | switch (S->getStmtClass()) { |
| 974 | case Stmt::NullStmtClass: |
| 975 | // - null statements, |
| 976 | return true; |
| 977 | |
| 978 | case Stmt::DeclStmtClass: |
| 979 | // - static_assert-declarations |
| 980 | // - using-declarations, |
| 981 | // - using-directives, |
| 982 | // - typedef declarations and alias-declarations that do not define |
| 983 | // classes or enumerations, |
| 984 | if (!CheckConstexprDeclStmt(SemaRef, Dcl, cast<DeclStmt>(S), Cxx1yLoc)) |
| 985 | return false; |
| 986 | return true; |
| 987 | |
| 988 | case Stmt::ReturnStmtClass: |
| 989 | // - and exactly one return statement; |
| 990 | if (isa<CXXConstructorDecl>(Dcl)) { |
| 991 | // C++1y allows return statements in constexpr constructors. |
| 992 | if (!Cxx1yLoc.isValid()) |
| 993 | Cxx1yLoc = S->getLocStart(); |
| 994 | return true; |
| 995 | } |
| 996 | |
| 997 | ReturnStmts.push_back(S->getLocStart()); |
| 998 | return true; |
| 999 | |
| 1000 | case Stmt::CompoundStmtClass: { |
| 1001 | // C++1y allows compound-statements. |
| 1002 | if (!Cxx1yLoc.isValid()) |
| 1003 | Cxx1yLoc = S->getLocStart(); |
| 1004 | |
| 1005 | CompoundStmt *CompStmt = cast<CompoundStmt>(S); |
| Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 1006 | for (auto *BodyIt : CompStmt->body()) { |
| 1007 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, BodyIt, ReturnStmts, |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1008 | Cxx1yLoc)) |
| 1009 | return false; |
| 1010 | } |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
| 1014 | case Stmt::AttributedStmtClass: |
| 1015 | if (!Cxx1yLoc.isValid()) |
| 1016 | Cxx1yLoc = S->getLocStart(); |
| 1017 | return true; |
| 1018 | |
| 1019 | case Stmt::IfStmtClass: { |
| 1020 | // C++1y allows if-statements. |
| 1021 | if (!Cxx1yLoc.isValid()) |
| 1022 | Cxx1yLoc = S->getLocStart(); |
| 1023 | |
| 1024 | IfStmt *If = cast<IfStmt>(S); |
| 1025 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, If->getThen(), ReturnStmts, |
| 1026 | Cxx1yLoc)) |
| 1027 | return false; |
| 1028 | if (If->getElse() && |
| 1029 | !CheckConstexprFunctionStmt(SemaRef, Dcl, If->getElse(), ReturnStmts, |
| 1030 | Cxx1yLoc)) |
| 1031 | return false; |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
| 1035 | case Stmt::WhileStmtClass: |
| 1036 | case Stmt::DoStmtClass: |
| 1037 | case Stmt::ForStmtClass: |
| 1038 | case Stmt::CXXForRangeStmtClass: |
| 1039 | case Stmt::ContinueStmtClass: |
| 1040 | // C++1y allows all of these. We don't allow them as extensions in C++11, |
| 1041 | // because they don't make sense without variable mutation. |
| 1042 | if (!SemaRef.getLangOpts().CPlusPlus1y) |
| 1043 | break; |
| 1044 | if (!Cxx1yLoc.isValid()) |
| 1045 | Cxx1yLoc = S->getLocStart(); |
| 1046 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1047 | if (*Children && |
| 1048 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1049 | Cxx1yLoc)) |
| 1050 | return false; |
| 1051 | return true; |
| 1052 | |
| 1053 | case Stmt::SwitchStmtClass: |
| 1054 | case Stmt::CaseStmtClass: |
| 1055 | case Stmt::DefaultStmtClass: |
| 1056 | case Stmt::BreakStmtClass: |
| 1057 | // C++1y allows switch-statements, and since they don't need variable |
| 1058 | // mutation, we can reasonably allow them in C++11 as an extension. |
| 1059 | if (!Cxx1yLoc.isValid()) |
| 1060 | Cxx1yLoc = S->getLocStart(); |
| 1061 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1062 | if (*Children && |
| 1063 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1064 | Cxx1yLoc)) |
| 1065 | return false; |
| 1066 | return true; |
| 1067 | |
| 1068 | default: |
| 1069 | if (!isa<Expr>(S)) |
| 1070 | break; |
| 1071 | |
| 1072 | // C++1y allows expression-statements. |
| 1073 | if (!Cxx1yLoc.isValid()) |
| 1074 | Cxx1yLoc = S->getLocStart(); |
| 1075 | return true; |
| 1076 | } |
| 1077 | |
| 1078 | SemaRef.Diag(S->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 1079 | << isa<CXXConstructorDecl>(Dcl); |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1083 | /// Check the body for the given constexpr function declaration only contains |
| 1084 | /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4. |
| 1085 | /// |
| 1086 | /// \return true if the body is OK, false if we have diagnosed a problem. |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1087 | bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1088 | if (isa<CXXTryStmt>(Body)) { |
| Richard Smith | 74388b4 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1089 | // C++11 [dcl.constexpr]p3: |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1090 | // The definition of a constexpr function shall satisfy the following |
| 1091 | // constraints: [...] |
| 1092 | // - its function-body shall be = delete, = default, or a |
| 1093 | // compound-statement |
| 1094 | // |
| Richard Smith | 74388b4 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1095 | // C++11 [dcl.constexpr]p4: |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1096 | // In the definition of a constexpr constructor, [...] |
| 1097 | // - its function-body shall not be a function-try-block; |
| 1098 | Diag(Body->getLocStart(), diag::err_constexpr_function_try_block) |
| 1099 | << isa<CXXConstructorDecl>(Dcl); |
| 1100 | return false; |
| 1101 | } |
| 1102 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1103 | SmallVector<SourceLocation, 4> ReturnStmts; |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1104 | |
| 1105 | // - its function-body shall be [...] a compound-statement that contains only |
| 1106 | // [... list of cases ...] |
| 1107 | CompoundStmt *CompBody = cast<CompoundStmt>(Body); |
| 1108 | SourceLocation Cxx1yLoc; |
| Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 1109 | for (auto *BodyIt : CompBody->body()) { |
| 1110 | if (!CheckConstexprFunctionStmt(*this, Dcl, BodyIt, ReturnStmts, Cxx1yLoc)) |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1111 | return false; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1114 | if (Cxx1yLoc.isValid()) |
| 1115 | Diag(Cxx1yLoc, |
| 1116 | getLangOpts().CPlusPlus1y |
| 1117 | ? diag::warn_cxx11_compat_constexpr_body_invalid_stmt |
| 1118 | : diag::ext_constexpr_body_invalid_stmt) |
| 1119 | << isa<CXXConstructorDecl>(Dcl); |
| 1120 | |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1121 | if (const CXXConstructorDecl *Constructor |
| 1122 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 1123 | const CXXRecordDecl *RD = Constructor->getParent(); |
| Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 1124 | // DR1359: |
| 1125 | // - every non-variant non-static data member and base class sub-object |
| 1126 | // shall be initialized; |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1127 | // DR1460: |
| 1128 | // - if the class is a union having variant members, exactly one of them |
| Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 1129 | // shall be initialized; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1130 | if (RD->isUnion()) { |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1131 | if (Constructor->getNumCtorInitializers() == 0 && |
| 1132 | RD->hasVariantMembers()) { |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1133 | Diag(Dcl->getLocation(), diag::err_constexpr_union_ctor_no_init); |
| 1134 | return false; |
| 1135 | } |
| Richard Smith | f368fb4 | 2011-10-10 16:38:04 +0000 | [diff] [blame] | 1136 | } else if (!Constructor->isDependentContext() && |
| 1137 | !Constructor->isDelegatingConstructor()) { |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1138 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 1139 | |
| 1140 | // Skip detailed checking if we have enough initializers, and we would |
| 1141 | // allow at most one initializer per member. |
| 1142 | bool AnyAnonStructUnionMembers = false; |
| 1143 | unsigned Fields = 0; |
| 1144 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1145 | E = RD->field_end(); I != E; ++I, ++Fields) { |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1146 | if (I->isAnonymousStructOrUnion()) { |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1147 | AnyAnonStructUnionMembers = true; |
| 1148 | break; |
| 1149 | } |
| 1150 | } |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1151 | // DR1460: |
| 1152 | // - if the class is a union-like class, but is not a union, for each of |
| 1153 | // its anonymous union members having variant members, exactly one of |
| 1154 | // them shall be initialized; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1155 | if (AnyAnonStructUnionMembers || |
| 1156 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 1157 | // Check initialization of non-static data members. Base classes are |
| 1158 | // always initialized so do not need to be checked. Dependent bases |
| 1159 | // might not have initializers in the member initializer list. |
| 1160 | llvm::SmallSet<Decl*, 16> Inits; |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 1161 | for (const auto *I: Constructor->inits()) { |
| 1162 | if (FieldDecl *FD = I->getMember()) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1163 | Inits.insert(FD); |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 1164 | else if (IndirectFieldDecl *ID = I->getIndirectMember()) |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1165 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 1166 | } |
| 1167 | |
| 1168 | bool Diagnosed = false; |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1169 | for (auto *I : RD->fields()) |
| 1170 | CheckConstexprCtorInitializer(*this, Dcl, I, Inits, Diagnosed); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1171 | if (Diagnosed) |
| 1172 | return false; |
| 1173 | } |
| 1174 | } |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1175 | } else { |
| 1176 | if (ReturnStmts.empty()) { |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1177 | // C++1y doesn't require constexpr functions to contain a 'return' |
| Richard Smith | 06ffb45 | 2014-04-22 23:14:23 +0000 | [diff] [blame] | 1178 | // statement. We still do, unless the return type might be void, because |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1179 | // otherwise if there's no return statement, the function cannot |
| 1180 | // be used in a core constant expression. |
| Richard Smith | 06ffb45 | 2014-04-22 23:14:23 +0000 | [diff] [blame] | 1181 | bool OK = getLangOpts().CPlusPlus1y && |
| 1182 | (Dcl->getReturnType()->isVoidType() || |
| 1183 | Dcl->getReturnType()->isDependentType()); |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1184 | Diag(Dcl->getLocation(), |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1185 | OK ? diag::warn_cxx11_compat_constexpr_body_no_return |
| 1186 | : diag::err_constexpr_body_no_return); |
| 1187 | return OK; |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1188 | } |
| 1189 | if (ReturnStmts.size() > 1) { |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1190 | Diag(ReturnStmts.back(), |
| 1191 | getLangOpts().CPlusPlus1y |
| 1192 | ? diag::warn_cxx11_compat_constexpr_body_multiple_return |
| 1193 | : diag::ext_constexpr_body_multiple_return); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1194 | for (unsigned I = 0; I < ReturnStmts.size() - 1; ++I) |
| 1195 | Diag(ReturnStmts[I], diag::note_constexpr_body_previous_return); |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
| Richard Smith | 74388b4 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1199 | // C++11 [dcl.constexpr]p5: |
| 1200 | // if no function argument values exist such that the function invocation |
| 1201 | // substitution would produce a constant expression, the program is |
| 1202 | // ill-formed; no diagnostic required. |
| 1203 | // C++11 [dcl.constexpr]p3: |
| 1204 | // - every constructor call and implicit conversion used in initializing the |
| 1205 | // return value shall be one of those allowed in a constant expression. |
| 1206 | // C++11 [dcl.constexpr]p4: |
| 1207 | // - every constructor involved in initializing non-static data members and |
| 1208 | // base class sub-objects shall be a constexpr constructor. |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1209 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1210 | if (!Expr::isPotentialConstantExpr(Dcl, Diags)) { |
| Richard Smith | f86b5dc | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 1211 | Diag(Dcl->getLocation(), diag::ext_constexpr_function_never_constant_expr) |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1212 | << isa<CXXConstructorDecl>(Dcl); |
| 1213 | for (size_t I = 0, N = Diags.size(); I != N; ++I) |
| 1214 | Diag(Diags[I].first, Diags[I].second); |
| Richard Smith | f86b5dc | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 1215 | // Don't return false here: we allow this for compatibility in |
| 1216 | // system headers. |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1219 | return true; |
| 1220 | } |
| 1221 | |
| Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1222 | /// isCurrentClassName - Determine whether the identifier II is the |
| 1223 | /// name of the class type currently being defined. In the case of |
| 1224 | /// nested classes, this will only return true if II is the name of |
| 1225 | /// the innermost class. |
| Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1226 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 1227 | const CXXScopeSpec *SS) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1228 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
| Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 1229 | |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1230 | CXXRecordDecl *CurDecl; |
| Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1231 | if (SS && SS->isSet() && !SS->isInvalid()) { |
| Douglas Gregor | e5bbb7d | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 1232 | DeclContext *DC = computeDeclContext(*SS, true); |
| Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1233 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 1234 | } else |
| 1235 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1236 | |
| Douglas Gregor | 1aa3edb | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 1237 | if (CurDecl && CurDecl->getIdentifier()) |
| Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1238 | return &II == CurDecl->getIdentifier(); |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 1239 | return false; |
| Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 1242 | /// \brief Determine whether the identifier II is a typo for the name of |
| 1243 | /// the class type currently being defined. If so, update it to the identifier |
| 1244 | /// that should have been used. |
| 1245 | bool Sema::isCurrentClassNameTypo(IdentifierInfo *&II, const CXXScopeSpec *SS) { |
| 1246 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
| 1247 | |
| 1248 | if (!getLangOpts().SpellChecking) |
| 1249 | return false; |
| 1250 | |
| 1251 | CXXRecordDecl *CurDecl; |
| 1252 | if (SS && SS->isSet() && !SS->isInvalid()) { |
| 1253 | DeclContext *DC = computeDeclContext(*SS, true); |
| 1254 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 1255 | } else |
| 1256 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1257 | |
| 1258 | if (CurDecl && CurDecl->getIdentifier() && II != CurDecl->getIdentifier() && |
| 1259 | 3 * II->getName().edit_distance(CurDecl->getIdentifier()->getName()) |
| 1260 | < II->getLength()) { |
| 1261 | II = CurDecl->getIdentifier(); |
| 1262 | return true; |
| 1263 | } |
| 1264 | |
| 1265 | return false; |
| 1266 | } |
| 1267 | |
| Douglas Gregor | dc97457 | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1268 | /// \brief Determine whether the given class is a base class of the given |
| 1269 | /// class, including looking at dependent bases. |
| 1270 | static bool findCircularInheritance(const CXXRecordDecl *Class, |
| 1271 | const CXXRecordDecl *Current) { |
| 1272 | SmallVector<const CXXRecordDecl*, 8> Queue; |
| 1273 | |
| 1274 | Class = Class->getCanonicalDecl(); |
| 1275 | while (true) { |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1276 | for (const auto &I : Current->bases()) { |
| 1277 | CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl(); |
| Douglas Gregor | dc97457 | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1278 | if (!Base) |
| 1279 | continue; |
| 1280 | |
| 1281 | Base = Base->getDefinition(); |
| 1282 | if (!Base) |
| 1283 | continue; |
| 1284 | |
| 1285 | if (Base->getCanonicalDecl() == Class) |
| 1286 | return true; |
| 1287 | |
| 1288 | Queue.push_back(Base); |
| 1289 | } |
| 1290 | |
| 1291 | if (Queue.empty()) |
| 1292 | return false; |
| 1293 | |
| Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1294 | Current = Queue.pop_back_val(); |
| Douglas Gregor | dc97457 | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | return false; |
| Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | /// \brief Check the validity of a C++ base class specifier. |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1301 | /// |
| 1302 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 1303 | /// and returns NULL otherwise. |
| 1304 | CXXBaseSpecifier * |
| 1305 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 1306 | SourceRange SpecifierRange, |
| 1307 | bool Virtual, AccessSpecifier Access, |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1308 | TypeSourceInfo *TInfo, |
| 1309 | SourceLocation EllipsisLoc) { |
| Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1310 | QualType BaseType = TInfo->getType(); |
| 1311 | |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1312 | // C++ [class.union]p1: |
| 1313 | // A union shall not have base classes. |
| 1314 | if (Class->isUnion()) { |
| 1315 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1316 | << SpecifierRange; |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1320 | if (EllipsisLoc.isValid() && |
| 1321 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1322 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1323 | << TInfo->getTypeLoc().getSourceRange(); |
| 1324 | EllipsisLoc = SourceLocation(); |
| 1325 | } |
| Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1326 | |
| 1327 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 1328 | |
| 1329 | if (BaseType->isDependentType()) { |
| 1330 | // Make sure that we don't have circular inheritance among our dependent |
| 1331 | // bases. For non-dependent bases, the check for completeness below handles |
| 1332 | // this. |
| 1333 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 1334 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 1335 | ((BaseDecl = BaseDecl->getDefinition()) && |
| Douglas Gregor | dc97457 | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1336 | findCircularInheritance(Class, BaseDecl))) { |
| Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1337 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 1338 | << BaseType << Context.getTypeDeclType(Class); |
| 1339 | |
| 1340 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 1341 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 1342 | << BaseType; |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1349 | Class->getTagKind() == TTK_Class, |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1350 | Access, TInfo, EllipsisLoc); |
| Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1351 | } |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1352 | |
| 1353 | // Base specifiers must be record types. |
| 1354 | if (!BaseType->isRecordType()) { |
| 1355 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | // C++ [class.union]p1: |
| 1360 | // A union shall not be used as a base class. |
| 1361 | if (BaseType->isUnionType()) { |
| 1362 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | // C++ [class.derived]p2: |
| 1367 | // The class-name in a base-specifier shall not be an incompletely |
| 1368 | // defined class. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | if (RequireCompleteType(BaseLoc, BaseType, |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1370 | diag::err_incomplete_base_class, SpecifierRange)) { |
| John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1371 | Class->setInvalidDecl(); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1372 | return 0; |
| John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1373 | } |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1374 | |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1375 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1376 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1377 | assert(BaseDecl && "Record type has no declaration"); |
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1378 | BaseDecl = BaseDecl->getDefinition(); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1379 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
| David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1380 | CXXRecordDecl *CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1381 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
| Eli Friedman | 89c038e | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1382 | |
| David Majnemer | 9b1754d | 2013-11-02 12:00:36 +0000 | [diff] [blame] | 1383 | // A class which contains a flexible array member is not suitable for use as a |
| 1384 | // base class: |
| 1385 | // - If the layout determines that a base comes before another base, |
| 1386 | // the flexible array member would index into the subsequent base. |
| 1387 | // - If the layout determines that base comes before the derived class, |
| 1388 | // the flexible array member would index into the derived class. |
| 1389 | if (CXXBaseDecl->hasFlexibleArrayMember()) { |
| 1390 | Diag(BaseLoc, diag::err_base_class_has_flexible_array_member) |
| 1391 | << CXXBaseDecl->getDeclName(); |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| Anders Carlsson | 65c76d3 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1395 | // C++ [class]p3: |
| David Majnemer | 4589760 | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1396 | // If a class is marked final and it appears as a base-type-specifier in |
| Anders Carlsson | 65c76d3 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1397 | // base-clause, the program is ill-formed. |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1398 | if (FinalAttr *FA = CXXBaseDecl->getAttr<FinalAttr>()) { |
| David Majnemer | 4589760 | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1399 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1400 | << CXXBaseDecl->getDeclName() |
| 1401 | << FA->isSpelledAsSealed(); |
| Anders Carlsson | fc1eef4 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1402 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 1403 | << CXXBaseDecl->getDeclName(); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1407 | if (BaseDecl->isInvalidDecl()) |
| 1408 | Class->setInvalidDecl(); |
| David Majnemer | 4589760 | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1409 | |
| Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1410 | // Create the base specifier. |
| Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1411 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1412 | Class->getTagKind() == TTK_Class, |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1413 | Access, TInfo, EllipsisLoc); |
| Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1416 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1417 | /// one entry in the base class list of a class specifier, for |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | /// example: |
| 1419 | /// class foo : public bar, virtual private baz { |
| Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1420 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1421 | BaseResult |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1422 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
| Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1423 | ParsedAttributes &Attributes, |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1424 | bool Virtual, AccessSpecifier Access, |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1425 | ParsedType basetype, SourceLocation BaseLoc, |
| 1426 | SourceLocation EllipsisLoc) { |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1427 | if (!classdecl) |
| 1428 | return true; |
| 1429 | |
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1430 | AdjustDeclIfTemplate(classdecl); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1431 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
| Douglas Gregor | beab56e | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1432 | if (!Class) |
| 1433 | return true; |
| 1434 | |
| Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1435 | // We do not support any C++11 attributes on base-specifiers yet. |
| 1436 | // Diagnose any attributes we see. |
| 1437 | if (!Attributes.empty()) { |
| 1438 | for (AttributeList *Attr = Attributes.getList(); Attr; |
| 1439 | Attr = Attr->getNext()) { |
| 1440 | if (Attr->isInvalid() || |
| 1441 | Attr->getKind() == AttributeList::IgnoredAttribute) |
| 1442 | continue; |
| 1443 | Diag(Attr->getLoc(), |
| 1444 | Attr->getKind() == AttributeList::UnknownAttribute |
| 1445 | ? diag::warn_unknown_attribute_ignored |
| 1446 | : diag::err_base_specifier_attribute) |
| 1447 | << Attr->getName(); |
| 1448 | } |
| 1449 | } |
| 1450 | |
| Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1451 | TypeSourceInfo *TInfo = 0; |
| 1452 | GetTypeFromParser(basetype, &TInfo); |
| Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1453 | |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1454 | if (EllipsisLoc.isInvalid() && |
| 1455 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
| Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1456 | UPPC_BaseType)) |
| 1457 | return true; |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1458 | |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1459 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1460 | Virtual, Access, TInfo, |
| 1461 | EllipsisLoc)) |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1462 | return BaseSpec; |
| Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 1463 | else |
| 1464 | Class->setInvalidDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1466 | return true; |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1467 | } |
| Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1468 | |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1469 | /// \brief Performs the actual work of attaching the given base class |
| 1470 | /// specifiers to a C++ class. |
| 1471 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1472 | unsigned NumBases) { |
| 1473 | if (NumBases == 0) |
| 1474 | return false; |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Used to keep track of which base types we have already seen, so |
| 1477 | // that we can properly diagnose redundant direct base types. Note |
| Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1478 | // that the key is always the unqualified canonical type of the base |
| 1479 | // class. |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1480 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1481 | |
| 1482 | // Copy non-redundant base specifiers into permanent storage. |
| Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1483 | unsigned NumGoodBases = 0; |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1484 | bool Invalid = false; |
| Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1485 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | QualType NewBaseType |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1487 | = Context.getCanonicalType(Bases[idx]->getType()); |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1488 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
| Benjamin Kramer | 73ecd70 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1489 | |
| 1490 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1491 | if (KnownBase) { |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1492 | // C++ [class.mi]p3: |
| 1493 | // A class shall not be specified as a direct base class of a |
| 1494 | // derived class more than once. |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1495 | Diag(Bases[idx]->getLocStart(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1496 | diag::err_duplicate_base_class) |
| Benjamin Kramer | 73ecd70 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1497 | << KnownBase->getType() |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1498 | << Bases[idx]->getSourceRange(); |
| Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1499 | |
| 1500 | // Delete the duplicate base class specifier; we're going to |
| 1501 | // overwrite its pointer later. |
| Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1502 | Context.Deallocate(Bases[idx]); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1503 | |
| 1504 | Invalid = true; |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1505 | } else { |
| 1506 | // Okay, add this new base class. |
| Benjamin Kramer | 73ecd70 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1507 | KnownBase = Bases[idx]; |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1508 | Bases[NumGoodBases++] = Bases[idx]; |
| John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1509 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 1510 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 1511 | if (Class->isInterface() && |
| 1512 | (!RD->isInterface() || |
| 1513 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 1514 | // The Microsoft extension __interface does not permit bases that |
| 1515 | // are not themselves public interfaces. |
| 1516 | Diag(KnownBase->getLocStart(), diag::err_invalid_base_in_interface) |
| 1517 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getName() |
| 1518 | << RD->getSourceRange(); |
| 1519 | Invalid = true; |
| 1520 | } |
| 1521 | if (RD->hasAttr<WeakAttr>()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1522 | Class->addAttr(WeakAttr::CreateImplicit(Context)); |
| John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1523 | } |
| Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | // Attach the remaining base class specifiers to the derived class. |
| Douglas Gregor | 4a62bdf | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1528 | Class->setBases(Bases, NumGoodBases); |
| Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1529 | |
| 1530 | // Delete the remaining (good) base class specifiers, since their |
| 1531 | // data has been copied into the CXXRecordDecl. |
| 1532 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
| Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1533 | Context.Deallocate(Bases[idx]); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1534 | |
| 1535 | return Invalid; |
| 1536 | } |
| 1537 | |
| 1538 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1539 | /// class, after checking whether there are any duplicate base |
| 1540 | /// classes. |
| Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1541 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1542 | unsigned NumBases) { |
| 1543 | if (!ClassDecl || !Bases || !NumBases) |
| 1544 | return; |
| 1545 | |
| 1546 | AdjustDeclIfTemplate(ClassDecl); |
| Robert Wilhelm | e3cea80 | 2013-07-22 05:04:01 +0000 | [diff] [blame] | 1547 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases, NumBases); |
| Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1548 | } |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1549 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1550 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1551 | /// derived from the type \p Base. |
| 1552 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1553 | if (!getLangOpts().CPlusPlus) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1554 | return false; |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1555 | |
| Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1556 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1557 | if (!DerivedRD) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1558 | return false; |
| 1559 | |
| Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1560 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1561 | if (!BaseRD) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1562 | return false; |
| Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1563 | |
| 1564 | // If either the base or the derived type is invalid, don't try to |
| 1565 | // check whether one is derived from the other. |
| 1566 | if (BaseRD->isInvalidDecl() || DerivedRD->isInvalidDecl()) |
| 1567 | return false; |
| 1568 | |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1569 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1570 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1574 | /// derived from the type \p Base. |
| 1575 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1576 | if (!getLangOpts().CPlusPlus) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1577 | return false; |
| 1578 | |
| Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1579 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1580 | if (!DerivedRD) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1581 | return false; |
| 1582 | |
| Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1583 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1584 | if (!BaseRD) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1585 | return false; |
| 1586 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1587 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1588 | } |
| 1589 | |
| Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1590 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1591 | CXXCastPath &BasePathArray) { |
| Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1592 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1593 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1594 | |
| 1595 | const CXXBasePath &Path = Paths.front(); |
| 1596 | |
| 1597 | // We first go backward and check if we have a virtual base. |
| 1598 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1599 | // the nearest virtual base. |
| 1600 | unsigned Start = 0; |
| 1601 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1602 | if (Path[I - 1].Base->isVirtual()) { |
| 1603 | Start = I - 1; |
| 1604 | break; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | // Now add all bases. |
| 1609 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1610 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
| Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1613 | /// \brief Determine whether the given base path includes a virtual |
| 1614 | /// base class. |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1615 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 1616 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 1617 | BEnd = BasePath.end(); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1618 | B != BEnd; ++B) |
| 1619 | if ((*B)->isVirtual()) |
| 1620 | return true; |
| 1621 | |
| 1622 | return false; |
| 1623 | } |
| 1624 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1625 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1626 | /// conversion (where Derived and Base are class types) is |
| 1627 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1628 | /// that all of the base classes are accessible). Returns true |
| 1629 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1630 | /// otherwise. Loc is the location where this routine should point to |
| 1631 | /// if there is an error, and Range is the source range to highlight |
| 1632 | /// if there is an error. |
| 1633 | bool |
| 1634 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1635 | unsigned InaccessibleBaseID, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1636 | unsigned AmbigiousBaseConvID, |
| 1637 | SourceLocation Loc, SourceRange Range, |
| Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1638 | DeclarationName Name, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1639 | CXXCastPath *BasePath) { |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1640 | // First, determine whether the path from Derived to Base is |
| 1641 | // ambiguous. This is slightly more expensive than checking whether |
| 1642 | // the Derived to Base conversion exists, because here we need to |
| 1643 | // explore multiple paths to determine if there is an ambiguity. |
| 1644 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1645 | /*DetectVirtual=*/false); |
| 1646 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1647 | assert(DerivationOkay && |
| 1648 | "Can only be used with a derived-to-base conversion"); |
| 1649 | (void)DerivationOkay; |
| 1650 | |
| 1651 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
| Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1652 | if (InaccessibleBaseID) { |
| 1653 | // Check that the base class can be accessed. |
| 1654 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1655 | InaccessibleBaseID)) { |
| 1656 | case AR_inaccessible: |
| 1657 | return true; |
| 1658 | case AR_accessible: |
| 1659 | case AR_dependent: |
| 1660 | case AR_delayed: |
| 1661 | break; |
| Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1662 | } |
| John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1663 | } |
| Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1664 | |
| 1665 | // Build a base path if necessary. |
| 1666 | if (BasePath) |
| 1667 | BuildBasePathArray(Paths, *BasePath); |
| 1668 | return false; |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1671 | if (AmbigiousBaseConvID) { |
| 1672 | // We know that the derived-to-base conversion is ambiguous, and |
| 1673 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1674 | // search just one more time to compute all of the possible paths so |
| 1675 | // that we can print them out. This is more expensive than any of |
| 1676 | // the previous derived-to-base checks we've done, but at this point |
| 1677 | // performance isn't as much of an issue. |
| 1678 | Paths.clear(); |
| 1679 | Paths.setRecordingPaths(true); |
| 1680 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1681 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1682 | (void)StillOkay; |
| 1683 | |
| 1684 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1685 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1686 | // conversions in the diagnostic. We only print one of the paths |
| 1687 | // to each base class subobject. |
| 1688 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1689 | |
| 1690 | Diag(Loc, AmbigiousBaseConvID) |
| 1691 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1692 | } |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1693 | return true; |
| 1694 | } |
| 1695 | |
| 1696 | bool |
| 1697 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1698 | SourceLocation Loc, SourceRange Range, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1699 | CXXCastPath *BasePath, |
| Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1700 | bool IgnoreAccess) { |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1701 | return CheckDerivedToBaseConversion(Derived, Base, |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1702 | IgnoreAccess ? 0 |
| 1703 | : diag::err_upcast_to_inaccessible_base, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1704 | diag::err_ambiguous_derived_to_base_conv, |
| Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1705 | Loc, Range, DeclarationName(), |
| 1706 | BasePath); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | |
| 1710 | /// @brief Builds a string representing ambiguous paths from a |
| 1711 | /// specific derived class to different subobjects of the same base |
| 1712 | /// class. |
| 1713 | /// |
| 1714 | /// This function builds a string that can be used in error messages |
| 1715 | /// to show the different paths that one can take through the |
| 1716 | /// inheritance hierarchy to go from the derived class to different |
| 1717 | /// subobjects of a base class. The result looks something like this: |
| 1718 | /// @code |
| 1719 | /// struct D -> struct B -> struct A |
| 1720 | /// struct D -> struct C -> struct A |
| 1721 | /// @endcode |
| 1722 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1723 | std::string PathDisplayStr; |
| 1724 | std::set<unsigned> DisplayedPaths; |
| 1725 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1726 | Path != Paths.end(); ++Path) { |
| 1727 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1728 | // We haven't displayed a path to this particular base |
| 1729 | // class subobject yet. |
| 1730 | PathDisplayStr += "\n "; |
| 1731 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1732 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1733 | Element != Path->end(); ++Element) |
| 1734 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | return PathDisplayStr; |
| 1739 | } |
| 1740 | |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1741 | //===----------------------------------------------------------------------===// |
| 1742 | // C++ class member Handling |
| 1743 | //===----------------------------------------------------------------------===// |
| 1744 | |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1745 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
| Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1746 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1747 | SourceLocation ASLoc, |
| 1748 | SourceLocation ColonLoc, |
| 1749 | AttributeList *Attrs) { |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1750 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1751 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1752 | ASLoc, ColonLoc); |
| 1753 | CurContext->addHiddenDecl(ASDecl); |
| Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1754 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1757 | /// CheckOverrideControl - Check C++11 override control semantics. |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1758 | void Sema::CheckOverrideControl(NamedDecl *D) { |
| Richard Smith | 09b031f | 2012-09-06 18:32:18 +0000 | [diff] [blame] | 1759 | if (D->isInvalidDecl()) |
| 1760 | return; |
| 1761 | |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1762 | // We only care about "override" and "final" declarations. |
| 1763 | if (!D->hasAttr<OverrideAttr>() && !D->hasAttr<FinalAttr>()) |
| 1764 | return; |
| Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1765 | |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1766 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
| Anders Carlsson | fa8e5d3 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1767 | |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1768 | // We can't check dependent instance methods. |
| 1769 | if (MD && MD->isInstance() && |
| 1770 | (MD->getParent()->hasAnyDependentBases() || |
| 1771 | MD->getType()->isDependentType())) |
| 1772 | return; |
| 1773 | |
| 1774 | if (MD && !MD->isVirtual()) { |
| 1775 | // If we have a non-virtual method, check if if hides a virtual method. |
| 1776 | // (In that case, it's most likely the method has the wrong type.) |
| 1777 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 1778 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 1779 | |
| 1780 | if (!OverloadedMethods.empty()) { |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1781 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1782 | Diag(OA->getLocation(), |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1783 | diag::override_keyword_hides_virtual_member_function) |
| 1784 | << "override" << (OverloadedMethods.size() > 1); |
| 1785 | } else if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1786 | Diag(FA->getLocation(), |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1787 | diag::override_keyword_hides_virtual_member_function) |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1788 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 1789 | << (OverloadedMethods.size() > 1); |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1790 | } |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1791 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
| 1792 | MD->setInvalidDecl(); |
| 1793 | return; |
| 1794 | } |
| 1795 | // Fall through into the general case diagnostic. |
| 1796 | // FIXME: We might want to attempt typo correction here. |
| 1797 | } |
| 1798 | |
| 1799 | if (!MD || !MD->isVirtual()) { |
| 1800 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1801 | Diag(OA->getLocation(), |
| 1802 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1803 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 1804 | D->dropAttr<OverrideAttr>(); |
| 1805 | } |
| 1806 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 1807 | Diag(FA->getLocation(), |
| 1808 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1809 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 1810 | << FixItHint::CreateRemoval(FA->getLocation()); |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1811 | D->dropAttr<FinalAttr>(); |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1812 | } |
| Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1813 | return; |
| 1814 | } |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1815 | |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1816 | // C++11 [class.virtual]p5: |
| 1817 | // If a virtual function is marked with the virt-specifier override and |
| 1818 | // does not override a member function of a base class, the program is |
| 1819 | // ill-formed. |
| 1820 | bool HasOverriddenMethods = |
| 1821 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
| 1822 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 1823 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 1824 | << MD->getDeclName(); |
| Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1827 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
| Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1828 | /// function overrides a virtual member function marked 'final', according to |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1829 | /// C++11 [class.virtual]p4. |
| Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1830 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1831 | const CXXMethodDecl *Old) { |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1832 | FinalAttr *FA = Old->getAttr<FinalAttr>(); |
| 1833 | if (!FA) |
| Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1834 | return false; |
| 1835 | |
| 1836 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1837 | << New->getDeclName() |
| 1838 | << FA->isSpelledAsSealed(); |
| Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1839 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1840 | return true; |
| Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1843 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1844 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 1845 | // FIXME: Destruction of ObjC lifetime types has side-effects. |
| 1846 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 1847 | return !RD->isCompleteDefinition() || |
| 1848 | !RD->hasTrivialDefaultConstructor() || |
| 1849 | !RD->hasTrivialDestructor(); |
| Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1850 | return false; |
| 1851 | } |
| 1852 | |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1853 | static AttributeList *getMSPropertyAttr(AttributeList *list) { |
| 1854 | for (AttributeList* it = list; it != 0; it = it->getNext()) |
| 1855 | if (it->isDeclspecPropertyAttribute()) |
| 1856 | return it; |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1860 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1861 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1862 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1863 | /// one has been parsed, and 'InitStyle' is set if an in-class initializer is |
| 1864 | /// present (but parsing it has been deferred). |
| Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1865 | NamedDecl * |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1866 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
| Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1867 | MultiTemplateParamsArg TemplateParameterLists, |
| Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1868 | Expr *BW, const VirtSpecifiers &VS, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1869 | InClassInitStyle InitStyle) { |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1870 | const DeclSpec &DS = D.getDeclSpec(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1871 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1872 | DeclarationName Name = NameInfo.getName(); |
| 1873 | SourceLocation Loc = NameInfo.getLoc(); |
| Douglas Gregor | 23ab745 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1874 | |
| 1875 | // For anonymous bitfields, the location should point to the type. |
| 1876 | if (Loc.isInvalid()) |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1877 | Loc = D.getLocStart(); |
| Douglas Gregor | 23ab745 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1878 | |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1879 | Expr *BitWidth = static_cast<Expr*>(BW); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1880 | |
| John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1881 | assert(isa<CXXRecordDecl>(CurContext)); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1882 | assert(!DS.isFriendSpecified()); |
| 1883 | |
| Richard Smith | cfcdf3a | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1884 | bool isFunc = D.isDeclarationOfFunction(); |
| John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1885 | |
| John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1886 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 1887 | // The Microsoft extension __interface only permits public member functions |
| 1888 | // and prohibits constructors, destructors, operators, non-public member |
| 1889 | // functions, static methods and data members. |
| 1890 | unsigned InvalidDecl; |
| 1891 | bool ShowDeclName = true; |
| 1892 | if (!isFunc) |
| 1893 | InvalidDecl = (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) ? 0 : 1; |
| 1894 | else if (AS != AS_public) |
| 1895 | InvalidDecl = 2; |
| 1896 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 1897 | InvalidDecl = 3; |
| 1898 | else switch (Name.getNameKind()) { |
| 1899 | case DeclarationName::CXXConstructorName: |
| 1900 | InvalidDecl = 4; |
| 1901 | ShowDeclName = false; |
| 1902 | break; |
| 1903 | |
| 1904 | case DeclarationName::CXXDestructorName: |
| 1905 | InvalidDecl = 5; |
| 1906 | ShowDeclName = false; |
| 1907 | break; |
| 1908 | |
| 1909 | case DeclarationName::CXXOperatorName: |
| 1910 | case DeclarationName::CXXConversionFunctionName: |
| 1911 | InvalidDecl = 6; |
| 1912 | break; |
| 1913 | |
| 1914 | default: |
| 1915 | InvalidDecl = 0; |
| 1916 | break; |
| 1917 | } |
| 1918 | |
| 1919 | if (InvalidDecl) { |
| 1920 | if (ShowDeclName) |
| 1921 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1922 | << (InvalidDecl-1) << Name; |
| 1923 | else |
| 1924 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1925 | << (InvalidDecl-1) << ""; |
| 1926 | return 0; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1930 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1931 | // duration (auto, register) or with the extern storage-class-specifier. |
| Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1932 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1933 | // data members and cannot be applied to names declared const or static, |
| 1934 | // and cannot be applied to reference members. |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1935 | switch (DS.getStorageClassSpec()) { |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1936 | case DeclSpec::SCS_unspecified: |
| 1937 | case DeclSpec::SCS_typedef: |
| 1938 | case DeclSpec::SCS_static: |
| 1939 | break; |
| 1940 | case DeclSpec::SCS_mutable: |
| 1941 | if (isFunc) { |
| 1942 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1944 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1945 | // declarator. Otherwise we could get follow-up errors. |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1946 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1947 | } |
| 1948 | break; |
| 1949 | default: |
| 1950 | Diag(DS.getStorageClassSpecLoc(), |
| 1951 | diag::err_storageclass_invalid_for_member); |
| 1952 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1953 | break; |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1956 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1957 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
| Argyrios Kyrtzidis | 1207d31 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1958 | !isFunc); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1959 | |
| David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1960 | if (DS.isConstexprSpecified() && isInstField) { |
| 1961 | SemaDiagnosticBuilder B = |
| 1962 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr_member); |
| 1963 | SourceLocation ConstexprLoc = DS.getConstexprSpecLoc(); |
| 1964 | if (InitStyle == ICIS_NoInit) { |
| Richard Smith | 82dce55 | 2014-04-14 21:00:40 +0000 | [diff] [blame] | 1965 | B << 0 << 0; |
| 1966 | if (D.getDeclSpec().getTypeQualifiers() & DeclSpec::TQ_const) |
| 1967 | B << FixItHint::CreateRemoval(ConstexprLoc); |
| 1968 | else { |
| 1969 | B << FixItHint::CreateReplacement(ConstexprLoc, "const"); |
| 1970 | D.getMutableDeclSpec().ClearConstexprSpec(); |
| 1971 | const char *PrevSpec; |
| 1972 | unsigned DiagID; |
| 1973 | bool Failed = D.getMutableDeclSpec().SetTypeQual( |
| 1974 | DeclSpec::TQ_const, ConstexprLoc, PrevSpec, DiagID, getLangOpts()); |
| 1975 | (void)Failed; |
| 1976 | assert(!Failed && "Making a constexpr member const shouldn't fail"); |
| 1977 | } |
| David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1978 | } else { |
| 1979 | B << 1; |
| 1980 | const char *PrevSpec; |
| 1981 | unsigned DiagID; |
| David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1982 | if (D.getMutableDeclSpec().SetStorageClassSpec( |
| Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1983 | *this, DeclSpec::SCS_static, ConstexprLoc, PrevSpec, DiagID, |
| 1984 | Context.getPrintingPolicy())) { |
| Matt Beaumont-Gay | efc270d | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 1985 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_mutable && |
| David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1986 | "This is the only DeclSpec that should fail to be applied"); |
| 1987 | B << 1; |
| 1988 | } else { |
| 1989 | B << 0 << FixItHint::CreateInsertion(ConstexprLoc, "static "); |
| 1990 | isInstField = false; |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | |
| Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1995 | NamedDecl *Member; |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1996 | if (isInstField) { |
| Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1997 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| Douglas Gregor | bb64afc | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1998 | |
| 1999 | // Data members must have identifiers for names. |
| Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 2000 | if (!Name.isIdentifier()) { |
| Douglas Gregor | bb64afc | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 2001 | Diag(Loc, diag::err_bad_variable_name) |
| 2002 | << Name; |
| 2003 | return 0; |
| 2004 | } |
| Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2005 | |
| Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 2006 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 2007 | |
| Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2008 | // Member field could not be with "template" keyword. |
| 2009 | // So TemplateParameterLists should be empty in this case. |
| 2010 | if (TemplateParameterLists.size()) { |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2011 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
| Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2012 | if (TemplateParams->size()) { |
| 2013 | // There is no such thing as a member field template. |
| 2014 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 2015 | << II |
| 2016 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 2017 | TemplateParams->getRAngleLoc()); |
| 2018 | } else { |
| 2019 | // There is an extraneous 'template<>' for this member. |
| 2020 | Diag(TemplateParams->getTemplateLoc(), |
| 2021 | diag::err_template_member_noparams) |
| 2022 | << II |
| 2023 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 2024 | TemplateParams->getRAngleLoc()); |
| 2025 | } |
| 2026 | return 0; |
| 2027 | } |
| 2028 | |
| Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2029 | if (SS.isSet() && !SS.isInvalid()) { |
| 2030 | // The user provided a superfluous scope specifier inside a class |
| 2031 | // definition: |
| 2032 | // |
| 2033 | // class X { |
| 2034 | // int X::member; |
| 2035 | // }; |
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 2036 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 2037 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc()); |
| Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2038 | else |
| 2039 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 2040 | << Name << SS.getRange(); |
| Douglas Gregor | c3ae7c3 | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 2041 | |
| Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2042 | SS.clear(); |
| 2043 | } |
| Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2044 | |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2045 | AttributeList *MSPropertyAttr = |
| 2046 | getMSPropertyAttr(D.getDeclSpec().getAttributes().getList()); |
| Eli Friedman | c37dbf7 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 2047 | if (MSPropertyAttr) { |
| 2048 | Member = HandleMSProperty(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 2049 | BitWidth, InitStyle, AS, MSPropertyAttr); |
| 2050 | if (!Member) |
| 2051 | return 0; |
| 2052 | isInstField = false; |
| 2053 | } else { |
| 2054 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 2055 | BitWidth, InitStyle, AS); |
| 2056 | assert(Member && "HandleField never returns null"); |
| 2057 | } |
| 2058 | } else { |
| 2059 | assert(InitStyle == ICIS_NoInit || D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static); |
| 2060 | |
| 2061 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
| 2062 | if (!Member) |
| 2063 | return 0; |
| 2064 | |
| 2065 | // Non-instance-fields can't have a bitfield. |
| 2066 | if (BitWidth) { |
| Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2067 | if (Member->isInvalidDecl()) { |
| 2068 | // don't emit another diagnostic. |
| Douglas Gregor | 212cab3 | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 2069 | } else if (isa<VarDecl>(Member)) { |
| Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2070 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 2071 | // "static member 'A' cannot be a bit-field" |
| 2072 | Diag(Loc, diag::err_static_not_bitfield) |
| 2073 | << Name << BitWidth->getSourceRange(); |
| 2074 | } else if (isa<TypedefDecl>(Member)) { |
| 2075 | // "typedef member 'x' cannot be a bit-field" |
| 2076 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 2077 | << Name << BitWidth->getSourceRange(); |
| 2078 | } else { |
| 2079 | // A function typedef ("typedef int f(); f a;"). |
| 2080 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 2081 | Diag(Loc, diag::err_not_integral_type_bitfield) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | << Name << cast<ValueDecl>(Member)->getType() |
| Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 2083 | << BitWidth->getSourceRange(); |
| Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2084 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
| Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2086 | BitWidth = 0; |
| 2087 | Member->setInvalidDecl(); |
| 2088 | } |
| Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 2089 | |
| 2090 | Member->setAccess(AS); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2092 | // If we have declared a member function template or static data member |
| 2093 | // template, set the access of the templated declaration as well. |
| Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2094 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 2095 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2096 | else if (VarTemplateDecl *VarTmpl = dyn_cast<VarTemplateDecl>(Member)) |
| 2097 | VarTmpl->getTemplatedDecl()->setAccess(AS); |
| Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 2098 | } |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2099 | |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2100 | if (VS.isOverrideSpecified()) |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2101 | Member->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context, 0)); |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2102 | if (VS.isFinalSpecified()) |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2103 | Member->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context, |
| 2104 | VS.isFinalSpelledSealed())); |
| Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2105 | |
| Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2106 | if (VS.getLastLocation().isValid()) { |
| 2107 | // Update the end location of a method that has a virt-specifiers. |
| 2108 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 2109 | MD->setRangeEnd(VS.getLastLocation()); |
| 2110 | } |
| Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2111 | |
| Anders Carlsson | c87f861 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 2112 | CheckOverrideControl(Member); |
| Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2113 | |
| Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 2114 | assert((Name || isInstField) && "No identifier for non-field ?"); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2115 | |
| Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2116 | if (isInstField) { |
| 2117 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 2118 | FieldCollector->Add(FD); |
| 2119 | |
| 2120 | if (Diags.getDiagnosticLevel(diag::warn_unused_private_field, |
| 2121 | FD->getLocation()) |
| 2122 | != DiagnosticsEngine::Ignored) { |
| 2123 | // Remember all explicit private FieldDecls that have a name, no side |
| 2124 | // effects and are not part of a dependent type declaration. |
| 2125 | if (!FD->isImplicit() && FD->getDeclName() && |
| 2126 | FD->getAccess() == AS_private && |
| Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 2127 | !FD->hasAttr<UnusedAttr>() && |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2128 | !FD->getParent()->isDependentContext() && |
| Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2129 | !InitializationHasSideEffects(*FD)) |
| 2130 | UnusedPrivateFields.insert(FD); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2134 | return Member; |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2137 | namespace { |
| 2138 | class UninitializedFieldVisitor |
| 2139 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 2140 | Sema &S; |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2141 | // List of Decls to generate a warning on. Also remove Decls that become |
| 2142 | // initialized. |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2143 | llvm::SmallPtrSet<ValueDecl*, 4> &Decls; |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2144 | // If non-null, add a note to the warning pointing back to the constructor. |
| 2145 | const CXXConstructorDecl *Constructor; |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2146 | public: |
| 2147 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2148 | UninitializedFieldVisitor(Sema &S, |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2149 | llvm::SmallPtrSet<ValueDecl*, 4> &Decls, |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2150 | const CXXConstructorDecl *Constructor) |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2151 | : Inherited(S.Context), S(S), Decls(Decls), |
| 2152 | Constructor(Constructor) { } |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2153 | |
| Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2154 | void HandleMemberExpr(MemberExpr *ME, bool CheckReferenceOnly) { |
| Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2155 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
| 2156 | return; |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2157 | |
| Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2158 | // FieldME is the inner-most MemberExpr that is not an anonymous struct |
| 2159 | // or union. |
| 2160 | MemberExpr *FieldME = ME; |
| 2161 | |
| 2162 | Expr *Base = ME; |
| 2163 | while (isa<MemberExpr>(Base)) { |
| 2164 | ME = cast<MemberExpr>(Base); |
| 2165 | |
| 2166 | if (isa<VarDecl>(ME->getMemberDecl())) |
| 2167 | return; |
| 2168 | |
| 2169 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2170 | if (!FD->isAnonymousStructOrUnion()) |
| 2171 | FieldME = ME; |
| 2172 | |
| 2173 | Base = ME->getBase(); |
| 2174 | } |
| 2175 | |
| Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2176 | if (!isa<CXXThisExpr>(Base)) |
| 2177 | return; |
| 2178 | |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2179 | ValueDecl* FoundVD = FieldME->getMemberDecl(); |
| 2180 | |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2181 | if (!Decls.count(FoundVD)) |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2182 | return; |
| 2183 | |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2184 | const bool IsReference = FoundVD->getType()->isReferenceType(); |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2185 | |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2186 | // Prevent double warnings on use of unbounded references. |
| 2187 | if (IsReference != CheckReferenceOnly) |
| 2188 | return; |
| 2189 | |
| 2190 | unsigned diag = IsReference |
| 2191 | ? diag::warn_reference_field_is_uninit |
| 2192 | : diag::warn_field_is_uninit; |
| 2193 | S.Diag(FieldME->getExprLoc(), diag) << FoundVD; |
| 2194 | if (Constructor) |
| 2195 | S.Diag(Constructor->getLocation(), |
| 2196 | diag::note_uninit_in_this_constructor) |
| 2197 | << (Constructor->isDefaultConstructor() && Constructor->isImplicit()); |
| 2198 | |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | void HandleValue(Expr *E) { |
| 2202 | E = E->IgnoreParens(); |
| 2203 | |
| 2204 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2205 | HandleMemberExpr(ME, false /*CheckReferenceOnly*/); |
| Nick Lewycky | c363afb | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2206 | return; |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 2210 | HandleValue(CO->getTrueExpr()); |
| 2211 | HandleValue(CO->getFalseExpr()); |
| 2212 | return; |
| 2213 | } |
| 2214 | |
| 2215 | if (BinaryConditionalOperator *BCO = |
| 2216 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 2217 | HandleValue(BCO->getCommon()); |
| 2218 | HandleValue(BCO->getFalseExpr()); |
| 2219 | return; |
| 2220 | } |
| 2221 | |
| 2222 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2223 | switch (BO->getOpcode()) { |
| 2224 | default: |
| 2225 | return; |
| 2226 | case(BO_PtrMemD): |
| 2227 | case(BO_PtrMemI): |
| 2228 | HandleValue(BO->getLHS()); |
| 2229 | return; |
| 2230 | case(BO_Comma): |
| 2231 | HandleValue(BO->getRHS()); |
| 2232 | return; |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | |
| Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2237 | void VisitMemberExpr(MemberExpr *ME) { |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2238 | // All uses of unbounded reference fields will warn. |
| Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2239 | HandleMemberExpr(ME, true /*CheckReferenceOnly*/); |
| Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2240 | |
| 2241 | Inherited::VisitMemberExpr(ME); |
| 2242 | } |
| 2243 | |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2244 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 2245 | if (E->getCastKind() == CK_LValueToRValue) |
| 2246 | HandleValue(E->getSubExpr()); |
| 2247 | |
| 2248 | Inherited::VisitImplicitCastExpr(E); |
| 2249 | } |
| 2250 | |
| Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2251 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2252 | if (E->getConstructor()->isCopyConstructor()) |
| Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2253 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(E->getArg(0))) |
| 2254 | if (ICE->getCastKind() == CK_NoOp) |
| 2255 | if (MemberExpr *ME = dyn_cast<MemberExpr>(ICE->getSubExpr())) |
| Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2256 | HandleMemberExpr(ME, false /*CheckReferenceOnly*/); |
| Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2257 | |
| 2258 | Inherited::VisitCXXConstructExpr(E); |
| 2259 | } |
| 2260 | |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2261 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 2262 | Expr *Callee = E->getCallee(); |
| 2263 | if (isa<MemberExpr>(Callee)) |
| 2264 | HandleValue(Callee); |
| 2265 | |
| 2266 | Inherited::VisitCXXMemberCallExpr(E); |
| 2267 | } |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2268 | |
| 2269 | void VisitBinaryOperator(BinaryOperator *E) { |
| 2270 | // If a field assignment is detected, remove the field from the |
| 2271 | // uninitiailized field set. |
| 2272 | if (E->getOpcode() == BO_Assign) |
| 2273 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E->getLHS())) |
| 2274 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2275 | if (!FD->getType()->isReferenceType()) |
| 2276 | Decls.erase(FD); |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2277 | |
| 2278 | Inherited::VisitBinaryOperator(E); |
| 2279 | } |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2280 | }; |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2281 | static void CheckInitExprContainsUninitializedFields( |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2282 | Sema &S, Expr *E, llvm::SmallPtrSet<ValueDecl*, 4> &Decls, |
| 2283 | const CXXConstructorDecl *Constructor) { |
| 2284 | if (Decls.size() == 0) |
| Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2285 | return; |
| 2286 | |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2287 | if (!E) |
| 2288 | return; |
| 2289 | |
| 2290 | if (CXXDefaultInitExpr *Default = dyn_cast<CXXDefaultInitExpr>(E)) { |
| 2291 | E = Default->getExpr(); |
| 2292 | if (!E) |
| 2293 | return; |
| 2294 | // In class initializers will point to the constructor. |
| 2295 | UninitializedFieldVisitor(S, Decls, Constructor).Visit(E); |
| 2296 | } else { |
| 2297 | UninitializedFieldVisitor(S, Decls, 0).Visit(E); |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2302 | // foo(foo) |
| 2303 | // where foo is not also a parameter to the constructor. |
| 2304 | // Also diagnose across field uninitialized use such as |
| 2305 | // x(y), y(x) |
| 2306 | // TODO: implement -Wuninitialized and fold this into that framework. |
| 2307 | static void DiagnoseUninitializedFields( |
| 2308 | Sema &SemaRef, const CXXConstructorDecl *Constructor) { |
| 2309 | |
| 2310 | if (SemaRef.getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, |
| 2311 | Constructor->getLocation()) |
| 2312 | == DiagnosticsEngine::Ignored) { |
| 2313 | return; |
| 2314 | } |
| 2315 | |
| 2316 | if (Constructor->isInvalidDecl()) |
| 2317 | return; |
| 2318 | |
| 2319 | const CXXRecordDecl *RD = Constructor->getParent(); |
| 2320 | |
| 2321 | // Holds fields that are uninitialized. |
| 2322 | llvm::SmallPtrSet<ValueDecl*, 4> UninitializedFields; |
| 2323 | |
| 2324 | // At the beginning, all fields are uninitialized. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2325 | for (auto *I : RD->decls()) { |
| 2326 | if (auto *FD = dyn_cast<FieldDecl>(I)) { |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2327 | UninitializedFields.insert(FD); |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2328 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(I)) { |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2329 | UninitializedFields.insert(IFD->getAnonField()); |
| 2330 | } |
| 2331 | } |
| 2332 | |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 2333 | for (const auto *FieldInit : Constructor->inits()) { |
| 2334 | Expr *InitExpr = FieldInit->getInit(); |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2335 | |
| 2336 | CheckInitExprContainsUninitializedFields( |
| 2337 | SemaRef, InitExpr, UninitializedFields, Constructor); |
| 2338 | |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 2339 | if (FieldDecl *Field = FieldInit->getAnyMember()) |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2340 | UninitializedFields.erase(Field); |
| 2341 | } |
| Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2342 | } |
| 2343 | } // namespace |
| 2344 | |
| Richard Smith | 7410817 | 2014-01-17 03:11:34 +0000 | [diff] [blame] | 2345 | /// \brief Enter a new C++ default initializer scope. After calling this, the |
| 2346 | /// caller must call \ref ActOnFinishCXXInClassMemberInitializer, even if |
| 2347 | /// parsing or instantiating the initializer failed. |
| 2348 | void Sema::ActOnStartCXXInClassMemberInitializer() { |
| 2349 | // Create a synthetic function scope to represent the call to the constructor |
| 2350 | // that notionally surrounds a use of this initializer. |
| 2351 | PushFunctionScope(); |
| 2352 | } |
| 2353 | |
| 2354 | /// \brief This is invoked after parsing an in-class initializer for a |
| 2355 | /// non-static C++ class member, and after instantiating an in-class initializer |
| 2356 | /// in a class template. Such actions are deferred until the class is complete. |
| 2357 | void Sema::ActOnFinishCXXInClassMemberInitializer(Decl *D, |
| 2358 | SourceLocation InitLoc, |
| 2359 | Expr *InitExpr) { |
| 2360 | // Pop the notional constructor scope we created earlier. |
| 2361 | PopFunctionScopeInfo(0, D); |
| 2362 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2363 | FieldDecl *FD = cast<FieldDecl>(D); |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2364 | assert(FD->getInClassInitStyle() != ICIS_NoInit && |
| 2365 | "must set init style when field is created"); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2366 | |
| 2367 | if (!InitExpr) { |
| 2368 | FD->setInvalidDecl(); |
| 2369 | FD->removeInClassInitializer(); |
| 2370 | return; |
| 2371 | } |
| 2372 | |
| Peter Collingbourne | 9f58d7b | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2373 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 2374 | FD->setInvalidDecl(); |
| 2375 | FD->removeInClassInitializer(); |
| 2376 | return; |
| 2377 | } |
| 2378 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2379 | ExprResult Init = InitExpr; |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2380 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
| Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2381 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2382 | InitializationKind Kind = FD->getInClassInitStyle() == ICIS_ListInit |
| Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2383 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2384 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), InitLoc); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2385 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
| 2386 | Init = Seq.Perform(*this, Entity, Kind, InitExpr); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2387 | if (Init.isInvalid()) { |
| 2388 | FD->setInvalidDecl(); |
| 2389 | return; |
| 2390 | } |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2393 | // C++11 [class.base.init]p7: |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2394 | // The initialization of each base and member constitutes a |
| 2395 | // full-expression. |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2396 | Init = ActOnFinishFullExpr(Init.take(), InitLoc); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2397 | if (Init.isInvalid()) { |
| 2398 | FD->setInvalidDecl(); |
| 2399 | return; |
| 2400 | } |
| 2401 | |
| 2402 | InitExpr = Init.release(); |
| 2403 | |
| 2404 | FD->setInClassInitializer(InitExpr); |
| 2405 | } |
| 2406 | |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2407 | /// \brief Find the direct and/or virtual base specifiers that |
| 2408 | /// correspond to the given base type, for use in base initialization |
| 2409 | /// within a constructor. |
| 2410 | static bool FindBaseInitializer(Sema &SemaRef, |
| 2411 | CXXRecordDecl *ClassDecl, |
| 2412 | QualType BaseType, |
| 2413 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 2414 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 2415 | // First, check for a direct base class. |
| 2416 | DirectBaseSpec = 0; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2417 | for (const auto &Base : ClassDecl->bases()) { |
| 2418 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base.getType())) { |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2419 | // We found a direct base of this type. That's what we're |
| 2420 | // initializing. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2421 | DirectBaseSpec = &Base; |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2422 | break; |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | // Check for a virtual base class. |
| 2427 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 2428 | // there are no virtual bases. |
| 2429 | VirtualBaseSpec = 0; |
| 2430 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 2431 | // We haven't found a base yet; search the class hierarchy for a |
| 2432 | // virtual base class. |
| 2433 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 2434 | /*DetectVirtual=*/false); |
| 2435 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 2436 | BaseType, Paths)) { |
| 2437 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2438 | Path != Paths.end(); ++Path) { |
| 2439 | if (Path->back().Base->isVirtual()) { |
| 2440 | VirtualBaseSpec = Path->back().Base; |
| 2441 | break; |
| 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | return DirectBaseSpec || VirtualBaseSpec; |
| 2448 | } |
| 2449 | |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2450 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 2451 | MemInitResult |
| 2452 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 2453 | Scope *S, |
| 2454 | CXXScopeSpec &SS, |
| 2455 | IdentifierInfo *MemberOrBase, |
| 2456 | ParsedType TemplateTypeTy, |
| David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2457 | const DeclSpec &DS, |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2458 | SourceLocation IdLoc, |
| 2459 | Expr *InitList, |
| 2460 | SourceLocation EllipsisLoc) { |
| 2461 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2462 | DS, IdLoc, InitList, |
| David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2463 | EllipsisLoc); |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | /// \brief Handle a C++ member initializer using parentheses syntax. |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2467 | MemInitResult |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2468 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2469 | Scope *S, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2470 | CXXScopeSpec &SS, |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2471 | IdentifierInfo *MemberOrBase, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2472 | ParsedType TemplateTypeTy, |
| David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2473 | const DeclSpec &DS, |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2474 | SourceLocation IdLoc, |
| 2475 | SourceLocation LParenLoc, |
| Dmitri Gribenko | 139474d | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2476 | ArrayRef<Expr *> Args, |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2477 | SourceLocation RParenLoc, |
| 2478 | SourceLocation EllipsisLoc) { |
| Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2479 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, |
| Dmitri Gribenko | 139474d | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2480 | Args, RParenLoc); |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2481 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2482 | DS, IdLoc, List, EllipsisLoc); |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2485 | namespace { |
| 2486 | |
| Kaelyn Uhrain | 8811b39 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 2487 | // Callback to only accept typo corrections that can be a valid C++ member |
| 2488 | // intializer: either a non-static field member or a base class. |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2489 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2490 | public: |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2491 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 2492 | : ClassDecl(ClassDecl) {} |
| 2493 | |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 2494 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2495 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 2496 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 2497 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2498 | return isa<TypeDecl>(ND); |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2499 | } |
| 2500 | return false; |
| 2501 | } |
| 2502 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2503 | private: |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2504 | CXXRecordDecl *ClassDecl; |
| 2505 | }; |
| 2506 | |
| 2507 | } |
| 2508 | |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2509 | /// \brief Handle a C++ member initializer. |
| 2510 | MemInitResult |
| 2511 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 2512 | Scope *S, |
| 2513 | CXXScopeSpec &SS, |
| 2514 | IdentifierInfo *MemberOrBase, |
| 2515 | ParsedType TemplateTypeTy, |
| David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2516 | const DeclSpec &DS, |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2517 | SourceLocation IdLoc, |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2518 | Expr *Init, |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2519 | SourceLocation EllipsisLoc) { |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2520 | if (!ConstructorD) |
| 2521 | return true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2522 | |
| Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2523 | AdjustDeclIfTemplate(ConstructorD); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
| 2525 | CXXConstructorDecl *Constructor |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2526 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2527 | if (!Constructor) { |
| 2528 | // The user wrote a constructor initializer on a function that is |
| 2529 | // not a C++ constructor. Ignore the error for now, because we may |
| 2530 | // have more member initializers coming; we'll diagnose it just |
| 2531 | // once in ActOnMemInitializers. |
| 2532 | return true; |
| 2533 | } |
| 2534 | |
| 2535 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2536 | |
| 2537 | // C++ [class.base.init]p2: |
| 2538 | // Names in a mem-initializer-id are looked up in the scope of the |
| Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2539 | // constructor's class and, if not found in that scope, are looked |
| 2540 | // up in the scope containing the constructor's definition. |
| 2541 | // [Note: if the constructor's class contains a member with the |
| 2542 | // same name as a direct or virtual base class of the class, a |
| 2543 | // mem-initializer-id naming the member or base class and composed |
| 2544 | // of a single identifier refers to the class member. A |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2545 | // mem-initializer-id for the hidden base class may be specified |
| 2546 | // using a qualified name. ] |
| Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2547 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
| Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2548 | // Look for a member, first. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | DeclContext::lookup_result Result |
| Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2550 | = ClassDecl->lookup(MemberOrBase); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2551 | if (!Result.empty()) { |
| Peter Collingbourne | 05156e3 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 2552 | ValueDecl *Member; |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2553 | if ((Member = dyn_cast<FieldDecl>(Result.front())) || |
| 2554 | (Member = dyn_cast<IndirectFieldDecl>(Result.front()))) { |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2555 | if (EllipsisLoc.isValid()) |
| 2556 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2557 | << MemberOrBase |
| 2558 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2559 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2560 | return BuildMemberInitializer(Member, Init, IdLoc); |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2561 | } |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2562 | } |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2563 | } |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2564 | // It didn't name a member, so see if it names a class. |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2565 | QualType BaseType; |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2566 | TypeSourceInfo *TInfo = 0; |
| John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2567 | |
| 2568 | if (TemplateTypeTy) { |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2569 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
| David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2570 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 2571 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2572 | } else { |
| 2573 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 2574 | LookupParsedName(R, S, &SS); |
| 2575 | |
| 2576 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 2577 | if (!TyD) { |
| 2578 | if (R.isAmbiguous()) return true; |
| 2579 | |
| John McCall | da6841b | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 2580 | // We don't want access-control diagnostics here. |
| 2581 | R.suppressDiagnostics(); |
| 2582 | |
| Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2583 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 2584 | bool NotUnknownSpecialization = false; |
| 2585 | DeclContext *DC = computeDeclContext(SS, false); |
| 2586 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 2587 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 2588 | |
| 2589 | if (!NotUnknownSpecialization) { |
| 2590 | // When the scope specifier can refer to a member of an unknown |
| 2591 | // specialization, we take it as a type name. |
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 2592 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 2593 | SS.getWithLocInContext(Context), |
| 2594 | *MemberOrBase, IdLoc); |
| Douglas Gregor | 281c486 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 2595 | if (BaseType.isNull()) |
| 2596 | return true; |
| 2597 | |
| Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2598 | R.clear(); |
| Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2599 | R.setLookupName(MemberOrBase); |
| Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2603 | // If no results were found, try to correct typos. |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2604 | TypoCorrection Corr; |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2605 | MemInitializerValidatorCCC Validator(ClassDecl); |
| Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2606 | if (R.empty() && BaseType.isNull() && |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2607 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
| John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2608 | Validator, CTK_ErrorRecovery, ClassDecl))) { |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2609 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2610 | // We have found a non-static data member with a similar |
| 2611 | // name to what was typed; complain and initialize that |
| 2612 | // member. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2613 | diagnoseTypo(Corr, |
| 2614 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2615 | << MemberOrBase << true); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2616 | return BuildMemberInitializer(Member, Init, IdLoc); |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2617 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2618 | const CXXBaseSpecifier *DirectBaseSpec; |
| 2619 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 2620 | if (FindBaseInitializer(*this, ClassDecl, |
| 2621 | Context.getTypeDeclType(Type), |
| 2622 | DirectBaseSpec, VirtualBaseSpec)) { |
| 2623 | // We have found a direct or virtual base class with a |
| 2624 | // similar name to what was typed; complain and initialize |
| 2625 | // that base class. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2626 | diagnoseTypo(Corr, |
| 2627 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2628 | << MemberOrBase << false, |
| 2629 | PDiag() /*Suppress note, we provide our own.*/); |
| Douglas Gregor | 43a0857 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2630 | |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2631 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec ? DirectBaseSpec |
| 2632 | : VirtualBaseSpec; |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2633 | Diag(BaseSpec->getLocStart(), |
| Douglas Gregor | 43a0857 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2634 | diag::note_base_class_specified_here) |
| 2635 | << BaseSpec->getType() |
| 2636 | << BaseSpec->getSourceRange(); |
| 2637 | |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2638 | TyD = Type; |
| 2639 | } |
| 2640 | } |
| 2641 | } |
| 2642 | |
| Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2643 | if (!TyD && BaseType.isNull()) { |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2644 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2645 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
| Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2646 | return true; |
| 2647 | } |
| John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2650 | if (BaseType.isNull()) { |
| 2651 | BaseType = Context.getTypeDeclType(TyD); |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2652 | if (SS.isSet()) |
| Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2653 | // FIXME: preserve source range information |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2654 | BaseType = Context.getElaboratedType(ETK_None, SS.getScopeRep(), |
| 2655 | BaseType); |
| John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2656 | } |
| 2657 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2659 | if (!TInfo) |
| 2660 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2661 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2662 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
| Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
| Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2665 | /// Checks a member initializer expression for cases where reference (or |
| 2666 | /// pointer) members are bound to by-value parameters (or their addresses). |
| Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2667 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 2668 | Expr *Init, |
| 2669 | SourceLocation IdLoc) { |
| 2670 | QualType MemberTy = Member->getType(); |
| 2671 | |
| 2672 | // We only handle pointers and references currently. |
| 2673 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 2674 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 2675 | return; |
| 2676 | |
| 2677 | const bool IsPointer = MemberTy->isPointerType(); |
| 2678 | if (IsPointer) { |
| 2679 | if (const UnaryOperator *Op |
| 2680 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 2681 | // The only case we're worried about with pointers requires taking the |
| 2682 | // address. |
| 2683 | if (Op->getOpcode() != UO_AddrOf) |
| 2684 | return; |
| 2685 | |
| 2686 | Init = Op->getSubExpr(); |
| 2687 | } else { |
| 2688 | // We only handle address-of expression initializers for pointers. |
| 2689 | return; |
| 2690 | } |
| 2691 | } |
| 2692 | |
| Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2693 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
| Chandler Carruth | d551d4e | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2694 | // We only warn when referring to a non-reference parameter declaration. |
| 2695 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 2696 | if (!Parameter || Parameter->getType()->isReferenceType()) |
| Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2697 | return; |
| 2698 | |
| 2699 | S.Diag(Init->getExprLoc(), |
| 2700 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 2701 | : diag::warn_bind_ref_member_to_parameter) |
| 2702 | << Member << Parameter << Init->getSourceRange(); |
| Chandler Carruth | d551d4e | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2703 | } else { |
| 2704 | // Other initializers are fine. |
| 2705 | return; |
| Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2706 | } |
| Chandler Carruth | d551d4e | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2707 | |
| 2708 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 2709 | << (unsigned)IsPointer; |
| Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2712 | MemInitResult |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2713 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2714 | SourceLocation IdLoc) { |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2715 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 2716 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 2717 | assert((DirectMember || IndirectMember) && |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2718 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 2719 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2720 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
| Peter Collingbourne | 9f58d7b | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2721 | return true; |
| 2722 | |
| Douglas Gregor | 266bb5f | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 2723 | if (Member->isInvalidDecl()) |
| 2724 | return true; |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2725 | |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2726 | MultiExprArg Args; |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2727 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2728 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2729 | } else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2730 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2731 | } else { |
| 2732 | // Template instantiation doesn't reconstruct ParenListExprs for us. |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2733 | Args = Init; |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2734 | } |
| Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2735 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2736 | SourceRange InitRange = Init->getSourceRange(); |
| Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2737 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2738 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2739 | // Can't check initialization for a member of dependent type or when |
| 2740 | // any of the arguments are type-dependent expressions. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2741 | DiscardCleanupsInEvaluationContext(); |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2742 | } else { |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2743 | bool InitList = false; |
| 2744 | if (isa<InitListExpr>(Init)) { |
| 2745 | InitList = true; |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2746 | Args = Init; |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2749 | // Initialize the member. |
| 2750 | InitializedEntity MemberEntity = |
| 2751 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 2752 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 2753 | InitializationKind Kind = |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2754 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 2755 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 2756 | InitRange.getEnd()); |
| John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2757 | |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2758 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args); |
| 2759 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, Args, 0); |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2760 | if (MemberInit.isInvalid()) |
| 2761 | return true; |
| 2762 | |
| Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 2763 | CheckForDanglingReferenceOrPointer(*this, Member, MemberInit.get(), IdLoc); |
| 2764 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2765 | // C++11 [class.base.init]p7: |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2766 | // The initialization of each base and member constitutes a |
| 2767 | // full-expression. |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2768 | MemberInit = ActOnFinishFullExpr(MemberInit.get(), InitRange.getBegin()); |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2769 | if (MemberInit.isInvalid()) |
| 2770 | return true; |
| 2771 | |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2772 | Init = MemberInit.get(); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2775 | if (DirectMember) { |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2776 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 2777 | InitRange.getBegin(), Init, |
| 2778 | InitRange.getEnd()); |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2779 | } else { |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2780 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 2781 | InitRange.getBegin(), Init, |
| 2782 | InitRange.getEnd()); |
| Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2783 | } |
| Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2786 | MemInitResult |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2787 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2788 | CXXRecordDecl *ClassDecl) { |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2789 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2790 | if (!LangOpts.CPlusPlus11) |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2791 | return Diag(NameLoc, diag::err_delegating_ctor) |
| Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2792 | << TInfo->getTypeLoc().getLocalSourceRange(); |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2793 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
| Sebastian Redl | 9cb4be2 | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 2794 | |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2795 | bool InitList = true; |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2796 | MultiExprArg Args = Init; |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2797 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2798 | InitList = false; |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2799 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2802 | SourceRange InitRange = Init->getSourceRange(); |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2803 | // Initialize the object. |
| 2804 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 2805 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 2806 | InitializationKind Kind = |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2807 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 2808 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 2809 | InitRange.getEnd()); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2810 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2811 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2812 | Args, 0); |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2813 | if (DelegationInit.isInvalid()) |
| 2814 | return true; |
| 2815 | |
| Matt Beaumont-Gay | 3e59fac | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 2816 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 2817 | "Delegating constructor with no target?"); |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2818 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2819 | // C++11 [class.base.init]p7: |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2820 | // The initialization of each base and member constitutes a |
| 2821 | // full-expression. |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2822 | DelegationInit = ActOnFinishFullExpr(DelegationInit.get(), |
| 2823 | InitRange.getBegin()); |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2824 | if (DelegationInit.isInvalid()) |
| 2825 | return true; |
| 2826 | |
| Eli Friedman | a9e9ebc | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 2827 | // If we are in a dependent context, template instantiation will |
| 2828 | // perform this type-checking again. Just save the arguments that we |
| 2829 | // received in a ParenListExpr. |
| 2830 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2831 | // of the information that we have about the base |
| 2832 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2833 | // and this approach is far more likely to get the corner cases right. |
| 2834 | if (CurContext->isDependentContext()) |
| 2835 | DelegationInit = Owned(Init); |
| 2836 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2837 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2838 | DelegationInit.takeAs<Expr>(), |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2839 | InitRange.getEnd()); |
| Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | MemInitResult |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2843 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2844 | Expr *Init, CXXRecordDecl *ClassDecl, |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2845 | SourceLocation EllipsisLoc) { |
| Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2846 | SourceLocation BaseLoc |
| 2847 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2848 | |
| Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2849 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 2850 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 2851 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2852 | |
| 2853 | // C++ [class.base.init]p2: |
| 2854 | // [...] Unless the mem-initializer-id names a nonstatic data |
| Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2855 | // member of the constructor's class or a direct or virtual base |
| Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2856 | // of that class, the mem-initializer is ill-formed. A |
| 2857 | // mem-initializer-list can initialize a base class using any |
| 2858 | // name that denotes that base class type. |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2859 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
| Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2860 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2861 | SourceRange InitRange = Init->getSourceRange(); |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2862 | if (EllipsisLoc.isValid()) { |
| 2863 | // This is a pack expansion. |
| 2864 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 2865 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2866 | << SourceRange(BaseLoc, InitRange.getEnd()); |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2867 | |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2868 | EllipsisLoc = SourceLocation(); |
| 2869 | } |
| 2870 | } else { |
| 2871 | // Check for any unexpanded parameter packs. |
| 2872 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 2873 | return true; |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2874 | |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2875 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2876 | return true; |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2877 | } |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2878 | |
| Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2879 | // Check for direct and virtual base classes. |
| 2880 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 2881 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 2882 | if (!Dependent) { |
| Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2883 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 2884 | BaseType)) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2885 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
| Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2886 | |
| Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2887 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 2888 | VirtualBaseSpec); |
| 2889 | |
| 2890 | // C++ [base.class.init]p2: |
| 2891 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 2892 | // constructor's class or a direct or virtual base of that class, the |
| 2893 | // mem-initializer is ill-formed. |
| 2894 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 2895 | // If the class has any dependent bases, then it's possible that |
| 2896 | // one of those types will resolve to the same type as |
| 2897 | // BaseType. Therefore, just treat this as a dependent base |
| 2898 | // class initialization. FIXME: Should we try to check the |
| 2899 | // initialization anyway? It seems odd. |
| 2900 | if (ClassDecl->hasAnyDependentBases()) |
| 2901 | Dependent = true; |
| 2902 | else |
| 2903 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 2904 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 2905 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | if (Dependent) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2910 | DiscardCleanupsInEvaluationContext(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2912 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 2913 | /*IsVirtual=*/false, |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2914 | InitRange.getBegin(), Init, |
| 2915 | InitRange.getEnd(), EllipsisLoc); |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2916 | } |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2917 | |
| 2918 | // C++ [base.class.init]p2: |
| 2919 | // If a mem-initializer-id is ambiguous because it designates both |
| 2920 | // a direct non-virtual base class and an inherited virtual base |
| 2921 | // class, the mem-initializer is ill-formed. |
| 2922 | if (DirectBaseSpec && VirtualBaseSpec) |
| 2923 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
| Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2924 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2925 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2926 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec; |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2927 | if (!BaseSpec) |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2928 | BaseSpec = VirtualBaseSpec; |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Initialize the base. |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2931 | bool InitList = true; |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2932 | MultiExprArg Args = Init; |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2933 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2934 | InitList = false; |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2935 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2936 | } |
| Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2937 | |
| 2938 | InitializedEntity BaseEntity = |
| 2939 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 2940 | InitializationKind Kind = |
| 2941 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 2942 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 2943 | InitRange.getEnd()); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2944 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args); |
| 2945 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, Args, 0); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2946 | if (BaseInit.isInvalid()) |
| 2947 | return true; |
| John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2948 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2949 | // C++11 [class.base.init]p7: |
| 2950 | // The initialization of each base and member constitutes a |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2951 | // full-expression. |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2952 | BaseInit = ActOnFinishFullExpr(BaseInit.get(), InitRange.getBegin()); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2953 | if (BaseInit.isInvalid()) |
| 2954 | return true; |
| 2955 | |
| 2956 | // If we are in a dependent context, template instantiation will |
| 2957 | // perform this type-checking again. Just save the arguments that we |
| 2958 | // received in a ParenListExpr. |
| 2959 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2960 | // of the information that we have about the base |
| 2961 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2962 | // and this approach is far more likely to get the corner cases right. |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2963 | if (CurContext->isDependentContext()) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2964 | BaseInit = Owned(Init); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2965 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2966 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2967 | BaseSpec->isVirtual(), |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2968 | InitRange.getBegin(), |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2969 | BaseInit.takeAs<Expr>(), |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2970 | InitRange.getEnd(), EllipsisLoc); |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2973 | // Create a static_cast\<T&&>(expr). |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2974 | static Expr *CastForMoving(Sema &SemaRef, Expr *E, QualType T = QualType()) { |
| 2975 | if (T.isNull()) T = E->getType(); |
| 2976 | QualType TargetType = SemaRef.BuildReferenceType( |
| 2977 | T, /*SpelledAsLValue*/false, SourceLocation(), DeclarationName()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2978 | SourceLocation ExprLoc = E->getLocStart(); |
| 2979 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 2980 | TargetType, ExprLoc); |
| 2981 | |
| 2982 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 2983 | SourceRange(ExprLoc, ExprLoc), |
| 2984 | E->getSourceRange()).take(); |
| 2985 | } |
| 2986 | |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2987 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 2988 | /// initialize its base or member. |
| 2989 | enum ImplicitInitializerKind { |
| 2990 | IIK_Default, |
| 2991 | IIK_Copy, |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2992 | IIK_Move, |
| 2993 | IIK_Inherit |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2994 | }; |
| 2995 | |
| Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2996 | static bool |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2997 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2998 | ImplicitInitializerKind ImplicitInitKind, |
| Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2999 | CXXBaseSpecifier *BaseSpec, |
| Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3000 | bool IsInheritedVirtualBase, |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3001 | CXXCtorInitializer *&CXXBaseInit) { |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3002 | InitializedEntity InitEntity |
| Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3003 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 3004 | IsInheritedVirtualBase); |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3005 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3006 | ExprResult BaseInit; |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3007 | |
| 3008 | switch (ImplicitInitKind) { |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3009 | case IIK_Inherit: { |
| 3010 | const CXXRecordDecl *Inherited = |
| 3011 | Constructor->getInheritedConstructor()->getParent(); |
| 3012 | const CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 3013 | if (Base && Inherited->getCanonicalDecl() == Base->getCanonicalDecl()) { |
| 3014 | // C++11 [class.inhctor]p8: |
| 3015 | // Each expression in the expression-list is of the form |
| 3016 | // static_cast<T&&>(p), where p is the name of the corresponding |
| 3017 | // constructor parameter and T is the declared type of p. |
| 3018 | SmallVector<Expr*, 16> Args; |
| 3019 | for (unsigned I = 0, E = Constructor->getNumParams(); I != E; ++I) { |
| 3020 | ParmVarDecl *PD = Constructor->getParamDecl(I); |
| 3021 | ExprResult ArgExpr = |
| 3022 | SemaRef.BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), |
| 3023 | VK_LValue, SourceLocation()); |
| 3024 | if (ArgExpr.isInvalid()) |
| 3025 | return true; |
| 3026 | Args.push_back(CastForMoving(SemaRef, ArgExpr.take(), PD->getType())); |
| 3027 | } |
| 3028 | |
| 3029 | InitializationKind InitKind = InitializationKind::CreateDirect( |
| 3030 | Constructor->getLocation(), SourceLocation(), SourceLocation()); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3031 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, Args); |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3032 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, Args); |
| 3033 | break; |
| 3034 | } |
| 3035 | } |
| 3036 | // Fall through. |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3037 | case IIK_Default: { |
| 3038 | InitializationKind InitKind |
| 3039 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3040 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3041 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3042 | break; |
| 3043 | } |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3044 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3045 | case IIK_Move: |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3046 | case IIK_Copy: { |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3047 | bool Moving = ImplicitInitKind == IIK_Move; |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3048 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 3049 | QualType ParamType = Param->getType().getNonReferenceType(); |
| Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3050 | |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3051 | Expr *CopyCtorArg = |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3052 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3053 | SourceLocation(), Param, false, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3054 | Constructor->getLocation(), ParamType, |
| 3055 | VK_LValue, 0); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3056 | |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3057 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 3058 | |
| Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 3059 | // Cast to the base class to avoid ambiguities. |
| Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 3060 | QualType ArgTy = |
| 3061 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 3062 | ParamType.getQualifiers()); |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3063 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3064 | if (Moving) { |
| 3065 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 3066 | } |
| 3067 | |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3068 | CXXCastPath BasePath; |
| 3069 | BasePath.push_back(BaseSpec); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3070 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 3071 | CK_UncheckedDerivedToBase, |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3072 | Moving ? VK_XValue : VK_LValue, |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3073 | &BasePath).take(); |
| Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 3074 | |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3075 | InitializationKind InitKind |
| 3076 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 3077 | SourceLocation(), SourceLocation()); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3078 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| 3079 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3080 | break; |
| 3081 | } |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3082 | } |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3083 | |
| Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3084 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3085 | if (BaseInit.isInvalid()) |
| Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3086 | return true; |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3087 | |
| Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3088 | CXXBaseInit = |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3089 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3090 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 3091 | SourceLocation()), |
| 3092 | BaseSpec->isVirtual(), |
| 3093 | SourceLocation(), |
| 3094 | BaseInit.takeAs<Expr>(), |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3095 | SourceLocation(), |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3096 | SourceLocation()); |
| 3097 | |
| Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3098 | return false; |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3101 | static bool RefersToRValueRef(Expr *MemRef) { |
| 3102 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 3103 | return Referenced->getType()->isRValueReferenceType(); |
| 3104 | } |
| 3105 | |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3106 | static bool |
| 3107 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3108 | ImplicitInitializerKind ImplicitInitKind, |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3109 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3110 | CXXCtorInitializer *&CXXMemberInit) { |
| Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3111 | if (Field->isInvalidDecl()) |
| 3112 | return true; |
| 3113 | |
| Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3114 | SourceLocation Loc = Constructor->getLocation(); |
| 3115 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3116 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 3117 | bool Moving = ImplicitInitKind == IIK_Move; |
| Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3118 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 3119 | QualType ParamType = Param->getType().getNonReferenceType(); |
| John McCall | 1b1a1db | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 3120 | |
| 3121 | // Suppress copying zero-width bitfields. |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3122 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 3123 | return false; |
| Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3124 | |
| Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3125 | Expr *MemberExprBase = |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3126 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
| John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3127 | SourceLocation(), Param, false, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3128 | Loc, ParamType, VK_LValue, 0); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3129 | |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3130 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 3131 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3132 | if (Moving) { |
| 3133 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 3134 | } |
| 3135 | |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3136 | // Build a reference to this field within the parameter. |
| 3137 | CXXScopeSpec SS; |
| 3138 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 3139 | Sema::LookupMemberName); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3140 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 3141 | : cast<ValueDecl>(Field), AS_public); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3142 | MemberLookup.resolveKind(); |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3143 | ExprResult CtorArg |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3144 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3145 | ParamType, Loc, |
| 3146 | /*IsArrow=*/false, |
| 3147 | SS, |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3148 | /*TemplateKWLoc=*/SourceLocation(), |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3149 | /*FirstQualifierInScope=*/0, |
| 3150 | MemberLookup, |
| 3151 | /*TemplateArgs=*/0); |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3152 | if (CtorArg.isInvalid()) |
| Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3153 | return true; |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3154 | |
| 3155 | // C++11 [class.copy]p15: |
| 3156 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 3157 | // with static_cast<T&&>(x.m); |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3158 | if (RefersToRValueRef(CtorArg.get())) { |
| 3159 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3162 | // When the field we are copying is an array, create index variables for |
| 3163 | // each dimension of the array. We use these index variables to subscript |
| 3164 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 3165 | // necessary iteration with these index variables. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3166 | SmallVector<VarDecl *, 4> IndexVariables; |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3167 | QualType BaseType = Field->getType(); |
| 3168 | QualType SizeType = SemaRef.Context.getSizeType(); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3169 | bool InitializingArray = false; |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3170 | while (const ConstantArrayType *Array |
| 3171 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3172 | InitializingArray = true; |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3173 | // Create the iteration variable for this array index. |
| 3174 | IdentifierInfo *IterationVarName = 0; |
| 3175 | { |
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3176 | SmallString<8> Str; |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3177 | llvm::raw_svector_ostream OS(Str); |
| 3178 | OS << "__i" << IndexVariables.size(); |
| 3179 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 3180 | } |
| 3181 | VarDecl *IterationVar |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3182 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3183 | IterationVarName, SizeType, |
| 3184 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3185 | SC_None); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3186 | IndexVariables.push_back(IterationVar); |
| 3187 | |
| 3188 | // Create a reference to the iteration variable. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3189 | ExprResult IterationVarRef |
| Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3190 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3191 | assert(!IterationVarRef.isInvalid() && |
| 3192 | "Reference to invented variable cannot fail!"); |
| Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3193 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.take()); |
| 3194 | assert(!IterationVarRef.isInvalid() && |
| 3195 | "Conversion of invented variable cannot fail!"); |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3196 | |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3197 | // Subscript the array with this iteration variable. |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3198 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3199 | IterationVarRef.take(), |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3200 | Loc); |
| 3201 | if (CtorArg.isInvalid()) |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3202 | return true; |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3203 | |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3204 | BaseType = Array->getElementType(); |
| 3205 | } |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3206 | |
| 3207 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 3208 | if (Moving && InitializingArray) |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3209 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3210 | |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3211 | // Construct the entity that we will be initializing. For an array, this |
| 3212 | // will be first element in the array, which may require several levels |
| 3213 | // of array-subscript entities. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3214 | SmallVector<InitializedEntity, 4> Entities; |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3215 | Entities.reserve(1 + IndexVariables.size()); |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3216 | if (Indirect) |
| 3217 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 3218 | else |
| 3219 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3220 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 3221 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 3222 | 0, |
| 3223 | Entities.back())); |
| 3224 | |
| 3225 | // Direct-initialize to use the copy constructor. |
| 3226 | InitializationKind InitKind = |
| 3227 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 3228 | |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3229 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3230 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, CtorArgE); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3231 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3232 | ExprResult MemberInit |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3233 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
| Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3234 | MultiExprArg(&CtorArgE, 1)); |
| Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3235 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3236 | if (MemberInit.isInvalid()) |
| 3237 | return true; |
| 3238 | |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3239 | if (Indirect) { |
| 3240 | assert(IndexVariables.size() == 0 && |
| 3241 | "Indirect field improperly initialized"); |
| 3242 | CXXMemberInit |
| 3243 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3244 | Loc, Loc, |
| 3245 | MemberInit.takeAs<Expr>(), |
| 3246 | Loc); |
| 3247 | } else |
| 3248 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 3249 | Loc, MemberInit.takeAs<Expr>(), |
| 3250 | Loc, |
| 3251 | IndexVariables.data(), |
| 3252 | IndexVariables.size()); |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3253 | return false; |
| 3254 | } |
| 3255 | |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3256 | assert((ImplicitInitKind == IIK_Default || ImplicitInitKind == IIK_Inherit) && |
| 3257 | "Unhandled implicit init kind!"); |
| Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3258 | |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3259 | QualType FieldBaseElementType = |
| 3260 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 3261 | |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3262 | if (FieldBaseElementType->isRecordType()) { |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3263 | InitializedEntity InitEntity |
| 3264 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 3265 | : InitializedEntity::InitializeMember(Field); |
| Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3266 | InitializationKind InitKind = |
| Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3267 | InitializationKind::CreateDefault(Loc); |
| Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3268 | |
| 3269 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3270 | ExprResult MemberInit = |
| 3271 | InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3272 | |
| Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3273 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3274 | if (MemberInit.isInvalid()) |
| 3275 | return true; |
| 3276 | |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3277 | if (Indirect) |
| 3278 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3279 | Indirect, Loc, |
| 3280 | Loc, |
| 3281 | MemberInit.get(), |
| 3282 | Loc); |
| 3283 | else |
| 3284 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3285 | Field, Loc, Loc, |
| 3286 | MemberInit.get(), |
| 3287 | Loc); |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3288 | return false; |
| 3289 | } |
| Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3290 | |
| Alexis Hunt | 8b45518 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3291 | if (!Field->getParent()->isUnion()) { |
| 3292 | if (FieldBaseElementType->isReferenceType()) { |
| 3293 | SemaRef.Diag(Constructor->getLocation(), |
| 3294 | diag::err_uninitialized_member_in_ctor) |
| 3295 | << (int)Constructor->isImplicit() |
| 3296 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3297 | << 0 << Field->getDeclName(); |
| 3298 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3299 | return true; |
| 3300 | } |
| Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3301 | |
| Alexis Hunt | 8b45518 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3302 | if (FieldBaseElementType.isConstQualified()) { |
| 3303 | SemaRef.Diag(Constructor->getLocation(), |
| 3304 | diag::err_uninitialized_member_in_ctor) |
| 3305 | << (int)Constructor->isImplicit() |
| 3306 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3307 | << 1 << Field->getDeclName(); |
| 3308 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3309 | return true; |
| 3310 | } |
| Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3311 | } |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3312 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3313 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3314 | FieldBaseElementType->isObjCRetainableType() && |
| 3315 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 3316 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
| Douglas Gregor | 6fa6942 | 2012-07-23 04:23:39 +0000 | [diff] [blame] | 3317 | // ARC: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3318 | // Default-initialize Objective-C pointers to NULL. |
| 3319 | CXXMemberInit |
| 3320 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3321 | Loc, Loc, |
| 3322 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 3323 | Loc); |
| 3324 | return false; |
| 3325 | } |
| 3326 | |
| Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3327 | // Nothing to initialize. |
| 3328 | CXXMemberInit = 0; |
| 3329 | return false; |
| 3330 | } |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3331 | |
| 3332 | namespace { |
| 3333 | struct BaseAndFieldInfo { |
| 3334 | Sema &S; |
| 3335 | CXXConstructorDecl *Ctor; |
| 3336 | bool AnyErrorsInInits; |
| 3337 | ImplicitInitializerKind IIK; |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3338 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3339 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3340 | llvm::DenseMap<TagDecl*, FieldDecl*> ActiveUnionMember; |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3341 | |
| 3342 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 3343 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3344 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 3345 | if (Generated && Ctor->isCopyConstructor()) |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3346 | IIK = IIK_Copy; |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3347 | else if (Generated && Ctor->isMoveConstructor()) |
| 3348 | IIK = IIK_Move; |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3349 | else if (Ctor->getInheritedConstructor()) |
| 3350 | IIK = IIK_Inherit; |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3351 | else |
| 3352 | IIK = IIK_Default; |
| 3353 | } |
| Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3354 | |
| 3355 | bool isImplicitCopyOrMove() const { |
| 3356 | switch (IIK) { |
| 3357 | case IIK_Copy: |
| 3358 | case IIK_Move: |
| 3359 | return true; |
| 3360 | |
| 3361 | case IIK_Default: |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3362 | case IIK_Inherit: |
| Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3363 | return false; |
| 3364 | } |
| David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3365 | |
| 3366 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
| Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3367 | } |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3368 | |
| 3369 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 3370 | AllToInit.push_back(Init); |
| 3371 | |
| 3372 | // Check whether this initializer makes the field "used". |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3373 | if (Init->getInit()->HasSideEffects(S.Context)) |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3374 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 3375 | |
| 3376 | return false; |
| 3377 | } |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3378 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3379 | bool isInactiveUnionMember(FieldDecl *Field) { |
| 3380 | RecordDecl *Record = Field->getParent(); |
| 3381 | if (!Record->isUnion()) |
| 3382 | return false; |
| 3383 | |
| Richard Smith | 8d18385 | 2013-12-10 20:56:03 +0000 | [diff] [blame] | 3384 | if (FieldDecl *Active = |
| 3385 | ActiveUnionMember.lookup(Record->getCanonicalDecl())) |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3386 | return Active != Field->getCanonicalDecl(); |
| 3387 | |
| 3388 | // In an implicit copy or move constructor, ignore any in-class initializer. |
| 3389 | if (isImplicitCopyOrMove()) |
| 3390 | return true; |
| 3391 | |
| 3392 | // If there's no explicit initialization, the field is active only if it |
| 3393 | // has an in-class initializer... |
| 3394 | if (Field->hasInClassInitializer()) |
| 3395 | return false; |
| 3396 | // ... or it's an anonymous struct or union whose class has an in-class |
| 3397 | // initializer. |
| 3398 | if (!Field->isAnonymousStructOrUnion()) |
| 3399 | return true; |
| 3400 | CXXRecordDecl *FieldRD = Field->getType()->getAsCXXRecordDecl(); |
| 3401 | return !FieldRD->hasInClassInitializer(); |
| 3402 | } |
| 3403 | |
| 3404 | /// \brief Determine whether the given field is, or is within, a union member |
| 3405 | /// that is inactive (because there was an initializer given for a different |
| 3406 | /// member of the union, or because the union was not initialized at all). |
| 3407 | bool isWithinInactiveUnionMember(FieldDecl *Field, |
| 3408 | IndirectFieldDecl *Indirect) { |
| 3409 | if (!Indirect) |
| 3410 | return isInactiveUnionMember(Field); |
| 3411 | |
| Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 3412 | for (auto *C : Indirect->chain()) { |
| Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3413 | FieldDecl *Field = dyn_cast<FieldDecl>(C); |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3414 | if (Field && isInactiveUnionMember(Field)) |
| Richard Smith | c94ec84 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3415 | return true; |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3416 | } |
| 3417 | return false; |
| 3418 | } |
| 3419 | }; |
| Richard Smith | c94ec84 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
| Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3422 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 3423 | /// array type. |
| 3424 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 3425 | if (T->isIncompleteArrayType()) |
| 3426 | return true; |
| 3427 | |
| 3428 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 3429 | if (!ArrayT->getSize()) |
| 3430 | return true; |
| 3431 | |
| 3432 | T = ArrayT->getElementType(); |
| 3433 | } |
| 3434 | |
| 3435 | return false; |
| 3436 | } |
| 3437 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3438 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3439 | FieldDecl *Field, |
| 3440 | IndirectFieldDecl *Indirect = 0) { |
| Eli Friedman | b13e64e | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3441 | if (Field->isInvalidDecl()) |
| 3442 | return false; |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3443 | |
| Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 3444 | // Overwhelmingly common case: we have a direct initializer for this field. |
| Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3445 | if (CXXCtorInitializer *Init = |
| 3446 | Info.AllBaseFields.lookup(Field->getCanonicalDecl())) |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3447 | return Info.addFieldInitializer(Init); |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3448 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3449 | // C++11 [class.base.init]p8: |
| 3450 | // if the entity is a non-static data member that has a |
| 3451 | // brace-or-equal-initializer and either |
| 3452 | // -- the constructor's class is a union and no other variant member of that |
| 3453 | // union is designated by a mem-initializer-id or |
| 3454 | // -- the constructor's class is not a union, and, if the entity is a member |
| 3455 | // of an anonymous union, no other member of that union is designated by |
| 3456 | // a mem-initializer-id, |
| 3457 | // the entity is initialized as specified in [dcl.init]. |
| 3458 | // |
| 3459 | // We also apply the same rules to handle anonymous structs within anonymous |
| 3460 | // unions. |
| 3461 | if (Info.isWithinInactiveUnionMember(Field, Indirect)) |
| 3462 | return false; |
| 3463 | |
| Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3464 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3465 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 3466 | Info.Ctor->getLocation(), Field); |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3467 | CXXCtorInitializer *Init; |
| 3468 | if (Indirect) |
| 3469 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3470 | SourceLocation(), |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3471 | SourceLocation(), DIE, |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3472 | SourceLocation()); |
| 3473 | else |
| 3474 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3475 | SourceLocation(), |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3476 | SourceLocation(), DIE, |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3477 | SourceLocation()); |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3478 | return Info.addFieldInitializer(Init); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3481 | // Don't initialize incomplete or zero-length arrays. |
| 3482 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 3483 | return false; |
| 3484 | |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3485 | // Don't try to build an implicit initializer if there were semantic |
| 3486 | // errors in any of the initializers (and therefore we might be |
| 3487 | // missing some that the user actually wrote). |
| Eli Friedman | b13e64e | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3488 | if (Info.AnyErrorsInInits) |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3489 | return false; |
| 3490 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3491 | CXXCtorInitializer *Init = 0; |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3492 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 3493 | Indirect, Init)) |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3494 | return true; |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3495 | |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3496 | if (!Init) |
| 3497 | return false; |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3498 | |
| Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3499 | return Info.addFieldInitializer(Init); |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3500 | } |
| Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3501 | |
| 3502 | bool |
| 3503 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 3504 | CXXCtorInitializer *Initializer) { |
| Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3505 | assert(Initializer->isDelegatingInitializer()); |
| Alexis Hunt | 5583d56 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 3506 | Constructor->setNumCtorInitializers(1); |
| 3507 | CXXCtorInitializer **initializer = |
| 3508 | new (Context) CXXCtorInitializer*[1]; |
| 3509 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 3510 | Constructor->setCtorInitializers(initializer); |
| 3511 | |
| Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3512 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3513 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
| Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3514 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 3515 | } |
| 3516 | |
| Alexis Hunt | e262299 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 3517 | DelegatingCtorDecls.push_back(Constructor); |
| Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3518 | |
| Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3519 | return false; |
| 3520 | } |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3521 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3522 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors, |
| 3523 | ArrayRef<CXXCtorInitializer *> Initializers) { |
| Douglas Gregor | 5223529 | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 3524 | if (Constructor->isDependentContext()) { |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3525 | // Just store the initializers as written, they will be checked during |
| 3526 | // instantiation. |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3527 | if (!Initializers.empty()) { |
| 3528 | Constructor->setNumCtorInitializers(Initializers.size()); |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3529 | CXXCtorInitializer **baseOrMemberInitializers = |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3530 | new (Context) CXXCtorInitializer*[Initializers.size()]; |
| 3531 | memcpy(baseOrMemberInitializers, Initializers.data(), |
| 3532 | Initializers.size() * sizeof(CXXCtorInitializer*)); |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3533 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3534 | } |
| Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3535 | |
| 3536 | // Let template instantiation know whether we had errors. |
| 3537 | if (AnyErrors) |
| 3538 | Constructor->setInvalidDecl(); |
| 3539 | |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3540 | return false; |
| 3541 | } |
| 3542 | |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3543 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3544 | |
| Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3545 | // We need to build the initializer AST according to order of construction |
| 3546 | // and not what user specified in the Initializers list. |
| Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3547 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
| Douglas Gregor | c14922f | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 3548 | if (!ClassDecl) |
| 3549 | return true; |
| 3550 | |
| Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3551 | bool HadError = false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3553 | for (unsigned i = 0; i < Initializers.size(); i++) { |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3554 | CXXCtorInitializer *Member = Initializers[i]; |
| Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3555 | |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3556 | if (Member->isBaseInitializer()) |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3557 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3558 | else { |
| Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3559 | Info.AllBaseFields[Member->getAnyMember()->getCanonicalDecl()] = Member; |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3560 | |
| 3561 | if (IndirectFieldDecl *F = Member->getIndirectMember()) { |
| Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 3562 | for (auto *C : F->chain()) { |
| Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3563 | FieldDecl *FD = dyn_cast<FieldDecl>(C); |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3564 | if (FD && FD->getParent()->isUnion()) |
| 3565 | Info.ActiveUnionMember.insert(std::make_pair( |
| 3566 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 3567 | } |
| 3568 | } else if (FieldDecl *FD = Member->getMember()) { |
| 3569 | if (FD->getParent()->isUnion()) |
| 3570 | Info.ActiveUnionMember.insert(std::make_pair( |
| 3571 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 3572 | } |
| 3573 | } |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3576 | // Keep track of the direct virtual bases. |
| 3577 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3578 | for (auto &I : ClassDecl->bases()) { |
| 3579 | if (I.isVirtual()) |
| 3580 | DirectVBases.insert(&I); |
| Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3583 | // Push virtual bases before others. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 3584 | for (auto &VBase : ClassDecl->vbases()) { |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3585 | if (CXXCtorInitializer *Value |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 3586 | = Info.AllBaseFields.lookup(VBase.getType()->getAs<RecordType>())) { |
| Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3587 | // [class.base.init]p7, per DR257: |
| 3588 | // A mem-initializer where the mem-initializer-id names a virtual base |
| 3589 | // class is ignored during execution of a constructor of any class that |
| 3590 | // is not the most derived class. |
| 3591 | if (ClassDecl->isAbstract()) { |
| 3592 | // FIXME: Provide a fixit to remove the base specifier. This requires |
| 3593 | // tracking the location of the associated comma for a base specifier. |
| 3594 | Diag(Value->getSourceLocation(), diag::warn_abstract_vbase_init_ignored) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 3595 | << VBase.getType() << ClassDecl; |
| Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3596 | DiagnoseAbstractType(ClassDecl); |
| 3597 | } |
| 3598 | |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3599 | Info.AllToInit.push_back(Value); |
| Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3600 | } else if (!AnyErrors && !ClassDecl->isAbstract()) { |
| 3601 | // [class.base.init]p8, per DR257: |
| 3602 | // If a given [...] base class is not named by a mem-initializer-id |
| 3603 | // [...] and the entity is not a virtual base class of an abstract |
| 3604 | // class, then [...] the entity is default-initialized. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 3605 | bool IsInheritedVirtualBase = !DirectVBases.count(&VBase); |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3606 | CXXCtorInitializer *CXXBaseInit; |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3607 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 3608 | &VBase, IsInheritedVirtualBase, |
| Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3609 | CXXBaseInit)) { |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3610 | HadError = true; |
| 3611 | continue; |
| 3612 | } |
| Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3613 | |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3614 | Info.AllToInit.push_back(CXXBaseInit); |
| Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3615 | } |
| 3616 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3618 | // Non-virtual bases. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3619 | for (auto &Base : ClassDecl->bases()) { |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3620 | // Virtuals are in the virtual base list and already constructed. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3621 | if (Base.isVirtual()) |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3622 | continue; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3624 | if (CXXCtorInitializer *Value |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3625 | = Info.AllBaseFields.lookup(Base.getType()->getAs<RecordType>())) { |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3626 | Info.AllToInit.push_back(Value); |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3627 | } else if (!AnyErrors) { |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3628 | CXXCtorInitializer *CXXBaseInit; |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3629 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3630 | &Base, /*IsInheritedVirtualBase=*/false, |
| Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3631 | CXXBaseInit)) { |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3632 | HadError = true; |
| Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3633 | continue; |
| Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3634 | } |
| Fariborz Jahanian | 59a1cd4 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 3635 | |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3636 | Info.AllToInit.push_back(CXXBaseInit); |
| Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3640 | // Fields. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3641 | for (auto *Mem : ClassDecl->decls()) { |
| 3642 | if (auto *F = dyn_cast<FieldDecl>(Mem)) { |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3643 | // C++ [class.bit]p2: |
| 3644 | // A declaration for a bit-field that omits the identifier declares an |
| 3645 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 3646 | // initialized. |
| 3647 | if (F->isUnnamedBitfield()) |
| 3648 | continue; |
| Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3649 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3650 | // If we're not generating the implicit copy/move constructor, then we'll |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3651 | // handle anonymous struct/union fields based on their individual |
| 3652 | // indirect fields. |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3653 | if (F->isAnonymousStructOrUnion() && !Info.isImplicitCopyOrMove()) |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3654 | continue; |
| 3655 | |
| 3656 | if (CollectFieldInitializer(*this, Info, F)) |
| 3657 | HadError = true; |
| Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3658 | continue; |
| 3659 | } |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3660 | |
| 3661 | // Beyond this point, we only consider default initialization. |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3662 | if (Info.isImplicitCopyOrMove()) |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3663 | continue; |
| 3664 | |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 3665 | if (auto *F = dyn_cast<IndirectFieldDecl>(Mem)) { |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3666 | if (F->getType()->isIncompleteArrayType()) { |
| 3667 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 3668 | "Incomplete array type is not valid"); |
| 3669 | continue; |
| 3670 | } |
| 3671 | |
| Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3672 | // Initialize each field of an anonymous struct individually. |
| 3673 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 3674 | HadError = true; |
| 3675 | |
| 3676 | continue; |
| 3677 | } |
| Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3678 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3679 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3680 | unsigned NumInitializers = Info.AllToInit.size(); |
| Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3681 | if (NumInitializers > 0) { |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3682 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3683 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3684 | new (Context) CXXCtorInitializer*[NumInitializers]; |
| John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3685 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3686 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3687 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
| Rafael Espindola | 13327bb | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 3688 | |
| John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3689 | // Constructors implicitly reference the base and member |
| 3690 | // destructors. |
| 3691 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 3692 | Constructor->getParent()); |
| Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3693 | } |
| Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3694 | |
| 3695 | return HadError; |
| Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3698 | static void PopulateKeysForFields(FieldDecl *Field, SmallVectorImpl<const void*> &IdealInits) { |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3699 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
| David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3700 | const RecordDecl *RD = RT->getDecl(); |
| 3701 | if (RD->isAnonymousStructOrUnion()) { |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3702 | for (auto *Field : RD->fields()) |
| 3703 | PopulateKeysForFields(Field, IdealInits); |
| David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3704 | return; |
| 3705 | } |
| Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3706 | } |
| Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3707 | IdealInits.push_back(Field->getCanonicalDecl()); |
| Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3710 | static const void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 3711 | return Context.getCanonicalType(BaseType).getTypePtr(); |
| Anders Carlsson | bcec05c | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3714 | static const void *GetKeyForMember(ASTContext &Context, |
| 3715 | CXXCtorInitializer *Member) { |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3716 | if (!Member->isAnyMemberInitializer()) |
| Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3717 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
| Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3718 | |
| Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3719 | return Member->getAnyMember()->getCanonicalDecl(); |
| Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3722 | static void DiagnoseBaseOrMemInitializerOrder( |
| 3723 | Sema &SemaRef, const CXXConstructorDecl *Constructor, |
| 3724 | ArrayRef<CXXCtorInitializer *> Inits) { |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3725 | if (Constructor->getDeclContext()->isDependentContext()) |
| Anders Carlsson | 35d6e3e | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 3726 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3728 | // Don't check initializers order unless the warning is enabled at the |
| 3729 | // location of at least one initializer. |
| 3730 | bool ShouldCheckOrder = false; |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3731 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3732 | CXXCtorInitializer *Init = Inits[InitIndex]; |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3733 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 3734 | Init->getSourceLocation()) |
| David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3735 | != DiagnosticsEngine::Ignored) { |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3736 | ShouldCheckOrder = true; |
| 3737 | break; |
| 3738 | } |
| 3739 | } |
| 3740 | if (!ShouldCheckOrder) |
| Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3741 | return; |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3742 | |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3743 | // Build the list of bases and members in the order that they'll |
| 3744 | // actually be initialized. The explicit initializers should be in |
| 3745 | // this same order but may be missing things. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3746 | SmallVector<const void*, 32> IdealInitKeys; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | |
| Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3748 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3749 | |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3750 | // 1. Virtual bases. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 3751 | for (const auto &VBase : ClassDecl->vbases()) |
| 3752 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase.getType())); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3753 | |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3754 | // 2. Non-virtual bases. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3755 | for (const auto &Base : ClassDecl->bases()) { |
| 3756 | if (Base.isVirtual()) |
| Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3757 | continue; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3758 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base.getType())); |
| Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3759 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3761 | // 3. Direct fields. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3762 | for (auto *Field : ClassDecl->fields()) { |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3763 | if (Field->isUnnamedBitfield()) |
| 3764 | continue; |
| 3765 | |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3766 | PopulateKeysForFields(Field, IdealInitKeys); |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3769 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 3770 | unsigned IdealIndex = 0; |
| Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3771 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3772 | CXXCtorInitializer *PrevInit = 0; |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3773 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3774 | CXXCtorInitializer *Init = Inits[InitIndex]; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3775 | const void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3776 | |
| 3777 | // Scan forward to try to find this initializer in the idealized |
| 3778 | // initializers list. |
| 3779 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3780 | if (InitKey == IdealInitKeys[IdealIndex]) |
| Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3781 | break; |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3782 | |
| 3783 | // If we didn't find this initializer, it must be because we |
| 3784 | // scanned past it on a previous iteration. That can only |
| 3785 | // happen if we're out of order; emit a warning. |
| Douglas Gregor | aabdfcb | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 3786 | if (IdealIndex == NumIdealInits && PrevInit) { |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3787 | Sema::SemaDiagnosticBuilder D = |
| 3788 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 3789 | diag::warn_initializer_out_of_order); |
| 3790 | |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3791 | if (PrevInit->isAnyMemberInitializer()) |
| 3792 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3793 | else |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3794 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3795 | |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3796 | if (Init->isAnyMemberInitializer()) |
| 3797 | D << 0 << Init->getAnyMember()->getDeclName(); |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3798 | else |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3799 | D << 1 << Init->getTypeSourceInfo()->getType(); |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3800 | |
| 3801 | // Move back to the initializer's location in the ideal list. |
| 3802 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3803 | if (InitKey == IdealInitKeys[IdealIndex]) |
| Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3804 | break; |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3805 | |
| 3806 | assert(IdealIndex != NumIdealInits && |
| 3807 | "initializer not found in initializer list"); |
| Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3808 | } |
| John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3809 | |
| 3810 | PrevInit = Init; |
| Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3811 | } |
| Anders Carlsson | 75fdaa4 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3814 | namespace { |
| 3815 | bool CheckRedundantInit(Sema &S, |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3816 | CXXCtorInitializer *Init, |
| 3817 | CXXCtorInitializer *&PrevInit) { |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3818 | if (!PrevInit) { |
| 3819 | PrevInit = Init; |
| 3820 | return false; |
| 3821 | } |
| 3822 | |
| Douglas Gregor | ea306a1 | 2013-03-25 23:28:23 +0000 | [diff] [blame] | 3823 | if (FieldDecl *Field = Init->getAnyMember()) |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3824 | S.Diag(Init->getSourceLocation(), |
| 3825 | diag::err_multiple_mem_initialization) |
| 3826 | << Field->getDeclName() |
| 3827 | << Init->getSourceRange(); |
| 3828 | else { |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3829 | const Type *BaseClass = Init->getBaseClass(); |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3830 | assert(BaseClass && "neither field nor base"); |
| 3831 | S.Diag(Init->getSourceLocation(), |
| 3832 | diag::err_multiple_base_initialization) |
| 3833 | << QualType(BaseClass, 0) |
| 3834 | << Init->getSourceRange(); |
| 3835 | } |
| 3836 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 3837 | << 0 << PrevInit->getSourceRange(); |
| 3838 | |
| 3839 | return true; |
| 3840 | } |
| 3841 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3842 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3843 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 3844 | |
| 3845 | bool CheckRedundantUnionInit(Sema &S, |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3846 | CXXCtorInitializer *Init, |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3847 | RedundantUnionMap &Unions) { |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3848 | FieldDecl *Field = Init->getAnyMember(); |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3849 | RecordDecl *Parent = Field->getParent(); |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3850 | NamedDecl *Child = Field; |
| David Blaikie | 0f65d59 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3851 | |
| 3852 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3853 | if (Parent->isUnion()) { |
| 3854 | UnionEntry &En = Unions[Parent]; |
| 3855 | if (En.first && En.first != Child) { |
| 3856 | S.Diag(Init->getSourceLocation(), |
| 3857 | diag::err_multiple_mem_union_initialization) |
| 3858 | << Field->getDeclName() |
| 3859 | << Init->getSourceRange(); |
| 3860 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 3861 | << 0 << En.second->getSourceRange(); |
| 3862 | return true; |
| David Blaikie | 256ee19 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 3863 | } |
| 3864 | if (!En.first) { |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3865 | En.first = Child; |
| 3866 | En.second = Init; |
| 3867 | } |
| David Blaikie | 0f65d59 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3868 | if (!Parent->isAnonymousStructOrUnion()) |
| 3869 | return false; |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | Child = Parent; |
| 3873 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| David Blaikie | 0f65d59 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3874 | } |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3875 | |
| 3876 | return false; |
| 3877 | } |
| 3878 | } |
| 3879 | |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3880 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3881 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3882 | SourceLocation ColonLoc, |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3883 | ArrayRef<CXXCtorInitializer*> MemInits, |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3884 | bool AnyErrors) { |
| 3885 | if (!ConstructorDecl) |
| 3886 | return; |
| 3887 | |
| 3888 | AdjustDeclIfTemplate(ConstructorDecl); |
| 3889 | |
| 3890 | CXXConstructorDecl *Constructor |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3891 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3892 | |
| 3893 | if (!Constructor) { |
| 3894 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 3895 | return; |
| 3896 | } |
| 3897 | |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3898 | // Mapping for the duplicate initializers check. |
| 3899 | // For member initializers, this is keyed with a FieldDecl*. |
| 3900 | // For base initializers, this is keyed with a Type*. |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3901 | llvm::DenseMap<const void *, CXXCtorInitializer *> Members; |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3902 | |
| 3903 | // Mapping for the inconsistent anonymous-union initializers check. |
| 3904 | RedundantUnionMap MemberUnions; |
| 3905 | |
| Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3906 | bool HadError = false; |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3907 | for (unsigned i = 0; i < MemInits.size(); i++) { |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3908 | CXXCtorInitializer *Init = MemInits[i]; |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3909 | |
| Abramo Bagnara | 341d783 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 3910 | // Set the source order index. |
| 3911 | Init->setSourceOrder(i); |
| 3912 | |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3913 | if (Init->isAnyMemberInitializer()) { |
| Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3914 | const void *Key = GetKeyForMember(Context, Init); |
| 3915 | if (CheckRedundantInit(*this, Init, Members[Key]) || |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3916 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 3917 | HadError = true; |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3918 | } else if (Init->isBaseInitializer()) { |
| Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 3919 | const void *Key = GetKeyForMember(Context, Init); |
| John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3920 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 3921 | HadError = true; |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3922 | } else { |
| 3923 | assert(Init->isDelegatingInitializer()); |
| 3924 | // This must be the only initializer |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3925 | if (MemInits.size() != 1) { |
| Richard Smith | 21f06f0 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3926 | Diag(Init->getSourceLocation(), |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3927 | diag::err_delegating_initializer_alone) |
| Richard Smith | 21f06f0 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3928 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
| Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3929 | // We will treat this as being the only initializer. |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3930 | } |
| Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3931 | SetDelegatingInitializer(Constructor, MemInits[i]); |
| Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3932 | // Return immediately as the initializer is set. |
| 3933 | return; |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3934 | } |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
| Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3937 | if (HadError) |
| 3938 | return; |
| 3939 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3940 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits); |
| Anders Carlsson | 4c8cb01 | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 3941 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3942 | SetCtorInitializers(Constructor, AnyErrors, MemInits); |
| Richard Trieu | 3a446ff | 2013-09-16 21:54:53 +0000 | [diff] [blame] | 3943 | |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 3944 | DiagnoseUninitializedFields(*this, Constructor); |
| Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
| Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3947 | void |
| John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3948 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 3949 | CXXRecordDecl *ClassDecl) { |
| Richard Smith | 2010404 | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 3950 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 3951 | // have destructors implicitly called. |
| 3952 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3953 | return; |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3954 | |
| 3955 | // FIXME: all the access-control diagnostics are positioned on the |
| 3956 | // field/base declaration. That's probably good; that said, the |
| 3957 | // user might reasonably want to know why the destructor is being |
| 3958 | // emitted, and we currently don't say. |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3959 | |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3960 | // Non-static data members. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3961 | for (auto *Field : ClassDecl->fields()) { |
| Fariborz Jahanian | 16f94c6 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 3962 | if (Field->isInvalidDecl()) |
| 3963 | continue; |
| Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3964 | |
| 3965 | // Don't destroy incomplete or zero-length arrays. |
| 3966 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 3967 | continue; |
| 3968 | |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3969 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 3970 | |
| 3971 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 3972 | if (!RT) |
| 3973 | continue; |
| 3974 | |
| 3975 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3976 | if (FieldClassDecl->isInvalidDecl()) |
| 3977 | continue; |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3978 | if (FieldClassDecl->hasIrrelevantDestructor()) |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3979 | continue; |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 3980 | // The destructor for an implicit anonymous union member is never invoked. |
| 3981 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 3982 | continue; |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3983 | |
| Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3984 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
| Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3985 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3986 | CheckDestructorAccess(Field->getLocation(), Dtor, |
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3987 | PDiag(diag::err_access_dtor_field) |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3988 | << Field->getDeclName() |
| 3989 | << FieldType); |
| 3990 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3991 | MarkFunctionReferenced(Location, Dtor); |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3992 | DiagnoseUseOfDecl(Dtor, Location); |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3995 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 3996 | |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3997 | // Bases. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3998 | for (const auto &Base : ClassDecl->bases()) { |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3999 | // Bases are always records in a well-formed non-dependent class. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4000 | const RecordType *RT = Base.getType()->getAs<RecordType>(); |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4001 | |
| 4002 | // Remember direct virtual bases. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4003 | if (Base.isVirtual()) |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4004 | DirectVirtualBases.insert(RT); |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4005 | |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4006 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4007 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 4008 | if (BaseClassDecl->isInvalidDecl()) |
| 4009 | continue; |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4010 | if (BaseClassDecl->hasIrrelevantDestructor()) |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4011 | continue; |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4012 | |
| Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4013 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
| Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4014 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4015 | |
| 4016 | // FIXME: caret should be on the start of the class name |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4017 | CheckDestructorAccess(Base.getLocStart(), Dtor, |
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4018 | PDiag(diag::err_access_dtor_base) |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4019 | << Base.getType() |
| 4020 | << Base.getSourceRange(), |
| John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4021 | Context.getTypeDeclType(ClassDecl)); |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4022 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4023 | MarkFunctionReferenced(Location, Dtor); |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4024 | DiagnoseUseOfDecl(Dtor, Location); |
| Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | // Virtual bases. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4028 | for (const auto &VBase : ClassDecl->vbases()) { |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4029 | // Bases are always records in a well-formed non-dependent class. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4030 | const RecordType *RT = VBase.getType()->castAs<RecordType>(); |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4031 | |
| 4032 | // Ignore direct virtual bases. |
| 4033 | if (DirectVirtualBases.count(RT)) |
| 4034 | continue; |
| 4035 | |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4036 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4037 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 4038 | if (BaseClassDecl->isInvalidDecl()) |
| 4039 | continue; |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4040 | if (BaseClassDecl->hasIrrelevantDestructor()) |
| Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4041 | continue; |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4042 | |
| Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4043 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
| Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4044 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
| David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4045 | if (CheckDestructorAccess( |
| 4046 | ClassDecl->getLocation(), Dtor, |
| 4047 | PDiag(diag::err_access_dtor_vbase) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4048 | << Context.getTypeDeclType(ClassDecl) << VBase.getType(), |
| David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4049 | Context.getTypeDeclType(ClassDecl)) == |
| 4050 | AR_accessible) { |
| 4051 | CheckDerivedToBaseConversion( |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 4052 | Context.getTypeDeclType(ClassDecl), VBase.getType(), |
| David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4053 | diag::err_access_dtor_vbase, 0, ClassDecl->getLocation(), |
| 4054 | SourceRange(), DeclarationName(), 0); |
| 4055 | } |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4056 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4057 | MarkFunctionReferenced(Location, Dtor); |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4058 | DiagnoseUseOfDecl(Dtor, Location); |
| Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4062 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
| Fariborz Jahanian | 16094c2 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 4063 | if (!CDtorDecl) |
| Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 4064 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | if (CXXConstructorDecl *Constructor |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 4067 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) { |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4068 | SetCtorInitializers(Constructor, /*AnyErrors=*/false); |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 4069 | DiagnoseUninitializedFields(*this, Constructor); |
| 4070 | } |
| Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4074 | unsigned DiagID, AbstractDiagSelID SelID) { |
| Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4075 | class NonAbstractTypeDiagnoser : public TypeDiagnoser { |
| 4076 | unsigned DiagID; |
| 4077 | AbstractDiagSelID SelID; |
| 4078 | |
| 4079 | public: |
| 4080 | NonAbstractTypeDiagnoser(unsigned DiagID, AbstractDiagSelID SelID) |
| 4081 | : TypeDiagnoser(DiagID == 0), DiagID(DiagID), SelID(SelID) { } |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4082 | |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 4083 | void diagnose(Sema &S, SourceLocation Loc, QualType T) override { |
| Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4084 | if (Suppressed) return; |
| Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4085 | if (SelID == -1) |
| 4086 | S.Diag(Loc, DiagID) << T; |
| 4087 | else |
| 4088 | S.Diag(Loc, DiagID) << SelID << T; |
| 4089 | } |
| 4090 | } Diagnoser(DiagID, SelID); |
| 4091 | |
| 4092 | return RequireNonAbstractType(Loc, T, Diagnoser); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
| Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 4095 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4096 | TypeDiagnoser &Diagnoser) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4097 | if (!getLangOpts().CPlusPlus) |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4098 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4099 | |
| Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 4100 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
| Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4101 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4103 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4104 | // Find the innermost pointer type. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4105 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
| Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4106 | PT = T; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
| Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4108 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
| Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4109 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
| Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4110 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4112 | const RecordType *RT = T->getAs<RecordType>(); |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4113 | if (!RT) |
| 4114 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4115 | |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4116 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4117 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4118 | // We can't answer whether something is abstract until it has a |
| 4119 | // definition. If it's currently being defined, we'll walk back |
| 4120 | // over all the declarations when we have a full definition. |
| 4121 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 4122 | if (!Def || Def->isBeingDefined()) |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4123 | return false; |
| 4124 | |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4125 | if (!RD->isAbstract()) |
| 4126 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
| Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4128 | Diagnoser.diagnose(*this, Loc, T); |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4129 | DiagnoseAbstractType(RD); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4130 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4131 | return true; |
| 4132 | } |
| 4133 | |
| 4134 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 4135 | // Check if we've already emitted the list of pure virtual functions |
| 4136 | // for this class. |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4137 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4138 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | |
| Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 4140 | // If the diagnostic is suppressed, don't emit the notes. We're only |
| 4141 | // going to emit them once, so try to attach them to a diagnostic we're |
| 4142 | // actually going to show. |
| 4143 | if (Diags.isLastDiagnosticIgnored()) |
| 4144 | return; |
| 4145 | |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4146 | CXXFinalOverriderMap FinalOverriders; |
| 4147 | RD->getFinalOverriders(FinalOverriders); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4148 | |
| Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 4149 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 4150 | // more than once. |
| 4151 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 4152 | |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4153 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 4154 | MEnd = FinalOverriders.end(); |
| 4155 | M != MEnd; |
| 4156 | ++M) { |
| 4157 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 4158 | SOEnd = M->second.end(); |
| 4159 | SO != SOEnd; ++SO) { |
| 4160 | // C++ [class.abstract]p4: |
| 4161 | // A class is abstract if it contains or inherits at least one |
| 4162 | // pure virtual function for which the final overrider is pure |
| 4163 | // virtual. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4164 | |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4165 | // |
| 4166 | if (SO->second.size() != 1) |
| 4167 | continue; |
| 4168 | |
| 4169 | if (!SO->second.front().Method->isPure()) |
| 4170 | continue; |
| 4171 | |
| Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 4172 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 4173 | continue; |
| 4174 | |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4175 | Diag(SO->second.front().Method->getLocation(), |
| 4176 | diag::note_pure_virtual_function) |
| Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 4177 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4178 | } |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | if (!PureVirtualClassDiagSet) |
| 4182 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 4183 | PureVirtualClassDiagSet->insert(RD); |
| Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
| Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4186 | namespace { |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4187 | struct AbstractUsageInfo { |
| 4188 | Sema &S; |
| 4189 | CXXRecordDecl *Record; |
| 4190 | CanQualType AbstractType; |
| 4191 | bool Invalid; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4193 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 4194 | : S(S), Record(Record), |
| 4195 | AbstractType(S.Context.getCanonicalType( |
| 4196 | S.Context.getTypeDeclType(Record))), |
| 4197 | Invalid(false) {} |
| Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4198 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4199 | void DiagnoseAbstractType() { |
| 4200 | if (Invalid) return; |
| 4201 | S.DiagnoseAbstractType(Record); |
| 4202 | Invalid = true; |
| 4203 | } |
| Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4204 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4205 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 4206 | }; |
| 4207 | |
| 4208 | struct CheckAbstractUsage { |
| 4209 | AbstractUsageInfo &Info; |
| 4210 | const NamedDecl *Ctx; |
| 4211 | |
| 4212 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 4213 | : Info(Info), Ctx(Ctx) {} |
| 4214 | |
| 4215 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4216 | switch (TL.getTypeLocClass()) { |
| 4217 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 4218 | #define TYPELOC(CLASS, PARENT) \ |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4219 | case TypeLoc::CLASS: Check(TL.castAs<CLASS##TypeLoc>(), Sel); break; |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4220 | #include "clang/AST/TypeLocNodes.def" |
| Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4221 | } |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4222 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4223 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4224 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 4225 | Visit(TL.getReturnLoc(), Sema::AbstractReturnType); |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 4226 | for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) { |
| 4227 | if (!TL.getParam(I)) |
| Douglas Gregor | 385d3fd | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 4228 | continue; |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 4229 | |
| 4230 | TypeSourceInfo *TSI = TL.getParam(I)->getTypeSourceInfo(); |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4231 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
| Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4232 | } |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4233 | } |
| Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4234 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4235 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4236 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 4237 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4238 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4239 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4240 | // Visit the type parameters from a permissive context. |
| 4241 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 4242 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 4243 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 4244 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 4245 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 4246 | // TODO: other template argument types? |
| Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4247 | } |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4248 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4249 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4250 | // Visit pointee types from a permissive context. |
| 4251 | #define CheckPolymorphic(Type) \ |
| 4252 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 4253 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 4254 | } |
| 4255 | CheckPolymorphic(PointerTypeLoc) |
| 4256 | CheckPolymorphic(ReferenceTypeLoc) |
| 4257 | CheckPolymorphic(MemberPointerTypeLoc) |
| 4258 | CheckPolymorphic(BlockPointerTypeLoc) |
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4259 | CheckPolymorphic(AtomicTypeLoc) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4260 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4261 | /// Handle all the types we haven't given a more specific |
| 4262 | /// implementation for above. |
| 4263 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4264 | // Every other kind of type that we haven't called out already |
| 4265 | // that has an inner type is either (1) sugar or (2) contains that |
| 4266 | // inner type in some way as a subobject. |
| 4267 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 4268 | return Visit(Next, Sel); |
| 4269 | |
| 4270 | // If there's no inner type and we're in a permissive context, |
| 4271 | // don't diagnose. |
| 4272 | if (Sel == Sema::AbstractNone) return; |
| 4273 | |
| 4274 | // Check whether the type matches the abstract type. |
| 4275 | QualType T = TL.getType(); |
| 4276 | if (T->isArrayType()) { |
| 4277 | Sel = Sema::AbstractArrayType; |
| 4278 | T = Info.S.Context.getBaseElementType(T); |
| Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4279 | } |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4280 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 4281 | if (CT != Info.AbstractType) return; |
| 4282 | |
| 4283 | // It matched; do some magic. |
| 4284 | if (Sel == Sema::AbstractArrayType) { |
| 4285 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 4286 | << T << TL.getSourceRange(); |
| 4287 | } else { |
| 4288 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 4289 | << Sel << T << TL.getSourceRange(); |
| 4290 | } |
| 4291 | Info.DiagnoseAbstractType(); |
| 4292 | } |
| 4293 | }; |
| 4294 | |
| 4295 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 4296 | Sema::AbstractDiagSelID Sel) { |
| 4297 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 4298 | } |
| 4299 | |
| 4300 | } |
| 4301 | |
| 4302 | /// Check for invalid uses of an abstract type in a method declaration. |
| 4303 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4304 | CXXMethodDecl *MD) { |
| 4305 | // No need to do the check on definitions, which require that |
| 4306 | // the return/param types be complete. |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 4307 | if (MD->doesThisDeclarationHaveABody()) |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4308 | return; |
| 4309 | |
| 4310 | // For safety's sake, just ignore it if we don't have type source |
| 4311 | // information. This should never happen for non-implicit methods, |
| 4312 | // but... |
| 4313 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 4314 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 4315 | } |
| 4316 | |
| 4317 | /// Check for invalid uses of an abstract type within a class definition. |
| 4318 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4319 | CXXRecordDecl *RD) { |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 4320 | for (auto *D : RD->decls()) { |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4321 | if (D->isImplicit()) continue; |
| 4322 | |
| 4323 | // Methods and method templates. |
| 4324 | if (isa<CXXMethodDecl>(D)) { |
| 4325 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 4326 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 4327 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 4328 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 4329 | |
| 4330 | // Fields and static variables. |
| 4331 | } else if (isa<FieldDecl>(D)) { |
| 4332 | FieldDecl *FD = cast<FieldDecl>(D); |
| 4333 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 4334 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 4335 | } else if (isa<VarDecl>(D)) { |
| 4336 | VarDecl *VD = cast<VarDecl>(D); |
| 4337 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 4338 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 4339 | |
| 4340 | // Nested classes and class templates. |
| 4341 | } else if (isa<CXXRecordDecl>(D)) { |
| 4342 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 4343 | } else if (isa<ClassTemplateDecl>(D)) { |
| 4344 | CheckAbstractClassUsage(Info, |
| 4345 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 4346 | } |
| 4347 | } |
| Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4350 | /// \brief Perform semantic checks on a class definition that has been |
| 4351 | /// completing, introducing implicitly-declared members, checking for |
| 4352 | /// abstract types, etc. |
| Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4353 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 4354 | if (!Record) |
| Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4355 | return; |
| 4356 | |
| John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4357 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 4358 | AbstractUsageInfo Info(*this, Record); |
| 4359 | CheckAbstractClassUsage(Info, Record); |
| 4360 | } |
| Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4361 | |
| 4362 | // If this is not an aggregate type and has no user-declared constructor, |
| 4363 | // complain about any non-static data members of reference or const scalar |
| 4364 | // type, since they will never get initializers. |
| 4365 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| Douglas Gregor | fbf19a0 | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 4366 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 4367 | !Record->isLambda()) { |
| Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4368 | bool Complained = false; |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4369 | for (const auto *F : Record->fields()) { |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4370 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4371 | continue; |
| 4372 | |
| Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4373 | if (F->getType()->isReferenceType() || |
| Benjamin Kramer | 659d7fc | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 4374 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
| Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4375 | if (!Complained) { |
| 4376 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 4377 | << Record->getTagKind() << Record; |
| 4378 | Complained = true; |
| 4379 | } |
| 4380 | |
| 4381 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 4382 | << F->getType()->isReferenceType() |
| 4383 | << F->getDeclName(); |
| 4384 | } |
| 4385 | } |
| 4386 | } |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4387 | |
| Anders Carlsson | e771e76 | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 4388 | if (Record->isDynamicClass() && !Record->isDependentType()) |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4389 | DynamicClasses.push_back(Record); |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4390 | |
| 4391 | if (Record->getIdentifier()) { |
| 4392 | // C++ [class.mem]p13: |
| 4393 | // If T is the name of a class, then each of the following shall have a |
| 4394 | // name different from T: |
| 4395 | // - every member of every anonymous union that is a member of class T. |
| 4396 | // |
| 4397 | // C++ [class.mem]p14: |
| 4398 | // In addition, if class T has a user-declared constructor (12.1), every |
| 4399 | // non-static data member of class T shall have a name different from T. |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4400 | DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 4401 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 4402 | ++I) { |
| 4403 | NamedDecl *D = *I; |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4404 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 4405 | isa<IndirectFieldDecl>(D)) { |
| 4406 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 4407 | << D->getDeclName(); |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | } |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4410 | } |
| Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4411 | } |
| Argyrios Kyrtzidis | 7f3986d | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4412 | |
| Argyrios Kyrtzidis | 33799ca | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4413 | // Warn if the class has virtual methods but non-virtual public destructor. |
| Douglas Gregor | 0cf82f6 | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 4414 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
| Argyrios Kyrtzidis | 7f3986d | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4415 | CXXDestructorDecl *dtor = Record->getDestructor(); |
| Argyrios Kyrtzidis | 33799ca | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4416 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
| Argyrios Kyrtzidis | 7f3986d | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4417 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 4418 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 4419 | } |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4420 | |
| David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4421 | if (Record->isAbstract()) { |
| 4422 | if (FinalAttr *FA = Record->getAttr<FinalAttr>()) { |
| 4423 | Diag(Record->getLocation(), diag::warn_abstract_final_class) |
| 4424 | << FA->isSpelledAsSealed(); |
| 4425 | DiagnoseAbstractType(Record); |
| 4426 | } |
| David Blaikie | 348df50 | 2012-09-21 03:21:07 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4429 | if (!Record->isDependentType()) { |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 4430 | for (auto *M : Record->methods()) { |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4431 | // See if a method overloads virtual methods in a base |
| 4432 | // class without overriding any. |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4433 | if (!M->isStatic()) |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 4434 | DiagnoseHiddenVirtualMethods(M); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4435 | |
| 4436 | // Check whether the explicitly-defaulted special members are valid. |
| 4437 | if (!M->isInvalidDecl() && M->isExplicitlyDefaulted()) |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 4438 | CheckExplicitlyDefaultedSpecialMember(M); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4439 | |
| 4440 | // For an explicitly defaulted or deleted special member, we defer |
| 4441 | // determining triviality until the class is complete. That time is now! |
| 4442 | if (!M->isImplicit() && !M->isUserProvided()) { |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 4443 | CXXSpecialMember CSM = getSpecialMember(M); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4444 | if (CSM != CXXInvalid) { |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 4445 | M->setTrivial(SpecialMemberIsTrivial(M, CSM)); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4446 | |
| 4447 | // Inform the class that we've finished declaring this member. |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 4448 | Record->finishedDefaultedOrDeletedMember(M); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4449 | } |
| 4450 | } |
| 4451 | } |
| 4452 | } |
| 4453 | |
| 4454 | // C++11 [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 4455 | // function that is not a constructor declares that member function to be |
| 4456 | // const. [...] The class of which that function is a member shall be |
| 4457 | // a literal type. |
| 4458 | // |
| 4459 | // If the class has virtual bases, any constexpr members will already have |
| 4460 | // been diagnosed by the checks performed on the member declaration, so |
| 4461 | // suppress this (less useful) diagnostic. |
| 4462 | // |
| 4463 | // We delay this until we know whether an explicitly-defaulted (or deleted) |
| 4464 | // destructor for the class is trivial. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4465 | if (LangOpts.CPlusPlus11 && !Record->isDependentType() && |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4466 | !Record->isLiteral() && !Record->getNumVBases()) { |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 4467 | for (const auto *M : Record->methods()) { |
| 4468 | if (M->isConstexpr() && M->isInstance() && !isa<CXXConstructorDecl>(M)) { |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4469 | switch (Record->getTemplateSpecializationKind()) { |
| 4470 | case TSK_ImplicitInstantiation: |
| 4471 | case TSK_ExplicitInstantiationDeclaration: |
| 4472 | case TSK_ExplicitInstantiationDefinition: |
| 4473 | // If a template instantiates to a non-literal type, but its members |
| 4474 | // instantiate to constexpr functions, the template is technically |
| 4475 | // ill-formed, but we allow it for sanity. |
| 4476 | continue; |
| 4477 | |
| 4478 | case TSK_Undeclared: |
| 4479 | case TSK_ExplicitSpecialization: |
| 4480 | RequireLiteralType(M->getLocation(), Context.getRecordType(Record), |
| 4481 | diag::err_constexpr_method_non_literal); |
| 4482 | break; |
| 4483 | } |
| 4484 | |
| 4485 | // Only produce one error per class. |
| 4486 | break; |
| 4487 | } |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4488 | } |
| 4489 | } |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4490 | |
| John McCall | 95833f3 | 2014-02-27 20:30:49 +0000 | [diff] [blame] | 4491 | // ms_struct is a request to use the same ABI rules as MSVC. Check |
| 4492 | // whether this class uses any C++ features that are implemented |
| 4493 | // completely differently in MSVC, and if so, emit a diagnostic. |
| 4494 | // That diagnostic defaults to an error, but we allow projects to |
| 4495 | // map it down to a warning (or ignore it). It's a fairly common |
| 4496 | // practice among users of the ms_struct pragma to mass-annotate |
| 4497 | // headers, sweeping up a bunch of types that the project doesn't |
| 4498 | // really rely on MSVC-compatible layout for. We must therefore |
| 4499 | // support "ms_struct except for C++ stuff" as a secondary ABI. |
| 4500 | if (Record->isMsStruct(Context) && |
| 4501 | (Record->isPolymorphic() || Record->getNumBases())) { |
| 4502 | Diag(Record->getLocation(), diag::warn_cxx_ms_struct); |
| Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4505 | // Declare inheriting constructors. We do this eagerly here because: |
| 4506 | // - The standard requires an eager diagnostic for conflicting inheriting |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4507 | // constructors from different classes. |
| 4508 | // - The lazy declaration of the other implicit constructors is so as to not |
| 4509 | // waste space and performance on classes that are not meant to be |
| 4510 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4511 | // have inheriting constructors. |
| 4512 | DeclareInheritingConstructors(Record); |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4515 | /// Look up the special member function that would be called by a special |
| 4516 | /// member function for a subobject of class type. |
| 4517 | /// |
| 4518 | /// \param Class The class type of the subobject. |
| 4519 | /// \param CSM The kind of special member function. |
| 4520 | /// \param FieldQuals If the subobject is a field, its cv-qualifiers. |
| 4521 | /// \param ConstRHS True if this is a copy operation with a const object |
| 4522 | /// on its RHS, that is, if the argument to the outer special member |
| 4523 | /// function is 'const' and this is not a field marked 'mutable'. |
| 4524 | static Sema::SpecialMemberOverloadResult *lookupCallFromSpecialMember( |
| 4525 | Sema &S, CXXRecordDecl *Class, Sema::CXXSpecialMember CSM, |
| 4526 | unsigned FieldQuals, bool ConstRHS) { |
| 4527 | unsigned LHSQuals = 0; |
| 4528 | if (CSM == Sema::CXXCopyAssignment || CSM == Sema::CXXMoveAssignment) |
| 4529 | LHSQuals = FieldQuals; |
| 4530 | |
| 4531 | unsigned RHSQuals = FieldQuals; |
| 4532 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 4533 | RHSQuals = 0; |
| 4534 | else if (ConstRHS) |
| 4535 | RHSQuals |= Qualifiers::Const; |
| 4536 | |
| 4537 | return S.LookupSpecialMember(Class, CSM, |
| 4538 | RHSQuals & Qualifiers::Const, |
| 4539 | RHSQuals & Qualifiers::Volatile, |
| 4540 | false, |
| 4541 | LHSQuals & Qualifiers::Const, |
| 4542 | LHSQuals & Qualifiers::Volatile); |
| 4543 | } |
| 4544 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4545 | /// Is the special member function which would be selected to perform the |
| 4546 | /// specified operation on the specified class type a constexpr constructor? |
| 4547 | static bool specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4548 | Sema::CXXSpecialMember CSM, |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4549 | unsigned Quals, bool ConstRHS) { |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4550 | Sema::SpecialMemberOverloadResult *SMOR = |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4551 | lookupCallFromSpecialMember(S, ClassDecl, CSM, Quals, ConstRHS); |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4552 | if (!SMOR || !SMOR->getMethod()) |
| 4553 | // A constructor we wouldn't select can't be "involved in initializing" |
| 4554 | // anything. |
| 4555 | return true; |
| 4556 | return SMOR->getMethod()->isConstexpr(); |
| 4557 | } |
| 4558 | |
| 4559 | /// Determine whether the specified special member function would be constexpr |
| 4560 | /// if it were implicitly defined. |
| 4561 | static bool defaultedSpecialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4562 | Sema::CXXSpecialMember CSM, |
| 4563 | bool ConstArg) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4564 | if (!S.getLangOpts().CPlusPlus11) |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4565 | return false; |
| 4566 | |
| 4567 | // C++11 [dcl.constexpr]p4: |
| 4568 | // In the definition of a constexpr constructor [...] |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4569 | bool Ctor = true; |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4570 | switch (CSM) { |
| 4571 | case Sema::CXXDefaultConstructor: |
| Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4572 | // Since default constructor lookup is essentially trivial (and cannot |
| 4573 | // involve, for instance, template instantiation), we compute whether a |
| 4574 | // defaulted default constructor is constexpr directly within CXXRecordDecl. |
| 4575 | // |
| 4576 | // This is important for performance; we need to know whether the default |
| 4577 | // constructor is constexpr to determine whether the type is a literal type. |
| 4578 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 4579 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4580 | case Sema::CXXCopyConstructor: |
| 4581 | case Sema::CXXMoveConstructor: |
| Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4582 | // For copy or move constructors, we need to perform overload resolution. |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4583 | break; |
| 4584 | |
| 4585 | case Sema::CXXCopyAssignment: |
| 4586 | case Sema::CXXMoveAssignment: |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4587 | if (!S.getLangOpts().CPlusPlus1y) |
| 4588 | return false; |
| 4589 | // In C++1y, we need to perform overload resolution. |
| 4590 | Ctor = false; |
| 4591 | break; |
| 4592 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4593 | case Sema::CXXDestructor: |
| 4594 | case Sema::CXXInvalid: |
| 4595 | return false; |
| 4596 | } |
| 4597 | |
| 4598 | // -- if the class is a non-empty union, or for each non-empty anonymous |
| 4599 | // union member of a non-union class, exactly one non-static data member |
| 4600 | // shall be initialized; [DR1359] |
| Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4601 | // |
| 4602 | // If we squint, this is guaranteed, since exactly one non-static data member |
| 4603 | // will be initialized (if the constructor isn't deleted), we just don't know |
| 4604 | // which one. |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4605 | if (Ctor && ClassDecl->isUnion()) |
| Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4606 | return true; |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4607 | |
| 4608 | // -- the class shall not have any virtual base classes; |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4609 | if (Ctor && ClassDecl->getNumVBases()) |
| 4610 | return false; |
| 4611 | |
| 4612 | // C++1y [class.copy]p26: |
| 4613 | // -- [the class] is a literal type, and |
| 4614 | if (!Ctor && !ClassDecl->isLiteral()) |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4615 | return false; |
| 4616 | |
| 4617 | // -- every constructor involved in initializing [...] base class |
| 4618 | // sub-objects shall be a constexpr constructor; |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4619 | // -- the assignment operator selected to copy/move each direct base |
| 4620 | // class is a constexpr function, and |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4621 | for (const auto &B : ClassDecl->bases()) { |
| 4622 | const RecordType *BaseType = B.getType()->getAs<RecordType>(); |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4623 | if (!BaseType) continue; |
| 4624 | |
| 4625 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4626 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, 0, ConstArg)) |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4627 | return false; |
| 4628 | } |
| 4629 | |
| 4630 | // -- every constructor involved in initializing non-static data members |
| 4631 | // [...] shall be a constexpr constructor; |
| 4632 | // -- every non-static data member and base class sub-object shall be |
| 4633 | // initialized |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4634 | // -- for each non-static data member of X that is of class type (or array |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4635 | // thereof), the assignment operator selected to copy/move that member is |
| 4636 | // a constexpr function |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4637 | for (const auto *F : ClassDecl->fields()) { |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4638 | if (F->isInvalidDecl()) |
| 4639 | continue; |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4640 | QualType BaseType = S.Context.getBaseElementType(F->getType()); |
| 4641 | if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4642 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4643 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, |
| 4644 | BaseType.getCVRQualifiers(), |
| 4645 | ConstArg && !F->isMutable())) |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4646 | return false; |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | // All OK, it's constexpr! |
| 4651 | return true; |
| 4652 | } |
| 4653 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4654 | static Sema::ImplicitExceptionSpecification |
| 4655 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, CXXMethodDecl *MD) { |
| 4656 | switch (S.getSpecialMember(MD)) { |
| 4657 | case Sema::CXXDefaultConstructor: |
| 4658 | return S.ComputeDefaultedDefaultCtorExceptionSpec(Loc, MD); |
| 4659 | case Sema::CXXCopyConstructor: |
| 4660 | return S.ComputeDefaultedCopyCtorExceptionSpec(MD); |
| 4661 | case Sema::CXXCopyAssignment: |
| 4662 | return S.ComputeDefaultedCopyAssignmentExceptionSpec(MD); |
| 4663 | case Sema::CXXMoveConstructor: |
| 4664 | return S.ComputeDefaultedMoveCtorExceptionSpec(MD); |
| 4665 | case Sema::CXXMoveAssignment: |
| 4666 | return S.ComputeDefaultedMoveAssignmentExceptionSpec(MD); |
| 4667 | case Sema::CXXDestructor: |
| 4668 | return S.ComputeDefaultedDtorExceptionSpec(MD); |
| 4669 | case Sema::CXXInvalid: |
| 4670 | break; |
| 4671 | } |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4672 | assert(cast<CXXConstructorDecl>(MD)->getInheritedConstructor() && |
| 4673 | "only special members have implicit exception specs"); |
| 4674 | return S.ComputeInheritingCtorExceptionSpec(cast<CXXConstructorDecl>(MD)); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 4677 | static FunctionProtoType::ExtProtoInfo getImplicitMethodEPI(Sema &S, |
| 4678 | CXXMethodDecl *MD) { |
| 4679 | FunctionProtoType::ExtProtoInfo EPI; |
| 4680 | |
| 4681 | // Build an exception specification pointing back at this member. |
| 4682 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 4683 | EPI.ExceptionSpecDecl = MD; |
| 4684 | |
| 4685 | // Set the calling convention to the default for C++ instance methods. |
| 4686 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv( |
| 4687 | S.Context.getDefaultCallingConvention(/*IsVariadic=*/false, |
| 4688 | /*IsCXXMethod=*/true)); |
| 4689 | return EPI; |
| 4690 | } |
| 4691 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4692 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD) { |
| 4693 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 4694 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 4695 | return; |
| 4696 | |
| Richard Smith | 7f78227 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4697 | // Evaluate the exception specification. |
| 4698 | ImplicitExceptionSpecification ExceptSpec = |
| 4699 | computeImplicitExceptionSpec(*this, Loc, MD); |
| 4700 | |
| Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 4701 | FunctionProtoType::ExtProtoInfo EPI; |
| 4702 | ExceptSpec.getEPI(EPI); |
| 4703 | |
| Richard Smith | 7f78227 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4704 | // Update the type of the special member to use it. |
| Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 4705 | UpdateExceptionSpec(MD, EPI); |
| Richard Smith | 7f78227 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4706 | |
| 4707 | // A user-provided destructor can be defined outside the class. When that |
| 4708 | // happens, be sure to update the exception specification on both |
| 4709 | // declarations. |
| 4710 | const FunctionProtoType *CanonicalFPT = |
| 4711 | MD->getCanonicalDecl()->getType()->castAs<FunctionProtoType>(); |
| 4712 | if (CanonicalFPT->getExceptionSpecType() == EST_Unevaluated) |
| Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 4713 | UpdateExceptionSpec(MD->getCanonicalDecl(), EPI); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4716 | void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) { |
| 4717 | CXXRecordDecl *RD = MD->getParent(); |
| 4718 | CXXSpecialMember CSM = getSpecialMember(MD); |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4719 | |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4720 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 4721 | "not an explicitly-defaulted special member"); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4722 | |
| 4723 | // Whether this was the first-declared instance of the constructor. |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4724 | // This affects whether we implicitly add an exception spec and constexpr. |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4725 | bool First = MD == MD->getCanonicalDecl(); |
| 4726 | |
| 4727 | bool HadError = false; |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4728 | |
| 4729 | // C++11 [dcl.fct.def.default]p1: |
| 4730 | // A function that is explicitly defaulted shall |
| 4731 | // -- be a special member function (checked elsewhere), |
| 4732 | // -- have the same type (except for ref-qualifiers, and except that a |
| 4733 | // copy operation can take a non-const reference) as an implicit |
| 4734 | // declaration, and |
| 4735 | // -- not have default arguments. |
| 4736 | unsigned ExpectedParams = 1; |
| 4737 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 4738 | ExpectedParams = 0; |
| 4739 | if (MD->getNumParams() != ExpectedParams) { |
| 4740 | // This also checks for default arguments: a copy or move constructor with a |
| 4741 | // default argument is classified as a default constructor, and assignment |
| 4742 | // operations and destructors can't have default arguments. |
| 4743 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 4744 | << CSM << MD->getSourceRange(); |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4745 | HadError = true; |
| Richard Smith | 50d705b | 2012-12-07 02:10:28 +0000 | [diff] [blame] | 4746 | } else if (MD->isVariadic()) { |
| 4747 | Diag(MD->getLocation(), diag::err_defaulted_special_member_variadic) |
| 4748 | << CSM << MD->getSourceRange(); |
| 4749 | HadError = true; |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4752 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4753 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4754 | bool CanHaveConstParam = false; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4755 | if (CSM == CXXCopyConstructor) |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4756 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4757 | else if (CSM == CXXCopyAssignment) |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4758 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4759 | |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4760 | QualType ReturnType = Context.VoidTy; |
| 4761 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 4762 | // Check for return type matching. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4763 | ReturnType = Type->getReturnType(); |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4764 | QualType ExpectedReturnType = |
| 4765 | Context.getLValueReferenceType(Context.getTypeDeclType(RD)); |
| 4766 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 4767 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 4768 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 4769 | HadError = true; |
| 4770 | } |
| 4771 | |
| 4772 | // A defaulted special member cannot have cv-qualifiers. |
| 4773 | if (Type->getTypeQuals()) { |
| 4774 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4775 | << (CSM == CXXMoveAssignment) << getLangOpts().CPlusPlus1y; |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4776 | HadError = true; |
| 4777 | } |
| 4778 | } |
| 4779 | |
| 4780 | // Check for parameter type matching. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4781 | QualType ArgType = ExpectedParams ? Type->getParamType(0) : QualType(); |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4782 | bool HasConstParam = false; |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4783 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 4784 | // Argument must be reference to possibly-const T. |
| 4785 | QualType ReferentType = ArgType->getPointeeType(); |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4786 | HasConstParam = ReferentType.isConstQualified(); |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4787 | |
| 4788 | if (ReferentType.isVolatileQualified()) { |
| 4789 | Diag(MD->getLocation(), |
| 4790 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 4791 | HadError = true; |
| 4792 | } |
| 4793 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4794 | if (HasConstParam && !CanHaveConstParam) { |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4795 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 4796 | Diag(MD->getLocation(), |
| 4797 | diag::err_defaulted_special_member_copy_const_param) |
| 4798 | << (CSM == CXXCopyAssignment); |
| 4799 | // FIXME: Explain why this special member can't be const. |
| 4800 | } else { |
| 4801 | Diag(MD->getLocation(), |
| 4802 | diag::err_defaulted_special_member_move_const_param) |
| 4803 | << (CSM == CXXMoveAssignment); |
| 4804 | } |
| 4805 | HadError = true; |
| 4806 | } |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4807 | } else if (ExpectedParams) { |
| 4808 | // A copy assignment operator can take its argument by value, but a |
| 4809 | // defaulted one cannot. |
| 4810 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
| Alexis Hunt | 604aeb3 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4811 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4812 | HadError = true; |
| 4813 | } |
| Alexis Hunt | 604aeb3 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4814 | |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4815 | // C++11 [dcl.fct.def.default]p2: |
| 4816 | // An explicitly-defaulted function may be declared constexpr only if it |
| 4817 | // would have been implicitly declared as constexpr, |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4818 | // Do not apply this rule to members of class templates, since core issue 1358 |
| 4819 | // makes such functions always instantiate to constexpr functions. For |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4820 | // functions which cannot be constexpr (for non-constructors in C++11 and for |
| 4821 | // destructors in C++1y), this is checked elsewhere. |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4822 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 4823 | HasConstParam); |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4824 | if ((getLangOpts().CPlusPlus1y ? !isa<CXXDestructorDecl>(MD) |
| 4825 | : isa<CXXConstructorDecl>(MD)) && |
| 4826 | MD->isConstexpr() && !Constexpr && |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4827 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 4828 | Diag(MD->getLocStart(), diag::err_incorrect_defaulted_constexpr) << CSM; |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4829 | // FIXME: Explain why the special member can't be constexpr. |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4830 | HadError = true; |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4831 | } |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4832 | |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4833 | // and may have an explicit exception-specification only if it is compatible |
| 4834 | // with the exception-specification on the implicit declaration. |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4835 | if (Type->hasExceptionSpec()) { |
| 4836 | // Delay the check if this is the first declaration of the special member, |
| 4837 | // since we may not have parsed some necessary in-class initializers yet. |
| Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4838 | if (First) { |
| 4839 | // If the exception specification needs to be instantiated, do so now, |
| 4840 | // before we clobber it with an EST_Unevaluated specification below. |
| 4841 | if (Type->getExceptionSpecType() == EST_Uninstantiated) { |
| 4842 | InstantiateExceptionSpec(MD->getLocStart(), MD); |
| 4843 | Type = MD->getType()->getAs<FunctionProtoType>(); |
| 4844 | } |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4845 | DelayedDefaultedMemberExceptionSpecs.push_back(std::make_pair(MD, Type)); |
| Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4846 | } else |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4847 | CheckExplicitlyDefaultedMemberExceptionSpec(MD, Type); |
| 4848 | } |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4849 | |
| 4850 | // If a function is explicitly defaulted on its first declaration, |
| 4851 | if (First) { |
| 4852 | // -- it is implicitly considered to be constexpr if the implicit |
| 4853 | // definition would be, |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4854 | MD->setConstexpr(Constexpr); |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4855 | |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4856 | // -- it is implicitly considered to have the same exception-specification |
| 4857 | // as if it had been implicitly declared, |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4858 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
| 4859 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 4860 | EPI.ExceptionSpecDecl = MD; |
| Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 4861 | MD->setType(Context.getFunctionType(ReturnType, |
| 4862 | ArrayRef<QualType>(&ArgType, |
| 4863 | ExpectedParams), |
| 4864 | EPI)); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4867 | if (ShouldDeleteSpecialMember(MD, CSM)) { |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4868 | if (First) { |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 4869 | SetDeclDeleted(MD, MD->getLocation()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4870 | } else { |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4871 | // C++11 [dcl.fct.def.default]p4: |
| 4872 | // [For a] user-provided explicitly-defaulted function [...] if such a |
| 4873 | // function is implicitly defined as deleted, the program is ill-formed. |
| 4874 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
| Richard Smith | 566184a | 2014-01-22 20:09:10 +0000 | [diff] [blame] | 4875 | ShouldDeleteSpecialMember(MD, CSM, /*Diagnose*/true); |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4876 | HadError = true; |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4877 | } |
| 4878 | } |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4879 | |
| Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4880 | if (HadError) |
| 4881 | MD->setInvalidDecl(); |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4884 | /// Check whether the exception specification provided for an |
| 4885 | /// explicitly-defaulted special member matches the exception specification |
| 4886 | /// that would have been generated for an implicit special member, per |
| 4887 | /// C++11 [dcl.fct.def.default]p2. |
| 4888 | void Sema::CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4889 | CXXMethodDecl *MD, const FunctionProtoType *SpecifiedType) { |
| 4890 | // Compute the implicit exception specification. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 4891 | CallingConv CC = Context.getDefaultCallingConvention(/*IsVariadic=*/false, |
| 4892 | /*IsCXXMethod=*/true); |
| 4893 | FunctionProtoType::ExtProtoInfo EPI(CC); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4894 | computeImplicitExceptionSpec(*this, MD->getLocation(), MD).getEPI(EPI); |
| 4895 | const FunctionProtoType *ImplicitType = cast<FunctionProtoType>( |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4896 | Context.getFunctionType(Context.VoidTy, None, EPI)); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4897 | |
| 4898 | // Ensure that it matches. |
| 4899 | CheckEquivalentExceptionSpec( |
| 4900 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 4901 | << getSpecialMember(MD), PDiag(), |
| 4902 | ImplicitType, SourceLocation(), |
| 4903 | SpecifiedType, MD->getLocation()); |
| 4904 | } |
| 4905 | |
| Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 4906 | void Sema::CheckDelayedMemberExceptionSpecs() { |
| 4907 | SmallVector<std::pair<const CXXDestructorDecl *, const CXXDestructorDecl *>, |
| 4908 | 2> Checks; |
| 4909 | SmallVector<std::pair<CXXMethodDecl *, const FunctionProtoType *>, 2> Specs; |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4910 | |
| Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 4911 | std::swap(Checks, DelayedDestructorExceptionSpecChecks); |
| 4912 | std::swap(Specs, DelayedDefaultedMemberExceptionSpecs); |
| 4913 | |
| 4914 | // Perform any deferred checking of exception specifications for virtual |
| 4915 | // destructors. |
| 4916 | for (unsigned i = 0, e = Checks.size(); i != e; ++i) { |
| 4917 | const CXXDestructorDecl *Dtor = Checks[i].first; |
| 4918 | assert(!Dtor->getParent()->isDependentType() && |
| 4919 | "Should not ever add destructors of templates into the list."); |
| 4920 | CheckOverridingFunctionExceptionSpec(Dtor, Checks[i].second); |
| 4921 | } |
| 4922 | |
| 4923 | // Check that any explicitly-defaulted methods have exception specifications |
| 4924 | // compatible with their implicit exception specifications. |
| 4925 | for (unsigned I = 0, N = Specs.size(); I != N; ++I) |
| 4926 | CheckExplicitlyDefaultedMemberExceptionSpec(Specs[I].first, |
| 4927 | Specs[I].second); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4928 | } |
| 4929 | |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4930 | namespace { |
| 4931 | struct SpecialMemberDeletionInfo { |
| 4932 | Sema &S; |
| 4933 | CXXMethodDecl *MD; |
| 4934 | Sema::CXXSpecialMember CSM; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4935 | bool Diagnose; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4936 | |
| 4937 | // Properties of the special member, computed for convenience. |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4938 | bool IsConstructor, IsAssignment, IsMove, ConstArg; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4939 | SourceLocation Loc; |
| 4940 | |
| 4941 | bool AllFieldsAreConst; |
| 4942 | |
| 4943 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4944 | Sema::CXXSpecialMember CSM, bool Diagnose) |
| 4945 | : S(S), MD(MD), CSM(CSM), Diagnose(Diagnose), |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4946 | IsConstructor(false), IsAssignment(false), IsMove(false), |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4947 | ConstArg(false), Loc(MD->getLocation()), |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4948 | AllFieldsAreConst(true) { |
| 4949 | switch (CSM) { |
| 4950 | case Sema::CXXDefaultConstructor: |
| 4951 | case Sema::CXXCopyConstructor: |
| 4952 | IsConstructor = true; |
| 4953 | break; |
| 4954 | case Sema::CXXMoveConstructor: |
| 4955 | IsConstructor = true; |
| 4956 | IsMove = true; |
| 4957 | break; |
| 4958 | case Sema::CXXCopyAssignment: |
| 4959 | IsAssignment = true; |
| 4960 | break; |
| 4961 | case Sema::CXXMoveAssignment: |
| 4962 | IsAssignment = true; |
| 4963 | IsMove = true; |
| 4964 | break; |
| 4965 | case Sema::CXXDestructor: |
| 4966 | break; |
| 4967 | case Sema::CXXInvalid: |
| 4968 | llvm_unreachable("invalid special member kind"); |
| 4969 | } |
| 4970 | |
| 4971 | if (MD->getNumParams()) { |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4972 | if (const ReferenceType *RT = |
| 4973 | MD->getParamDecl(0)->getType()->getAs<ReferenceType>()) |
| 4974 | ConstArg = RT->getPointeeType().isConstQualified(); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4975 | } |
| 4976 | } |
| 4977 | |
| 4978 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 4979 | |
| 4980 | /// Look up the corresponding special member in the given class. |
| Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4981 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class, |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4982 | unsigned Quals, bool IsMutable) { |
| 4983 | return lookupCallFromSpecialMember(S, Class, CSM, Quals, |
| 4984 | ConstArg && !IsMutable); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4987 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4988 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4989 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4990 | bool shouldDeleteForField(FieldDecl *FD); |
| 4991 | bool shouldDeleteForAllConstMembers(); |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4992 | |
| Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4993 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 4994 | unsigned Quals); |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4995 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 4996 | Sema::SpecialMemberOverloadResult *SMOR, |
| 4997 | bool IsDtorCallInCtor); |
| John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4998 | |
| 4999 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5000 | }; |
| 5001 | } |
| 5002 | |
| John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5003 | /// Is the given special member inaccessible when used on the given |
| 5004 | /// sub-object. |
| 5005 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 5006 | CXXMethodDecl *target) { |
| 5007 | /// If we're operating on a base class, the object type is the |
| 5008 | /// type of this special member. |
| 5009 | QualType objectTy; |
| Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 5010 | AccessSpecifier access = target->getAccess(); |
| John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5011 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 5012 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 5013 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 5014 | |
| 5015 | // If we're operating on a field, the object type is the type of the field. |
| 5016 | } else { |
| 5017 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 5018 | } |
| 5019 | |
| 5020 | return S.isSpecialMemberAccessibleForDeletion(target, access, objectTy); |
| 5021 | } |
| 5022 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5023 | /// Check whether we should delete a special member due to the implicit |
| 5024 | /// definition containing a call to a special member of a subobject. |
| 5025 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 5026 | Subobject Subobj, Sema::SpecialMemberOverloadResult *SMOR, |
| 5027 | bool IsDtorCallInCtor) { |
| 5028 | CXXMethodDecl *Decl = SMOR->getMethod(); |
| 5029 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 5030 | |
| 5031 | int DiagKind = -1; |
| 5032 | |
| 5033 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 5034 | DiagKind = !Decl ? 0 : 1; |
| 5035 | else if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5036 | DiagKind = 2; |
| John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5037 | else if (!isAccessible(Subobj, Decl)) |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5038 | DiagKind = 3; |
| 5039 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 5040 | !Decl->isTrivial()) { |
| 5041 | // A member of a union must have a trivial corresponding special member. |
| 5042 | // As a weird special case, a destructor call from a union's constructor |
| 5043 | // must be accessible and non-deleted, but need not be trivial. Such a |
| 5044 | // destructor is never actually called, but is semantically checked as |
| 5045 | // if it were. |
| 5046 | DiagKind = 4; |
| 5047 | } |
| 5048 | |
| 5049 | if (DiagKind == -1) |
| 5050 | return false; |
| 5051 | |
| 5052 | if (Diagnose) { |
| 5053 | if (Field) { |
| 5054 | S.Diag(Field->getLocation(), |
| 5055 | diag::note_deleted_special_member_class_subobject) |
| 5056 | << CSM << MD->getParent() << /*IsField*/true |
| 5057 | << Field << DiagKind << IsDtorCallInCtor; |
| 5058 | } else { |
| 5059 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 5060 | S.Diag(Base->getLocStart(), |
| 5061 | diag::note_deleted_special_member_class_subobject) |
| 5062 | << CSM << MD->getParent() << /*IsField*/false |
| 5063 | << Base->getType() << DiagKind << IsDtorCallInCtor; |
| 5064 | } |
| 5065 | |
| 5066 | if (DiagKind == 1) |
| 5067 | S.NoteDeletedFunction(Decl); |
| 5068 | // FIXME: Explain inaccessibility if DiagKind == 3. |
| 5069 | } |
| 5070 | |
| 5071 | return true; |
| 5072 | } |
| 5073 | |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5074 | /// Check whether we should delete a special member function due to having a |
| Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5075 | /// direct or virtual base class or non-static data member of class type M. |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5076 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
| Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5077 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5078 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5079 | bool IsMutable = Field && Field->isMutable(); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5080 | |
| 5081 | // C++11 [class.ctor]p5: |
| Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5082 | // -- any direct or virtual base class, or non-static data member with no |
| 5083 | // brace-or-equal-initializer, has class type M (or array thereof) and |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5084 | // either M has no default constructor or overload resolution as applied |
| 5085 | // to M's default constructor results in an ambiguity or in a function |
| 5086 | // that is deleted or inaccessible |
| 5087 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 5088 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 5089 | // overload resolution, as applied to B's corresponding special member, |
| 5090 | // results in an ambiguity or a function that is deleted or inaccessible |
| 5091 | // from the defaulted special member |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5092 | // C++11 [class.dtor]p5: |
| 5093 | // -- any direct or virtual base class [...] has a type with a destructor |
| 5094 | // that is deleted or inaccessible |
| 5095 | if (!(CSM == Sema::CXXDefaultConstructor && |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5096 | Field && Field->hasInClassInitializer()) && |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5097 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals, IsMutable), |
| 5098 | false)) |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5099 | return true; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5100 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5101 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 5102 | // -- any direct or virtual base class or non-static data member has a |
| 5103 | // type with a destructor that is deleted or inaccessible |
| 5104 | if (IsConstructor) { |
| 5105 | Sema::SpecialMemberOverloadResult *SMOR = |
| 5106 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 5107 | false, false, false, false, false); |
| 5108 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 5109 | return true; |
| 5110 | } |
| 5111 | |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5112 | return false; |
| 5113 | } |
| 5114 | |
| 5115 | /// Check whether we should delete a special member function due to the class |
| 5116 | /// having a particular direct or virtual base class. |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5117 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5118 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
| Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5119 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | /// Check whether we should delete a special member function due to the class |
| 5123 | /// having a particular non-static data member. |
| 5124 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 5125 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 5126 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 5127 | |
| 5128 | if (CSM == Sema::CXXDefaultConstructor) { |
| 5129 | // For a default constructor, all references must be initialized in-class |
| 5130 | // and, if a union, it must have a non-const member. |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5131 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 5132 | if (Diagnose) |
| 5133 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 5134 | << MD->getParent() << FD << FieldType << /*Reference*/0; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5135 | return true; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5136 | } |
| Richard Smith | 619ecdc | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 5137 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 5138 | // const-qualified type (or array thereof) with no |
| 5139 | // brace-or-equal-initializer does not have a user-provided default |
| 5140 | // constructor. |
| 5141 | if (!inUnion() && FieldType.isConstQualified() && |
| 5142 | !FD->hasInClassInitializer() && |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5143 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 5144 | if (Diagnose) |
| 5145 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| Richard Smith | c5f98f3 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 5146 | << MD->getParent() << FD << FD->getType() << /*Const*/1; |
| Richard Smith | 619ecdc | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 5147 | return true; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | if (inUnion() && !FieldType.isConstQualified()) |
| 5151 | AllFieldsAreConst = false; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5152 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 5153 | // For a copy constructor, data members must not be of rvalue reference |
| 5154 | // type. |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5155 | if (FieldType->isRValueReferenceType()) { |
| 5156 | if (Diagnose) |
| 5157 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 5158 | << MD->getParent() << FD << FieldType; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5159 | return true; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5160 | } |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5161 | } else if (IsAssignment) { |
| 5162 | // For an assignment operator, data members must not be of reference type. |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5163 | if (FieldType->isReferenceType()) { |
| 5164 | if (Diagnose) |
| 5165 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 5166 | << IsMove << MD->getParent() << FD << FieldType << /*Reference*/0; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5167 | return true; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5168 | } |
| 5169 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 5170 | // C++11 [class.copy]p23: |
| 5171 | // -- a non-static data member of const non-class type (or array thereof) |
| 5172 | if (Diagnose) |
| 5173 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| Richard Smith | c5f98f3 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 5174 | << IsMove << MD->getParent() << FD << FD->getType() << /*Const*/1; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5175 | return true; |
| 5176 | } |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | if (FieldRecord) { |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5180 | // Some additional restrictions exist on the variant members. |
| 5181 | if (!inUnion() && FieldRecord->isUnion() && |
| 5182 | FieldRecord->isAnonymousStructOrUnion()) { |
| 5183 | bool AllVariantFieldsAreConst = true; |
| 5184 | |
| Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5185 | // FIXME: Handle anonymous unions declared within anonymous unions. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5186 | for (auto *UI : FieldRecord->fields()) { |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5187 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5188 | |
| 5189 | if (!UnionFieldType.isConstQualified()) |
| 5190 | AllVariantFieldsAreConst = false; |
| 5191 | |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5192 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 5193 | if (UnionFieldRecord && |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5194 | shouldDeleteForClassSubobject(UnionFieldRecord, UI, |
| Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5195 | UnionFieldType.getCVRQualifiers())) |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5196 | return true; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | // At least one member in each anonymous union must be non-const |
| Douglas Gregor | 232ee49 | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 5200 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5201 | !FieldRecord->field_empty()) { |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5202 | if (Diagnose) |
| 5203 | S.Diag(FieldRecord->getLocation(), |
| 5204 | diag::note_deleted_default_ctor_all_const) |
| 5205 | << MD->getParent() << /*anonymous union*/1; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5206 | return true; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5207 | } |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5208 | |
| Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5209 | // Don't check the implicit member of the anonymous union type. |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5210 | // This is technically non-conformant, but sanity demands it. |
| 5211 | return false; |
| 5212 | } |
| 5213 | |
| Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5214 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 5215 | FieldType.getCVRQualifiers())) |
| Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5216 | return true; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | return false; |
| 5220 | } |
| 5221 | |
| 5222 | /// C++11 [class.ctor] p5: |
| 5223 | /// A defaulted default constructor for a class X is defined as deleted if |
| 5224 | /// X is a union and all of its variant members are of const-qualified type. |
| 5225 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
| Douglas Gregor | 232ee49 | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 5226 | // This is a silly definition, because it gives an empty union a deleted |
| 5227 | // default constructor. Don't do that. |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5228 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5229 | !MD->getParent()->field_empty()) { |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5230 | if (Diagnose) |
| 5231 | S.Diag(MD->getParent()->getLocation(), |
| 5232 | diag::note_deleted_default_ctor_all_const) |
| 5233 | << MD->getParent() << /*not anonymous union*/0; |
| 5234 | return true; |
| 5235 | } |
| 5236 | return false; |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /// Determine whether a defaulted special member function should be defined as |
| 5240 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 5241 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5242 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5243 | bool Diagnose) { |
| Richard Smith | f716bb8 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 5244 | if (MD->isInvalidDecl()) |
| 5245 | return false; |
| Alexis Hunt | d6da876 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5246 | CXXRecordDecl *RD = MD->getParent(); |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5247 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5248 | if (!LangOpts.CPlusPlus11 || RD->isInvalidDecl()) |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5249 | return false; |
| 5250 | |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5251 | // C++11 [expr.lambda.prim]p19: |
| 5252 | // The closure type associated with a lambda-expression has a |
| 5253 | // deleted (8.4.3) default constructor and a deleted copy |
| 5254 | // assignment operator. |
| 5255 | if (RD->isLambda() && |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5256 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 5257 | if (Diagnose) |
| 5258 | Diag(RD->getLocation(), diag::note_lambda_decl); |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5259 | return true; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
| Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5262 | // For an anonymous struct or union, the copy and assignment special members |
| 5263 | // will never be used, so skip the check. For an anonymous union declared at |
| 5264 | // namespace scope, the constructor and destructor are used. |
| 5265 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 5266 | RD->isAnonymousStructOrUnion()) |
| 5267 | return false; |
| 5268 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5269 | // C++11 [class.copy]p7, p18: |
| 5270 | // If the class definition declares a move constructor or move assignment |
| 5271 | // operator, an implicitly declared copy constructor or copy assignment |
| 5272 | // operator is defined as deleted. |
| 5273 | if (MD->isImplicit() && |
| 5274 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
| 5275 | CXXMethodDecl *UserDeclaredMove = 0; |
| 5276 | |
| 5277 | // In Microsoft mode, a user-declared move only causes the deletion of the |
| 5278 | // corresponding copy operation, not both copy operations. |
| 5279 | if (RD->hasUserDeclaredMoveConstructor() && |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 5280 | (!getLangOpts().MSVCCompat || CSM == CXXCopyConstructor)) { |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5281 | if (!Diagnose) return true; |
| Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5282 | |
| 5283 | // Find any user-declared move constructor. |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 5284 | for (auto *I : RD->ctors()) { |
| Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5285 | if (I->isMoveConstructor()) { |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 5286 | UserDeclaredMove = I; |
| Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5287 | break; |
| 5288 | } |
| 5289 | } |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5290 | assert(UserDeclaredMove); |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5291 | } else if (RD->hasUserDeclaredMoveAssignment() && |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 5292 | (!getLangOpts().MSVCCompat || CSM == CXXCopyAssignment)) { |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5293 | if (!Diagnose) return true; |
| Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5294 | |
| 5295 | // Find any user-declared move assignment operator. |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 5296 | for (auto *I : RD->methods()) { |
| Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5297 | if (I->isMoveAssignmentOperator()) { |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 5298 | UserDeclaredMove = I; |
| Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5299 | break; |
| 5300 | } |
| 5301 | } |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5302 | assert(UserDeclaredMove); |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | if (UserDeclaredMove) { |
| 5306 | Diag(UserDeclaredMove->getLocation(), |
| 5307 | diag::note_deleted_copy_user_declared_move) |
| Richard Smith | f989e51 | 2012-04-02 21:07:48 +0000 | [diff] [blame] | 5308 | << (CSM == CXXCopyAssignment) << RD |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5309 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 5310 | return true; |
| 5311 | } |
| 5312 | } |
| Alexis Hunt | d6da876 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5313 | |
| Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5314 | // Do access control from the special member function |
| 5315 | ContextRAII MethodContext(*this, MD); |
| 5316 | |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5317 | // C++11 [class.dtor]p5: |
| 5318 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 5319 | // results in an ambiguity or in a function that is deleted or inaccessible |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5320 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5321 | FunctionDecl *OperatorDelete = 0; |
| 5322 | DeclarationName Name = |
| 5323 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 5324 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5325 | OperatorDelete, false)) { |
| 5326 | if (Diagnose) |
| 5327 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5328 | return true; |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5329 | } |
| Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5332 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, Diagnose); |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5333 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5334 | for (auto &BI : RD->bases()) |
| 5335 | if (!BI.isVirtual() && |
| 5336 | SMI.shouldDeleteForBase(&BI)) |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5337 | return true; |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5338 | |
| Richard Smith | d162703 | 2013-07-22 18:06:23 +0000 | [diff] [blame] | 5339 | // Per DR1611, do not consider virtual bases of constructors of abstract |
| 5340 | // classes, since we are not going to construct them. |
| Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 5341 | if (!RD->isAbstract() || !SMI.IsConstructor) { |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 5342 | for (auto &BI : RD->vbases()) |
| 5343 | if (SMI.shouldDeleteForBase(&BI)) |
| Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 5344 | return true; |
| 5345 | } |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5346 | |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5347 | for (auto *FI : RD->fields()) |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5348 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5349 | SMI.shouldDeleteForField(FI)) |
| Alexis Hunt | a671bca | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 5350 | return true; |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5351 | |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5352 | if (SMI.shouldDeleteForAllConstMembers()) |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5353 | return true; |
| 5354 | |
| 5355 | return false; |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5358 | /// Perform lookup for a special member of the specified kind, and determine |
| 5359 | /// whether it is trivial. If the triviality can be determined without the |
| 5360 | /// lookup, skip it. This is intended for use when determining whether a |
| 5361 | /// special member of a containing object is trivial, and thus does not ever |
| 5362 | /// perform overload resolution for default constructors. |
| 5363 | /// |
| 5364 | /// If \p Selected is not \c NULL, \c *Selected will be filled in with the |
| 5365 | /// member that was most likely to be intended to be trivial, if any. |
| 5366 | static bool findTrivialSpecialMember(Sema &S, CXXRecordDecl *RD, |
| 5367 | Sema::CXXSpecialMember CSM, unsigned Quals, |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5368 | bool ConstRHS, CXXMethodDecl **Selected) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5369 | if (Selected) |
| 5370 | *Selected = 0; |
| 5371 | |
| 5372 | switch (CSM) { |
| 5373 | case Sema::CXXInvalid: |
| 5374 | llvm_unreachable("not a special member"); |
| 5375 | |
| 5376 | case Sema::CXXDefaultConstructor: |
| 5377 | // C++11 [class.ctor]p5: |
| 5378 | // A default constructor is trivial if: |
| 5379 | // - all the [direct subobjects] have trivial default constructors |
| 5380 | // |
| 5381 | // Note, no overload resolution is performed in this case. |
| 5382 | if (RD->hasTrivialDefaultConstructor()) |
| 5383 | return true; |
| 5384 | |
| 5385 | if (Selected) { |
| 5386 | // If there's a default constructor which could have been trivial, dig it |
| 5387 | // out. Otherwise, if there's any user-provided default constructor, point |
| 5388 | // to that as an example of why there's not a trivial one. |
| 5389 | CXXConstructorDecl *DefCtor = 0; |
| 5390 | if (RD->needsImplicitDefaultConstructor()) |
| 5391 | S.DeclareImplicitDefaultConstructor(RD); |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 5392 | for (auto *CI : RD->ctors()) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5393 | if (!CI->isDefaultConstructor()) |
| 5394 | continue; |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 5395 | DefCtor = CI; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5396 | if (!DefCtor->isUserProvided()) |
| 5397 | break; |
| 5398 | } |
| 5399 | |
| 5400 | *Selected = DefCtor; |
| 5401 | } |
| 5402 | |
| 5403 | return false; |
| 5404 | |
| 5405 | case Sema::CXXDestructor: |
| 5406 | // C++11 [class.dtor]p5: |
| 5407 | // A destructor is trivial if: |
| 5408 | // - all the direct [subobjects] have trivial destructors |
| 5409 | if (RD->hasTrivialDestructor()) |
| 5410 | return true; |
| 5411 | |
| 5412 | if (Selected) { |
| 5413 | if (RD->needsImplicitDestructor()) |
| 5414 | S.DeclareImplicitDestructor(RD); |
| 5415 | *Selected = RD->getDestructor(); |
| 5416 | } |
| 5417 | |
| 5418 | return false; |
| 5419 | |
| 5420 | case Sema::CXXCopyConstructor: |
| 5421 | // C++11 [class.copy]p12: |
| 5422 | // A copy constructor is trivial if: |
| 5423 | // - the constructor selected to copy each direct [subobject] is trivial |
| 5424 | if (RD->hasTrivialCopyConstructor()) { |
| 5425 | if (Quals == Qualifiers::Const) |
| 5426 | // We must either select the trivial copy constructor or reach an |
| 5427 | // ambiguity; no need to actually perform overload resolution. |
| 5428 | return true; |
| 5429 | } else if (!Selected) { |
| 5430 | return false; |
| 5431 | } |
| 5432 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5433 | // treat that as a language defect, as suggested on cxx-abi-dev, to treat |
| 5434 | // cases like B as having a non-trivial copy constructor: |
| 5435 | // struct A { template<typename T> A(T&); }; |
| 5436 | // struct B { mutable A a; }; |
| 5437 | goto NeedOverloadResolution; |
| 5438 | |
| 5439 | case Sema::CXXCopyAssignment: |
| 5440 | // C++11 [class.copy]p25: |
| 5441 | // A copy assignment operator is trivial if: |
| 5442 | // - the assignment operator selected to copy each direct [subobject] is |
| 5443 | // trivial |
| 5444 | if (RD->hasTrivialCopyAssignment()) { |
| 5445 | if (Quals == Qualifiers::Const) |
| 5446 | return true; |
| 5447 | } else if (!Selected) { |
| 5448 | return false; |
| 5449 | } |
| 5450 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5451 | // treat that as a language defect. |
| 5452 | goto NeedOverloadResolution; |
| 5453 | |
| 5454 | case Sema::CXXMoveConstructor: |
| 5455 | case Sema::CXXMoveAssignment: |
| 5456 | NeedOverloadResolution: |
| 5457 | Sema::SpecialMemberOverloadResult *SMOR = |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5458 | lookupCallFromSpecialMember(S, RD, CSM, Quals, ConstRHS); |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5459 | |
| 5460 | // The standard doesn't describe how to behave if the lookup is ambiguous. |
| 5461 | // We treat it as not making the member non-trivial, just like the standard |
| 5462 | // mandates for the default constructor. This should rarely matter, because |
| 5463 | // the member will also be deleted. |
| 5464 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5465 | return true; |
| 5466 | |
| 5467 | if (!SMOR->getMethod()) { |
| 5468 | assert(SMOR->getKind() == |
| 5469 | Sema::SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 5470 | return false; |
| 5471 | } |
| 5472 | |
| 5473 | // We deliberately don't check if we found a deleted special member. We're |
| 5474 | // not supposed to! |
| 5475 | if (Selected) |
| 5476 | *Selected = SMOR->getMethod(); |
| 5477 | return SMOR->getMethod()->isTrivial(); |
| 5478 | } |
| 5479 | |
| 5480 | llvm_unreachable("unknown special method kind"); |
| 5481 | } |
| 5482 | |
| Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5483 | static CXXConstructorDecl *findUserDeclaredCtor(CXXRecordDecl *RD) { |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 5484 | for (auto *CI : RD->ctors()) |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5485 | if (!CI->isImplicit()) |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 5486 | return CI; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5487 | |
| 5488 | // Look for constructor templates. |
| 5489 | typedef CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> tmpl_iter; |
| 5490 | for (tmpl_iter TI(RD->decls_begin()), TE(RD->decls_end()); TI != TE; ++TI) { |
| 5491 | if (CXXConstructorDecl *CD = |
| 5492 | dyn_cast<CXXConstructorDecl>(TI->getTemplatedDecl())) |
| 5493 | return CD; |
| 5494 | } |
| 5495 | |
| 5496 | return 0; |
| 5497 | } |
| 5498 | |
| 5499 | /// The kind of subobject we are checking for triviality. The values of this |
| 5500 | /// enumeration are used in diagnostics. |
| 5501 | enum TrivialSubobjectKind { |
| 5502 | /// The subobject is a base class. |
| 5503 | TSK_BaseClass, |
| 5504 | /// The subobject is a non-static data member. |
| 5505 | TSK_Field, |
| 5506 | /// The object is actually the complete object. |
| 5507 | TSK_CompleteObject |
| 5508 | }; |
| 5509 | |
| 5510 | /// Check whether the special member selected for a given type would be trivial. |
| 5511 | static bool checkTrivialSubobjectCall(Sema &S, SourceLocation SubobjLoc, |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5512 | QualType SubType, bool ConstRHS, |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5513 | Sema::CXXSpecialMember CSM, |
| 5514 | TrivialSubobjectKind Kind, |
| 5515 | bool Diagnose) { |
| 5516 | CXXRecordDecl *SubRD = SubType->getAsCXXRecordDecl(); |
| 5517 | if (!SubRD) |
| 5518 | return true; |
| 5519 | |
| 5520 | CXXMethodDecl *Selected; |
| 5521 | if (findTrivialSpecialMember(S, SubRD, CSM, SubType.getCVRQualifiers(), |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5522 | ConstRHS, Diagnose ? &Selected : 0)) |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5523 | return true; |
| 5524 | |
| 5525 | if (Diagnose) { |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5526 | if (ConstRHS) |
| 5527 | SubType.addConst(); |
| 5528 | |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5529 | if (!Selected && CSM == Sema::CXXDefaultConstructor) { |
| 5530 | S.Diag(SubobjLoc, diag::note_nontrivial_no_def_ctor) |
| 5531 | << Kind << SubType.getUnqualifiedType(); |
| 5532 | if (CXXConstructorDecl *CD = findUserDeclaredCtor(SubRD)) |
| 5533 | S.Diag(CD->getLocation(), diag::note_user_declared_ctor); |
| 5534 | } else if (!Selected) |
| 5535 | S.Diag(SubobjLoc, diag::note_nontrivial_no_copy) |
| 5536 | << Kind << SubType.getUnqualifiedType() << CSM << SubType; |
| 5537 | else if (Selected->isUserProvided()) { |
| 5538 | if (Kind == TSK_CompleteObject) |
| 5539 | S.Diag(Selected->getLocation(), diag::note_nontrivial_user_provided) |
| 5540 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5541 | else { |
| 5542 | S.Diag(SubobjLoc, diag::note_nontrivial_user_provided) |
| 5543 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5544 | S.Diag(Selected->getLocation(), diag::note_declared_at); |
| 5545 | } |
| 5546 | } else { |
| 5547 | if (Kind != TSK_CompleteObject) |
| 5548 | S.Diag(SubobjLoc, diag::note_nontrivial_subobject) |
| 5549 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5550 | |
| 5551 | // Explain why the defaulted or deleted special member isn't trivial. |
| 5552 | S.SpecialMemberIsTrivial(Selected, CSM, Diagnose); |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | return false; |
| 5557 | } |
| 5558 | |
| 5559 | /// Check whether the members of a class type allow a special member to be |
| 5560 | /// trivial. |
| 5561 | static bool checkTrivialClassMembers(Sema &S, CXXRecordDecl *RD, |
| 5562 | Sema::CXXSpecialMember CSM, |
| 5563 | bool ConstArg, bool Diagnose) { |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5564 | for (const auto *FI : RD->fields()) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5565 | if (FI->isInvalidDecl() || FI->isUnnamedBitfield()) |
| 5566 | continue; |
| 5567 | |
| 5568 | QualType FieldType = S.Context.getBaseElementType(FI->getType()); |
| 5569 | |
| 5570 | // Pretend anonymous struct or union members are members of this class. |
| 5571 | if (FI->isAnonymousStructOrUnion()) { |
| 5572 | if (!checkTrivialClassMembers(S, FieldType->getAsCXXRecordDecl(), |
| 5573 | CSM, ConstArg, Diagnose)) |
| 5574 | return false; |
| 5575 | continue; |
| 5576 | } |
| 5577 | |
| 5578 | // C++11 [class.ctor]p5: |
| 5579 | // A default constructor is trivial if [...] |
| 5580 | // -- no non-static data member of its class has a |
| 5581 | // brace-or-equal-initializer |
| 5582 | if (CSM == Sema::CXXDefaultConstructor && FI->hasInClassInitializer()) { |
| 5583 | if (Diagnose) |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5584 | S.Diag(FI->getLocation(), diag::note_nontrivial_in_class_init) << FI; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5585 | return false; |
| 5586 | } |
| 5587 | |
| 5588 | // Objective C ARC 4.3.5: |
| 5589 | // [...] nontrivally ownership-qualified types are [...] not trivially |
| 5590 | // default constructible, copy constructible, move constructible, copy |
| 5591 | // assignable, move assignable, or destructible [...] |
| 5592 | if (S.getLangOpts().ObjCAutoRefCount && |
| 5593 | FieldType.hasNonTrivialObjCLifetime()) { |
| 5594 | if (Diagnose) |
| 5595 | S.Diag(FI->getLocation(), diag::note_nontrivial_objc_ownership) |
| 5596 | << RD << FieldType.getObjCLifetime(); |
| 5597 | return false; |
| 5598 | } |
| 5599 | |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5600 | bool ConstRHS = ConstArg && !FI->isMutable(); |
| 5601 | if (!checkTrivialSubobjectCall(S, FI->getLocation(), FieldType, ConstRHS, |
| 5602 | CSM, TSK_Field, Diagnose)) |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5603 | return false; |
| 5604 | } |
| 5605 | |
| 5606 | return true; |
| 5607 | } |
| 5608 | |
| 5609 | /// Diagnose why the specified class does not have a trivial special member of |
| 5610 | /// the given kind. |
| 5611 | void Sema::DiagnoseNontrivial(const CXXRecordDecl *RD, CXXSpecialMember CSM) { |
| 5612 | QualType Ty = Context.getRecordType(RD); |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5613 | |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5614 | bool ConstArg = (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment); |
| 5615 | checkTrivialSubobjectCall(*this, RD->getLocation(), Ty, ConstArg, CSM, |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5616 | TSK_CompleteObject, /*Diagnose*/true); |
| 5617 | } |
| 5618 | |
| 5619 | /// Determine whether a defaulted or deleted special member function is trivial, |
| 5620 | /// as specified in C++11 [class.ctor]p5, C++11 [class.copy]p12, |
| 5621 | /// C++11 [class.copy]p25, and C++11 [class.dtor]p5. |
| 5622 | bool Sema::SpecialMemberIsTrivial(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5623 | bool Diagnose) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5624 | assert(!MD->isUserProvided() && CSM != CXXInvalid && "not special enough"); |
| 5625 | |
| 5626 | CXXRecordDecl *RD = MD->getParent(); |
| 5627 | |
| 5628 | bool ConstArg = false; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5629 | |
| Richard Smith | 2002bfe | 2013-11-04 02:02:27 +0000 | [diff] [blame] | 5630 | // C++11 [class.copy]p12, p25: [DR1593] |
| 5631 | // A [special member] is trivial if [...] its parameter-type-list is |
| 5632 | // equivalent to the parameter-type-list of an implicit declaration [...] |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5633 | switch (CSM) { |
| 5634 | case CXXDefaultConstructor: |
| 5635 | case CXXDestructor: |
| 5636 | // Trivial default constructors and destructors cannot have parameters. |
| 5637 | break; |
| 5638 | |
| 5639 | case CXXCopyConstructor: |
| 5640 | case CXXCopyAssignment: { |
| 5641 | // Trivial copy operations always have const, non-volatile parameter types. |
| 5642 | ConstArg = true; |
| Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5643 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5644 | const ReferenceType *RT = Param0->getType()->getAs<ReferenceType>(); |
| 5645 | if (!RT || RT->getPointeeType().getCVRQualifiers() != Qualifiers::Const) { |
| 5646 | if (Diagnose) |
| 5647 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5648 | << Param0->getSourceRange() << Param0->getType() |
| 5649 | << Context.getLValueReferenceType( |
| 5650 | Context.getRecordType(RD).withConst()); |
| 5651 | return false; |
| 5652 | } |
| 5653 | break; |
| 5654 | } |
| 5655 | |
| 5656 | case CXXMoveConstructor: |
| 5657 | case CXXMoveAssignment: { |
| 5658 | // Trivial move operations always have non-cv-qualified parameters. |
| Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5659 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5660 | const RValueReferenceType *RT = |
| 5661 | Param0->getType()->getAs<RValueReferenceType>(); |
| 5662 | if (!RT || RT->getPointeeType().getCVRQualifiers()) { |
| 5663 | if (Diagnose) |
| 5664 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5665 | << Param0->getSourceRange() << Param0->getType() |
| 5666 | << Context.getRValueReferenceType(Context.getRecordType(RD)); |
| 5667 | return false; |
| 5668 | } |
| 5669 | break; |
| 5670 | } |
| 5671 | |
| 5672 | case CXXInvalid: |
| 5673 | llvm_unreachable("not a special member"); |
| 5674 | } |
| 5675 | |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5676 | if (MD->getMinRequiredArguments() < MD->getNumParams()) { |
| 5677 | if (Diagnose) |
| 5678 | Diag(MD->getParamDecl(MD->getMinRequiredArguments())->getLocation(), |
| 5679 | diag::note_nontrivial_default_arg) |
| 5680 | << MD->getParamDecl(MD->getMinRequiredArguments())->getSourceRange(); |
| 5681 | return false; |
| 5682 | } |
| 5683 | if (MD->isVariadic()) { |
| 5684 | if (Diagnose) |
| 5685 | Diag(MD->getLocation(), diag::note_nontrivial_variadic); |
| 5686 | return false; |
| 5687 | } |
| 5688 | |
| 5689 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5690 | // A copy/move [constructor or assignment operator] is trivial if |
| 5691 | // -- the [member] selected to copy/move each direct base class subobject |
| 5692 | // is trivial |
| 5693 | // |
| 5694 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5695 | // A [default constructor or destructor] is trivial if |
| 5696 | // -- all the direct base classes have trivial [default constructors or |
| 5697 | // destructors] |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5698 | for (const auto &BI : RD->bases()) |
| 5699 | if (!checkTrivialSubobjectCall(*this, BI.getLocStart(), BI.getType(), |
| Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5700 | ConstArg, CSM, TSK_BaseClass, Diagnose)) |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5701 | return false; |
| 5702 | |
| 5703 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5704 | // A copy/move [constructor or assignment operator] for a class X is |
| 5705 | // trivial if |
| 5706 | // -- for each non-static data member of X that is of class type (or array |
| 5707 | // thereof), the constructor selected to copy/move that member is |
| 5708 | // trivial |
| 5709 | // |
| 5710 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5711 | // A [default constructor or destructor] is trivial if |
| 5712 | // -- for all of the non-static data members of its class that are of class |
| 5713 | // type (or array thereof), each such class has a trivial [default |
| 5714 | // constructor or destructor] |
| 5715 | if (!checkTrivialClassMembers(*this, RD, CSM, ConstArg, Diagnose)) |
| 5716 | return false; |
| 5717 | |
| 5718 | // C++11 [class.dtor]p5: |
| 5719 | // A destructor is trivial if [...] |
| 5720 | // -- the destructor is not virtual |
| 5721 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 5722 | if (Diagnose) |
| 5723 | Diag(MD->getLocation(), diag::note_nontrivial_virtual_dtor) << RD; |
| 5724 | return false; |
| 5725 | } |
| 5726 | |
| 5727 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5728 | // A [special member] for class X is trivial if [...] |
| 5729 | // -- class X has no virtual functions and no virtual base classes |
| 5730 | if (CSM != CXXDestructor && MD->getParent()->isDynamicClass()) { |
| 5731 | if (!Diagnose) |
| 5732 | return false; |
| 5733 | |
| 5734 | if (RD->getNumVBases()) { |
| 5735 | // Check for virtual bases. We already know that the corresponding |
| 5736 | // member in all bases is trivial, so vbases must all be direct. |
| 5737 | CXXBaseSpecifier &BS = *RD->vbases_begin(); |
| 5738 | assert(BS.isVirtual()); |
| 5739 | Diag(BS.getLocStart(), diag::note_nontrivial_has_virtual) << RD << 1; |
| 5740 | return false; |
| 5741 | } |
| 5742 | |
| 5743 | // Must have a virtual method. |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 5744 | for (const auto *MI : RD->methods()) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5745 | if (MI->isVirtual()) { |
| 5746 | SourceLocation MLoc = MI->getLocStart(); |
| 5747 | Diag(MLoc, diag::note_nontrivial_has_virtual) << RD << 0; |
| 5748 | return false; |
| 5749 | } |
| 5750 | } |
| 5751 | |
| 5752 | llvm_unreachable("dynamic class with no vbases and no virtual functions"); |
| 5753 | } |
| 5754 | |
| 5755 | // Looks like it's trivial! |
| 5756 | return true; |
| 5757 | } |
| 5758 | |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5759 | /// \brief Data used with FindHiddenVirtualMethod |
| Benjamin Kramer | 024e619 | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5760 | namespace { |
| 5761 | struct FindHiddenVirtualMethodData { |
| 5762 | Sema *S; |
| 5763 | CXXMethodDecl *Method; |
| 5764 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5765 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| Benjamin Kramer | 024e619 | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5766 | }; |
| 5767 | } |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5768 | |
| David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5769 | /// \brief Check whether any most overriden method from MD in Methods |
| 5770 | static bool CheckMostOverridenMethods(const CXXMethodDecl *MD, |
| 5771 | const llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5772 | if (MD->size_overridden_methods() == 0) |
| 5773 | return Methods.count(MD->getCanonicalDecl()); |
| 5774 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5775 | E = MD->end_overridden_methods(); |
| 5776 | I != E; ++I) |
| 5777 | if (CheckMostOverridenMethods(*I, Methods)) |
| 5778 | return true; |
| 5779 | return false; |
| 5780 | } |
| 5781 | |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5782 | /// \brief Member lookup function that determines whether a given C++ |
| 5783 | /// method overloads virtual methods in a base class without overriding any, |
| 5784 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 5785 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 5786 | CXXBasePath &Path, |
| 5787 | void *UserData) { |
| 5788 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 5789 | |
| 5790 | FindHiddenVirtualMethodData &Data |
| 5791 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 5792 | |
| 5793 | DeclarationName Name = Data.Method->getDeclName(); |
| 5794 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 5795 | |
| 5796 | bool foundSameNameMethod = false; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5797 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5798 | for (Path.Decls = BaseRecord->lookup(Name); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5799 | !Path.Decls.empty(); |
| 5800 | Path.Decls = Path.Decls.slice(1)) { |
| 5801 | NamedDecl *D = Path.Decls.front(); |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5802 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| Argyrios Kyrtzidis | 7dd856a | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 5803 | MD = MD->getCanonicalDecl(); |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5804 | foundSameNameMethod = true; |
| 5805 | // Interested only in hidden virtual methods. |
| 5806 | if (!MD->isVirtual()) |
| 5807 | continue; |
| 5808 | // If the method we are checking overrides a method from its base |
| 5809 | // don't warn about the other overloaded methods. |
| 5810 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 5811 | return true; |
| 5812 | // Collect the overload only if its hidden. |
| David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5813 | if (!CheckMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods)) |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5814 | overloadedMethods.push_back(MD); |
| 5815 | } |
| 5816 | } |
| 5817 | |
| 5818 | if (foundSameNameMethod) |
| 5819 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 5820 | overloadedMethods.end()); |
| 5821 | return foundSameNameMethod; |
| 5822 | } |
| 5823 | |
| David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5824 | /// \brief Add the most overriden methods from MD to Methods |
| 5825 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
| 5826 | llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5827 | if (MD->size_overridden_methods() == 0) |
| 5828 | Methods.insert(MD->getCanonicalDecl()); |
| 5829 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5830 | E = MD->end_overridden_methods(); |
| 5831 | I != E; ++I) |
| 5832 | AddMostOverridenMethods(*I, Methods); |
| 5833 | } |
| 5834 | |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5835 | /// \brief Check if a method overloads virtual methods in a base class without |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5836 | /// overriding any. |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5837 | void Sema::FindHiddenVirtualMethods(CXXMethodDecl *MD, |
| 5838 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
| Benjamin Kramer | 1458c1c | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 5839 | if (!MD->getDeclName().isIdentifier()) |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5840 | return; |
| 5841 | |
| 5842 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 5843 | /*bool RecordPaths=*/false, |
| 5844 | /*bool DetectVirtual=*/false); |
| 5845 | FindHiddenVirtualMethodData Data; |
| 5846 | Data.Method = MD; |
| 5847 | Data.S = this; |
| 5848 | |
| 5849 | // Keep the base methods that were overriden or introduced in the subclass |
| 5850 | // by 'using' in a set. A base method not in this set is hidden. |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5851 | CXXRecordDecl *DC = MD->getParent(); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5852 | DeclContext::lookup_result R = DC->lookup(MD->getDeclName()); |
| 5853 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 5854 | NamedDecl *ND = *I; |
| 5855 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*I)) |
| David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5856 | ND = shad->getTargetDecl(); |
| 5857 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 5858 | AddMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods); |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5861 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths)) |
| 5862 | OverloadedMethods = Data.OverloadedMethods; |
| 5863 | } |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5864 | |
| Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5865 | void Sema::NoteHiddenVirtualMethods(CXXMethodDecl *MD, |
| 5866 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
| 5867 | for (unsigned i = 0, e = OverloadedMethods.size(); i != e; ++i) { |
| 5868 | CXXMethodDecl *overloadedMD = OverloadedMethods[i]; |
| 5869 | PartialDiagnostic PD = PDiag( |
| 5870 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 5871 | HandleFunctionTypeMismatch(PD, MD->getType(), overloadedMD->getType()); |
| 5872 | Diag(overloadedMD->getLocation(), PD); |
| 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | /// \brief Diagnose methods which overload virtual methods in a base class |
| 5877 | /// without overriding any. |
| 5878 | void Sema::DiagnoseHiddenVirtualMethods(CXXMethodDecl *MD) { |
| 5879 | if (MD->isInvalidDecl()) |
| 5880 | return; |
| 5881 | |
| 5882 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
| 5883 | MD->getLocation()) == DiagnosticsEngine::Ignored) |
| 5884 | return; |
| 5885 | |
| 5886 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 5887 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 5888 | if (!OverloadedMethods.empty()) { |
| 5889 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 5890 | << MD << (OverloadedMethods.size() > 1); |
| 5891 | |
| 5892 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
| Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5893 | } |
| Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5894 | } |
| 5895 | |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5896 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5897 | Decl *TagDecl, |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5898 | SourceLocation LBrac, |
| Douglas Gregor | c48a10d | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 5899 | SourceLocation RBrac, |
| 5900 | AttributeList *AttrList) { |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5901 | if (!TagDecl) |
| 5902 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5903 | |
| Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5904 | AdjustDeclIfTemplate(TagDecl); |
| Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5905 | |
| Rafael Espindola | 06e1b13 | 2012-07-12 04:32:30 +0000 | [diff] [blame] | 5906 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 5907 | if (l->getKind() != AttributeList::AT_Visibility) |
| 5908 | continue; |
| 5909 | l->setInvalid(); |
| 5910 | Diag(l->getLoc(), diag::warn_attribute_after_definition_ignored) << |
| 5911 | l->getName(); |
| 5912 | } |
| 5913 | |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5914 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5915 | // strict aliasing violation! |
| 5916 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
| David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5917 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5918 | |
| Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5919 | CheckCompletedCXXClass( |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5920 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
| Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5921 | } |
| 5922 | |
| Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5923 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 5924 | /// special functions, such as the default constructor, copy |
| 5925 | /// constructor, or destructor, to the given C++ class (C++ |
| 5926 | /// [special]p1). This routine can only be executed just before the |
| 5927 | /// definition of the class is complete. |
| Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5928 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
| Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5929 | if (!ClassDecl->hasUserDeclaredConstructor()) |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5930 | ++ASTContext::NumImplicitDefaultConstructors; |
| Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5931 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5932 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5933 | ++ASTContext::NumImplicitCopyConstructors; |
| Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5934 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5935 | // If the properties or semantics of the copy constructor couldn't be |
| 5936 | // determined while the class was being declared, force a declaration |
| 5937 | // of it now. |
| 5938 | if (ClassDecl->needsOverloadResolutionForCopyConstructor()) |
| 5939 | DeclareImplicitCopyConstructor(ClassDecl); |
| 5940 | } |
| 5941 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5942 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveConstructor()) { |
| Richard Smith | 966c1fb | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5943 | ++ASTContext::NumImplicitMoveConstructors; |
| 5944 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5945 | if (ClassDecl->needsOverloadResolutionForMoveConstructor()) |
| 5946 | DeclareImplicitMoveConstructor(ClassDecl); |
| 5947 | } |
| 5948 | |
| Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5949 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 5950 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5951 | |
| 5952 | // If we have a dynamic class, then the copy assignment operator may be |
| Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5953 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5954 | // it shows up in the right place in the vtable and that we diagnose |
| 5955 | // problems with the implicit exception specification. |
| 5956 | if (ClassDecl->isDynamicClass() || |
| 5957 | ClassDecl->needsOverloadResolutionForCopyAssignment()) |
| Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5958 | DeclareImplicitCopyAssignment(ClassDecl); |
| 5959 | } |
| Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5960 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5961 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveAssignment()) { |
| Richard Smith | 966c1fb | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5962 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 5963 | |
| 5964 | // Likewise for the move assignment operator. |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5965 | if (ClassDecl->isDynamicClass() || |
| 5966 | ClassDecl->needsOverloadResolutionForMoveAssignment()) |
| Richard Smith | 966c1fb | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5967 | DeclareImplicitMoveAssignment(ClassDecl); |
| 5968 | } |
| 5969 | |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5970 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 5971 | ++ASTContext::NumImplicitDestructors; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5972 | |
| 5973 | // If we have a dynamic class, then the destructor may be virtual, so we |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5974 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 5975 | // shows up in the right place in the vtable and that we diagnose problems |
| 5976 | // with the implicit exception specification. |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5977 | if (ClassDecl->isDynamicClass() || |
| 5978 | ClassDecl->needsOverloadResolutionForDestructor()) |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5979 | DeclareImplicitDestructor(ClassDecl); |
| 5980 | } |
| Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5981 | } |
| 5982 | |
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 5983 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 5984 | if (!D) |
| 5985 | return; |
| 5986 | |
| 5987 | int NumParamList = D->getNumTemplateParameterLists(); |
| 5988 | for (int i = 0; i < NumParamList; i++) { |
| 5989 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 5990 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5991 | ParamEnd = Params->end(); |
| 5992 | Param != ParamEnd; ++Param) { |
| 5993 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5994 | if (Named->getDeclName()) { |
| 5995 | S->AddDecl(Named); |
| 5996 | IdResolver.AddDecl(Named); |
| 5997 | } |
| 5998 | } |
| 5999 | } |
| 6000 | } |
| 6001 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6002 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
| Douglas Gregor | e61ef62 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 6003 | if (!D) |
| 6004 | return; |
| 6005 | |
| 6006 | TemplateParameterList *Params = 0; |
| 6007 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 6008 | Params = Template->getTemplateParameters(); |
| 6009 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 6010 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 6011 | Params = PartialSpec->getTemplateParameters(); |
| 6012 | else |
| Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 6013 | return; |
| 6014 | |
| Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 6015 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 6016 | ParamEnd = Params->end(); |
| 6017 | Param != ParamEnd; ++Param) { |
| 6018 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 6019 | if (Named->getDeclName()) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6020 | S->AddDecl(Named); |
| Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 6021 | IdResolver.AddDecl(Named); |
| 6022 | } |
| 6023 | } |
| 6024 | } |
| 6025 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6026 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6027 | if (!RecordD) return; |
| 6028 | AdjustDeclIfTemplate(RecordD); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6029 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6030 | PushDeclContext(S, Record); |
| 6031 | } |
| 6032 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6033 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
| John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6034 | if (!RecordD) return; |
| 6035 | PopDeclContext(); |
| 6036 | } |
| 6037 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6038 | /// This is used to implement the constant expression evaluation part of the |
| 6039 | /// attribute enable_if extension. There is nothing in standard C++ which would |
| 6040 | /// require reentering parameters. |
| 6041 | void Sema::ActOnReenterCXXMethodParameter(Scope *S, ParmVarDecl *Param) { |
| 6042 | if (!Param) |
| 6043 | return; |
| 6044 | |
| 6045 | S->AddDecl(Param); |
| 6046 | if (Param->getDeclName()) |
| 6047 | IdResolver.AddDecl(Param); |
| 6048 | } |
| 6049 | |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6050 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 6051 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 6052 | /// parsing those parts of the given Method declaration that could |
| 6053 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 6054 | /// arguments. This action should enter the scope of the given |
| 6055 | /// Method declaration as if we had just parsed the qualified method |
| 6056 | /// name. However, it should not bring the parameters into scope; |
| 6057 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6058 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 6062 | /// C++ method declaration. We're (re-)introducing the given |
| 6063 | /// function parameter into scope for use in parsing later parts of |
| 6064 | /// the method declaration. For example, we could see an |
| 6065 | /// ActOnParamDefaultArgument event for this parameter. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6066 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 6067 | if (!ParamD) |
| 6068 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6069 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6070 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
| Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6071 | |
| 6072 | // If this parameter has an unparsed default argument, clear it out |
| 6073 | // to make way for the parsed default argument. |
| 6074 | if (Param->hasUnparsedDefaultArg()) |
| 6075 | Param->setDefaultArg(0); |
| 6076 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6077 | S->AddDecl(Param); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6078 | if (Param->getDeclName()) |
| 6079 | IdResolver.AddDecl(Param); |
| 6080 | } |
| 6081 | |
| 6082 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 6083 | /// processing the delayed method declaration for Method. The method |
| 6084 | /// declaration is now considered finished. There may be a separate |
| 6085 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 6086 | /// immediately!) for this method, if it was also defined inside the |
| 6087 | /// class body. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6088 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
| Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 6089 | if (!MethodD) |
| 6090 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6091 | |
| Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 6092 | AdjustDeclIfTemplate(MethodD); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6093 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6094 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6095 | |
| 6096 | // Now that we have our default arguments, check the constructor |
| 6097 | // again. It could produce additional diagnostics or affect whether |
| 6098 | // the class has implicitly-declared destructors, among other |
| 6099 | // things. |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6100 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 6101 | CheckConstructor(Constructor); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6102 | |
| 6103 | // Check the default arguments, which we may have added. |
| 6104 | if (!Method->isInvalidDecl()) |
| 6105 | CheckCXXDefaultArguments(Method); |
| 6106 | } |
| 6107 | |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6108 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6109 | /// the well-formedness of the constructor declarator @p D with type @p |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6110 | /// R. If there are any errors in the declarator, this routine will |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6111 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 6112 | /// will be updated to reflect a well-formed type for the constructor and |
| 6113 | /// returned. |
| 6114 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6115 | StorageClass &SC) { |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6116 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6117 | |
| 6118 | // C++ [class.ctor]p3: |
| 6119 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 6120 | // constructor can be invoked for a const, volatile or const |
| 6121 | // volatile object. A constructor shall not be declared const, |
| 6122 | // volatile, or const volatile (9.3.2). |
| 6123 | if (isVirtual) { |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6124 | if (!D.isInvalidType()) |
| 6125 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 6126 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 6127 | << SourceRange(D.getIdentifierLoc()); |
| 6128 | D.setInvalidType(); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6129 | } |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6130 | if (SC == SC_Static) { |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6131 | if (!D.isInvalidType()) |
| 6132 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 6133 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6134 | << SourceRange(D.getIdentifierLoc()); |
| 6135 | D.setInvalidType(); |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6136 | SC = SC_None; |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6137 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6138 | |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6139 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6140 | if (FTI.TypeQuals != 0) { |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6141 | if (FTI.TypeQuals & Qualifiers::Const) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6142 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6143 | << "const" << SourceRange(D.getIdentifierLoc()); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6144 | if (FTI.TypeQuals & Qualifiers::Volatile) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6145 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6146 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6147 | if (FTI.TypeQuals & Qualifiers::Restrict) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6148 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6149 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6150 | D.setInvalidType(); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6151 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6152 | |
| Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6153 | // C++0x [class.ctor]p4: |
| 6154 | // A constructor shall not be declared with a ref-qualifier. |
| 6155 | if (FTI.hasRefQualifier()) { |
| 6156 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 6157 | << FTI.RefQualifierIsLValueRef |
| 6158 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6159 | D.setInvalidType(); |
| 6160 | } |
| 6161 | |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6162 | // Rebuild the function type "R" without any type qualifiers (in |
| 6163 | // case any of the errors above fired) and with "void" as the |
| Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6164 | // return type, since constructors don't have return types. |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6165 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6166 | if (Proto->getReturnType() == Context.VoidTy && !D.isInvalidType()) |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6167 | return R; |
| 6168 | |
| 6169 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6170 | EPI.TypeQuals = 0; |
| Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6171 | EPI.RefQualifier = RQ_None; |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6172 | |
| 6173 | return Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6176 | /// CheckConstructor - Checks a fully-formed constructor for |
| 6177 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 6178 | /// the constructor declarator is invalid. |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6179 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6180 | CXXRecordDecl *ClassDecl |
| Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 6181 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 6182 | if (!ClassDecl) |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6183 | return Constructor->setInvalidDecl(); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6184 | |
| 6185 | // C++ [class.copy]p3: |
| 6186 | // A declaration of a constructor for a class X is ill-formed if |
| 6187 | // its first parameter is of type (optionally cv-qualified) X and |
| 6188 | // either there are no other parameters or else all other |
| 6189 | // parameters have default arguments. |
| Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 6190 | if (!Constructor->isInvalidDecl() && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6191 | ((Constructor->getNumParams() == 1) || |
| 6192 | (Constructor->getNumParams() > 1 && |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 6193 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 6194 | Constructor->getTemplateSpecializationKind() |
| 6195 | != TSK_ImplicitInstantiation) { |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6196 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 6197 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 6198 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6199 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| Douglas Gregor | fd42e95 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 6200 | const char *ConstRef |
| 6201 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 6202 | : " const &"; |
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6203 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
| Douglas Gregor | fd42e95 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 6204 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 6205 | |
| 6206 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 6207 | // to fix the type. |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6208 | Constructor->setInvalidDecl(); |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6209 | } |
| 6210 | } |
| Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
| John McCall | deb646e | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 6213 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 6214 | /// well-formedness, issuing any diagnostics required. Returns true |
| 6215 | /// on error. |
| Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6216 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6217 | CXXRecordDecl *RD = Destructor->getParent(); |
| 6218 | |
| Peter Collingbourne | b289fe6 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 6219 | if (!Destructor->getOperatorDelete() && Destructor->isVirtual()) { |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6220 | SourceLocation Loc; |
| 6221 | |
| 6222 | if (!Destructor->isImplicit()) |
| 6223 | Loc = Destructor->getLocation(); |
| 6224 | else |
| 6225 | Loc = RD->getLocation(); |
| 6226 | |
| 6227 | // If we have a virtual destructor, look up the deallocation function |
| 6228 | FunctionDecl *OperatorDelete = 0; |
| 6229 | DeclarationName Name = |
| 6230 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6231 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
| Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6232 | return true; |
| Richard Smith | f03bd30 | 2013-12-05 08:30:59 +0000 | [diff] [blame] | 6233 | // If there's no class-specific operator delete, look up the global |
| 6234 | // non-array delete. |
| 6235 | if (!OperatorDelete) |
| 6236 | OperatorDelete = FindUsualDeallocationFunction(Loc, true, Name); |
| John McCall | 1e5d75d | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 6237 | |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6238 | MarkFunctionReferenced(Loc, OperatorDelete); |
| Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6239 | |
| 6240 | Destructor->setOperatorDelete(OperatorDelete); |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6241 | } |
| Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6242 | |
| 6243 | return false; |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6246 | static inline bool |
| Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 6247 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 6248 | return (FTI.NumParams == 1 && !FTI.isVariadic && FTI.Params[0].Ident == 0 && |
| 6249 | FTI.Params[0].Param && |
| 6250 | cast<ParmVarDecl>(FTI.Params[0].Param)->getType()->isVoidType()); |
| Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6253 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 6254 | /// the well-formednes of the destructor declarator @p D with type @p |
| 6255 | /// R. If there are any errors in the declarator, this routine will |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6256 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 6257 | /// will be updated to reflect a well-formed type for the destructor and |
| 6258 | /// returned. |
| Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6259 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6260 | StorageClass& SC) { |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6261 | // C++ [class.dtor]p1: |
| 6262 | // [...] A typedef-name that names a class is a class-name |
| 6263 | // (7.1.3); however, a typedef-name that names a class shall not |
| 6264 | // be used as the identifier in the declarator for a destructor |
| 6265 | // declaration. |
| Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6266 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6267 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6268 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6269 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6270 | else if (const TemplateSpecializationType *TST = |
| 6271 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 6272 | if (TST->isTypeAlias()) |
| 6273 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 6274 | << DeclaratorType << 1; |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6275 | |
| 6276 | // C++ [class.dtor]p2: |
| 6277 | // A destructor is used to destroy objects of its class type. A |
| 6278 | // destructor takes no parameters, and no return type can be |
| 6279 | // specified for it (not even void). The address of a destructor |
| 6280 | // shall not be taken. A destructor shall not be static. A |
| 6281 | // destructor can be invoked for a const, volatile or const |
| 6282 | // volatile object. A destructor shall not be declared const, |
| 6283 | // volatile or const volatile (9.3.2). |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6284 | if (SC == SC_Static) { |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6285 | if (!D.isInvalidType()) |
| 6286 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 6287 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6288 | << SourceRange(D.getIdentifierLoc()) |
| 6289 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6290 | |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6291 | SC = SC_None; |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6292 | } |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6293 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6294 | // Destructors don't have return types, but the parser will |
| 6295 | // happily parse something like: |
| 6296 | // |
| 6297 | // class X { |
| 6298 | // float ~X(); |
| 6299 | // }; |
| 6300 | // |
| 6301 | // The return type will be eliminated later. |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6302 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 6303 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6304 | << SourceRange(D.getIdentifierLoc()); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6305 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6306 | |
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6307 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6308 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6309 | if (FTI.TypeQuals & Qualifiers::Const) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6310 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6311 | << "const" << SourceRange(D.getIdentifierLoc()); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6312 | if (FTI.TypeQuals & Qualifiers::Volatile) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6313 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6314 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6315 | if (FTI.TypeQuals & Qualifiers::Restrict) |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6316 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6317 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6318 | D.setInvalidType(); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
| Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6321 | // C++0x [class.dtor]p2: |
| 6322 | // A destructor shall not be declared with a ref-qualifier. |
| 6323 | if (FTI.hasRefQualifier()) { |
| 6324 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 6325 | << FTI.RefQualifierIsLValueRef |
| 6326 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6327 | D.setInvalidType(); |
| 6328 | } |
| 6329 | |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6330 | // Make sure we don't have any parameters. |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 6331 | if (FTI.NumParams > 0 && !FTIHasSingleVoidArgument(FTI)) { |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6332 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 6333 | |
| 6334 | // Delete the parameters. |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 6335 | FTI.freeParams(); |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6336 | D.setInvalidType(); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6339 | // Make sure the destructor isn't variadic. |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6340 | if (FTI.isVariadic) { |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6341 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
| Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6342 | D.setInvalidType(); |
| 6343 | } |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6344 | |
| 6345 | // Rebuild the function type "R" without any type qualifiers or |
| 6346 | // parameters (in case any of the errors above fired) and with |
| 6347 | // "void" as the return type, since destructors don't have return |
| Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6348 | // types. |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6349 | if (!D.isInvalidType()) |
| 6350 | return R; |
| 6351 | |
| Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6352 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6353 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6354 | EPI.Variadic = false; |
| 6355 | EPI.TypeQuals = 0; |
| Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6356 | EPI.RefQualifier = RQ_None; |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6357 | return Context.getFunctionType(Context.VoidTy, None, EPI); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6360 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 6361 | /// well-formednes of the conversion function declarator @p D with |
| 6362 | /// type @p R. If there are any errors in the declarator, this routine |
| 6363 | /// will emit diagnostics and return true. Otherwise, it will return |
| 6364 | /// false. Either way, the type @p R will be updated to reflect a |
| 6365 | /// well-formed type for the conversion operator. |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6366 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6367 | StorageClass& SC) { |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6368 | // C++ [class.conv.fct]p1: |
| 6369 | // Neither parameter types nor return type can be specified. The |
| Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6370 | // type of a conversion function (8.3.5) is "function taking no |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6371 | // parameter returning conversion-type-id." |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6372 | if (SC == SC_Static) { |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6373 | if (!D.isInvalidType()) |
| 6374 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| Eli Friedman | 600bc24 | 2013-06-20 20:58:02 +0000 | [diff] [blame] | 6375 | << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6376 | << D.getName().getSourceRange(); |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6377 | D.setInvalidType(); |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6378 | SC = SC_None; |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6379 | } |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6380 | |
| 6381 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 6382 | |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6383 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6384 | // Conversion functions don't have return types, but the parser will |
| 6385 | // happily parse something like: |
| 6386 | // |
| 6387 | // class X { |
| 6388 | // float operator bool(); |
| 6389 | // }; |
| 6390 | // |
| 6391 | // The return type will be changed later anyway. |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6392 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 6393 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6394 | << SourceRange(D.getIdentifierLoc()); |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6395 | D.setInvalidType(); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6398 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 6399 | |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6400 | // Make sure we don't have any parameters. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6401 | if (Proto->getNumParams() > 0) { |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6402 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 6403 | |
| 6404 | // Delete the parameters. |
| Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 6405 | D.getFunctionTypeInfo().freeParams(); |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6406 | D.setInvalidType(); |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6407 | } else if (Proto->isVariadic()) { |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6408 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6409 | D.setInvalidType(); |
| 6410 | } |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6411 | |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6412 | // Diagnose "&operator bool()" and other such nonsense. This |
| 6413 | // is actually a gcc extension which we don't support. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6414 | if (Proto->getReturnType() != ConvType) { |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6415 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6416 | << Proto->getReturnType(); |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6417 | D.setInvalidType(); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6418 | ConvType = Proto->getReturnType(); |
| John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6421 | // C++ [class.conv.fct]p4: |
| 6422 | // The conversion-type-id shall not represent a function type nor |
| 6423 | // an array type. |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6424 | if (ConvType->isArrayType()) { |
| 6425 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 6426 | ConvType = Context.getPointerType(ConvType); |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6427 | D.setInvalidType(); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6428 | } else if (ConvType->isFunctionType()) { |
| 6429 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 6430 | ConvType = Context.getPointerType(ConvType); |
| Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6431 | D.setInvalidType(); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | // Rebuild the function type "R" without any parameters (in case any |
| 6435 | // of the errors above fired) and with the conversion type as the |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6436 | // return type. |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6437 | if (D.isInvalidType()) |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6438 | R = Context.getFunctionType(ConvType, None, Proto->getExtProtoInfo()); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6439 | |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6440 | // C++0x explicit conversion operators. |
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6441 | if (D.getDeclSpec().isExplicitSpecified()) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6442 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6443 | getLangOpts().CPlusPlus11 ? |
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6444 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 6445 | diag::ext_explicit_conversion_functions) |
| Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6446 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6447 | } |
| 6448 | |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6449 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 6450 | /// the declaration of the given C++ conversion function. This routine |
| 6451 | /// is responsible for recording the conversion function in the C++ |
| 6452 | /// class, if possible. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6453 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6454 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 6455 | |
| Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 6456 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6457 | |
| 6458 | // Make sure we aren't redeclaring the conversion function. |
| 6459 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6460 | |
| 6461 | // C++ [class.conv.fct]p1: |
| 6462 | // [...] A conversion function is never used to convert a |
| 6463 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 6464 | // same object type (or a reference to it), to a (possibly |
| 6465 | // cv-qualified) base class of that type (or a reference to it), |
| 6466 | // or to (possibly cv-qualified) void. |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6467 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 6468 | // virtual function that overrides a virtual function in a base class. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6469 | QualType ClassType |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6470 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6471 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6472 | ConvType = ConvTypeRef->getPointeeType(); |
| Douglas Gregor | 6309e3d | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6473 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 6474 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
| Douglas Gregor | e47191c | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 6475 | /* Suppress diagnostics for instantiations. */; |
| Douglas Gregor | 6309e3d | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6476 | else if (ConvType->isRecordType()) { |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6477 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 6478 | if (ConvType == ClassType) |
| Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6479 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6480 | << ClassType; |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6481 | else if (IsDerivedFrom(ClassType, ConvType)) |
| Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6482 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6483 | << ClassType << ConvType; |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6484 | } else if (ConvType->isVoidType()) { |
| Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6485 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6486 | << ClassType << ConvType; |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6489 | if (FunctionTemplateDecl *ConversionTemplate |
| 6490 | = Conversion->getDescribedFunctionTemplate()) |
| 6491 | return ConversionTemplate; |
| 6492 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6493 | return Conversion; |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6494 | } |
| 6495 | |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6496 | //===----------------------------------------------------------------------===// |
| 6497 | // Namespace Handling |
| 6498 | //===----------------------------------------------------------------------===// |
| 6499 | |
| Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6500 | /// \brief Diagnose a mismatch in 'inline' qualifiers when a namespace is |
| 6501 | /// reopened. |
| 6502 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 6503 | SourceLocation Loc, |
| 6504 | IdentifierInfo *II, bool *IsInline, |
| 6505 | NamespaceDecl *PrevNS) { |
| 6506 | assert(*IsInline != PrevNS->isInline()); |
| John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6507 | |
| Richard Smith | f501cc3 | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6508 | // HACK: Work around a bug in libstdc++4.6's <atomic>, where |
| 6509 | // std::__atomic[0,1,2] are defined as non-inline namespaces, then reopened as |
| 6510 | // inline namespaces, with the intention of bringing names into namespace std. |
| 6511 | // |
| 6512 | // We support this just well enough to get that case working; this is not |
| 6513 | // sufficient to support reopening namespaces as inline in general. |
| Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6514 | if (*IsInline && II && II->getName().startswith("__atomic") && |
| 6515 | S.getSourceManager().isInSystemHeader(Loc)) { |
| Richard Smith | f501cc3 | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6516 | // Mark all prior declarations of the namespace as inline. |
| Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6517 | for (NamespaceDecl *NS = PrevNS->getMostRecentDecl(); NS; |
| 6518 | NS = NS->getPreviousDecl()) |
| 6519 | NS->setInline(*IsInline); |
| 6520 | // Patch up the lookup table for the containing namespace. This isn't really |
| 6521 | // correct, but it's good enough for this particular case. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 6522 | for (auto *I : PrevNS->decls()) |
| 6523 | if (auto *ND = dyn_cast<NamedDecl>(I)) |
| Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6524 | PrevNS->getParent()->makeDeclVisibleInContext(ND); |
| 6525 | return; |
| 6526 | } |
| 6527 | |
| 6528 | if (PrevNS->isInline()) |
| 6529 | // The user probably just forgot the 'inline', so suggest that it |
| 6530 | // be added back. |
| 6531 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 6532 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 6533 | else |
| Richard Smith | 5b5d21e | 2014-03-12 23:36:42 +0000 | [diff] [blame] | 6534 | S.Diag(Loc, diag::err_inline_namespace_mismatch) << *IsInline; |
| Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6535 | |
| 6536 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 6537 | *IsInline = PrevNS->isInline(); |
| 6538 | } |
| John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6539 | |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6540 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 6541 | /// definition. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6542 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 6543 | SourceLocation InlineLoc, |
| Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6544 | SourceLocation NamespaceLoc, |
| John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6545 | SourceLocation IdentLoc, |
| 6546 | IdentifierInfo *II, |
| 6547 | SourceLocation LBrace, |
| 6548 | AttributeList *AttrList) { |
| Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6549 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 6550 | // For anonymous namespace, take the location of the left brace. |
| 6551 | SourceLocation Loc = II ? IdentLoc : LBrace; |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6552 | bool IsInline = InlineLoc.isValid(); |
| Douglas Gregor | 21b3b29 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6553 | bool IsInvalid = false; |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6554 | bool IsStd = false; |
| 6555 | bool AddToKnown = false; |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6556 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 6557 | |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6558 | NamespaceDecl *PrevNS = 0; |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6559 | if (II) { |
| 6560 | // C++ [namespace.def]p2: |
| Douglas Gregor | 412c362 | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 6561 | // The identifier in an original-namespace-definition shall not |
| 6562 | // have been previously defined in the declarative region in |
| 6563 | // which the original-namespace-definition appears. The |
| 6564 | // identifier in an original-namespace-definition is the name of |
| 6565 | // the namespace. Subsequently in that declarative region, it is |
| 6566 | // treated as an original-namespace-name. |
| 6567 | // |
| 6568 | // Since namespace names are unique in their scope, and we don't |
| Douglas Gregor | b578fbe | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6569 | // look through using directives, just look for any ordinary names. |
| 6570 | |
| 6571 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6572 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 6573 | Decl::IDNS_Namespace; |
| Douglas Gregor | b578fbe | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6574 | NamedDecl *PrevDecl = 0; |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6575 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 6576 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 6577 | ++I) { |
| 6578 | if ((*I)->getIdentifierNamespace() & IDNS) { |
| 6579 | PrevDecl = *I; |
| Douglas Gregor | b578fbe | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6580 | break; |
| 6581 | } |
| 6582 | } |
| 6583 | |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6584 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 6585 | |
| 6586 | if (PrevNS) { |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6587 | // This is an extended namespace definition. |
| Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6588 | if (IsInline != PrevNS->isInline()) |
| 6589 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 6590 | &IsInline, PrevNS); |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6591 | } else if (PrevDecl) { |
| 6592 | // This is an invalid name redefinition. |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6593 | Diag(Loc, diag::err_redefinition_different_kind) |
| 6594 | << II; |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6595 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| Douglas Gregor | 21b3b29 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6596 | IsInvalid = true; |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6597 | // Continue on to push Namespc as current DeclContext and return it. |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6598 | } else if (II->isStr("std") && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6599 | CurContext->getRedeclContext()->isTranslationUnit()) { |
| Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 6600 | // This is the first "real" definition of the namespace "std", so update |
| 6601 | // our cache of the "std" namespace to point at this definition. |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6602 | PrevNS = getStdNamespace(); |
| 6603 | IsStd = true; |
| 6604 | AddToKnown = !IsInline; |
| 6605 | } else { |
| 6606 | // We've seen this namespace for the first time. |
| 6607 | AddToKnown = !IsInline; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6608 | } |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6609 | } else { |
| John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6610 | // Anonymous namespaces. |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6611 | |
| 6612 | // Determine whether the parent already has an anonymous namespace. |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6613 | DeclContext *Parent = CurContext->getRedeclContext(); |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6614 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6615 | PrevNS = TU->getAnonymousNamespace(); |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6616 | } else { |
| 6617 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6618 | PrevNS = ND->getAnonymousNamespace(); |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6619 | } |
| 6620 | |
| Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6621 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 6622 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 6623 | &IsInline, PrevNS); |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
| 6626 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 6627 | StartLoc, Loc, II, PrevNS); |
| Douglas Gregor | 21b3b29 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6628 | if (IsInvalid) |
| 6629 | Namespc->setInvalidDecl(); |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6630 | |
| 6631 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 6632 | |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6633 | // FIXME: Should we be merging attributes? |
| 6634 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
| Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6635 | PushNamespaceVisibilityAttr(Attr, Loc); |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6636 | |
| 6637 | if (IsStd) |
| 6638 | StdNamespace = Namespc; |
| 6639 | if (AddToKnown) |
| 6640 | KnownNamespaces[Namespc] = false; |
| 6641 | |
| 6642 | if (II) { |
| 6643 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 6644 | } else { |
| 6645 | // Link the anonymous namespace into its parent. |
| 6646 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 6647 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 6648 | TU->setAnonymousNamespace(Namespc); |
| 6649 | } else { |
| 6650 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6651 | } |
| John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6652 | |
| Douglas Gregor | f9f54ea | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 6653 | CurContext->addDecl(Namespc); |
| 6654 | |
| John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6655 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 6656 | // behaves as if it were replaced by |
| 6657 | // namespace unique { /* empty body */ } |
| 6658 | // using namespace unique; |
| 6659 | // namespace unique { namespace-body } |
| 6660 | // where all occurrences of 'unique' in a translation unit are |
| 6661 | // replaced by the same identifier and this identifier differs |
| 6662 | // from all other identifiers in the entire program. |
| 6663 | |
| 6664 | // We just create the namespace with an empty name and then add an |
| 6665 | // implicit using declaration, just like the standard suggests. |
| 6666 | // |
| 6667 | // CodeGen enforces the "universally unique" aspect by giving all |
| 6668 | // declarations semantically contained within an anonymous |
| 6669 | // namespace internal linkage. |
| 6670 | |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6671 | if (!PrevNS) { |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6672 | UsingDirectiveDecl* UD |
| Nick Lewycky | 3811582 | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6673 | = UsingDirectiveDecl::Create(Context, Parent, |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6674 | /* 'using' */ LBrace, |
| 6675 | /* 'namespace' */ SourceLocation(), |
| Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6676 | /* qualifier */ NestedNameSpecifierLoc(), |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6677 | /* identifier */ SourceLocation(), |
| 6678 | Namespc, |
| Nick Lewycky | 3811582 | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6679 | /* Ancestor */ Parent); |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6680 | UD->setImplicit(); |
| Nick Lewycky | 3811582 | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6681 | Parent->addDecl(UD); |
| John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6682 | } |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
| Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 6685 | ActOnDocumentableDecl(Namespc); |
| 6686 | |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6687 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 6688 | // redefinition), push it as current DeclContext and try to continue parsing. |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6689 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 6690 | // for the namespace has the declarations that showed up in that particular |
| 6691 | // namespace definition. |
| Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6692 | PushDeclContext(NamespcScope, Namespc); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6693 | return Namespc; |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6696 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 6697 | /// is a namespace alias, returns the namespace it points to. |
| 6698 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 6699 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 6700 | return AD->getNamespace(); |
| 6701 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 6702 | } |
| 6703 | |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6704 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 6705 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6706 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6707 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 6708 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6709 | Namespc->setRBraceLoc(RBrace); |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6710 | PopDeclContext(); |
| Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 6711 | if (Namespc->hasAttr<VisibilityAttr>()) |
| Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6712 | PopPragmaVisibility(true, RBrace); |
| Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6713 | } |
| Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6714 | |
| John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 6715 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 6716 | return cast_or_null<CXXRecordDecl>( |
| 6717 | StdBadAlloc.get(Context.getExternalSource())); |
| 6718 | } |
| 6719 | |
| 6720 | NamespaceDecl *Sema::getStdNamespace() const { |
| 6721 | return cast_or_null<NamespaceDecl>( |
| 6722 | StdNamespace.get(Context.getExternalSource())); |
| 6723 | } |
| 6724 | |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6725 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 6726 | /// implicitly define the namespace. |
| Argyrios Kyrtzidis | 4f8e173 | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6727 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6728 | if (!StdNamespace) { |
| 6729 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 6730 | StdNamespace = NamespaceDecl::Create(Context, |
| 6731 | Context.getTranslationUnitDecl(), |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6732 | /*Inline=*/false, |
| Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6733 | SourceLocation(), SourceLocation(), |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6734 | &PP.getIdentifierTable().get("std"), |
| 6735 | /*PrevDecl=*/0); |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6736 | getStdNamespace()->setImplicit(true); |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6737 | } |
| 6738 | |
| Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6739 | return getStdNamespace(); |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6742 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6743 | assert(getLangOpts().CPlusPlus && |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6744 | "Looking for std::initializer_list outside of C++."); |
| 6745 | |
| 6746 | // We're looking for implicit instantiations of |
| 6747 | // template <typename E> class std::initializer_list. |
| 6748 | |
| 6749 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 6750 | return false; |
| 6751 | |
| Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6752 | ClassTemplateDecl *Template = 0; |
| 6753 | const TemplateArgument *Arguments = 0; |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6754 | |
| Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6755 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6756 | |
| Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6757 | ClassTemplateSpecializationDecl *Specialization = |
| 6758 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 6759 | if (!Specialization) |
| 6760 | return false; |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6761 | |
| Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6762 | Template = Specialization->getSpecializedTemplate(); |
| 6763 | Arguments = Specialization->getTemplateArgs().data(); |
| 6764 | } else if (const TemplateSpecializationType *TST = |
| 6765 | Ty->getAs<TemplateSpecializationType>()) { |
| 6766 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 6767 | TST->getTemplateName().getAsTemplateDecl()); |
| 6768 | Arguments = TST->getArgs(); |
| 6769 | } |
| 6770 | if (!Template) |
| 6771 | return false; |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6772 | |
| 6773 | if (!StdInitializerList) { |
| 6774 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 6775 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 6776 | if (TemplateClass->getIdentifier() != |
| 6777 | &PP.getIdentifierTable().get("initializer_list") || |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6778 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 6779 | TemplateClass->getDeclContext())) |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6780 | return false; |
| 6781 | // This is a template called std::initializer_list, but is it the right |
| 6782 | // template? |
| 6783 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6784 | if (Params->getMinRequiredArguments() != 1) |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6785 | return false; |
| 6786 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 6787 | return false; |
| 6788 | |
| 6789 | // It's the right template. |
| 6790 | StdInitializerList = Template; |
| 6791 | } |
| 6792 | |
| 6793 | if (Template != StdInitializerList) |
| 6794 | return false; |
| 6795 | |
| 6796 | // This is an instance of std::initializer_list. Find the argument type. |
| Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6797 | if (Element) |
| 6798 | *Element = Arguments[0].getAsType(); |
| Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6799 | return true; |
| 6800 | } |
| 6801 | |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6802 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 6803 | NamespaceDecl *Std = S.getStdNamespace(); |
| 6804 | if (!Std) { |
| 6805 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6806 | return 0; |
| 6807 | } |
| 6808 | |
| 6809 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 6810 | Loc, Sema::LookupOrdinaryName); |
| 6811 | if (!S.LookupQualifiedName(Result, Std)) { |
| 6812 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6813 | return 0; |
| 6814 | } |
| 6815 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 6816 | if (!Template) { |
| 6817 | Result.suppressDiagnostics(); |
| 6818 | // We found something weird. Complain about the first thing we found. |
| 6819 | NamedDecl *Found = *Result.begin(); |
| 6820 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 6821 | return 0; |
| 6822 | } |
| 6823 | |
| 6824 | // We found some template called std::initializer_list. Now verify that it's |
| 6825 | // correct. |
| 6826 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6827 | if (Params->getMinRequiredArguments() != 1 || |
| 6828 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6829 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 6830 | return 0; |
| 6831 | } |
| 6832 | |
| 6833 | return Template; |
| 6834 | } |
| 6835 | |
| 6836 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 6837 | if (!StdInitializerList) { |
| 6838 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 6839 | if (!StdInitializerList) |
| 6840 | return QualType(); |
| 6841 | } |
| 6842 | |
| 6843 | TemplateArgumentListInfo Args(Loc, Loc); |
| 6844 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 6845 | Context.getTrivialTypeSourceInfo(Element, |
| 6846 | Loc))); |
| 6847 | return Context.getCanonicalType( |
| 6848 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 6849 | } |
| 6850 | |
| Sebastian Redl | be24ec2 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 6851 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 6852 | // C++ [dcl.init.list]p2: |
| 6853 | // A constructor is an initializer-list constructor if its first parameter |
| 6854 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 6855 | // std::initializer_list<E> for some type E, and either there are no other |
| 6856 | // parameters or else all other parameters have default arguments. |
| 6857 | if (Ctor->getNumParams() < 1 || |
| 6858 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 6859 | return false; |
| 6860 | |
| 6861 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 6862 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 6863 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 6864 | |
| 6865 | return isStdInitializerList(ArgType, 0); |
| 6866 | } |
| 6867 | |
| Douglas Gregor | a172e08 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6868 | /// \brief Determine whether a using statement is in a context where it will be |
| 6869 | /// apply in all contexts. |
| 6870 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 6871 | switch (CurContext->getDeclKind()) { |
| 6872 | case Decl::TranslationUnit: |
| 6873 | return true; |
| 6874 | case Decl::LinkageSpec: |
| 6875 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 6876 | default: |
| 6877 | return false; |
| 6878 | } |
| 6879 | } |
| 6880 | |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6881 | namespace { |
| 6882 | |
| 6883 | // Callback to only accept typo corrections that are namespaces. |
| 6884 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 6885 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 6886 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 6887 | if (NamedDecl *ND = candidate.getCorrectionDecl()) |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6888 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6889 | return false; |
| 6890 | } |
| 6891 | }; |
| 6892 | |
| 6893 | } |
| 6894 | |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6895 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 6896 | CXXScopeSpec &SS, |
| 6897 | SourceLocation IdentLoc, |
| 6898 | IdentifierInfo *Ident) { |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6899 | NamespaceValidatorCCC Validator; |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6900 | R.clear(); |
| 6901 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6902 | R.getLookupKind(), Sc, &SS, |
| John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 6903 | Validator, |
| 6904 | Sema::CTK_ErrorRecovery)) { |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6905 | if (DeclContext *DC = S.computeDeclContext(SS, false)) { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6906 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 6907 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6908 | Ident->getName().equals(CorrectedStr); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6909 | S.diagnoseTypo(Corrected, |
| 6910 | S.PDiag(diag::err_using_directive_member_suggest) |
| 6911 | << Ident << DC << DroppedSpecifier << SS.getRange(), |
| 6912 | S.PDiag(diag::note_namespace_defined_here)); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6913 | } else { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6914 | S.diagnoseTypo(Corrected, |
| 6915 | S.PDiag(diag::err_using_directive_suggest) << Ident, |
| 6916 | S.PDiag(diag::note_namespace_defined_here)); |
| Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6917 | } |
| Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6918 | R.addDecl(Corrected.getCorrectionDecl()); |
| 6919 | return true; |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6920 | } |
| 6921 | return false; |
| 6922 | } |
| 6923 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6924 | Decl *Sema::ActOnUsingDirective(Scope *S, |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6925 | SourceLocation UsingLoc, |
| 6926 | SourceLocation NamespcLoc, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6927 | CXXScopeSpec &SS, |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6928 | SourceLocation IdentLoc, |
| 6929 | IdentifierInfo *NamespcName, |
| 6930 | AttributeList *AttrList) { |
| Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6931 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 6932 | assert(NamespcName && "Invalid NamespcName."); |
| 6933 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
| John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6934 | |
| 6935 | // This can only happen along a recovery path. |
| 6936 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6937 | S = S->getParent(); |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6938 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
| Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6939 | |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6940 | UsingDirectiveDecl *UDir = 0; |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6941 | NestedNameSpecifier *Qualifier = 0; |
| 6942 | if (SS.isSet()) |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 6943 | Qualifier = SS.getScopeRep(); |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6944 | |
| Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 6945 | // Lookup namespace name. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6946 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 6947 | LookupParsedName(R, S, &SS); |
| 6948 | if (R.isAmbiguous()) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6949 | return 0; |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6950 | |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6951 | if (R.empty()) { |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6952 | R.clear(); |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6953 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 6954 | // "std" hasn't been defined yet, for GCC compatibility. |
| 6955 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 6956 | NamespcName->isStr("std")) { |
| 6957 | Diag(IdentLoc, diag::ext_using_undefined_std); |
| Argyrios Kyrtzidis | 4f8e173 | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6958 | R.addDecl(getOrCreateStdNamespace()); |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6959 | R.resolveKind(); |
| 6960 | } |
| 6961 | // Otherwise, attempt typo correction. |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6962 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
| Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6963 | } |
| 6964 | |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6965 | if (!R.empty()) { |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6966 | NamedDecl *Named = R.getFoundDecl(); |
| 6967 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 6968 | && "expected namespace decl"); |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6969 | // C++ [namespace.udir]p1: |
| 6970 | // A using-directive specifies that the names in the nominated |
| 6971 | // namespace can be used in the scope in which the |
| 6972 | // using-directive appears after the using-directive. During |
| 6973 | // unqualified name lookup (3.4.1), the names appear as if they |
| 6974 | // were declared in the nearest enclosing namespace which |
| 6975 | // contains both the using-directive and the nominated |
| Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6976 | // namespace. [Note: in this context, "contains" means "contains |
| 6977 | // directly or indirectly". ] |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6978 | |
| 6979 | // Find enclosing context containing both using-directive and |
| 6980 | // nominated namespace. |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6981 | NamespaceDecl *NS = getNamespaceDecl(Named); |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6982 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 6983 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 6984 | CommonAncestor = CommonAncestor->getParent(); |
| 6985 | |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6986 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
| Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6987 | SS.getWithLocInContext(Context), |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6988 | IdentLoc, Named, CommonAncestor); |
| Douglas Gregor | 96a4bdd | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6989 | |
| Douglas Gregor | a172e08 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6990 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
| Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 6991 | !SourceMgr.isInMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
| Douglas Gregor | 96a4bdd | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6992 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 6993 | } |
| 6994 | |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6995 | PushUsingDirective(S, UDir); |
| Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6996 | } else { |
| Chris Lattner | 8dca2e9 | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 6997 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
| Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6998 | } |
| 6999 | |
| Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7000 | if (UDir) |
| 7001 | ProcessDeclAttributeList(S, UDir, AttrList); |
| 7002 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7003 | return UDir; |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7004 | } |
| 7005 | |
| 7006 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7007 | // If the scope has an associated entity and the using directive is at |
| 7008 | // namespace or translation unit scope, add the UsingDirectiveDecl into |
| 7009 | // its lookup structure so qualified name lookup can find it. |
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7010 | DeclContext *Ctx = S->getEntity(); |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7011 | if (Ctx && !Ctx->isFunctionOrMethod()) |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 7012 | Ctx->addDecl(UDir); |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7013 | else |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7014 | // Otherwise, it is at block sope. The using-directives will affect lookup |
| 7015 | // only to the end of the scope. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7016 | S->PushUsingDirective(UDir); |
| Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7017 | } |
| Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7018 | |
| Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7019 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7020 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
| John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 7021 | AccessSpecifier AS, |
| 7022 | bool HasUsingKeyword, |
| 7023 | SourceLocation UsingLoc, |
| 7024 | CXXScopeSpec &SS, |
| 7025 | UnqualifiedId &Name, |
| 7026 | AttributeList *AttrList, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7027 | bool HasTypenameKeyword, |
| John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 7028 | SourceLocation TypenameLoc) { |
| Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7029 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7030 | |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7031 | switch (Name.getKind()) { |
| Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 7032 | case UnqualifiedId::IK_ImplicitSelfParam: |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7033 | case UnqualifiedId::IK_Identifier: |
| 7034 | case UnqualifiedId::IK_OperatorFunctionId: |
| Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 7035 | case UnqualifiedId::IK_LiteralOperatorId: |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7036 | case UnqualifiedId::IK_ConversionFunctionId: |
| 7037 | break; |
| 7038 | |
| 7039 | case UnqualifiedId::IK_ConstructorName: |
| Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 7040 | case UnqualifiedId::IK_ConstructorTemplateId: |
| Richard Smith | d494c50 | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 7041 | // C++11 inheriting constructors. |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7042 | Diag(Name.getLocStart(), |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7043 | getLangOpts().CPlusPlus11 ? |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7044 | diag::warn_cxx98_compat_using_decl_constructor : |
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7045 | diag::err_using_decl_constructor) |
| 7046 | << SS.getRange(); |
| 7047 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7048 | if (getLangOpts().CPlusPlus11) break; |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7049 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7050 | return 0; |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7051 | |
| 7052 | case UnqualifiedId::IK_DestructorName: |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7053 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7054 | << SS.getRange(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7055 | return 0; |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7056 | |
| 7057 | case UnqualifiedId::IK_TemplateId: |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7058 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7059 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7060 | return 0; |
| Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7061 | } |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7062 | |
| 7063 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 7064 | DeclarationName TargetName = TargetNameInfo.getName(); |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7065 | if (!TargetName) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7066 | return 0; |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7067 | |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7068 | // Warn about access declarations. |
| John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 7069 | if (!HasUsingKeyword) { |
| Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 7070 | Diag(Name.getLocStart(), |
| Richard Smith | f026b60 | 2013-06-13 02:12:17 +0000 | [diff] [blame] | 7071 | getLangOpts().CPlusPlus11 ? diag::err_access_decl |
| 7072 | : diag::warn_access_decl_deprecated) |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7073 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
| John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 7074 | } |
| 7075 | |
| Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 7076 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 7077 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 7078 | return 0; |
| 7079 | |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7080 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7081 | TargetNameInfo, AttrList, |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7082 | /* IsInstantiation */ false, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7083 | HasTypenameKeyword, TypenameLoc); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7084 | if (UD) |
| 7085 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7086 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7087 | return UD; |
| Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7088 | } |
| 7089 | |
| Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7090 | /// \brief Determine whether a using declaration considers the given |
| 7091 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 7092 | /// the same entity or are both typedefs of the same type. |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7093 | static bool |
| 7094 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2) { |
| 7095 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) |
| Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7096 | return true; |
| Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7097 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7098 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7099 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) |
| Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7100 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 7101 | TD2->getUnderlyingType()); |
| Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7102 | |
| 7103 | return false; |
| 7104 | } |
| 7105 | |
| 7106 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7107 | /// Determines whether to create a using shadow decl for a particular |
| 7108 | /// decl, given the set of decls existing prior to this using lookup. |
| 7109 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7110 | const LookupResult &Previous, |
| 7111 | UsingShadowDecl *&PrevShadow) { |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7112 | // Diagnose finding a decl which is not from a base class of the |
| 7113 | // current class. We do this now because there are cases where this |
| 7114 | // function will silently decide not to build a shadow decl, which |
| 7115 | // will pre-empt further diagnostics. |
| 7116 | // |
| 7117 | // We don't need to do this in C++0x because we do the check once on |
| 7118 | // the qualifier. |
| 7119 | // |
| 7120 | // FIXME: diagnose the following if we care enough: |
| 7121 | // struct A { int foo; }; |
| 7122 | // struct B : A { using A::foo; }; |
| 7123 | // template <class T> struct C : A {}; |
| 7124 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 7125 | // This is invalid (during instantiation) in C++03 because B::foo |
| 7126 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 7127 | // We can't diagnose it immediately because C<T> is an unknown |
| 7128 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 7129 | // to A::foo, which will look well-formed when we instantiate. |
| 7130 | // The right solution is to not collapse the shadow-decl chain. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7131 | if (!getLangOpts().CPlusPlus11 && CurContext->isRecord()) { |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7132 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 7133 | |
| 7134 | // Handle enums and anonymous structs. |
| 7135 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 7136 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 7137 | while (OrigRec->isAnonymousStructOrUnion()) |
| 7138 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 7139 | |
| 7140 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 7141 | if (OrigDC == CurContext) { |
| 7142 | Diag(Using->getLocation(), |
| 7143 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7144 | << Using->getQualifierLoc().getSourceRange(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7145 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 7146 | return true; |
| 7147 | } |
| 7148 | |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7149 | Diag(Using->getQualifierLoc().getBeginLoc(), |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7150 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7151 | << Using->getQualifier() |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7152 | << cast<CXXRecordDecl>(CurContext) |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7153 | << Using->getQualifierLoc().getSourceRange(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7154 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 7155 | return true; |
| 7156 | } |
| 7157 | } |
| 7158 | |
| 7159 | if (Previous.empty()) return false; |
| 7160 | |
| 7161 | NamedDecl *Target = Orig; |
| 7162 | if (isa<UsingShadowDecl>(Target)) |
| 7163 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 7164 | |
| John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7165 | // If the target happens to be one of the previous declarations, we |
| 7166 | // don't have a conflict. |
| 7167 | // |
| 7168 | // FIXME: but we might be increasing its access, in which case we |
| 7169 | // should redeclare it. |
| 7170 | NamedDecl *NonTag = 0, *Tag = 0; |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7171 | bool FoundEquivalentDecl = false; |
| John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7172 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7173 | I != E; ++I) { |
| 7174 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7175 | if (IsEquivalentForUsingDecl(Context, D, Target)) { |
| 7176 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(*I)) |
| 7177 | PrevShadow = Shadow; |
| 7178 | FoundEquivalentDecl = true; |
| 7179 | } |
| John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7180 | |
| 7181 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 7182 | } |
| 7183 | |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7184 | if (FoundEquivalentDecl) |
| 7185 | return false; |
| 7186 | |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7187 | if (FunctionDecl *FD = Target->getAsFunction()) { |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7188 | NamedDecl *OldDecl = 0; |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 7189 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7190 | case Ovl_Overload: |
| 7191 | return false; |
| 7192 | |
| 7193 | case Ovl_NonFunction: |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7194 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7195 | break; |
| Richard Smith | 1881930 | 2014-02-06 01:31:33 +0000 | [diff] [blame] | 7196 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7197 | // We found a decl with the exact signature. |
| 7198 | case Ovl_Match: |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7199 | // If we're in a record, we want to hide the target, so we |
| 7200 | // return true (without a diagnostic) to tell the caller not to |
| 7201 | // build a shadow decl. |
| 7202 | if (CurContext->isRecord()) |
| 7203 | return true; |
| 7204 | |
| 7205 | // If we're not in a record, this is an error. |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7206 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7207 | break; |
| 7208 | } |
| 7209 | |
| 7210 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7211 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 7212 | return true; |
| 7213 | } |
| 7214 | |
| 7215 | // Target is not a function. |
| 7216 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7217 | if (isa<TagDecl>(Target)) { |
| 7218 | // No conflict between a tag and a non-tag. |
| 7219 | if (!Tag) return false; |
| 7220 | |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7221 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7222 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7223 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 7224 | return true; |
| 7225 | } |
| 7226 | |
| 7227 | // No conflict between a tag and a non-tag. |
| 7228 | if (!NonTag) return false; |
| 7229 | |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7230 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7231 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7232 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 7233 | return true; |
| 7234 | } |
| 7235 | |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7236 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7237 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7238 | UsingDecl *UD, |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7239 | NamedDecl *Orig, |
| 7240 | UsingShadowDecl *PrevDecl) { |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7241 | |
| 7242 | // If we resolved to another shadow declaration, just coalesce them. |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7243 | NamedDecl *Target = Orig; |
| 7244 | if (isa<UsingShadowDecl>(Target)) { |
| 7245 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 7246 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7247 | } |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7248 | |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7249 | UsingShadowDecl *Shadow |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7250 | = UsingShadowDecl::Create(Context, CurContext, |
| 7251 | UD->getLocation(), UD, Target); |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7252 | UD->addShadowDecl(Shadow); |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7253 | |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 7254 | Shadow->setAccess(UD->getAccess()); |
| 7255 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 7256 | Shadow->setInvalidDecl(); |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7257 | |
| 7258 | Shadow->setPreviousDecl(PrevDecl); |
| 7259 | |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7260 | if (S) |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7261 | PushOnScopeChains(Shadow, S); |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7262 | else |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7263 | CurContext->addDecl(Shadow); |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7264 | |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7265 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7266 | return Shadow; |
| 7267 | } |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7268 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7269 | /// Hides a using shadow declaration. This is required by the current |
| 7270 | /// using-decl implementation when a resolvable using declaration in a |
| 7271 | /// class is followed by a declaration which would hide or override |
| 7272 | /// one or more of the using decl's targets; for example: |
| 7273 | /// |
| 7274 | /// struct Base { void foo(int); }; |
| 7275 | /// struct Derived : Base { |
| 7276 | /// using Base::foo; |
| 7277 | /// void foo(int); |
| 7278 | /// }; |
| 7279 | /// |
| 7280 | /// The governing language is C++03 [namespace.udecl]p12: |
| 7281 | /// |
| 7282 | /// When a using-declaration brings names from a base class into a |
| 7283 | /// derived class scope, member functions in the derived class |
| 7284 | /// override and/or hide member functions with the same name and |
| 7285 | /// parameter types in a base class (rather than conflicting). |
| 7286 | /// |
| 7287 | /// There are two ways to implement this: |
| 7288 | /// (1) optimistically create shadow decls when they're not hidden |
| 7289 | /// by existing declarations, or |
| 7290 | /// (2) don't create any shadow decls (or at least don't make them |
| 7291 | /// visible) until we've fully parsed/instantiated the class. |
| 7292 | /// The problem with (1) is that we might have to retroactively remove |
| 7293 | /// a shadow decl, which requires several O(n) operations because the |
| 7294 | /// decl structures are (very reasonably) not designed for removal. |
| 7295 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 7296 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
| John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7297 | if (Shadow->getDeclName().getNameKind() == |
| 7298 | DeclarationName::CXXConversionFunctionName) |
| 7299 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 7300 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7301 | // Remove it from the DeclContext... |
| 7302 | Shadow->getDeclContext()->removeDecl(Shadow); |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7303 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7304 | // ...and the scope, if applicable... |
| 7305 | if (S) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7306 | S->RemoveDecl(Shadow); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7307 | IdResolver.RemoveDecl(Shadow); |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7308 | } |
| 7309 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7310 | // ...and the using decl. |
| 7311 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 7312 | |
| 7313 | // TODO: complain somehow if Shadow was used. It shouldn't |
| John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7314 | // be possible for this to happen, because...? |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7315 | } |
| 7316 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7317 | namespace { |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7318 | class UsingValidatorCCC : public CorrectionCandidateCallback { |
| 7319 | public: |
| Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7320 | UsingValidatorCCC(bool HasTypenameKeyword, bool IsInstantiation, |
| 7321 | bool RequireMember) |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7322 | : HasTypenameKeyword(HasTypenameKeyword), |
| Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7323 | IsInstantiation(IsInstantiation), RequireMember(RequireMember) {} |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7324 | |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 7325 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7326 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 7327 | |
| 7328 | // Keywords are not valid here. |
| 7329 | if (!ND || isa<NamespaceDecl>(ND)) |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7330 | return false; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7331 | |
| Richard Smith | 30a615d | 2014-04-30 17:40:35 +0000 | [diff] [blame] | 7332 | // FIXME: We should check if ND is member of base class of class having |
| 7333 | // using declaration and direct base class in case using declaration names |
| 7334 | // a constructor. |
| 7335 | if (RequireMember && !ND->isCXXClassMember()) |
| 7336 | return false; |
| 7337 | |
| Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7338 | if (RequireMember && !isa<FieldDecl>(ND) && !isa<CXXMethodDecl>(ND) && |
| 7339 | !isa<TypeDecl>(ND)) |
| 7340 | return false; |
| 7341 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7342 | // Completely unqualified names are invalid for a 'using' declaration. |
| 7343 | if (Candidate.WillReplaceSpecifier() && !Candidate.getCorrectionSpecifier()) |
| 7344 | return false; |
| 7345 | |
| 7346 | if (isa<TypeDecl>(ND)) |
| 7347 | return HasTypenameKeyword || !IsInstantiation; |
| 7348 | |
| 7349 | return !HasTypenameKeyword; |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | private: |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7353 | bool HasTypenameKeyword; |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7354 | bool IsInstantiation; |
| Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7355 | bool RequireMember; |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7356 | }; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7357 | } // end anonymous namespace |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7358 | |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7359 | /// Builds a using declaration. |
| 7360 | /// |
| 7361 | /// \param IsInstantiation - Whether this call arises from an |
| 7362 | /// instantiation of an unresolved using declaration. We treat |
| 7363 | /// the lookup differently for these declarations. |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7364 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 7365 | SourceLocation UsingLoc, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7366 | CXXScopeSpec &SS, |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7367 | const DeclarationNameInfo &NameInfo, |
| Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7368 | AttributeList *AttrList, |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7369 | bool IsInstantiation, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7370 | bool HasTypenameKeyword, |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7371 | SourceLocation TypenameLoc) { |
| Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7372 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7373 | SourceLocation IdentLoc = NameInfo.getLoc(); |
| Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7374 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
| Eli Friedman | 561154d | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 7375 | |
| Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7376 | // FIXME: We ignore attributes for now. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7377 | |
| Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7378 | if (SS.isEmpty()) { |
| 7379 | Diag(IdentLoc, diag::err_using_requires_qualname); |
| Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7380 | return 0; |
| Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7381 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7382 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7383 | // Do the redeclaration lookup in the current scope. |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7384 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7385 | ForRedeclaration); |
| 7386 | Previous.setHideTags(false); |
| 7387 | if (S) { |
| 7388 | LookupName(Previous, S); |
| 7389 | |
| 7390 | // It is really dumb that we have to do this. |
| 7391 | LookupResult::Filter F = Previous.makeFilter(); |
| 7392 | while (F.hasNext()) { |
| 7393 | NamedDecl *D = F.next(); |
| 7394 | if (!isDeclInScope(D, CurContext, S)) |
| 7395 | F.erase(); |
| Richard Smith | 83e78f5 | 2014-04-11 01:03:38 +0000 | [diff] [blame] | 7396 | // If we found a local extern declaration that's not ordinarily visible, |
| 7397 | // and this declaration is being added to a non-block scope, ignore it. |
| 7398 | // We're only checking for scope conflicts here, not also for violations |
| 7399 | // of the linkage rules. |
| 7400 | else if (!CurContext->isFunctionOrMethod() && D->isLocalExternDecl() && |
| 7401 | !(D->getIdentifierNamespace() & Decl::IDNS_Ordinary)) |
| 7402 | F.erase(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7403 | } |
| 7404 | F.done(); |
| 7405 | } else { |
| 7406 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 7407 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 7408 | LookupQualifiedName(Previous, CurContext); |
| 7409 | } |
| 7410 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7411 | // Check for invalid redeclarations. |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7412 | if (CheckUsingDeclRedeclaration(UsingLoc, HasTypenameKeyword, |
| 7413 | SS, IdentLoc, Previous)) |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7414 | return 0; |
| 7415 | |
| 7416 | // Check for bad qualifiers. |
| Richard Smith | 7ad0b88 | 2014-04-02 21:44:35 +0000 | [diff] [blame] | 7417 | if (CheckUsingDeclQualifier(UsingLoc, SS, NameInfo, IdentLoc)) |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7418 | return 0; |
| 7419 | |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7420 | DeclContext *LookupContext = computeDeclContext(SS); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7421 | NamedDecl *D; |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7422 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7423 | if (!LookupContext) { |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7424 | if (HasTypenameKeyword) { |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7425 | // FIXME: not all declaration name kinds are legal here |
| 7426 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 7427 | UsingLoc, TypenameLoc, |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7428 | QualifierLoc, |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7429 | IdentLoc, NameInfo.getName()); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7430 | } else { |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7431 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 7432 | QualifierLoc, NameInfo); |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7433 | } |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7434 | } else { |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7435 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7436 | NameInfo, HasTypenameKeyword); |
| Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7437 | } |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7438 | D->setAccess(AS); |
| 7439 | CurContext->addDecl(D); |
| 7440 | |
| 7441 | if (!LookupContext) return D; |
| 7442 | UsingDecl *UD = cast<UsingDecl>(D); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7443 | |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7444 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7445 | UD->setInvalidDecl(); |
| 7446 | return UD; |
| Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7447 | } |
| 7448 | |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7449 | // The normal rules do not apply to inheriting constructor declarations. |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7450 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7451 | if (CheckInheritingConstructorUsingDecl(UD)) |
| Sebastian Redl | c1f8e49 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 7452 | UD->setInvalidDecl(); |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7453 | return UD; |
| 7454 | } |
| 7455 | |
| 7456 | // Otherwise, look up the target name. |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7457 | |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7458 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7459 | |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7460 | // Unlike most lookups, we don't always want to hide tag |
| 7461 | // declarations: tag names are visible through the using declaration |
| 7462 | // even if hidden by ordinary names, *except* in a dependent context |
| 7463 | // where it's important for the sanity of two-phase lookup. |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7464 | if (!IsInstantiation) |
| 7465 | R.setHideTags(false); |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7466 | |
| John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 7467 | // For the purposes of this lookup, we have a base object type |
| 7468 | // equal to that of the current context. |
| 7469 | if (CurContext->isRecord()) { |
| 7470 | R.setBaseObjectType( |
| 7471 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 7472 | } |
| 7473 | |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7474 | LookupQualifiedName(R, LookupContext); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7475 | |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7476 | // Try to correct typos if possible. |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7477 | if (R.empty()) { |
| Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7478 | UsingValidatorCCC CCC(HasTypenameKeyword, IsInstantiation, |
| 7479 | CurContext->isRecord()); |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7480 | if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), |
| John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 7481 | R.getLookupKind(), S, &SS, CCC, |
| 7482 | CTK_ErrorRecovery)){ |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7483 | // We reject any correction for which ND would be NULL. |
| 7484 | NamedDecl *ND = Corrected.getCorrectionDecl(); |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7485 | R.setLookupName(Corrected.getCorrection()); |
| 7486 | R.addDecl(ND); |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7487 | // We reject candidates where DroppedSpecifier == true, hence the |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7488 | // literal '0' below. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7489 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) |
| 7490 | << NameInfo.getName() << LookupContext << 0 |
| 7491 | << SS.getRange()); |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7492 | } else { |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7493 | Diag(IdentLoc, diag::err_no_member) |
| Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7494 | << NameInfo.getName() << LookupContext << SS.getRange(); |
| 7495 | UD->setInvalidDecl(); |
| 7496 | return UD; |
| 7497 | } |
| Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7498 | } |
| 7499 | |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7500 | if (R.isAmbiguous()) { |
| 7501 | UD->setInvalidDecl(); |
| 7502 | return UD; |
| 7503 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7504 | |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7505 | if (HasTypenameKeyword) { |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7506 | // If we asked for a typename and got a non-type decl, error out. |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7507 | if (!R.getAsSingle<TypeDecl>()) { |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7508 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 7509 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 7510 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 7511 | diag::note_using_decl_target); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7512 | UD->setInvalidDecl(); |
| 7513 | return UD; |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7514 | } |
| 7515 | } else { |
| 7516 | // If we asked for a non-typename and we got a type, error out, |
| 7517 | // but only if this is an instantiation of an unresolved using |
| 7518 | // decl. Otherwise just silently find the type name. |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7519 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7520 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 7521 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7522 | UD->setInvalidDecl(); |
| 7523 | return UD; |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7524 | } |
| Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7525 | } |
| 7526 | |
| Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7527 | // C++0x N2914 [namespace.udecl]p6: |
| 7528 | // A using-declaration shall not name a namespace. |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7529 | if (R.getAsSingle<NamespaceDecl>()) { |
| Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7530 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 7531 | << SS.getRange(); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7532 | UD->setInvalidDecl(); |
| 7533 | return UD; |
| Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7534 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7535 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7536 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7537 | UsingShadowDecl *PrevDecl = 0; |
| 7538 | if (!CheckUsingShadowDecl(UD, *I, Previous, PrevDecl)) |
| 7539 | BuildUsingShadowDecl(S, UD, *I, PrevDecl); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7540 | } |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7541 | |
| 7542 | return UD; |
| Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7543 | } |
| 7544 | |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7545 | /// Additional checks for a using declaration referring to a constructor name. |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7546 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7547 | assert(!UD->hasTypename() && "expecting a constructor name"); |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7548 | |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7549 | const Type *SourceType = UD->getQualifier()->getAsType(); |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7550 | assert(SourceType && |
| 7551 | "Using decl naming constructor doesn't have type in scope spec."); |
| 7552 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 7553 | |
| 7554 | // Check whether the named type is a direct base class. |
| 7555 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 7556 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 7557 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 7558 | BaseIt != BaseE; ++BaseIt) { |
| 7559 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 7560 | if (CanonicalSourceType == BaseType) |
| 7561 | break; |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7562 | if (BaseIt->getType()->isDependentType()) |
| 7563 | break; |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | if (BaseIt == BaseE) { |
| 7567 | // Did not find SourceType in the bases. |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7568 | Diag(UD->getUsingLoc(), |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7569 | diag::err_using_decl_constructor_not_in_direct_base) |
| 7570 | << UD->getNameInfo().getSourceRange() |
| 7571 | << QualType(SourceType, 0) << TargetClass; |
| 7572 | return true; |
| 7573 | } |
| 7574 | |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7575 | if (!CurContext->isDependentContext()) |
| 7576 | BaseIt->setInheritConstructors(); |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7577 | |
| 7578 | return false; |
| 7579 | } |
| 7580 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7581 | /// Checks that the given using declaration is not an invalid |
| 7582 | /// redeclaration. Note that this is checking only for the using decl |
| 7583 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 7584 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7585 | bool HasTypenameKeyword, |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7586 | const CXXScopeSpec &SS, |
| 7587 | SourceLocation NameLoc, |
| 7588 | const LookupResult &Prev) { |
| 7589 | // C++03 [namespace.udecl]p8: |
| 7590 | // C++0x [namespace.udecl]p10: |
| 7591 | // A using-declaration is a declaration and can therefore be used |
| 7592 | // repeatedly where (and only where) multiple declarations are |
| 7593 | // allowed. |
| Douglas Gregor | 4b718ee | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 7594 | // |
| John McCall | 032092f | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 7595 | // That's in non-member contexts. |
| 7596 | if (!CurContext->getRedeclContext()->isRecord()) |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7597 | return false; |
| 7598 | |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7599 | NestedNameSpecifier *Qual = SS.getScopeRep(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7600 | |
| 7601 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 7602 | NamedDecl *D = *I; |
| 7603 | |
| 7604 | bool DTypename; |
| 7605 | NestedNameSpecifier *DQual; |
| 7606 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7607 | DTypename = UD->hasTypename(); |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7608 | DQual = UD->getQualifier(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7609 | } else if (UnresolvedUsingValueDecl *UD |
| 7610 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 7611 | DTypename = false; |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7612 | DQual = UD->getQualifier(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7613 | } else if (UnresolvedUsingTypenameDecl *UD |
| 7614 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 7615 | DTypename = true; |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7616 | DQual = UD->getQualifier(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7617 | } else continue; |
| 7618 | |
| 7619 | // using decls differ if one says 'typename' and the other doesn't. |
| 7620 | // FIXME: non-dependent using decls? |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7621 | if (HasTypenameKeyword != DTypename) continue; |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7622 | |
| 7623 | // using decls differ if they name different scopes (but note that |
| 7624 | // template instantiation can cause this check to trigger when it |
| 7625 | // didn't before instantiation). |
| 7626 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 7627 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 7628 | continue; |
| 7629 | |
| 7630 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
| John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7631 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7632 | return true; |
| 7633 | } |
| 7634 | |
| 7635 | return false; |
| 7636 | } |
| 7637 | |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7638 | |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7639 | /// Checks that the given nested-name qualifier used in a using decl |
| 7640 | /// in the current context is appropriately related to the current |
| 7641 | /// scope. If an error is found, diagnoses it and returns true. |
| 7642 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 7643 | const CXXScopeSpec &SS, |
| Richard Smith | 7ad0b88 | 2014-04-02 21:44:35 +0000 | [diff] [blame] | 7644 | const DeclarationNameInfo &NameInfo, |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7645 | SourceLocation NameLoc) { |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7646 | DeclContext *NamedContext = computeDeclContext(SS); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7647 | |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7648 | if (!CurContext->isRecord()) { |
| 7649 | // C++03 [namespace.udecl]p3: |
| 7650 | // C++0x [namespace.udecl]p8: |
| 7651 | // A using-declaration for a class member shall be a member-declaration. |
| 7652 | |
| 7653 | // If we weren't able to compute a valid scope, it must be a |
| 7654 | // dependent class scope. |
| 7655 | if (!NamedContext || NamedContext->isRecord()) { |
| Richard Smith | 7ad0b88 | 2014-04-02 21:44:35 +0000 | [diff] [blame] | 7656 | auto *RD = dyn_cast<CXXRecordDecl>(NamedContext); |
| 7657 | if (RD && RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), RD)) |
| 7658 | RD = 0; |
| 7659 | |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7660 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 7661 | << SS.getRange(); |
| Richard Smith | 7ad0b88 | 2014-04-02 21:44:35 +0000 | [diff] [blame] | 7662 | |
| 7663 | // If we have a complete, non-dependent source type, try to suggest a |
| 7664 | // way to get the same effect. |
| 7665 | if (!RD) |
| 7666 | return true; |
| 7667 | |
| 7668 | // Find what this using-declaration was referring to. |
| 7669 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
| 7670 | R.setHideTags(false); |
| 7671 | R.suppressDiagnostics(); |
| 7672 | LookupQualifiedName(R, RD); |
| 7673 | |
| 7674 | if (R.getAsSingle<TypeDecl>()) { |
| 7675 | if (getLangOpts().CPlusPlus11) { |
| 7676 | // Convert 'using X::Y;' to 'using Y = X::Y;'. |
| 7677 | Diag(SS.getBeginLoc(), diag::note_using_decl_class_member_workaround) |
| 7678 | << 0 // alias declaration |
| 7679 | << FixItHint::CreateInsertion(SS.getBeginLoc(), |
| 7680 | NameInfo.getName().getAsString() + |
| 7681 | " = "); |
| 7682 | } else { |
| 7683 | // Convert 'using X::Y;' to 'typedef X::Y Y;'. |
| 7684 | SourceLocation InsertLoc = |
| 7685 | PP.getLocForEndOfToken(NameInfo.getLocEnd()); |
| 7686 | Diag(InsertLoc, diag::note_using_decl_class_member_workaround) |
| 7687 | << 1 // typedef declaration |
| 7688 | << FixItHint::CreateReplacement(UsingLoc, "typedef") |
| 7689 | << FixItHint::CreateInsertion( |
| 7690 | InsertLoc, " " + NameInfo.getName().getAsString()); |
| 7691 | } |
| 7692 | } else if (R.getAsSingle<VarDecl>()) { |
| 7693 | // Don't provide a fixit outside C++11 mode; we don't want to suggest |
| 7694 | // repeating the type of the static data member here. |
| 7695 | FixItHint FixIt; |
| 7696 | if (getLangOpts().CPlusPlus11) { |
| 7697 | // Convert 'using X::Y;' to 'auto &Y = X::Y;'. |
| 7698 | FixIt = FixItHint::CreateReplacement( |
| 7699 | UsingLoc, "auto &" + NameInfo.getName().getAsString() + " = "); |
| 7700 | } |
| 7701 | |
| 7702 | Diag(UsingLoc, diag::note_using_decl_class_member_workaround) |
| 7703 | << 2 // reference declaration |
| 7704 | << FixIt; |
| 7705 | } |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7706 | return true; |
| 7707 | } |
| 7708 | |
| 7709 | // Otherwise, everything is known to be fine. |
| 7710 | return false; |
| 7711 | } |
| 7712 | |
| 7713 | // The current scope is a record. |
| 7714 | |
| 7715 | // If the named context is dependent, we can't decide much. |
| 7716 | if (!NamedContext) { |
| 7717 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 7718 | // nested-name-specifier does not refer to a base class, which is |
| 7719 | // still possible in some cases. |
| 7720 | |
| 7721 | // Otherwise we have to conservatively report that things might be |
| 7722 | // okay. |
| 7723 | return false; |
| 7724 | } |
| 7725 | |
| 7726 | if (!NamedContext->isRecord()) { |
| 7727 | // Ideally this would point at the last name in the specifier, |
| 7728 | // but we don't have that level of source info. |
| 7729 | Diag(SS.getRange().getBegin(), |
| 7730 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| Aaron Ballman | 5fe6c80 | 2014-01-03 13:45:46 +0000 | [diff] [blame] | 7731 | << SS.getScopeRep() << SS.getRange(); |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7732 | return true; |
| 7733 | } |
| 7734 | |
| Douglas Gregor | 7c84229 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 7735 | if (!NamedContext->isDependentContext() && |
| 7736 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 7737 | return true; |
| 7738 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7739 | if (getLangOpts().CPlusPlus11) { |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7740 | // C++0x [namespace.udecl]p3: |
| 7741 | // In a using-declaration used as a member-declaration, the |
| 7742 | // nested-name-specifier shall name a base class of the class |
| 7743 | // being defined. |
| 7744 | |
| 7745 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 7746 | cast<CXXRecordDecl>(NamedContext))) { |
| 7747 | if (CurContext == NamedContext) { |
| 7748 | Diag(NameLoc, |
| 7749 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 7750 | << SS.getRange(); |
| 7751 | return true; |
| 7752 | } |
| 7753 | |
| 7754 | Diag(SS.getRange().getBegin(), |
| 7755 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7756 | << SS.getScopeRep() |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7757 | << cast<CXXRecordDecl>(CurContext) |
| 7758 | << SS.getRange(); |
| 7759 | return true; |
| 7760 | } |
| 7761 | |
| 7762 | return false; |
| 7763 | } |
| 7764 | |
| 7765 | // C++03 [namespace.udecl]p4: |
| 7766 | // A using-declaration used as a member-declaration shall refer |
| 7767 | // to a member of a base class of the class being defined [etc.]. |
| 7768 | |
| 7769 | // Salient point: SS doesn't have to name a base class as long as |
| 7770 | // lookup only finds members from base classes. Therefore we can |
| 7771 | // diagnose here only if we can prove that that can't happen, |
| 7772 | // i.e. if the class hierarchies provably don't intersect. |
| 7773 | |
| 7774 | // TODO: it would be nice if "definitely valid" results were cached |
| 7775 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 7776 | // need to be repeated. |
| 7777 | |
| 7778 | struct UserData { |
| Benjamin Kramer | 3adbe1c | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 7779 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7780 | |
| 7781 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7782 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7783 | Data->Bases.insert(Base); |
| 7784 | return true; |
| 7785 | } |
| 7786 | |
| 7787 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 7788 | return !Class->forallBases(collect, this); |
| 7789 | } |
| 7790 | |
| 7791 | /// Returns true if the base is dependent or is one of the |
| 7792 | /// accumulated base classes. |
| 7793 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7794 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7795 | return !Data->Bases.count(Base); |
| 7796 | } |
| 7797 | |
| 7798 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 7799 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 7800 | } |
| 7801 | }; |
| 7802 | |
| 7803 | UserData Data; |
| 7804 | |
| 7805 | // Returns false if we find a dependent base. |
| 7806 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 7807 | return false; |
| 7808 | |
| 7809 | // Returns false if the class has a dependent base or if it or one |
| 7810 | // of its bases is present in the base set of the current context. |
| 7811 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 7812 | return false; |
| 7813 | |
| 7814 | Diag(SS.getRange().getBegin(), |
| 7815 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7816 | << SS.getScopeRep() |
| John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7817 | << cast<CXXRecordDecl>(CurContext) |
| 7818 | << SS.getRange(); |
| 7819 | |
| 7820 | return true; |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7821 | } |
| 7822 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7823 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 7824 | AccessSpecifier AS, |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7825 | MultiTemplateParamsArg TemplateParamLists, |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7826 | SourceLocation UsingLoc, |
| 7827 | UnqualifiedId &Name, |
| Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7828 | AttributeList *AttrList, |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7829 | TypeResult Type) { |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7830 | // Skip up to the relevant declaration scope. |
| 7831 | while (S->getFlags() & Scope::TemplateParamScope) |
| 7832 | S = S->getParent(); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7833 | assert((S->getFlags() & Scope::DeclScope) && |
| 7834 | "got alias-declaration outside of declaration scope"); |
| 7835 | |
| 7836 | if (Type.isInvalid()) |
| 7837 | return 0; |
| 7838 | |
| 7839 | bool Invalid = false; |
| 7840 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 7841 | TypeSourceInfo *TInfo = 0; |
| Nick Lewycky | 82e4780 | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 7842 | GetTypeFromParser(Type.get(), &TInfo); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7843 | |
| 7844 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 7845 | return 0; |
| 7846 | |
| 7847 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7848 | UPPC_DeclarationType)) { |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7849 | Invalid = true; |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7850 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 7851 | TInfo->getTypeLoc().getBeginLoc()); |
| 7852 | } |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7853 | |
| 7854 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 7855 | LookupName(Previous, S); |
| 7856 | |
| 7857 | // Warn about shadowing the name of a template parameter. |
| 7858 | if (Previous.isSingleResult() && |
| 7859 | Previous.getFoundDecl()->isTemplateParameter()) { |
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 7860 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7861 | Previous.clear(); |
| 7862 | } |
| 7863 | |
| 7864 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 7865 | "name in alias declaration must be an identifier"); |
| 7866 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 7867 | Name.StartLocation, |
| 7868 | Name.Identifier, TInfo); |
| 7869 | |
| 7870 | NewTD->setAccess(AS); |
| 7871 | |
| 7872 | if (Invalid) |
| 7873 | NewTD->setInvalidDecl(); |
| 7874 | |
| Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7875 | ProcessDeclAttributeList(S, NewTD, AttrList); |
| 7876 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7877 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 7878 | Invalid |= NewTD->isInvalidDecl(); |
| 7879 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7880 | bool Redeclaration = false; |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7881 | |
| 7882 | NamedDecl *NewND; |
| 7883 | if (TemplateParamLists.size()) { |
| 7884 | TypeAliasTemplateDecl *OldDecl = 0; |
| 7885 | TemplateParameterList *OldTemplateParams = 0; |
| 7886 | |
| 7887 | if (TemplateParamLists.size() != 1) { |
| 7888 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7889 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 7890 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7891 | } |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7892 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7893 | |
| 7894 | // Only consider previous declarations in the same scope. |
| 7895 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 7896 | /*ExplicitInstantiationOrSpecialization*/false); |
| 7897 | if (!Previous.empty()) { |
| 7898 | Redeclaration = true; |
| 7899 | |
| 7900 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 7901 | if (!OldDecl && !Invalid) { |
| 7902 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 7903 | << Name.Identifier; |
| 7904 | |
| 7905 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 7906 | if (OldD->getLocation().isValid()) |
| 7907 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 7908 | |
| 7909 | Invalid = true; |
| 7910 | } |
| 7911 | |
| 7912 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 7913 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 7914 | OldDecl->getTemplateParameters(), |
| 7915 | /*Complain=*/true, |
| 7916 | TPL_TemplateMatch)) |
| 7917 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 7918 | else |
| 7919 | Invalid = true; |
| 7920 | |
| 7921 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 7922 | if (!Invalid && |
| 7923 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 7924 | NewTD->getUnderlyingType())) { |
| 7925 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 7926 | // but we can't reasonably accept it. |
| 7927 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 7928 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 7929 | if (OldTD->getLocation().isValid()) |
| 7930 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 7931 | Invalid = true; |
| 7932 | } |
| 7933 | } |
| 7934 | } |
| 7935 | |
| 7936 | // Merge any previous default template arguments into our parameters, |
| 7937 | // and check the parameter list. |
| 7938 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 7939 | TPC_TypeAliasTemplate)) |
| 7940 | return 0; |
| 7941 | |
| 7942 | TypeAliasTemplateDecl *NewDecl = |
| 7943 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 7944 | Name.Identifier, TemplateParams, |
| 7945 | NewTD); |
| 7946 | |
| 7947 | NewDecl->setAccess(AS); |
| 7948 | |
| 7949 | if (Invalid) |
| 7950 | NewDecl->setInvalidDecl(); |
| 7951 | else if (OldDecl) |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 7952 | NewDecl->setPreviousDecl(OldDecl); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7953 | |
| 7954 | NewND = NewDecl; |
| 7955 | } else { |
| 7956 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 7957 | NewND = NewTD; |
| 7958 | } |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7959 | |
| 7960 | if (!Redeclaration) |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7961 | PushOnScopeChains(NewND, S); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7962 | |
| Dmitri Gribenko | 7f4b377 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 7963 | ActOnDocumentableDecl(NewND); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7964 | return NewND; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7965 | } |
| 7966 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7967 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
| Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7968 | SourceLocation NamespaceLoc, |
| Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7969 | SourceLocation AliasLoc, |
| 7970 | IdentifierInfo *Alias, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7971 | CXXScopeSpec &SS, |
| Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7972 | SourceLocation IdentLoc, |
| 7973 | IdentifierInfo *Ident) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7974 | |
| Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7975 | // Lookup the namespace name. |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7976 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 7977 | LookupParsedName(R, S, &SS); |
| Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7978 | |
| Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7979 | // Check if we have a previous declaration with the same name. |
| Douglas Gregor | 5cf8d67 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 7980 | NamedDecl *PrevDecl |
| 7981 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 7982 | ForRedeclaration); |
| 7983 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 7984 | PrevDecl = 0; |
| 7985 | |
| 7986 | if (PrevDecl) { |
| Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7987 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7988 | // We already have an alias with the same name that points to the same |
| Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7989 | // namespace, so don't create a new one. |
| Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7990 | // FIXME: At some point, we'll want to create the (redundant) |
| 7991 | // declaration to maintain better source information. |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7992 | if (!R.isAmbiguous() && !R.empty() && |
| Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7993 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7994 | return 0; |
| Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7995 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7996 | |
| Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7997 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 7998 | diag::err_redefinition_different_kind; |
| 7999 | Diag(AliasLoc, DiagID) << Alias; |
| 8000 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8001 | return 0; |
| Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8004 | if (R.isAmbiguous()) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8005 | return 0; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8006 | |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8007 | if (R.empty()) { |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 8008 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
| Richard Smith | a974688 | 2012-04-05 23:13:23 +0000 | [diff] [blame] | 8009 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8010 | return 0; |
| Douglas Gregor | 9629e9a | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 8011 | } |
| Anders Carlsson | ac2c965 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 8012 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8013 | |
| Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8014 | NamespaceAliasDecl *AliasDecl = |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8015 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 8016 | Alias, SS.getWithLocInContext(Context), |
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8017 | IdentLoc, R.getFoundDecl()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8018 | |
| John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 8019 | PushOnScopeChains(AliasDecl, S); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8020 | return AliasDecl; |
| Anders Carlsson | 9205d55 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 8021 | } |
| 8022 | |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8023 | Sema::ImplicitExceptionSpecification |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8024 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(SourceLocation Loc, |
| 8025 | CXXMethodDecl *MD) { |
| 8026 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8027 | |
| Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8028 | // C++ [except.spec]p14: |
| 8029 | // An implicitly declared special member function (Clause 12) shall have an |
| 8030 | // exception-specification. [...] |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8031 | ImplicitExceptionSpecification ExceptSpec(*this); |
| Abramo Bagnara | dd8fc04 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8032 | if (ClassDecl->isInvalidDecl()) |
| 8033 | return ExceptSpec; |
| Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8034 | |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8035 | // Direct base-class constructors. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8036 | for (const auto &B : ClassDecl->bases()) { |
| 8037 | if (B.isVirtual()) // Handled below. |
| Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8038 | continue; |
| 8039 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8040 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8041 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8042 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8043 | // If this is a deleted function, add it anyway. This might be conformant |
| 8044 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8045 | if (Constructor) |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8046 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8047 | } |
| Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8048 | } |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8049 | |
| 8050 | // Virtual base-class constructors. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 8051 | for (const auto &B : ClassDecl->vbases()) { |
| 8052 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8053 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8054 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8055 | // If this is a deleted function, add it anyway. This might be conformant |
| 8056 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8057 | if (Constructor) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 8058 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8059 | } |
| Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8060 | } |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8061 | |
| 8062 | // Field constructors. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8063 | for (const auto *F : ClassDecl->fields()) { |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8064 | if (F->hasInClassInitializer()) { |
| 8065 | if (Expr *E = F->getInClassInitializer()) |
| 8066 | ExceptSpec.CalledExpr(E); |
| 8067 | else if (!F->isInvalidDecl()) |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8068 | // DR1351: |
| 8069 | // If the brace-or-equal-initializer of a non-static data member |
| 8070 | // invokes a defaulted default constructor of its class or of an |
| 8071 | // enclosing class in a potentially evaluated subexpression, the |
| 8072 | // program is ill-formed. |
| 8073 | // |
| 8074 | // This resolution is unworkable: the exception specification of the |
| 8075 | // default constructor can be needed in an unevaluated context, in |
| 8076 | // particular, in the operand of a noexcept-expression, and we can be |
| 8077 | // unable to compute an exception specification for an enclosed class. |
| 8078 | // |
| 8079 | // We do not allow an in-class initializer to require the evaluation |
| 8080 | // of the exception specification for any in-class initializer whose |
| 8081 | // definition is not lexically complete. |
| 8082 | Diag(Loc, diag::err_in_class_initializer_references_def_ctor) << MD; |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8083 | } else if (const RecordType *RecordTy |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8084 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8085 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8086 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 8087 | // If this is a deleted function, add it anyway. This might be conformant |
| 8088 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8089 | // In particular, the problem is that this function never gets called. It |
| 8090 | // might just be ill-formed because this function attempts to refer to |
| 8091 | // a deleted function here. |
| 8092 | if (Constructor) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8093 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8094 | } |
| Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8095 | } |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8096 | |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8097 | return ExceptSpec; |
| 8098 | } |
| 8099 | |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8100 | Sema::ImplicitExceptionSpecification |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8101 | Sema::ComputeInheritingCtorExceptionSpec(CXXConstructorDecl *CD) { |
| 8102 | CXXRecordDecl *ClassDecl = CD->getParent(); |
| 8103 | |
| 8104 | // C++ [except.spec]p14: |
| 8105 | // An inheriting constructor [...] shall have an exception-specification. [...] |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8106 | ImplicitExceptionSpecification ExceptSpec(*this); |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8107 | if (ClassDecl->isInvalidDecl()) |
| 8108 | return ExceptSpec; |
| 8109 | |
| 8110 | // Inherited constructor. |
| 8111 | const CXXConstructorDecl *InheritedCD = CD->getInheritedConstructor(); |
| 8112 | const CXXRecordDecl *InheritedDecl = InheritedCD->getParent(); |
| 8113 | // FIXME: Copying or moving the parameters could add extra exceptions to the |
| 8114 | // set, as could the default arguments for the inherited constructor. This |
| 8115 | // will be addressed when we implement the resolution of core issue 1351. |
| 8116 | ExceptSpec.CalledDecl(CD->getLocStart(), InheritedCD); |
| 8117 | |
| 8118 | // Direct base-class constructors. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8119 | for (const auto &B : ClassDecl->bases()) { |
| 8120 | if (B.isVirtual()) // Handled below. |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8121 | continue; |
| 8122 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8123 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8124 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8125 | if (BaseClassDecl == InheritedDecl) |
| 8126 | continue; |
| 8127 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8128 | if (Constructor) |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8129 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8130 | } |
| 8131 | } |
| 8132 | |
| 8133 | // Virtual base-class constructors. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 8134 | for (const auto &B : ClassDecl->vbases()) { |
| 8135 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8136 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8137 | if (BaseClassDecl == InheritedDecl) |
| 8138 | continue; |
| 8139 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8140 | if (Constructor) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 8141 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8142 | } |
| 8143 | } |
| 8144 | |
| 8145 | // Field constructors. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8146 | for (const auto *F : ClassDecl->fields()) { |
| Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8147 | if (F->hasInClassInitializer()) { |
| 8148 | if (Expr *E = F->getInClassInitializer()) |
| 8149 | ExceptSpec.CalledExpr(E); |
| 8150 | else if (!F->isInvalidDecl()) |
| 8151 | Diag(CD->getLocation(), |
| 8152 | diag::err_in_class_initializer_references_def_ctor) << CD; |
| 8153 | } else if (const RecordType *RecordTy |
| 8154 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 8155 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8156 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 8157 | if (Constructor) |
| 8158 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
| 8159 | } |
| 8160 | } |
| 8161 | |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8162 | return ExceptSpec; |
| 8163 | } |
| 8164 | |
| Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8165 | namespace { |
| 8166 | /// RAII object to register a special member as being currently declared. |
| 8167 | struct DeclaringSpecialMember { |
| 8168 | Sema &S; |
| 8169 | Sema::SpecialMemberDecl D; |
| 8170 | bool WasAlreadyBeingDeclared; |
| 8171 | |
| 8172 | DeclaringSpecialMember(Sema &S, CXXRecordDecl *RD, Sema::CXXSpecialMember CSM) |
| 8173 | : S(S), D(RD, CSM) { |
| 8174 | WasAlreadyBeingDeclared = !S.SpecialMembersBeingDeclared.insert(D); |
| 8175 | if (WasAlreadyBeingDeclared) |
| 8176 | // This almost never happens, but if it does, ensure that our cache |
| 8177 | // doesn't contain a stale result. |
| 8178 | S.SpecialMemberCache.clear(); |
| 8179 | |
| 8180 | // FIXME: Register a note to be produced if we encounter an error while |
| 8181 | // declaring the special member. |
| 8182 | } |
| 8183 | ~DeclaringSpecialMember() { |
| 8184 | if (!WasAlreadyBeingDeclared) |
| 8185 | S.SpecialMembersBeingDeclared.erase(D); |
| 8186 | } |
| 8187 | |
| 8188 | /// \brief Are we already trying to declare this special member? |
| 8189 | bool isAlreadyBeingDeclared() const { |
| 8190 | return WasAlreadyBeingDeclared; |
| 8191 | } |
| 8192 | }; |
| 8193 | } |
| 8194 | |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8195 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 8196 | CXXRecordDecl *ClassDecl) { |
| 8197 | // C++ [class.ctor]p5: |
| 8198 | // A default constructor for a class X is a constructor of class X |
| 8199 | // that can be called without an argument. If there is no |
| 8200 | // user-declared constructor for class X, a default constructor is |
| 8201 | // implicitly declared. An implicitly-declared default constructor |
| 8202 | // is an inline public member of its class. |
| Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 8203 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
| Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8204 | "Should not build implicit default constructor!"); |
| 8205 | |
| Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8206 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDefaultConstructor); |
| 8207 | if (DSM.isAlreadyBeingDeclared()) |
| 8208 | return 0; |
| 8209 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8210 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 8211 | CXXDefaultConstructor, |
| 8212 | false); |
| 8213 | |
| Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8214 | // Create the actual constructor declaration. |
| Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8215 | CanQualType ClassType |
| 8216 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8217 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8218 | DeclarationName Name |
| 8219 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8220 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8221 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8222 | Context, ClassDecl, ClassLoc, NameInfo, /*Type*/QualType(), /*TInfo=*/0, |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8223 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8224 | Constexpr); |
| Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8225 | DefaultCon->setAccess(AS_public); |
| Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 8226 | DefaultCon->setDefaulted(); |
| Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8227 | DefaultCon->setImplicit(); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8228 | |
| 8229 | // Build an exception specification pointing back at this constructor. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8230 | FunctionProtoType::ExtProtoInfo EPI = getImplicitMethodEPI(*this, DefaultCon); |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8231 | DefaultCon->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8232 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8233 | // We don't need to use SpecialMemberIsTrivial here; triviality for default |
| 8234 | // constructors is easy to compute. |
| 8235 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
| 8236 | |
| 8237 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8238 | SetDeclDeleted(DefaultCon, ClassLoc); |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8239 | |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8240 | // Note that we have declared this constructor. |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8241 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8242 | |
| Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8243 | if (Scope *S = getScopeForContext(ClassDecl)) |
| Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8244 | PushOnScopeChains(DefaultCon, S, false); |
| 8245 | ClassDecl->addDecl(DefaultCon); |
| Alexis Hunt | e77a28f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8246 | |
| Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8247 | return DefaultCon; |
| 8248 | } |
| 8249 | |
| Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8250 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 8251 | CXXConstructorDecl *Constructor) { |
| Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 8252 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8253 | !Constructor->doesThisDeclarationHaveABody() && |
| 8254 | !Constructor->isDeleted()) && |
| Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 8255 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8256 | |
| Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 8257 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8258 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
| Eli Friedman | d7686ef | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 8259 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8260 | SynthesizedFunctionScope Scope(*this, Constructor); |
| Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8261 | DiagnosticErrorTrap Trap(Diags); |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 8262 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
| Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8263 | Trap.hasErrorOccurred()) { |
| Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 8264 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 8265 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
| Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8266 | Constructor->setInvalidDecl(); |
| Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8267 | return; |
| Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8268 | } |
| Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8269 | |
| 8270 | SourceLocation Loc = Constructor->getLocation(); |
| Benjamin Kramer | e2a929d | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8271 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
| Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8272 | |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 8273 | Constructor->markUsed(Context); |
| Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8274 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8275 | |
| 8276 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8277 | L->CompletedImplicitDefinition(Constructor); |
| 8278 | } |
| Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 8279 | |
| 8280 | DiagnoseUninitializedFields(*this, Constructor); |
| Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8281 | } |
| 8282 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8283 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
| Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 8284 | // Perform any delayed checks on exception specifications. |
| 8285 | CheckDelayedMemberExceptionSpecs(); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8286 | } |
| 8287 | |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8288 | namespace { |
| 8289 | /// Information on inheriting constructors to declare. |
| 8290 | class InheritingConstructorInfo { |
| 8291 | public: |
| 8292 | InheritingConstructorInfo(Sema &SemaRef, CXXRecordDecl *Derived) |
| 8293 | : SemaRef(SemaRef), Derived(Derived) { |
| 8294 | // Mark the constructors that we already have in the derived class. |
| 8295 | // |
| 8296 | // C++11 [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 8297 | // unless there is a user-declared constructor with the same signature in |
| 8298 | // the class where the using-declaration appears. |
| 8299 | visitAll(Derived, &InheritingConstructorInfo::noteDeclaredInDerived); |
| 8300 | } |
| 8301 | |
| 8302 | void inheritAll(CXXRecordDecl *RD) { |
| 8303 | visitAll(RD, &InheritingConstructorInfo::inherit); |
| 8304 | } |
| 8305 | |
| 8306 | private: |
| 8307 | /// Information about an inheriting constructor. |
| 8308 | struct InheritingConstructor { |
| 8309 | InheritingConstructor() |
| 8310 | : DeclaredInDerived(false), BaseCtor(0), DerivedCtor(0) {} |
| 8311 | |
| 8312 | /// If \c true, a constructor with this signature is already declared |
| 8313 | /// in the derived class. |
| 8314 | bool DeclaredInDerived; |
| 8315 | |
| 8316 | /// The constructor which is inherited. |
| 8317 | const CXXConstructorDecl *BaseCtor; |
| 8318 | |
| 8319 | /// The derived constructor we declared. |
| 8320 | CXXConstructorDecl *DerivedCtor; |
| 8321 | }; |
| 8322 | |
| 8323 | /// Inheriting constructors with a given canonical type. There can be at |
| 8324 | /// most one such non-template constructor, and any number of templated |
| 8325 | /// constructors. |
| 8326 | struct InheritingConstructorsForType { |
| 8327 | InheritingConstructor NonTemplate; |
| Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8328 | SmallVector<std::pair<TemplateParameterList *, InheritingConstructor>, 4> |
| 8329 | Templates; |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8330 | |
| 8331 | InheritingConstructor &getEntry(Sema &S, const CXXConstructorDecl *Ctor) { |
| 8332 | if (FunctionTemplateDecl *FTD = Ctor->getDescribedFunctionTemplate()) { |
| 8333 | TemplateParameterList *ParamList = FTD->getTemplateParameters(); |
| 8334 | for (unsigned I = 0, N = Templates.size(); I != N; ++I) |
| 8335 | if (S.TemplateParameterListsAreEqual(ParamList, Templates[I].first, |
| 8336 | false, S.TPL_TemplateMatch)) |
| 8337 | return Templates[I].second; |
| 8338 | Templates.push_back(std::make_pair(ParamList, InheritingConstructor())); |
| 8339 | return Templates.back().second; |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8340 | } |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8341 | |
| 8342 | return NonTemplate; |
| 8343 | } |
| 8344 | }; |
| 8345 | |
| 8346 | /// Get or create the inheriting constructor record for a constructor. |
| 8347 | InheritingConstructor &getEntry(const CXXConstructorDecl *Ctor, |
| 8348 | QualType CtorType) { |
| 8349 | return Map[CtorType.getCanonicalType()->castAs<FunctionProtoType>()] |
| 8350 | .getEntry(SemaRef, Ctor); |
| 8351 | } |
| 8352 | |
| 8353 | typedef void (InheritingConstructorInfo::*VisitFn)(const CXXConstructorDecl*); |
| 8354 | |
| 8355 | /// Process all constructors for a class. |
| 8356 | void visitAll(const CXXRecordDecl *RD, VisitFn Callback) { |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 8357 | for (const auto *Ctor : RD->ctors()) |
| 8358 | (this->*Callback)(Ctor); |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8359 | for (CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> |
| 8360 | I(RD->decls_begin()), E(RD->decls_end()); |
| 8361 | I != E; ++I) { |
| 8362 | const FunctionDecl *FD = (*I)->getTemplatedDecl(); |
| 8363 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 8364 | (this->*Callback)(CD); |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8365 | } |
| 8366 | } |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8367 | |
| 8368 | /// Note that a constructor (or constructor template) was declared in Derived. |
| 8369 | void noteDeclaredInDerived(const CXXConstructorDecl *Ctor) { |
| 8370 | getEntry(Ctor, Ctor->getType()).DeclaredInDerived = true; |
| 8371 | } |
| 8372 | |
| 8373 | /// Inherit a single constructor. |
| 8374 | void inherit(const CXXConstructorDecl *Ctor) { |
| 8375 | const FunctionProtoType *CtorType = |
| 8376 | Ctor->getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8377 | ArrayRef<QualType> ArgTypes(CtorType->getParamTypes()); |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8378 | FunctionProtoType::ExtProtoInfo EPI = CtorType->getExtProtoInfo(); |
| 8379 | |
| 8380 | SourceLocation UsingLoc = getUsingLoc(Ctor->getParent()); |
| 8381 | |
| 8382 | // Core issue (no number yet): the ellipsis is always discarded. |
| 8383 | if (EPI.Variadic) { |
| 8384 | SemaRef.Diag(UsingLoc, diag::warn_using_decl_constructor_ellipsis); |
| 8385 | SemaRef.Diag(Ctor->getLocation(), |
| 8386 | diag::note_using_decl_constructor_ellipsis); |
| 8387 | EPI.Variadic = false; |
| 8388 | } |
| 8389 | |
| 8390 | // Declare a constructor for each number of parameters. |
| 8391 | // |
| 8392 | // C++11 [class.inhctor]p1: |
| 8393 | // The candidate set of inherited constructors from the class X named in |
| 8394 | // the using-declaration consists of [... modulo defects ...] for each |
| 8395 | // constructor or constructor template of X, the set of constructors or |
| 8396 | // constructor templates that results from omitting any ellipsis parameter |
| 8397 | // specification and successively omitting parameters with a default |
| 8398 | // argument from the end of the parameter-type-list |
| Richard Smith | 3c626ed | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 8399 | unsigned MinParams = minParamsToInherit(Ctor); |
| 8400 | unsigned Params = Ctor->getNumParams(); |
| 8401 | if (Params >= MinParams) { |
| 8402 | do |
| 8403 | declareCtor(UsingLoc, Ctor, |
| 8404 | SemaRef.Context.getFunctionType( |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8405 | Ctor->getReturnType(), ArgTypes.slice(0, Params), EPI)); |
| Richard Smith | 3c626ed | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 8406 | while (Params > MinParams && |
| 8407 | Ctor->getParamDecl(--Params)->hasDefaultArg()); |
| 8408 | } |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8409 | } |
| 8410 | |
| 8411 | /// Find the using-declaration which specified that we should inherit the |
| 8412 | /// constructors of \p Base. |
| 8413 | SourceLocation getUsingLoc(const CXXRecordDecl *Base) { |
| 8414 | // No fancy lookup required; just look for the base constructor name |
| 8415 | // directly within the derived class. |
| 8416 | ASTContext &Context = SemaRef.Context; |
| 8417 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8418 | Context.getCanonicalType(Context.getRecordType(Base))); |
| 8419 | DeclContext::lookup_const_result Decls = Derived->lookup(Name); |
| 8420 | return Decls.empty() ? Derived->getLocation() : Decls[0]->getLocation(); |
| 8421 | } |
| 8422 | |
| 8423 | unsigned minParamsToInherit(const CXXConstructorDecl *Ctor) { |
| 8424 | // C++11 [class.inhctor]p3: |
| 8425 | // [F]or each constructor template in the candidate set of inherited |
| 8426 | // constructors, a constructor template is implicitly declared |
| 8427 | if (Ctor->getDescribedFunctionTemplate()) |
| 8428 | return 0; |
| 8429 | |
| 8430 | // For each non-template constructor in the candidate set of inherited |
| 8431 | // constructors other than a constructor having no parameters or a |
| 8432 | // copy/move constructor having a single parameter, a constructor is |
| 8433 | // implicitly declared [...] |
| 8434 | if (Ctor->getNumParams() == 0) |
| 8435 | return 1; |
| 8436 | if (Ctor->isCopyOrMoveConstructor()) |
| 8437 | return 2; |
| 8438 | |
| 8439 | // Per discussion on core reflector, never inherit a constructor which |
| 8440 | // would become a default, copy, or move constructor of Derived either. |
| 8441 | const ParmVarDecl *PD = Ctor->getParamDecl(0); |
| 8442 | const ReferenceType *RT = PD->getType()->getAs<ReferenceType>(); |
| 8443 | return (RT && RT->getPointeeCXXRecordDecl() == Derived) ? 2 : 1; |
| 8444 | } |
| 8445 | |
| 8446 | /// Declare a single inheriting constructor, inheriting the specified |
| 8447 | /// constructor, with the given type. |
| 8448 | void declareCtor(SourceLocation UsingLoc, const CXXConstructorDecl *BaseCtor, |
| 8449 | QualType DerivedType) { |
| 8450 | InheritingConstructor &Entry = getEntry(BaseCtor, DerivedType); |
| 8451 | |
| 8452 | // C++11 [class.inhctor]p3: |
| 8453 | // ... a constructor is implicitly declared with the same constructor |
| 8454 | // characteristics unless there is a user-declared constructor with |
| 8455 | // the same signature in the class where the using-declaration appears |
| 8456 | if (Entry.DeclaredInDerived) |
| 8457 | return; |
| 8458 | |
| 8459 | // C++11 [class.inhctor]p7: |
| 8460 | // If two using-declarations declare inheriting constructors with the |
| 8461 | // same signature, the program is ill-formed |
| 8462 | if (Entry.DerivedCtor) { |
| 8463 | if (BaseCtor->getParent() != Entry.BaseCtor->getParent()) { |
| 8464 | // Only diagnose this once per constructor. |
| 8465 | if (Entry.DerivedCtor->isInvalidDecl()) |
| 8466 | return; |
| 8467 | Entry.DerivedCtor->setInvalidDecl(); |
| 8468 | |
| 8469 | SemaRef.Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 8470 | SemaRef.Diag(BaseCtor->getLocation(), |
| 8471 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 8472 | SemaRef.Diag(Entry.BaseCtor->getLocation(), |
| 8473 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 8474 | SemaRef.Diag(Entry.DerivedCtor->getLocation(), |
| 8475 | diag::note_using_decl_constructor_conflict_previous_using); |
| 8476 | } else { |
| 8477 | // Core issue (no number): if the same inheriting constructor is |
| 8478 | // produced by multiple base class constructors from the same base |
| 8479 | // class, the inheriting constructor is defined as deleted. |
| 8480 | SemaRef.SetDeclDeleted(Entry.DerivedCtor, UsingLoc); |
| 8481 | } |
| 8482 | |
| 8483 | return; |
| 8484 | } |
| 8485 | |
| 8486 | ASTContext &Context = SemaRef.Context; |
| 8487 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8488 | Context.getCanonicalType(Context.getRecordType(Derived))); |
| 8489 | DeclarationNameInfo NameInfo(Name, UsingLoc); |
| 8490 | |
| 8491 | TemplateParameterList *TemplateParams = 0; |
| 8492 | if (const FunctionTemplateDecl *FTD = |
| 8493 | BaseCtor->getDescribedFunctionTemplate()) { |
| 8494 | TemplateParams = FTD->getTemplateParameters(); |
| 8495 | // We're reusing template parameters from a different DeclContext. This |
| 8496 | // is questionable at best, but works out because the template depth in |
| 8497 | // both places is guaranteed to be 0. |
| 8498 | // FIXME: Rebuild the template parameters in the new context, and |
| 8499 | // transform the function type to refer to them. |
| 8500 | } |
| 8501 | |
| 8502 | // Build type source info pointing at the using-declaration. This is |
| 8503 | // required by template instantiation. |
| 8504 | TypeSourceInfo *TInfo = |
| 8505 | Context.getTrivialTypeSourceInfo(DerivedType, UsingLoc); |
| 8506 | FunctionProtoTypeLoc ProtoLoc = |
| 8507 | TInfo->getTypeLoc().IgnoreParens().castAs<FunctionProtoTypeLoc>(); |
| 8508 | |
| 8509 | CXXConstructorDecl *DerivedCtor = CXXConstructorDecl::Create( |
| 8510 | Context, Derived, UsingLoc, NameInfo, DerivedType, |
| 8511 | TInfo, BaseCtor->isExplicit(), /*Inline=*/true, |
| 8512 | /*ImplicitlyDeclared=*/true, /*Constexpr=*/BaseCtor->isConstexpr()); |
| 8513 | |
| 8514 | // Build an unevaluated exception specification for this constructor. |
| 8515 | const FunctionProtoType *FPT = DerivedType->castAs<FunctionProtoType>(); |
| 8516 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8517 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8518 | EPI.ExceptionSpecDecl = DerivedCtor; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8519 | DerivedCtor->setType(Context.getFunctionType(FPT->getReturnType(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8520 | FPT->getParamTypes(), EPI)); |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8521 | |
| 8522 | // Build the parameter declarations. |
| 8523 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8524 | for (unsigned I = 0, N = FPT->getNumParams(); I != N; ++I) { |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8525 | TypeSourceInfo *TInfo = |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8526 | Context.getTrivialTypeSourceInfo(FPT->getParamType(I), UsingLoc); |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8527 | ParmVarDecl *PD = ParmVarDecl::Create( |
| 8528 | Context, DerivedCtor, UsingLoc, UsingLoc, /*IdentifierInfo=*/0, |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8529 | FPT->getParamType(I), TInfo, SC_None, /*DefaultArg=*/0); |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8530 | PD->setScopeInfo(0, I); |
| 8531 | PD->setImplicit(); |
| 8532 | ParamDecls.push_back(PD); |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 8533 | ProtoLoc.setParam(I, PD); |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8534 | } |
| 8535 | |
| 8536 | // Set up the new constructor. |
| 8537 | DerivedCtor->setAccess(BaseCtor->getAccess()); |
| 8538 | DerivedCtor->setParams(ParamDecls); |
| 8539 | DerivedCtor->setInheritedConstructor(BaseCtor); |
| 8540 | if (BaseCtor->isDeleted()) |
| 8541 | SemaRef.SetDeclDeleted(DerivedCtor, UsingLoc); |
| 8542 | |
| 8543 | // If this is a constructor template, build the template declaration. |
| 8544 | if (TemplateParams) { |
| 8545 | FunctionTemplateDecl *DerivedTemplate = |
| 8546 | FunctionTemplateDecl::Create(SemaRef.Context, Derived, UsingLoc, Name, |
| 8547 | TemplateParams, DerivedCtor); |
| 8548 | DerivedTemplate->setAccess(BaseCtor->getAccess()); |
| 8549 | DerivedCtor->setDescribedFunctionTemplate(DerivedTemplate); |
| 8550 | Derived->addDecl(DerivedTemplate); |
| 8551 | } else { |
| 8552 | Derived->addDecl(DerivedCtor); |
| 8553 | } |
| 8554 | |
| 8555 | Entry.BaseCtor = BaseCtor; |
| 8556 | Entry.DerivedCtor = DerivedCtor; |
| 8557 | } |
| 8558 | |
| 8559 | Sema &SemaRef; |
| 8560 | CXXRecordDecl *Derived; |
| 8561 | typedef llvm::DenseMap<const Type *, InheritingConstructorsForType> MapType; |
| 8562 | MapType Map; |
| 8563 | }; |
| 8564 | } |
| 8565 | |
| 8566 | void Sema::DeclareInheritingConstructors(CXXRecordDecl *ClassDecl) { |
| 8567 | // Defer declaring the inheriting constructors until the class is |
| 8568 | // instantiated. |
| 8569 | if (ClassDecl->isDependentContext()) |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8570 | return; |
| 8571 | |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8572 | // Find base classes from which we might inherit constructors. |
| 8573 | SmallVector<CXXRecordDecl*, 4> InheritedBases; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8574 | for (const auto &BaseIt : ClassDecl->bases()) |
| 8575 | if (BaseIt.getInheritConstructors()) |
| 8576 | InheritedBases.push_back(BaseIt.getType()->getAsCXXRecordDecl()); |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8577 | |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8578 | // Go no further if we're not inheriting any constructors. |
| 8579 | if (InheritedBases.empty()) |
| 8580 | return; |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8581 | |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8582 | // Declare the inherited constructors. |
| 8583 | InheritingConstructorInfo ICI(*this, ClassDecl); |
| 8584 | for (unsigned I = 0, N = InheritedBases.size(); I != N; ++I) |
| 8585 | ICI.inheritAll(InheritedBases[I]); |
| Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8586 | } |
| 8587 | |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8588 | void Sema::DefineInheritingConstructor(SourceLocation CurrentLocation, |
| 8589 | CXXConstructorDecl *Constructor) { |
| 8590 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 8591 | assert(Constructor->getInheritedConstructor() && |
| 8592 | !Constructor->doesThisDeclarationHaveABody() && |
| 8593 | !Constructor->isDeleted()); |
| 8594 | |
| 8595 | SynthesizedFunctionScope Scope(*this, Constructor); |
| 8596 | DiagnosticErrorTrap Trap(Diags); |
| 8597 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
| 8598 | Trap.hasErrorOccurred()) { |
| 8599 | Diag(CurrentLocation, diag::note_inhctor_synthesized_at) |
| 8600 | << Context.getTagDeclType(ClassDecl); |
| 8601 | Constructor->setInvalidDecl(); |
| 8602 | return; |
| 8603 | } |
| 8604 | |
| 8605 | SourceLocation Loc = Constructor->getLocation(); |
| 8606 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
| 8607 | |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 8608 | Constructor->markUsed(Context); |
| Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8609 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 8610 | |
| 8611 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8612 | L->CompletedImplicitDefinition(Constructor); |
| 8613 | } |
| 8614 | } |
| 8615 | |
| 8616 | |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8617 | Sema::ImplicitExceptionSpecification |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8618 | Sema::ComputeDefaultedDtorExceptionSpec(CXXMethodDecl *MD) { |
| 8619 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8620 | |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8621 | // C++ [except.spec]p14: |
| 8622 | // An implicitly declared special member function (Clause 12) shall have |
| 8623 | // an exception-specification. |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8624 | ImplicitExceptionSpecification ExceptSpec(*this); |
| Abramo Bagnara | dd8fc04 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8625 | if (ClassDecl->isInvalidDecl()) |
| 8626 | return ExceptSpec; |
| 8627 | |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8628 | // Direct base-class destructors. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8629 | for (const auto &B : ClassDecl->bases()) { |
| 8630 | if (B.isVirtual()) // Handled below. |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8631 | continue; |
| 8632 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 8633 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) |
| 8634 | ExceptSpec.CalledDecl(B.getLocStart(), |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8635 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8636 | } |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8637 | |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8638 | // Virtual base-class destructors. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 8639 | for (const auto &B : ClassDecl->vbases()) { |
| 8640 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) |
| 8641 | ExceptSpec.CalledDecl(B.getLocStart(), |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8642 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8643 | } |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8644 | |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8645 | // Field destructors. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8646 | for (const auto *F : ClassDecl->fields()) { |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8647 | if (const RecordType *RecordTy |
| 8648 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8649 | ExceptSpec.CalledDecl(F->getLocation(), |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8650 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8651 | } |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8652 | |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8653 | return ExceptSpec; |
| 8654 | } |
| 8655 | |
| 8656 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 8657 | // C++ [class.dtor]p2: |
| 8658 | // If a class has no user-declared destructor, a destructor is |
| 8659 | // declared implicitly. An implicitly-declared destructor is an |
| 8660 | // inline public member of its class. |
| Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8661 | assert(ClassDecl->needsImplicitDestructor()); |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8662 | |
| Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8663 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDestructor); |
| 8664 | if (DSM.isAlreadyBeingDeclared()) |
| 8665 | return 0; |
| 8666 | |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8667 | // Create the actual destructor declaration. |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8668 | CanQualType ClassType |
| 8669 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8670 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8671 | DeclarationName Name |
| 8672 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8673 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8674 | CXXDestructorDecl *Destructor |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8675 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 8676 | QualType(), 0, /*isInline=*/true, |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8677 | /*isImplicitlyDeclared=*/true); |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8678 | Destructor->setAccess(AS_public); |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8679 | Destructor->setDefaulted(); |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8680 | Destructor->setImplicit(); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8681 | |
| 8682 | // Build an exception specification pointing back at this destructor. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8683 | FunctionProtoType::ExtProtoInfo EPI = getImplicitMethodEPI(*this, Destructor); |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8684 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8685 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8686 | AddOverriddenMethods(ClassDecl, Destructor); |
| 8687 | |
| 8688 | // We don't need to use SpecialMemberIsTrivial here; triviality for |
| 8689 | // destructors is easy to compute. |
| 8690 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
| 8691 | |
| 8692 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8693 | SetDeclDeleted(Destructor, ClassLoc); |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8694 | |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8695 | // Note that we have declared this destructor. |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8696 | ++ASTContext::NumImplicitDestructorsDeclared; |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8697 | |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8698 | // Introduce this destructor into its scope. |
| Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8699 | if (Scope *S = getScopeForContext(ClassDecl)) |
| Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8700 | PushOnScopeChains(Destructor, S, false); |
| 8701 | ClassDecl->addDecl(Destructor); |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8702 | |
| Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8703 | return Destructor; |
| 8704 | } |
| 8705 | |
| Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8706 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
| Douglas Gregor | d94105a | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 8707 | CXXDestructorDecl *Destructor) { |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8708 | assert((Destructor->isDefaulted() && |
| Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8709 | !Destructor->doesThisDeclarationHaveABody() && |
| 8710 | !Destructor->isDeleted()) && |
| Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8711 | "DefineImplicitDestructor - call it for implicit default dtor"); |
| Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 8712 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
| Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8713 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8714 | |
| Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8715 | if (Destructor->isInvalidDecl()) |
| 8716 | return; |
| 8717 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8718 | SynthesizedFunctionScope Scope(*this, Destructor); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8719 | |
| Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8720 | DiagnosticErrorTrap Trap(Diags); |
| John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 8721 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 8722 | Destructor->getParent()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8723 | |
| Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8724 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
| Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 8725 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8726 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 8727 | |
| 8728 | Destructor->setInvalidDecl(); |
| 8729 | return; |
| 8730 | } |
| 8731 | |
| Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8732 | SourceLocation Loc = Destructor->getLocation(); |
| Benjamin Kramer | e2a929d | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8733 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 8734 | Destructor->markUsed(Context); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8735 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8736 | |
| 8737 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8738 | L->CompletedImplicitDefinition(Destructor); |
| 8739 | } |
| Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8740 | } |
| 8741 | |
| Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8742 | /// \brief Perform any semantic analysis which needs to be delayed until all |
| 8743 | /// pending class member declarations have been parsed. |
| 8744 | void Sema::ActOnFinishCXXMemberDecls() { |
| Douglas Gregor | bc0e5c0 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 8745 | // If the context is an invalid C++ class, just suppress these checks. |
| 8746 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8747 | if (Record->isInvalidDecl()) { |
| Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 8748 | DelayedDefaultedMemberExceptionSpecs.clear(); |
| Douglas Gregor | bc0e5c0 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 8749 | DelayedDestructorExceptionSpecChecks.clear(); |
| 8750 | return; |
| 8751 | } |
| 8752 | } |
| Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8753 | } |
| 8754 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8755 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *ClassDecl, |
| 8756 | CXXDestructorDecl *Destructor) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8757 | assert(getLangOpts().CPlusPlus11 && |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8758 | "adjusting dtor exception specs was introduced in c++11"); |
| 8759 | |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8760 | // C++11 [class.dtor]p3: |
| 8761 | // A declaration of a destructor that does not have an exception- |
| 8762 | // specification is implicitly considered to have the same exception- |
| 8763 | // specification as an implicit declaration. |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8764 | const FunctionProtoType *DtorType = Destructor->getType()-> |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8765 | getAs<FunctionProtoType>(); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8766 | if (DtorType->hasExceptionSpec()) |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8767 | return; |
| 8768 | |
| Chandler Carruth | 9a79757 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 8769 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 8770 | // the only thing of interest in the destructor type is its extended info. |
| 8771 | // The return and arguments are fixed. |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8772 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
| 8773 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8774 | EPI.ExceptionSpecDecl = Destructor; |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8775 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
| Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8776 | |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8777 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 8778 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 8779 | // change in behavior can break conforming C++03 programs at runtime. |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8780 | // However, we don't have a body or an exception specification yet, so it |
| 8781 | // needs to be done somewhere else. |
| Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8782 | } |
| 8783 | |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8784 | namespace { |
| 8785 | /// \brief An abstract base class for all helper classes used in building the |
| 8786 | // copy/move operators. These classes serve as factory functions and help us |
| 8787 | // avoid using the same Expr* in the AST twice. |
| 8788 | class ExprBuilder { |
| 8789 | ExprBuilder(const ExprBuilder&) LLVM_DELETED_FUNCTION; |
| 8790 | ExprBuilder &operator=(const ExprBuilder&) LLVM_DELETED_FUNCTION; |
| 8791 | |
| 8792 | protected: |
| 8793 | static Expr *assertNotNull(Expr *E) { |
| 8794 | assert(E && "Expression construction must not fail."); |
| 8795 | return E; |
| 8796 | } |
| 8797 | |
| 8798 | public: |
| 8799 | ExprBuilder() {} |
| 8800 | virtual ~ExprBuilder() {} |
| 8801 | |
| 8802 | virtual Expr *build(Sema &S, SourceLocation Loc) const = 0; |
| 8803 | }; |
| 8804 | |
| 8805 | class RefBuilder: public ExprBuilder { |
| 8806 | VarDecl *Var; |
| 8807 | QualType VarType; |
| 8808 | |
| 8809 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8810 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8811 | return assertNotNull(S.BuildDeclRefExpr(Var, VarType, VK_LValue, Loc).take()); |
| 8812 | } |
| 8813 | |
| 8814 | RefBuilder(VarDecl *Var, QualType VarType) |
| 8815 | : Var(Var), VarType(VarType) {} |
| 8816 | }; |
| 8817 | |
| 8818 | class ThisBuilder: public ExprBuilder { |
| 8819 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8820 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8821 | return assertNotNull(S.ActOnCXXThis(Loc).takeAs<Expr>()); |
| 8822 | } |
| 8823 | }; |
| 8824 | |
| 8825 | class CastBuilder: public ExprBuilder { |
| 8826 | const ExprBuilder &Builder; |
| 8827 | QualType Type; |
| 8828 | ExprValueKind Kind; |
| 8829 | const CXXCastPath &Path; |
| 8830 | |
| 8831 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8832 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8833 | return assertNotNull(S.ImpCastExprToType(Builder.build(S, Loc), Type, |
| 8834 | CK_UncheckedDerivedToBase, Kind, |
| 8835 | &Path).take()); |
| 8836 | } |
| 8837 | |
| 8838 | CastBuilder(const ExprBuilder &Builder, QualType Type, ExprValueKind Kind, |
| 8839 | const CXXCastPath &Path) |
| 8840 | : Builder(Builder), Type(Type), Kind(Kind), Path(Path) {} |
| 8841 | }; |
| 8842 | |
| 8843 | class DerefBuilder: public ExprBuilder { |
| 8844 | const ExprBuilder &Builder; |
| 8845 | |
| 8846 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8847 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8848 | return assertNotNull( |
| 8849 | S.CreateBuiltinUnaryOp(Loc, UO_Deref, Builder.build(S, Loc)).take()); |
| 8850 | } |
| 8851 | |
| 8852 | DerefBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 8853 | }; |
| 8854 | |
| 8855 | class MemberBuilder: public ExprBuilder { |
| 8856 | const ExprBuilder &Builder; |
| 8857 | QualType Type; |
| 8858 | CXXScopeSpec SS; |
| 8859 | bool IsArrow; |
| 8860 | LookupResult &MemberLookup; |
| 8861 | |
| 8862 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8863 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8864 | return assertNotNull(S.BuildMemberReferenceExpr( |
| 8865 | Builder.build(S, Loc), Type, Loc, IsArrow, SS, SourceLocation(), 0, |
| 8866 | MemberLookup, 0).take()); |
| 8867 | } |
| 8868 | |
| 8869 | MemberBuilder(const ExprBuilder &Builder, QualType Type, bool IsArrow, |
| 8870 | LookupResult &MemberLookup) |
| 8871 | : Builder(Builder), Type(Type), IsArrow(IsArrow), |
| 8872 | MemberLookup(MemberLookup) {} |
| 8873 | }; |
| 8874 | |
| 8875 | class MoveCastBuilder: public ExprBuilder { |
| 8876 | const ExprBuilder &Builder; |
| 8877 | |
| 8878 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8879 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8880 | return assertNotNull(CastForMoving(S, Builder.build(S, Loc))); |
| 8881 | } |
| 8882 | |
| 8883 | MoveCastBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 8884 | }; |
| 8885 | |
| 8886 | class LvalueConvBuilder: public ExprBuilder { |
| 8887 | const ExprBuilder &Builder; |
| 8888 | |
| 8889 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8890 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8891 | return assertNotNull( |
| 8892 | S.DefaultLvalueConversion(Builder.build(S, Loc)).take()); |
| 8893 | } |
| 8894 | |
| 8895 | LvalueConvBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 8896 | }; |
| 8897 | |
| 8898 | class SubscriptBuilder: public ExprBuilder { |
| 8899 | const ExprBuilder &Base; |
| 8900 | const ExprBuilder &Index; |
| 8901 | |
| 8902 | public: |
| Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 8903 | virtual Expr *build(Sema &S, SourceLocation Loc) const override { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8904 | return assertNotNull(S.CreateBuiltinArraySubscriptExpr( |
| 8905 | Base.build(S, Loc), Loc, Index.build(S, Loc), Loc).take()); |
| 8906 | } |
| 8907 | |
| 8908 | SubscriptBuilder(const ExprBuilder &Base, const ExprBuilder &Index) |
| 8909 | : Base(Base), Index(Index) {} |
| 8910 | }; |
| 8911 | |
| 8912 | } // end anonymous namespace |
| 8913 | |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8914 | /// When generating a defaulted copy or move assignment operator, if a field |
| 8915 | /// should be copied with __builtin_memcpy rather than via explicit assignments, |
| 8916 | /// do so. This optimization only applies for arrays of scalars, and for arrays |
| 8917 | /// of class type where the selected copy/move-assignment operator is trivial. |
| 8918 | static StmtResult |
| 8919 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8920 | const ExprBuilder &ToB, const ExprBuilder &FromB) { |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8921 | // Compute the size of the memory buffer to be copied. |
| 8922 | QualType SizeType = S.Context.getSizeType(); |
| 8923 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 8924 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 8925 | |
| 8926 | // Take the address of the field references for "from" and "to". We |
| 8927 | // directly construct UnaryOperators here because semantic analysis |
| 8928 | // does not permit us to take the address of an xvalue. |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8929 | Expr *From = FromB.build(S, Loc); |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8930 | From = new (S.Context) UnaryOperator(From, UO_AddrOf, |
| 8931 | S.Context.getPointerType(From->getType()), |
| 8932 | VK_RValue, OK_Ordinary, Loc); |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8933 | Expr *To = ToB.build(S, Loc); |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8934 | To = new (S.Context) UnaryOperator(To, UO_AddrOf, |
| 8935 | S.Context.getPointerType(To->getType()), |
| 8936 | VK_RValue, OK_Ordinary, Loc); |
| 8937 | |
| 8938 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 8939 | bool NeedsCollectableMemCpy = |
| 8940 | E->isRecordType() && E->getAs<RecordType>()->getDecl()->hasObjectMember(); |
| 8941 | |
| 8942 | // Create a reference to the __builtin_objc_memmove_collectable function |
| 8943 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 8944 | "__builtin_objc_memmove_collectable" : |
| 8945 | "__builtin_memcpy"; |
| 8946 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 8947 | Sema::LookupOrdinaryName); |
| 8948 | S.LookupName(R, S.TUScope, true); |
| 8949 | |
| 8950 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 8951 | if (!MemCpy) |
| 8952 | // Something went horribly wrong earlier, and we will have complained |
| 8953 | // about it. |
| 8954 | return StmtError(); |
| 8955 | |
| 8956 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
| 8957 | VK_RValue, Loc, 0); |
| 8958 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 8959 | |
| 8960 | Expr *CallArgs[] = { |
| 8961 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 8962 | }; |
| 8963 | ExprResult Call = S.ActOnCallExpr(/*Scope=*/0, MemCpyRef.take(), |
| 8964 | Loc, CallArgs, Loc); |
| 8965 | |
| 8966 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 8967 | return S.Owned(Call.takeAs<Stmt>()); |
| 8968 | } |
| 8969 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8970 | /// \brief Builds a statement that copies/moves the given entity from \p From to |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8971 | /// \c To. |
| 8972 | /// |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8973 | /// This routine is used to copy/move the members of a class with an |
| 8974 | /// implicitly-declared copy/move assignment operator. When the entities being |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8975 | /// copied are arrays, this routine builds for loops to copy them. |
| 8976 | /// |
| 8977 | /// \param S The Sema object used for type-checking. |
| 8978 | /// |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8979 | /// \param Loc The location where the implicit copy/move is being generated. |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8980 | /// |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8981 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 8982 | /// must have this type. |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8983 | /// |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8984 | /// \param To The expression we are copying/moving to. |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8985 | /// |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8986 | /// \param From The expression we are copying/moving from. |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8987 | /// |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8988 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
| Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8989 | /// Otherwise, it's a non-static member subobject. |
| 8990 | /// |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8991 | /// \param Copying Whether we're copying or moving. |
| 8992 | /// |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8993 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 8994 | /// |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8995 | /// \returns A statement or a loop that copies the expressions, or StmtResult(0) |
| 8996 | /// if a memcpy should be used instead. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8997 | static StmtResult |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8998 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8999 | const ExprBuilder &To, const ExprBuilder &From, |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9000 | bool CopyingBaseSubobject, bool Copying, |
| 9001 | unsigned Depth = 0) { |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9002 | // C++11 [class.copy]p28: |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9003 | // Each subobject is assigned in the manner appropriate to its type: |
| 9004 | // |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9005 | // - if the subobject is of class type, as if by a call to operator= with |
| 9006 | // the subobject as the object expression and the corresponding |
| 9007 | // subobject of x as a single function argument (as if by explicit |
| 9008 | // qualification; that is, ignoring any possible virtual overriding |
| 9009 | // functions in more derived classes); |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9010 | // |
| 9011 | // C++03 [class.copy]p13: |
| 9012 | // - if the subobject is of class type, the copy assignment operator for |
| 9013 | // the class is used (as if by explicit qualification; that is, |
| 9014 | // ignoring any possible virtual overriding functions in more derived |
| 9015 | // classes); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9016 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 9017 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9018 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9019 | // Look for operator=. |
| 9020 | DeclarationName Name |
| 9021 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 9022 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 9023 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9024 | |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9025 | // Prior to C++11, filter out any result that isn't a copy/move-assignment |
| 9026 | // operator. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9027 | if (!S.getLangOpts().CPlusPlus11) { |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9028 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 9029 | while (F.hasNext()) { |
| 9030 | NamedDecl *D = F.next(); |
| 9031 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 9032 | if (Method->isCopyAssignmentOperator() || |
| 9033 | (!Copying && Method->isMoveAssignmentOperator())) |
| 9034 | continue; |
| 9035 | |
| 9036 | F.erase(); |
| 9037 | } |
| 9038 | F.done(); |
| John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 9039 | } |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9040 | |
| Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9041 | // Suppress the protected check (C++ [class.protected]) for each of the |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9042 | // assignment operators we found. This strange dance is required when |
| Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9043 | // we're assigning via a base classes's copy-assignment operator. To |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9044 | // ensure that we're getting the right base class subobject (without |
| Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9045 | // ambiguities), we need to cast "this" to that subobject type; to |
| 9046 | // ensure that we don't go through the virtual call mechanism, we need |
| 9047 | // to qualify the operator= name with the base class (see below). However, |
| 9048 | // this means that if the base class has a protected copy assignment |
| 9049 | // operator, the protected member access check will fail. So, we |
| 9050 | // rewrite "protected" access to "public" access in this case, since we |
| 9051 | // know by construction that we're calling from a derived class. |
| 9052 | if (CopyingBaseSubobject) { |
| 9053 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 9054 | L != LEnd; ++L) { |
| 9055 | if (L.getAccess() == AS_protected) |
| 9056 | L.setAccess(AS_public); |
| 9057 | } |
| 9058 | } |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9059 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9060 | // Create the nested-name-specifier that will be used to qualify the |
| 9061 | // reference to operator=; this is required to suppress the virtual |
| 9062 | // call mechanism. |
| 9063 | CXXScopeSpec SS; |
| Manuel Klimek | e716741 | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 9064 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9065 | SS.MakeTrivial(S.Context, |
| 9066 | NestedNameSpecifier::Create(S.Context, 0, false, |
| Manuel Klimek | e716741 | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 9067 | CanonicalT), |
| Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 9068 | Loc); |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9069 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9070 | // Create the reference to operator=. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9071 | ExprResult OpEqualRef |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9072 | = S.BuildMemberReferenceExpr(To.build(S, Loc), T, Loc, /*isArrow=*/false, |
| 9073 | SS, /*TemplateKWLoc=*/SourceLocation(), |
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9074 | /*FirstQualifierInScope=*/0, |
| 9075 | OpLookup, |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9076 | /*TemplateArgs=*/0, |
| 9077 | /*SuppressQualifierCheck=*/true); |
| 9078 | if (OpEqualRef.isInvalid()) |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9079 | return StmtError(); |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9080 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9081 | // Build the call to the assignment operator. |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9082 | |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9083 | Expr *FromInst = From.build(S, Loc); |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9084 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
| Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 9085 | OpEqualRef.takeAs<Expr>(), |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9086 | Loc, FromInst, Loc); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9087 | if (Call.isInvalid()) |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9088 | return StmtError(); |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9089 | |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9090 | // If we built a call to a trivial 'operator=' while copying an array, |
| 9091 | // bail out. We'll replace the whole shebang with a memcpy. |
| 9092 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 9093 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
| 9094 | return StmtResult((Stmt*)0); |
| 9095 | |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9096 | // Convert to an expression-statement, and clean up any produced |
| 9097 | // temporaries. |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9098 | return S.ActOnExprStmt(Call); |
| Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9099 | } |
| John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 9100 | |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9101 | // - if the subobject is of scalar type, the built-in assignment |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9102 | // operator is used. |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9103 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9104 | if (!ArrayTy) { |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9105 | ExprResult Assignment = S.CreateBuiltinBinOp( |
| 9106 | Loc, BO_Assign, To.build(S, Loc), From.build(S, Loc)); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9107 | if (Assignment.isInvalid()) |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9108 | return StmtError(); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9109 | return S.ActOnExprStmt(Assignment); |
| Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9110 | } |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9111 | |
| 9112 | // - if the subobject is an array, each element is assigned, in the |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9113 | // manner appropriate to the element type; |
| Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9114 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9115 | // Construct a loop over the array bounds, e.g., |
| 9116 | // |
| 9117 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 9118 | // |
| 9119 | // that will copy each of the array elements. |
| 9120 | QualType SizeType = S.Context.getSizeType(); |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9121 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9122 | // Create the iteration variable. |
| 9123 | IdentifierInfo *IterationVarName = 0; |
| 9124 | { |
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9125 | SmallString<8> Str; |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9126 | llvm::raw_svector_ostream OS(Str); |
| 9127 | OS << "__i" << Depth; |
| 9128 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 9129 | } |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9130 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9131 | IterationVarName, SizeType, |
| 9132 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9133 | SC_None); |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9134 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9135 | // Initialize the iteration variable to zero. |
| 9136 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
| Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 9137 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9138 | |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9139 | // Creates a reference to the iteration variable. |
| 9140 | RefBuilder IterationVarRef(IterationVar, SizeType); |
| 9141 | LvalueConvBuilder IterationVarRefRVal(IterationVarRef); |
| Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 9142 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9143 | // Create the DeclStmt that holds the iteration variable. |
| 9144 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9145 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9146 | // Subscript the "from" and "to" expressions with the iteration variable. |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9147 | SubscriptBuilder FromIndexCopy(From, IterationVarRefRVal); |
| 9148 | MoveCastBuilder FromIndexMove(FromIndexCopy); |
| 9149 | const ExprBuilder *FromIndex; |
| 9150 | if (Copying) |
| 9151 | FromIndex = &FromIndexCopy; |
| 9152 | else |
| 9153 | FromIndex = &FromIndexMove; |
| 9154 | |
| 9155 | SubscriptBuilder ToIndex(To, IterationVarRefRVal); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9156 | |
| 9157 | // Build the copy/move for an individual element of the array. |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9158 | StmtResult Copy = |
| 9159 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9160 | ToIndex, *FromIndex, CopyingBaseSubobject, |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9161 | Copying, Depth + 1); |
| 9162 | // Bail out if copying fails or if we determined that we should use memcpy. |
| 9163 | if (Copy.isInvalid() || !Copy.get()) |
| 9164 | return Copy; |
| 9165 | |
| 9166 | // Create the comparison against the array bound. |
| 9167 | llvm::APInt Upper |
| 9168 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 9169 | Expr *Comparison |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9170 | = new (S.Context) BinaryOperator(IterationVarRefRVal.build(S, Loc), |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9171 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 9172 | BO_NE, S.Context.BoolTy, |
| 9173 | VK_RValue, OK_Ordinary, Loc, false); |
| 9174 | |
| 9175 | // Create the pre-increment of the iteration variable. |
| 9176 | Expr *Increment |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9177 | = new (S.Context) UnaryOperator(IterationVarRef.build(S, Loc), UO_PreInc, |
| 9178 | SizeType, VK_LValue, OK_Ordinary, Loc); |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9179 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9180 | // Construct the loop that copies all elements of this array. |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9181 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9182 | S.MakeFullExpr(Comparison), |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9183 | 0, S.MakeFullDiscardedValueExpr(Increment), |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9184 | Loc, Copy.take()); |
| Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9185 | } |
| 9186 | |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9187 | static StmtResult |
| 9188 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9189 | const ExprBuilder &To, const ExprBuilder &From, |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9190 | bool CopyingBaseSubobject, bool Copying) { |
| 9191 | // Maybe we should use a memcpy? |
| 9192 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 9193 | T.isTriviallyCopyableType(S.Context)) |
| 9194 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 9195 | |
| 9196 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 9197 | CopyingBaseSubobject, |
| 9198 | Copying, 0)); |
| 9199 | |
| 9200 | // If we ended up picking a trivial assignment operator for an array of a |
| 9201 | // non-trivially-copyable class type, just emit a memcpy. |
| 9202 | if (!Result.isInvalid() && !Result.get()) |
| 9203 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 9204 | |
| 9205 | return Result; |
| 9206 | } |
| 9207 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9208 | Sema::ImplicitExceptionSpecification |
| 9209 | Sema::ComputeDefaultedCopyAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 9210 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9211 | |
| 9212 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 9213 | if (ClassDecl->isInvalidDecl()) |
| 9214 | return ExceptSpec; |
| 9215 | |
| 9216 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9217 | assert(T->getNumParams() == 1 && "not a copy assignment op"); |
| 9218 | unsigned ArgQuals = |
| 9219 | T->getParamType(0).getNonReferenceType().getCVRQualifiers(); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9220 | |
| Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9221 | // C++ [except.spec]p14: |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9222 | // An implicitly declared special member function (Clause 12) shall have an |
| Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9223 | // exception-specification. [...] |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9224 | |
| 9225 | // It is unspecified whether or not an implicit copy assignment operator |
| 9226 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 9227 | // made. As such, this exception specification is effectively unspecified. |
| 9228 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 9229 | // the side of assuming such calls to be made regardless of whether they |
| 9230 | // actually happen. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9231 | for (const auto &Base : ClassDecl->bases()) { |
| 9232 | if (Base.isVirtual()) |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9233 | continue; |
| 9234 | |
| Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 9235 | CXXRecordDecl *BaseClassDecl |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9236 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9237 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 9238 | ArgQuals, false, 0)) |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9239 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyAssign); |
| Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9240 | } |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9241 | |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 9242 | for (const auto &Base : ClassDecl->vbases()) { |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9243 | CXXRecordDecl *BaseClassDecl |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 9244 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9245 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 9246 | ArgQuals, false, 0)) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 9247 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyAssign); |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9248 | } |
| 9249 | |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9250 | for (const auto *Field : ClassDecl->fields()) { |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9251 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9252 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 9253 | if (CXXMethodDecl *CopyAssign = |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9254 | LookupCopyingAssignment(FieldClassDecl, |
| 9255 | ArgQuals | FieldType.getCVRQualifiers(), |
| 9256 | false, 0)) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9257 | ExceptSpec.CalledDecl(Field->getLocation(), CopyAssign); |
| Abramo Bagnara | dd8fc04 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 9258 | } |
| Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9259 | } |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9260 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9261 | return ExceptSpec; |
| Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9262 | } |
| 9263 | |
| 9264 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 9265 | // Note: The following rules are largely analoguous to the copy |
| 9266 | // constructor rules. Note that virtual bases are not taken into account |
| 9267 | // for determining the argument type of the operator. Note also that |
| 9268 | // operators taking an object instead of a reference are allowed. |
| Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9269 | assert(ClassDecl->needsImplicitCopyAssignment()); |
| Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9270 | |
| Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9271 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyAssignment); |
| 9272 | if (DSM.isAlreadyBeingDeclared()) |
| 9273 | return 0; |
| 9274 | |
| Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9275 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 9276 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9277 | bool Const = ClassDecl->implicitCopyAssignmentHasConstParam(); |
| 9278 | if (Const) |
| Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9279 | ArgType = ArgType.withConst(); |
| 9280 | ArgType = Context.getLValueReferenceType(ArgType); |
| 9281 | |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9282 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9283 | CXXCopyAssignment, |
| 9284 | Const); |
| 9285 | |
| Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9286 | // An implicitly-declared copy assignment operator is an inline public |
| 9287 | // member of its class. |
| 9288 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9289 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9290 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9291 | CXXMethodDecl *CopyAssignment = |
| 9292 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 9293 | /*TInfo=*/ 0, /*StorageClass=*/ SC_None, |
| 9294 | /*isInline=*/ true, Constexpr, SourceLocation()); |
| Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9295 | CopyAssignment->setAccess(AS_public); |
| Alexis Hunt | b2f2780 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 9296 | CopyAssignment->setDefaulted(); |
| Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9297 | CopyAssignment->setImplicit(); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9298 | |
| 9299 | // Build an exception specification pointing back at this member. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9300 | FunctionProtoType::ExtProtoInfo EPI = |
| 9301 | getImplicitMethodEPI(*this, CopyAssignment); |
| Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9302 | CopyAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9303 | |
| Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9304 | // Add the parameter to the operator. |
| 9305 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9306 | ClassLoc, ClassLoc, /*Id=*/0, |
| Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9307 | ArgType, /*TInfo=*/0, |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9308 | SC_None, 0); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9309 | CopyAssignment->setParams(FromParam); |
| Alexis Hunt | b2f2780 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 9310 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9311 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 9312 | |
| 9313 | CopyAssignment->setTrivial( |
| 9314 | ClassDecl->needsOverloadResolutionForCopyAssignment() |
| 9315 | ? SpecialMemberIsTrivial(CopyAssignment, CXXCopyAssignment) |
| 9316 | : ClassDecl->hasTrivialCopyAssignment()); |
| 9317 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9318 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 9319 | SetDeclDeleted(CopyAssignment, ClassLoc); |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9320 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9321 | // Note that we have added this copy-assignment operator. |
| 9322 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 9323 | |
| 9324 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9325 | PushOnScopeChains(CopyAssignment, S, false); |
| 9326 | ClassDecl->addDecl(CopyAssignment); |
| 9327 | |
| Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9328 | return CopyAssignment; |
| 9329 | } |
| 9330 | |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9331 | /// Diagnose an implicit copy operation for a class which is odr-used, but |
| 9332 | /// which is deprecated because the class has a user-declared copy constructor, |
| 9333 | /// copy assignment operator, or destructor. |
| 9334 | static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp, |
| 9335 | SourceLocation UseLoc) { |
| 9336 | assert(CopyOp->isImplicit()); |
| 9337 | |
| 9338 | CXXRecordDecl *RD = CopyOp->getParent(); |
| 9339 | CXXMethodDecl *UserDeclaredOperation = 0; |
| 9340 | |
| 9341 | // In Microsoft mode, assignment operations don't affect constructors and |
| 9342 | // vice versa. |
| 9343 | if (RD->hasUserDeclaredDestructor()) { |
| 9344 | UserDeclaredOperation = RD->getDestructor(); |
| 9345 | } else if (!isa<CXXConstructorDecl>(CopyOp) && |
| 9346 | RD->hasUserDeclaredCopyConstructor() && |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 9347 | !S.getLangOpts().MSVCCompat) { |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9348 | // Find any user-declared copy constructor. |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 9349 | for (auto *I : RD->ctors()) { |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9350 | if (I->isCopyConstructor()) { |
| Aaron Ballman | 2a4bd6d | 2014-03-13 16:51:27 +0000 | [diff] [blame] | 9351 | UserDeclaredOperation = I; |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9352 | break; |
| 9353 | } |
| 9354 | } |
| 9355 | assert(UserDeclaredOperation); |
| 9356 | } else if (isa<CXXConstructorDecl>(CopyOp) && |
| 9357 | RD->hasUserDeclaredCopyAssignment() && |
| Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 9358 | !S.getLangOpts().MSVCCompat) { |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9359 | // Find any user-declared move assignment operator. |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 9360 | for (auto *I : RD->methods()) { |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9361 | if (I->isCopyAssignmentOperator()) { |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 9362 | UserDeclaredOperation = I; |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9363 | break; |
| 9364 | } |
| 9365 | } |
| 9366 | assert(UserDeclaredOperation); |
| 9367 | } |
| 9368 | |
| 9369 | if (UserDeclaredOperation) { |
| 9370 | S.Diag(UserDeclaredOperation->getLocation(), |
| 9371 | diag::warn_deprecated_copy_operation) |
| 9372 | << RD << /*copy assignment*/!isa<CXXConstructorDecl>(CopyOp) |
| 9373 | << /*destructor*/isa<CXXDestructorDecl>(UserDeclaredOperation); |
| 9374 | S.Diag(UseLoc, diag::note_member_synthesized_at) |
| 9375 | << (isa<CXXConstructorDecl>(CopyOp) ? Sema::CXXCopyConstructor |
| 9376 | : Sema::CXXCopyAssignment) |
| 9377 | << RD; |
| 9378 | } |
| 9379 | } |
| 9380 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9381 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 9382 | CXXMethodDecl *CopyAssignOperator) { |
| Alexis Hunt | b2f2780 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 9383 | assert((CopyAssignOperator->isDefaulted() && |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9384 | CopyAssignOperator->isOverloadedOperator() && |
| 9385 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
| Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9386 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 9387 | !CopyAssignOperator->isDeleted()) && |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9388 | "DefineImplicitCopyAssignment called for wrong function"); |
| 9389 | |
| 9390 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 9391 | |
| 9392 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 9393 | CopyAssignOperator->setInvalidDecl(); |
| 9394 | return; |
| 9395 | } |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9396 | |
| 9397 | // C++11 [class.copy]p18: |
| 9398 | // The [definition of an implicitly declared copy assignment operator] is |
| 9399 | // deprecated if the class has a user-declared copy constructor or a |
| 9400 | // user-declared destructor. |
| 9401 | if (getLangOpts().CPlusPlus11 && CopyAssignOperator->isImplicit()) |
| 9402 | diagnoseDeprecatedCopyOperation(*this, CopyAssignOperator, CurrentLocation); |
| 9403 | |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 9404 | CopyAssignOperator->markUsed(Context); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9405 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9406 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
| Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 9407 | DiagnosticErrorTrap Trap(Diags); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9408 | |
| 9409 | // C++0x [class.copy]p30: |
| 9410 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 9411 | // for a non-union class X performs memberwise copy assignment of its |
| 9412 | // subobjects. The direct base classes of X are assigned first, in the |
| 9413 | // order of their declaration in the base-specifier-list, and then the |
| 9414 | // immediate non-static data members of X are assigned, in the order in |
| 9415 | // which they were declared in the class definition. |
| 9416 | |
| 9417 | // The statements that form the synthesized function body. |
| Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9418 | SmallVector<Stmt*, 8> Statements; |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9419 | |
| 9420 | // The parameter for the "other" object, which we are copying from. |
| 9421 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 9422 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 9423 | QualType OtherRefType = Other->getType(); |
| 9424 | if (const LValueReferenceType *OtherRef |
| 9425 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 9426 | OtherRefType = OtherRef->getPointeeType(); |
| 9427 | OtherQuals = OtherRefType.getQualifiers(); |
| 9428 | } |
| 9429 | |
| 9430 | // Our location for everything implicitly-generated. |
| 9431 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 9432 | |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9433 | // Builds a DeclRefExpr for the "other" object. |
| 9434 | RefBuilder OtherRef(Other, OtherRefType); |
| 9435 | |
| 9436 | // Builds the "this" pointer. |
| 9437 | ThisBuilder This; |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9438 | |
| 9439 | // Assign base classes. |
| 9440 | bool Invalid = false; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9441 | for (auto &Base : ClassDecl->bases()) { |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9442 | // Form the assignment: |
| 9443 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9444 | QualType BaseType = Base.getType().getUnqualifiedType(); |
| Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9445 | if (!BaseType->isRecordType()) { |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9446 | Invalid = true; |
| 9447 | continue; |
| 9448 | } |
| 9449 | |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9450 | CXXCastPath BasePath; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9451 | BasePath.push_back(&Base); |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9452 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9453 | // Construct the "from" expression, which is an implicit cast to the |
| 9454 | // appropriately-qualified base type. |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9455 | CastBuilder From(OtherRef, Context.getQualifiedType(BaseType, OtherQuals), |
| 9456 | VK_LValue, BasePath); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9457 | |
| 9458 | // Dereference "this". |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9459 | DerefBuilder DerefThis(This); |
| 9460 | CastBuilder To(DerefThis, |
| 9461 | Context.getCVRQualifiedType( |
| 9462 | BaseType, CopyAssignOperator->getTypeQualifiers()), |
| 9463 | VK_LValue, BasePath); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9464 | |
| 9465 | // Build the copy. |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9466 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9467 | To, From, |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9468 | /*CopyingBaseSubobject=*/true, |
| 9469 | /*Copying=*/true); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9470 | if (Copy.isInvalid()) { |
| Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9471 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9472 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9473 | CopyAssignOperator->setInvalidDecl(); |
| 9474 | return; |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
| 9477 | // Success! Record the copy. |
| 9478 | Statements.push_back(Copy.takeAs<Expr>()); |
| 9479 | } |
| 9480 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9481 | // Assign non-static members. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9482 | for (auto *Field : ClassDecl->fields()) { |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9483 | if (Field->isUnnamedBitfield()) |
| 9484 | continue; |
| Eli Friedman | c9817fd | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9485 | |
| 9486 | if (Field->isInvalidDecl()) { |
| 9487 | Invalid = true; |
| 9488 | continue; |
| 9489 | } |
| 9490 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9491 | // Check for members of reference type; we can't copy those. |
| 9492 | if (Field->getType()->isReferenceType()) { |
| 9493 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9494 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9495 | Diag(Field->getLocation(), diag::note_declared_at); |
| Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9496 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9497 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9498 | Invalid = true; |
| 9499 | continue; |
| 9500 | } |
| 9501 | |
| 9502 | // Check for members of const-qualified, non-class type. |
| 9503 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9504 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9505 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9506 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9507 | Diag(Field->getLocation(), diag::note_declared_at); |
| Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9508 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9509 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9510 | Invalid = true; |
| 9511 | continue; |
| 9512 | } |
| John McCall | 1b1a1db | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 9513 | |
| 9514 | // Suppress assigning zero-width bitfields. |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9515 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9516 | continue; |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9517 | |
| 9518 | QualType FieldType = Field->getType().getNonReferenceType(); |
| Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 9519 | if (FieldType->isIncompleteArrayType()) { |
| 9520 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9521 | "Incomplete array type is not valid"); |
| 9522 | continue; |
| 9523 | } |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9524 | |
| 9525 | // Build references to the field in the object we're copying from and to. |
| 9526 | CXXScopeSpec SS; // Intentionally empty |
| 9527 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9528 | LookupMemberName); |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9529 | MemberLookup.addDecl(Field); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9530 | MemberLookup.resolveKind(); |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9531 | |
| 9532 | MemberBuilder From(OtherRef, OtherRefType, /*IsArrow=*/false, MemberLookup); |
| 9533 | |
| 9534 | MemberBuilder To(This, getCurrentThisType(), /*IsArrow=*/true, MemberLookup); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9535 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9536 | // Build the copy of this field. |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9537 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9538 | To, From, |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9539 | /*CopyingBaseSubobject=*/false, |
| 9540 | /*Copying=*/true); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9541 | if (Copy.isInvalid()) { |
| Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9542 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9543 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9544 | CopyAssignOperator->setInvalidDecl(); |
| 9545 | return; |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9546 | } |
| 9547 | |
| 9548 | // Success! Record the copy. |
| 9549 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 9550 | } |
| 9551 | |
| 9552 | if (!Invalid) { |
| 9553 | // Add a "return *this;" |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9554 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9555 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9556 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9557 | if (Return.isInvalid()) |
| 9558 | Invalid = true; |
| 9559 | else { |
| 9560 | Statements.push_back(Return.takeAs<Stmt>()); |
| Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9561 | |
| 9562 | if (Trap.hasErrorOccurred()) { |
| 9563 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9564 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9565 | Invalid = true; |
| 9566 | } |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9567 | } |
| 9568 | } |
| 9569 | |
| 9570 | if (Invalid) { |
| 9571 | CopyAssignOperator->setInvalidDecl(); |
| 9572 | return; |
| 9573 | } |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9574 | |
| 9575 | StmtResult Body; |
| 9576 | { |
| 9577 | CompoundScopeRAII CompoundScope(*this); |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9578 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9579 | /*isStmtExpr=*/false); |
| 9580 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9581 | } |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9582 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
| Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9583 | |
| 9584 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9585 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 9586 | } |
| Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9587 | } |
| 9588 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9589 | Sema::ImplicitExceptionSpecification |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9590 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 9591 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9592 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9593 | ImplicitExceptionSpecification ExceptSpec(*this); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9594 | if (ClassDecl->isInvalidDecl()) |
| 9595 | return ExceptSpec; |
| 9596 | |
| 9597 | // C++0x [except.spec]p14: |
| 9598 | // An implicitly declared special member function (Clause 12) shall have an |
| 9599 | // exception-specification. [...] |
| 9600 | |
| 9601 | // It is unspecified whether or not an implicit move assignment operator |
| 9602 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 9603 | // made. As such, this exception specification is effectively unspecified. |
| 9604 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 9605 | // the side of assuming such calls to be made regardless of whether they |
| 9606 | // actually happen. |
| 9607 | // Note that a move constructor is not implicitly declared when there are |
| 9608 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9609 | for (const auto &Base : ClassDecl->bases()) { |
| 9610 | if (Base.isVirtual()) |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9611 | continue; |
| 9612 | |
| 9613 | CXXRecordDecl *BaseClassDecl |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9614 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9615 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9616 | 0, false, 0)) |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9617 | ExceptSpec.CalledDecl(Base.getLocStart(), MoveAssign); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9618 | } |
| 9619 | |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 9620 | for (const auto &Base : ClassDecl->vbases()) { |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9621 | CXXRecordDecl *BaseClassDecl |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 9622 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9623 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9624 | 0, false, 0)) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 9625 | ExceptSpec.CalledDecl(Base.getLocStart(), MoveAssign); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9626 | } |
| 9627 | |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9628 | for (const auto *Field : ClassDecl->fields()) { |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9629 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9630 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9631 | if (CXXMethodDecl *MoveAssign = |
| 9632 | LookupMovingAssignment(FieldClassDecl, |
| 9633 | FieldType.getCVRQualifiers(), |
| 9634 | false, 0)) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9635 | ExceptSpec.CalledDecl(Field->getLocation(), MoveAssign); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9636 | } |
| 9637 | } |
| 9638 | |
| 9639 | return ExceptSpec; |
| 9640 | } |
| 9641 | |
| 9642 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9643 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 9644 | |
| Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9645 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveAssignment); |
| 9646 | if (DSM.isAlreadyBeingDeclared()) |
| 9647 | return 0; |
| 9648 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9649 | // Note: The following rules are largely analoguous to the move |
| 9650 | // constructor rules. |
| 9651 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9652 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 9653 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 9654 | ArgType = Context.getRValueReferenceType(ArgType); |
| 9655 | |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9656 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9657 | CXXMoveAssignment, |
| 9658 | false); |
| 9659 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9660 | // An implicitly-declared move assignment operator is an inline public |
| 9661 | // member of its class. |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9662 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 9663 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9664 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9665 | CXXMethodDecl *MoveAssignment = |
| 9666 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 9667 | /*TInfo=*/0, /*StorageClass=*/SC_None, |
| 9668 | /*isInline=*/true, Constexpr, SourceLocation()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9669 | MoveAssignment->setAccess(AS_public); |
| 9670 | MoveAssignment->setDefaulted(); |
| 9671 | MoveAssignment->setImplicit(); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9672 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9673 | // Build an exception specification pointing back at this member. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9674 | FunctionProtoType::ExtProtoInfo EPI = |
| 9675 | getImplicitMethodEPI(*this, MoveAssignment); |
| Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9676 | MoveAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9677 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9678 | // Add the parameter to the operator. |
| 9679 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 9680 | ClassLoc, ClassLoc, /*Id=*/0, |
| 9681 | ArgType, /*TInfo=*/0, |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9682 | SC_None, 0); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9683 | MoveAssignment->setParams(FromParam); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9684 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9685 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 9686 | |
| 9687 | MoveAssignment->setTrivial( |
| 9688 | ClassDecl->needsOverloadResolutionForMoveAssignment() |
| 9689 | ? SpecialMemberIsTrivial(MoveAssignment, CXXMoveAssignment) |
| 9690 | : ClassDecl->hasTrivialMoveAssignment()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9691 | |
| Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 9692 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
| Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 9693 | ClassDecl->setImplicitMoveAssignmentIsDeleted(); |
| 9694 | SetDeclDeleted(MoveAssignment, ClassLoc); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9695 | } |
| 9696 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9697 | // Note that we have added this copy-assignment operator. |
| 9698 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 9699 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9700 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9701 | PushOnScopeChains(MoveAssignment, S, false); |
| 9702 | ClassDecl->addDecl(MoveAssignment); |
| 9703 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9704 | return MoveAssignment; |
| 9705 | } |
| 9706 | |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9707 | /// Check if we're implicitly defining a move assignment operator for a class |
| 9708 | /// with virtual bases. Such a move assignment might move-assign the virtual |
| 9709 | /// base multiple times. |
| 9710 | static void checkMoveAssignmentForRepeatedMove(Sema &S, CXXRecordDecl *Class, |
| 9711 | SourceLocation CurrentLocation) { |
| 9712 | assert(!Class->isDependentContext() && "should not define dependent move"); |
| 9713 | |
| 9714 | // Only a virtual base could get implicitly move-assigned multiple times. |
| 9715 | // Only a non-trivial move assignment can observe this. We only want to |
| 9716 | // diagnose if we implicitly define an assignment operator that assigns |
| 9717 | // two base classes, both of which move-assign the same virtual base. |
| 9718 | if (Class->getNumVBases() == 0 || Class->hasTrivialMoveAssignment() || |
| 9719 | Class->getNumBases() < 2) |
| 9720 | return; |
| 9721 | |
| 9722 | llvm::SmallVector<CXXBaseSpecifier *, 16> Worklist; |
| 9723 | typedef llvm::DenseMap<CXXRecordDecl*, CXXBaseSpecifier*> VBaseMap; |
| 9724 | VBaseMap VBases; |
| 9725 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9726 | for (auto &BI : Class->bases()) { |
| 9727 | Worklist.push_back(&BI); |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9728 | while (!Worklist.empty()) { |
| 9729 | CXXBaseSpecifier *BaseSpec = Worklist.pop_back_val(); |
| 9730 | CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 9731 | |
| 9732 | // If the base has no non-trivial move assignment operators, |
| 9733 | // we don't care about moves from it. |
| 9734 | if (!Base->hasNonTrivialMoveAssignment()) |
| 9735 | continue; |
| 9736 | |
| 9737 | // If there's nothing virtual here, skip it. |
| 9738 | if (!BaseSpec->isVirtual() && !Base->getNumVBases()) |
| 9739 | continue; |
| 9740 | |
| 9741 | // If we're not actually going to call a move assignment for this base, |
| 9742 | // or the selected move assignment is trivial, skip it. |
| 9743 | Sema::SpecialMemberOverloadResult *SMOR = |
| 9744 | S.LookupSpecialMember(Base, Sema::CXXMoveAssignment, |
| 9745 | /*ConstArg*/false, /*VolatileArg*/false, |
| 9746 | /*RValueThis*/true, /*ConstThis*/false, |
| 9747 | /*VolatileThis*/false); |
| 9748 | if (!SMOR->getMethod() || SMOR->getMethod()->isTrivial() || |
| 9749 | !SMOR->getMethod()->isMoveAssignmentOperator()) |
| 9750 | continue; |
| 9751 | |
| 9752 | if (BaseSpec->isVirtual()) { |
| 9753 | // We're going to move-assign this virtual base, and its move |
| 9754 | // assignment operator is not trivial. If this can happen for |
| 9755 | // multiple distinct direct bases of Class, diagnose it. (If it |
| 9756 | // only happens in one base, we'll diagnose it when synthesizing |
| 9757 | // that base class's move assignment operator.) |
| 9758 | CXXBaseSpecifier *&Existing = |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9759 | VBases.insert(std::make_pair(Base->getCanonicalDecl(), &BI)) |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9760 | .first->second; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9761 | if (Existing && Existing != &BI) { |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9762 | S.Diag(CurrentLocation, diag::warn_vbase_moved_multiple_times) |
| 9763 | << Class << Base; |
| 9764 | S.Diag(Existing->getLocStart(), diag::note_vbase_moved_here) |
| 9765 | << (Base->getCanonicalDecl() == |
| 9766 | Existing->getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 9767 | << Base << Existing->getType() << Existing->getSourceRange(); |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9768 | S.Diag(BI.getLocStart(), diag::note_vbase_moved_here) |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9769 | << (Base->getCanonicalDecl() == |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9770 | BI.getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 9771 | << Base << BI.getType() << BaseSpec->getSourceRange(); |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9772 | |
| 9773 | // Only diagnose each vbase once. |
| 9774 | Existing = 0; |
| 9775 | } |
| 9776 | } else { |
| 9777 | // Only walk over bases that have defaulted move assignment operators. |
| 9778 | // We assume that any user-provided move assignment operator handles |
| 9779 | // the multiple-moves-of-vbase case itself somehow. |
| 9780 | if (!SMOR->getMethod()->isDefaulted()) |
| 9781 | continue; |
| 9782 | |
| 9783 | // We're going to move the base classes of Base. Add them to the list. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9784 | for (auto &BI : Base->bases()) |
| 9785 | Worklist.push_back(&BI); |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9786 | } |
| 9787 | } |
| 9788 | } |
| 9789 | } |
| 9790 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9791 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 9792 | CXXMethodDecl *MoveAssignOperator) { |
| 9793 | assert((MoveAssignOperator->isDefaulted() && |
| 9794 | MoveAssignOperator->isOverloadedOperator() && |
| 9795 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
| Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9796 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 9797 | !MoveAssignOperator->isDeleted()) && |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9798 | "DefineImplicitMoveAssignment called for wrong function"); |
| 9799 | |
| 9800 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 9801 | |
| 9802 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 9803 | MoveAssignOperator->setInvalidDecl(); |
| 9804 | return; |
| 9805 | } |
| 9806 | |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 9807 | MoveAssignOperator->markUsed(Context); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9808 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9809 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9810 | DiagnosticErrorTrap Trap(Diags); |
| 9811 | |
| 9812 | // C++0x [class.copy]p28: |
| 9813 | // The implicitly-defined or move assignment operator for a non-union class |
| 9814 | // X performs memberwise move assignment of its subobjects. The direct base |
| 9815 | // classes of X are assigned first, in the order of their declaration in the |
| 9816 | // base-specifier-list, and then the immediate non-static data members of X |
| 9817 | // are assigned, in the order in which they were declared in the class |
| 9818 | // definition. |
| 9819 | |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9820 | // Issue a warning if our implicit move assignment operator will move |
| 9821 | // from a virtual base more than once. |
| 9822 | checkMoveAssignmentForRepeatedMove(*this, ClassDecl, CurrentLocation); |
| Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 9823 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9824 | // The statements that form the synthesized function body. |
| Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9825 | SmallVector<Stmt*, 8> Statements; |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9826 | |
| 9827 | // The parameter for the "other" object, which we are move from. |
| 9828 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 9829 | QualType OtherRefType = Other->getType()-> |
| 9830 | getAs<RValueReferenceType>()->getPointeeType(); |
| David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 9831 | assert(!OtherRefType.getQualifiers() && |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9832 | "Bad argument type of defaulted move assignment"); |
| 9833 | |
| 9834 | // Our location for everything implicitly-generated. |
| 9835 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 9836 | |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9837 | // Builds a reference to the "other" object. |
| 9838 | RefBuilder OtherRef(Other, OtherRefType); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9839 | // Cast to rvalue. |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9840 | MoveCastBuilder MoveOther(OtherRef); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9841 | |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9842 | // Builds the "this" pointer. |
| 9843 | ThisBuilder This; |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9844 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9845 | // Assign base classes. |
| 9846 | bool Invalid = false; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9847 | for (auto &Base : ClassDecl->bases()) { |
| Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9848 | // C++11 [class.copy]p28: |
| 9849 | // It is unspecified whether subobjects representing virtual base classes |
| 9850 | // are assigned more than once by the implicitly-defined copy assignment |
| 9851 | // operator. |
| 9852 | // FIXME: Do not assign to a vbase that will be assigned by some other base |
| 9853 | // class. For a move-assignment, this can result in the vbase being moved |
| 9854 | // multiple times. |
| 9855 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9856 | // Form the assignment: |
| 9857 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9858 | QualType BaseType = Base.getType().getUnqualifiedType(); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9859 | if (!BaseType->isRecordType()) { |
| 9860 | Invalid = true; |
| 9861 | continue; |
| 9862 | } |
| 9863 | |
| 9864 | CXXCastPath BasePath; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 9865 | BasePath.push_back(&Base); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9866 | |
| 9867 | // Construct the "from" expression, which is an implicit cast to the |
| 9868 | // appropriately-qualified base type. |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9869 | CastBuilder From(OtherRef, BaseType, VK_XValue, BasePath); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9870 | |
| 9871 | // Dereference "this". |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9872 | DerefBuilder DerefThis(This); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9873 | |
| 9874 | // Implicitly cast "this" to the appropriately-qualified base type. |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9875 | CastBuilder To(DerefThis, |
| 9876 | Context.getCVRQualifiedType( |
| 9877 | BaseType, MoveAssignOperator->getTypeQualifiers()), |
| 9878 | VK_LValue, BasePath); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9879 | |
| 9880 | // Build the move. |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9881 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9882 | To, From, |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9883 | /*CopyingBaseSubobject=*/true, |
| 9884 | /*Copying=*/false); |
| 9885 | if (Move.isInvalid()) { |
| 9886 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9887 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9888 | MoveAssignOperator->setInvalidDecl(); |
| 9889 | return; |
| 9890 | } |
| 9891 | |
| 9892 | // Success! Record the move. |
| 9893 | Statements.push_back(Move.takeAs<Expr>()); |
| 9894 | } |
| 9895 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9896 | // Assign non-static members. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9897 | for (auto *Field : ClassDecl->fields()) { |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9898 | if (Field->isUnnamedBitfield()) |
| 9899 | continue; |
| 9900 | |
| Eli Friedman | c9817fd | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9901 | if (Field->isInvalidDecl()) { |
| 9902 | Invalid = true; |
| 9903 | continue; |
| 9904 | } |
| 9905 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9906 | // Check for members of reference type; we can't move those. |
| 9907 | if (Field->getType()->isReferenceType()) { |
| 9908 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9909 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9910 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9911 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9912 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9913 | Invalid = true; |
| 9914 | continue; |
| 9915 | } |
| 9916 | |
| 9917 | // Check for members of const-qualified, non-class type. |
| 9918 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9919 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9920 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9921 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9922 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9923 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9924 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9925 | Invalid = true; |
| 9926 | continue; |
| 9927 | } |
| 9928 | |
| 9929 | // Suppress assigning zero-width bitfields. |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9930 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9931 | continue; |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9932 | |
| 9933 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 9934 | if (FieldType->isIncompleteArrayType()) { |
| 9935 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9936 | "Incomplete array type is not valid"); |
| 9937 | continue; |
| 9938 | } |
| 9939 | |
| 9940 | // Build references to the field in the object we're copying from and to. |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9941 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9942 | LookupMemberName); |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 9943 | MemberLookup.addDecl(Field); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9944 | MemberLookup.resolveKind(); |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9945 | MemberBuilder From(MoveOther, OtherRefType, |
| 9946 | /*IsArrow=*/false, MemberLookup); |
| 9947 | MemberBuilder To(This, getCurrentThisType(), |
| 9948 | /*IsArrow=*/true, MemberLookup); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9949 | |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9950 | assert(!From.build(*this, Loc)->isLValue() && // could be xvalue or prvalue |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9951 | "Member reference with rvalue base must be rvalue except for reference " |
| 9952 | "members, which aren't allowed for move assignment."); |
| 9953 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9954 | // Build the move of this field. |
| Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9955 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9956 | To, From, |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9957 | /*CopyingBaseSubobject=*/false, |
| 9958 | /*Copying=*/false); |
| 9959 | if (Move.isInvalid()) { |
| 9960 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9961 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9962 | MoveAssignOperator->setInvalidDecl(); |
| 9963 | return; |
| 9964 | } |
| Richard Smith | 11d1959 | 2012-11-12 23:33:00 +0000 | [diff] [blame] | 9965 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9966 | // Success! Record the copy. |
| 9967 | Statements.push_back(Move.takeAs<Stmt>()); |
| 9968 | } |
| 9969 | |
| 9970 | if (!Invalid) { |
| 9971 | // Add a "return *this;" |
| Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9972 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9973 | |
| 9974 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 9975 | if (Return.isInvalid()) |
| 9976 | Invalid = true; |
| 9977 | else { |
| 9978 | Statements.push_back(Return.takeAs<Stmt>()); |
| 9979 | |
| 9980 | if (Trap.hasErrorOccurred()) { |
| 9981 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9982 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9983 | Invalid = true; |
| 9984 | } |
| 9985 | } |
| 9986 | } |
| 9987 | |
| 9988 | if (Invalid) { |
| 9989 | MoveAssignOperator->setInvalidDecl(); |
| 9990 | return; |
| 9991 | } |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9992 | |
| 9993 | StmtResult Body; |
| 9994 | { |
| 9995 | CompoundScopeRAII CompoundScope(*this); |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9996 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9997 | /*isStmtExpr=*/false); |
| 9998 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9999 | } |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10000 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 10001 | |
| 10002 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10003 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 10004 | } |
| 10005 | } |
| 10006 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10007 | Sema::ImplicitExceptionSpecification |
| 10008 | Sema::ComputeDefaultedCopyCtorExceptionSpec(CXXMethodDecl *MD) { |
| 10009 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 10010 | |
| 10011 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 10012 | if (ClassDecl->isInvalidDecl()) |
| 10013 | return ExceptSpec; |
| 10014 | |
| 10015 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 10016 | assert(T->getNumParams() >= 1 && "not a copy ctor"); |
| 10017 | unsigned Quals = T->getParamType(0).getNonReferenceType().getCVRQualifiers(); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10018 | |
| Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10019 | // C++ [except.spec]p14: |
| 10020 | // An implicitly declared special member function (Clause 12) shall have an |
| 10021 | // exception-specification. [...] |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 10022 | for (const auto &Base : ClassDecl->bases()) { |
| Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10023 | // Virtual bases are handled below. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 10024 | if (Base.isVirtual()) |
| Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10025 | continue; |
| 10026 | |
| Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 10027 | CXXRecordDecl *BaseClassDecl |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 10028 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10029 | if (CXXConstructorDecl *CopyConstructor = |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 10030 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 10031 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyConstructor); |
| Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10032 | } |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 10033 | for (const auto &Base : ClassDecl->vbases()) { |
| Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 10034 | CXXRecordDecl *BaseClassDecl |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 10035 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10036 | if (CXXConstructorDecl *CopyConstructor = |
| Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 10037 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 10038 | ExceptSpec.CalledDecl(Base.getLocStart(), CopyConstructor); |
| Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10039 | } |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 10040 | for (const auto *Field : ClassDecl->fields()) { |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 10041 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10042 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 10043 | if (CXXConstructorDecl *CopyConstructor = |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10044 | LookupCopyingConstructor(FieldClassDecl, |
| 10045 | Quals | FieldType.getCVRQualifiers())) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10046 | ExceptSpec.CalledDecl(Field->getLocation(), CopyConstructor); |
| Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10047 | } |
| 10048 | } |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 10049 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10050 | return ExceptSpec; |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10051 | } |
| 10052 | |
| 10053 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 10054 | CXXRecordDecl *ClassDecl) { |
| 10055 | // C++ [class.copy]p4: |
| 10056 | // If the class definition does not explicitly declare a copy |
| 10057 | // constructor, one is declared implicitly. |
| Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 10058 | assert(ClassDecl->needsImplicitCopyConstructor()); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10059 | |
| Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10060 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyConstructor); |
| 10061 | if (DSM.isAlreadyBeingDeclared()) |
| 10062 | return 0; |
| 10063 | |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10064 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 10065 | QualType ArgType = ClassType; |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 10066 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10067 | if (Const) |
| 10068 | ArgType = ArgType.withConst(); |
| 10069 | ArgType = Context.getLValueReferenceType(ArgType); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10070 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10071 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 10072 | CXXCopyConstructor, |
| 10073 | Const); |
| 10074 | |
| Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10075 | DeclarationName Name |
| 10076 | = Context.DeclarationNames.getCXXConstructorName( |
| 10077 | Context.getCanonicalType(ClassType)); |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10078 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 10079 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10080 | |
| 10081 | // An implicitly-declared copy constructor is an inline public |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10082 | // member of its class. |
| 10083 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10084 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10085 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10086 | Constexpr); |
| Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10087 | CopyConstructor->setAccess(AS_public); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10088 | CopyConstructor->setDefaulted(); |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10089 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10090 | // Build an exception specification pointing back at this member. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10091 | FunctionProtoType::ExtProtoInfo EPI = |
| 10092 | getImplicitMethodEPI(*this, CopyConstructor); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10093 | CopyConstructor->setType( |
| Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 10094 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10095 | |
| Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10096 | // Add the parameter to the constructor. |
| 10097 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10098 | ClassLoc, ClassLoc, |
| Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10099 | /*IdentifierInfo=*/0, |
| 10100 | ArgType, /*TInfo=*/0, |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 10101 | SC_None, 0); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 10102 | CopyConstructor->setParams(FromParam); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10103 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10104 | CopyConstructor->setTrivial( |
| 10105 | ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 10106 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor) |
| 10107 | : ClassDecl->hasTrivialCopyConstructor()); |
| Alexis Hunt | e77a28f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 10108 | |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10109 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 10110 | SetDeclDeleted(CopyConstructor, ClassLoc); |
| Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10111 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10112 | // Note that we have declared this constructor. |
| 10113 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 10114 | |
| 10115 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 10116 | PushOnScopeChains(CopyConstructor, S, false); |
| 10117 | ClassDecl->addDecl(CopyConstructor); |
| 10118 | |
| Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10119 | return CopyConstructor; |
| 10120 | } |
| 10121 | |
| Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10122 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10123 | CXXConstructorDecl *CopyConstructor) { |
| 10124 | assert((CopyConstructor->isDefaulted() && |
| 10125 | CopyConstructor->isCopyConstructor() && |
| Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10126 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 10127 | !CopyConstructor->isDeleted()) && |
| Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10128 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10129 | |
| Anders Carlsson | 7a0ffdb | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 10130 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
| Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10131 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10132 | |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10133 | // C++11 [class.copy]p7: |
| Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 10134 | // The [definition of an implicitly declared copy constructor] is |
| Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10135 | // deprecated if the class has a user-declared copy assignment operator |
| 10136 | // or a user-declared destructor. |
| 10137 | if (getLangOpts().CPlusPlus11 && CopyConstructor->isImplicit()) |
| 10138 | diagnoseDeprecatedCopyOperation(*this, CopyConstructor, CurrentLocation); |
| 10139 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10140 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
| Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 10141 | DiagnosticErrorTrap Trap(Diags); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10142 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 10143 | if (SetCtorInitializers(CopyConstructor, /*AnyErrors=*/false) || |
| Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 10144 | Trap.hasErrorOccurred()) { |
| Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 10145 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 10146 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
| Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 10147 | CopyConstructor->setInvalidDecl(); |
| Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 10148 | } else { |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10149 | Sema::CompoundScopeRAII CompoundScope(*this); |
| Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 10150 | CopyConstructor->setBody(ActOnCompoundStmt( |
| 10151 | CopyConstructor->getLocation(), CopyConstructor->getLocation(), None, |
| 10152 | /*isStmtExpr=*/ false).takeAs<Stmt>()); |
| Anders Carlsson | 53e1ba9 | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 10153 | } |
| Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 10154 | |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10155 | CopyConstructor->markUsed(Context); |
| Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 10156 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10157 | L->CompletedImplicitDefinition(CopyConstructor); |
| 10158 | } |
| Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10159 | } |
| 10160 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10161 | Sema::ImplicitExceptionSpecification |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10162 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXMethodDecl *MD) { |
| 10163 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 10164 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10165 | // C++ [except.spec]p14: |
| 10166 | // An implicitly declared special member function (Clause 12) shall have an |
| 10167 | // exception-specification. [...] |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10168 | ImplicitExceptionSpecification ExceptSpec(*this); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10169 | if (ClassDecl->isInvalidDecl()) |
| 10170 | return ExceptSpec; |
| 10171 | |
| 10172 | // Direct base-class constructors. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 10173 | for (const auto &B : ClassDecl->bases()) { |
| 10174 | if (B.isVirtual()) // Handled below. |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10175 | continue; |
| 10176 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 10177 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10178 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10179 | CXXConstructorDecl *Constructor = |
| 10180 | LookupMovingConstructor(BaseClassDecl, 0); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10181 | // If this is a deleted function, add it anyway. This might be conformant |
| 10182 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10183 | if (Constructor) |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 10184 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10185 | } |
| 10186 | } |
| 10187 | |
| 10188 | // Virtual base-class constructors. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 10189 | for (const auto &B : ClassDecl->vbases()) { |
| 10190 | if (const RecordType *BaseType = B.getType()->getAs<RecordType>()) { |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10191 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10192 | CXXConstructorDecl *Constructor = |
| 10193 | LookupMovingConstructor(BaseClassDecl, 0); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10194 | // If this is a deleted function, add it anyway. This might be conformant |
| 10195 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10196 | if (Constructor) |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 10197 | ExceptSpec.CalledDecl(B.getLocStart(), Constructor); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10198 | } |
| 10199 | } |
| 10200 | |
| 10201 | // Field constructors. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 10202 | for (const auto *F : ClassDecl->fields()) { |
| Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10203 | QualType FieldType = Context.getBaseElementType(F->getType()); |
| 10204 | if (CXXRecordDecl *FieldRecDecl = FieldType->getAsCXXRecordDecl()) { |
| 10205 | CXXConstructorDecl *Constructor = |
| 10206 | LookupMovingConstructor(FieldRecDecl, FieldType.getCVRQualifiers()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10207 | // If this is a deleted function, add it anyway. This might be conformant |
| 10208 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10209 | // In particular, the problem is that this function never gets called. It |
| 10210 | // might just be ill-formed because this function attempts to refer to |
| 10211 | // a deleted function here. |
| 10212 | if (Constructor) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10213 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10214 | } |
| 10215 | } |
| 10216 | |
| 10217 | return ExceptSpec; |
| 10218 | } |
| 10219 | |
| 10220 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 10221 | CXXRecordDecl *ClassDecl) { |
| Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 10222 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 10223 | |
| Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10224 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveConstructor); |
| 10225 | if (DSM.isAlreadyBeingDeclared()) |
| 10226 | return 0; |
| 10227 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10228 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 10229 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10230 | |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10231 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 10232 | CXXMoveConstructor, |
| 10233 | false); |
| 10234 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10235 | DeclarationName Name |
| 10236 | = Context.DeclarationNames.getCXXConstructorName( |
| 10237 | Context.getCanonicalType(ClassType)); |
| 10238 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 10239 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 10240 | |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 10241 | // C++11 [class.copy]p11: |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10242 | // An implicitly-declared copy/move constructor is an inline public |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10243 | // member of its class. |
| 10244 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10245 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10246 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
| Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10247 | Constexpr); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10248 | MoveConstructor->setAccess(AS_public); |
| 10249 | MoveConstructor->setDefaulted(); |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10250 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10251 | // Build an exception specification pointing back at this member. |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10252 | FunctionProtoType::ExtProtoInfo EPI = |
| 10253 | getImplicitMethodEPI(*this, MoveConstructor); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10254 | MoveConstructor->setType( |
| Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 10255 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10256 | |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10257 | // Add the parameter to the constructor. |
| 10258 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 10259 | ClassLoc, ClassLoc, |
| 10260 | /*IdentifierInfo=*/0, |
| 10261 | ArgType, /*TInfo=*/0, |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10262 | SC_None, 0); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 10263 | MoveConstructor->setParams(FromParam); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10264 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10265 | MoveConstructor->setTrivial( |
| 10266 | ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 10267 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor) |
| 10268 | : ClassDecl->hasTrivialMoveConstructor()); |
| 10269 | |
| Alexis Hunt | 77c1f9f | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 10270 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
| Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 10271 | ClassDecl->setImplicitMoveConstructorIsDeleted(); |
| 10272 | SetDeclDeleted(MoveConstructor, ClassLoc); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10273 | } |
| 10274 | |
| 10275 | // Note that we have declared this constructor. |
| 10276 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 10277 | |
| 10278 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 10279 | PushOnScopeChains(MoveConstructor, S, false); |
| 10280 | ClassDecl->addDecl(MoveConstructor); |
| 10281 | |
| 10282 | return MoveConstructor; |
| 10283 | } |
| 10284 | |
| 10285 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 10286 | CXXConstructorDecl *MoveConstructor) { |
| 10287 | assert((MoveConstructor->isDefaulted() && |
| 10288 | MoveConstructor->isMoveConstructor() && |
| Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10289 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 10290 | !MoveConstructor->isDeleted()) && |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10291 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 10292 | |
| 10293 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 10294 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 10295 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10296 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10297 | DiagnosticErrorTrap Trap(Diags); |
| 10298 | |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 10299 | if (SetCtorInitializers(MoveConstructor, /*AnyErrors=*/false) || |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10300 | Trap.hasErrorOccurred()) { |
| 10301 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10302 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 10303 | MoveConstructor->setInvalidDecl(); |
| 10304 | } else { |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10305 | Sema::CompoundScopeRAII CompoundScope(*this); |
| Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 10306 | MoveConstructor->setBody(ActOnCompoundStmt( |
| 10307 | MoveConstructor->getLocation(), MoveConstructor->getLocation(), None, |
| 10308 | /*isStmtExpr=*/ false).takeAs<Stmt>()); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10309 | } |
| 10310 | |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10311 | MoveConstructor->markUsed(Context); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10312 | |
| 10313 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10314 | L->CompletedImplicitDefinition(MoveConstructor); |
| 10315 | } |
| 10316 | } |
| 10317 | |
| Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10318 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
| Eli Friedman | ebea0f2 | 2013-07-18 23:29:14 +0000 | [diff] [blame] | 10319 | return FD->isDeleted() && FD->isDefaulted() && isa<CXXMethodDecl>(FD); |
| Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10320 | } |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10321 | |
| 10322 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10323 | SourceLocation CurrentLocation, |
| 10324 | CXXConversionDecl *Conv) { |
| 10325 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 10326 | CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 10327 | // If we are defining a specialization of a conversion to function-ptr |
| 10328 | // cache the deduced template arguments for this specialization |
| 10329 | // so that we can use them to retrieve the corresponding call-operator |
| 10330 | // and static-invoker. |
| 10331 | const TemplateArgumentList *DeducedTemplateArgs = 0; |
| 10332 | |
| Douglas Gregor | 355efbb | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10333 | |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10334 | // Retrieve the corresponding call-operator specialization. |
| 10335 | if (Lambda->isGenericLambda()) { |
| 10336 | assert(Conv->isFunctionTemplateSpecialization()); |
| 10337 | FunctionTemplateDecl *CallOpTemplate = |
| 10338 | CallOp->getDescribedFunctionTemplate(); |
| 10339 | DeducedTemplateArgs = Conv->getTemplateSpecializationArgs(); |
| 10340 | void *InsertPos = 0; |
| 10341 | FunctionDecl *CallOpSpec = CallOpTemplate->findSpecialization( |
| 10342 | DeducedTemplateArgs->data(), |
| 10343 | DeducedTemplateArgs->size(), |
| 10344 | InsertPos); |
| 10345 | assert(CallOpSpec && |
| 10346 | "Conversion operator must have a corresponding call operator"); |
| 10347 | CallOp = cast<CXXMethodDecl>(CallOpSpec); |
| 10348 | } |
| 10349 | // Mark the call operator referenced (and add to pending instantiations |
| 10350 | // if necessary). |
| 10351 | // For both the conversion and static-invoker template specializations |
| 10352 | // we construct their body's in this function, so no need to add them |
| 10353 | // to the PendingInstantiations. |
| 10354 | MarkFunctionReferenced(CurrentLocation, CallOp); |
| 10355 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10356 | SynthesizedFunctionScope Scope(*this, Conv); |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10357 | DiagnosticErrorTrap Trap(Diags); |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10358 | |
| Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 10359 | // Retrieve the static invoker... |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10360 | CXXMethodDecl *Invoker = Lambda->getLambdaStaticInvoker(); |
| 10361 | // ... and get the corresponding specialization for a generic lambda. |
| 10362 | if (Lambda->isGenericLambda()) { |
| 10363 | assert(DeducedTemplateArgs && |
| 10364 | "Must have deduced template arguments from Conversion Operator"); |
| 10365 | FunctionTemplateDecl *InvokeTemplate = |
| 10366 | Invoker->getDescribedFunctionTemplate(); |
| 10367 | void *InsertPos = 0; |
| 10368 | FunctionDecl *InvokeSpec = InvokeTemplate->findSpecialization( |
| 10369 | DeducedTemplateArgs->data(), |
| 10370 | DeducedTemplateArgs->size(), |
| 10371 | InsertPos); |
| 10372 | assert(InvokeSpec && |
| 10373 | "Must have a corresponding static invoker specialization"); |
| 10374 | Invoker = cast<CXXMethodDecl>(InvokeSpec); |
| 10375 | } |
| 10376 | // Construct the body of the conversion function { return __invoke; }. |
| 10377 | Expr *FunctionRef = BuildDeclRefExpr(Invoker, Invoker->getType(), |
| 10378 | VK_LValue, Conv->getLocation()).take(); |
| 10379 | assert(FunctionRef && "Can't refer to __invoke function?"); |
| 10380 | Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take(); |
| 10381 | Conv->setBody(new (Context) CompoundStmt(Context, Return, |
| 10382 | Conv->getLocation(), |
| 10383 | Conv->getLocation())); |
| 10384 | |
| 10385 | Conv->markUsed(Context); |
| 10386 | Conv->setReferenced(); |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10387 | |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10388 | // Fill in the __invoke function with a dummy implementation. IR generation |
| 10389 | // will fill in the actual details. |
| 10390 | Invoker->markUsed(Context); |
| 10391 | Invoker->setReferenced(); |
| 10392 | Invoker->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
| 10393 | |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10394 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10395 | L->CompletedImplicitDefinition(Conv); |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10396 | L->CompletedImplicitDefinition(Invoker); |
| 10397 | } |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10398 | } |
| 10399 | |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10400 | |
| 10401 | |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10402 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 10403 | SourceLocation CurrentLocation, |
| 10404 | CXXConversionDecl *Conv) |
| 10405 | { |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 10406 | assert(!Conv->getParent()->isGenericLambda()); |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10407 | |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10408 | Conv->markUsed(Context); |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10409 | |
| Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10410 | SynthesizedFunctionScope Scope(*this, Conv); |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10411 | DiagnosticErrorTrap Trap(Diags); |
| 10412 | |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10413 | // Copy-initialize the lambda object as needed to capture it. |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10414 | Expr *This = ActOnCXXThis(CurrentLocation).take(); |
| 10415 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).take(); |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10416 | |
| Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10417 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 10418 | Conv->getLocation(), |
| 10419 | Conv, DerefThis); |
| 10420 | |
| 10421 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 10422 | // behavior. Note that only the general conversion function does this |
| 10423 | // (since it's unusable otherwise); in the case where we inline the |
| 10424 | // block literal, it has block literal lifetime semantics. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10425 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
| Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10426 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 10427 | CK_CopyAndAutoreleaseBlockObject, |
| 10428 | BuildBlock.get(), 0, VK_RValue); |
| 10429 | |
| 10430 | if (BuildBlock.isInvalid()) { |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10431 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10432 | Conv->setInvalidDecl(); |
| 10433 | return; |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10434 | } |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10435 | |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10436 | // Create the return statement that returns the block from the conversion |
| 10437 | // function. |
| Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10438 | StmtResult Return = ActOnReturnStmt(Conv->getLocation(), BuildBlock.get()); |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10439 | if (Return.isInvalid()) { |
| 10440 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 10441 | Conv->setInvalidDecl(); |
| 10442 | return; |
| 10443 | } |
| 10444 | |
| 10445 | // Set the body of the conversion function. |
| 10446 | Stmt *ReturnS = Return.take(); |
| Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 10447 | Conv->setBody(new (Context) CompoundStmt(Context, ReturnS, |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10448 | Conv->getLocation(), |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10449 | Conv->getLocation())); |
| 10450 | |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10451 | // We're done; notify the mutation listener, if any. |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10452 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10453 | L->CompletedImplicitDefinition(Conv); |
| 10454 | } |
| 10455 | } |
| 10456 | |
| Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10457 | /// \brief Determine whether the given list arguments contains exactly one |
| 10458 | /// "real" (non-default) argument. |
| 10459 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 10460 | switch (Args.size()) { |
| 10461 | case 0: |
| 10462 | return false; |
| 10463 | |
| 10464 | default: |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10465 | if (!Args[1]->isDefaultArgument()) |
| Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10466 | return false; |
| 10467 | |
| 10468 | // fall through |
| 10469 | case 1: |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10470 | return !Args[0]->isDefaultArgument(); |
| Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10471 | } |
| 10472 | |
| 10473 | return false; |
| 10474 | } |
| 10475 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10476 | ExprResult |
| Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10477 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10478 | CXXConstructorDecl *Constructor, |
| Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10479 | MultiExprArg ExprArgs, |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10480 | bool HadMultipleCandidates, |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10481 | bool IsListInitialization, |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10482 | bool RequiresZeroInit, |
| Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10483 | unsigned ConstructKind, |
| 10484 | SourceRange ParenRange) { |
| Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10485 | bool Elidable = false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10486 | |
| Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 10487 | // C++0x [class.copy]p34: |
| 10488 | // When certain criteria are met, an implementation is allowed to |
| 10489 | // omit the copy/move construction of a class object, even if the |
| 10490 | // copy/move constructor and/or destructor for the object have |
| 10491 | // side effects. [...] |
| 10492 | // - when a temporary class object that has not been bound to a |
| 10493 | // reference (12.2) would be copied/moved to a class object |
| 10494 | // with the same cv-unqualified type, the copy/move operation |
| 10495 | // can be omitted by constructing the temporary object |
| 10496 | // directly into the target of the omitted copy/move |
| John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10497 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
| Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10498 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10499 | Expr *SubExpr = ExprArgs[0]; |
| John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10500 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
| Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10501 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10502 | |
| 10503 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10504 | Elidable, ExprArgs, HadMultipleCandidates, |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10505 | IsListInitialization, RequiresZeroInit, |
| 10506 | ConstructKind, ParenRange); |
| Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10507 | } |
| 10508 | |
| Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10509 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 10510 | /// including handling of its default argument expressions. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10511 | ExprResult |
| Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10512 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 10513 | CXXConstructorDecl *Constructor, bool Elidable, |
| Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10514 | MultiExprArg ExprArgs, |
| Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10515 | bool HadMultipleCandidates, |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10516 | bool IsListInitialization, |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10517 | bool RequiresZeroInit, |
| Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10518 | unsigned ConstructKind, |
| 10519 | SourceRange ParenRange) { |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10520 | MarkFunctionReferenced(ConstructLoc, Constructor); |
| Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 10521 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
| Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10522 | Constructor, Elidable, ExprArgs, |
| Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10523 | HadMultipleCandidates, |
| 10524 | IsListInitialization, RequiresZeroInit, |
| Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10525 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 10526 | ParenRange)); |
| Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10527 | } |
| 10528 | |
| John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10529 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10530 | if (VD->isInvalidDecl()) return; |
| 10531 | |
| John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10532 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
| Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10533 | if (ClassDecl->isInvalidDecl()) return; |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10534 | if (ClassDecl->hasIrrelevantDestructor()) return; |
| Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10535 | if (ClassDecl->isDependentContext()) return; |
| John McCall | 47e4093 | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 10536 | |
| Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10537 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10538 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
| Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10539 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 10540 | PDiag(diag::err_access_dtor_var) |
| 10541 | << VD->getDeclName() |
| 10542 | << VD->getType()); |
| Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10543 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
| Anders Carlsson | 98766db | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 10544 | |
| Stephan Tolksdorf | 5604a27 | 2014-03-27 20:23:36 +0000 | [diff] [blame] | 10545 | if (Destructor->isTrivial()) return; |
| Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10546 | if (!VD->hasGlobalStorage()) return; |
| 10547 | |
| 10548 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 10549 | // class-static, function-static). |
| 10550 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 10551 | |
| 10552 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| Stephan Tolksdorf | 5604a27 | 2014-03-27 20:23:36 +0000 | [diff] [blame] | 10553 | if (!VD->isStaticLocal()) |
| Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10554 | Diag(VD->getLocation(), diag::warn_global_destructor); |
| Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10555 | } |
| 10556 | |
| Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10557 | /// \brief Given a constructor and the set of arguments provided for the |
| 10558 | /// constructor, convert the arguments and add any required default arguments |
| 10559 | /// to form a proper call to this constructor. |
| 10560 | /// |
| 10561 | /// \returns true if an error occurred, false otherwise. |
| 10562 | bool |
| 10563 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 10564 | MultiExprArg ArgsPtr, |
| Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10565 | SourceLocation Loc, |
| Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 10566 | SmallVectorImpl<Expr*> &ConvertedArgs, |
| Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10567 | bool AllowExplicit, |
| 10568 | bool IsListInitialization) { |
| Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10569 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 10570 | unsigned NumArgs = ArgsPtr.size(); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10571 | Expr **Args = ArgsPtr.data(); |
| Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10572 | |
| 10573 | const FunctionProtoType *Proto |
| 10574 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 10575 | assert(Proto && "Constructor without a prototype?"); |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 10576 | unsigned NumParams = Proto->getNumParams(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 10577 | |
| Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10578 | // If too few arguments are available, we'll fill in the rest with defaults. |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 10579 | if (NumArgs < NumParams) |
| 10580 | ConvertedArgs.reserve(NumParams); |
| Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10581 | else |
| Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10582 | ConvertedArgs.reserve(NumArgs); |
| Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10583 | |
| 10584 | VariadicCallType CallType = |
| 10585 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10586 | SmallVector<Expr *, 8> AllArgs; |
| Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10587 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10588 | Proto, 0, |
| 10589 | llvm::makeArrayRef(Args, NumArgs), |
| 10590 | AllArgs, |
| Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10591 | CallType, AllowExplicit, |
| 10592 | IsListInitialization); |
| Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 10593 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
| Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10594 | |
| Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10595 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs); |
| Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10596 | |
| Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 10597 | CheckConstructorCall(Constructor, |
| 10598 | llvm::makeArrayRef<const Expr *>(AllArgs.data(), |
| 10599 | AllArgs.size()), |
| Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10600 | Proto, Loc); |
| Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10601 | |
| Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10602 | return Invalid; |
| Douglas Gregor | c28b57d | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 10603 | } |
| 10604 | |
| Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10605 | static inline bool |
| 10606 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 10607 | const FunctionDecl *FnDecl) { |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10608 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
| Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10609 | if (isa<NamespaceDecl>(DC)) { |
| 10610 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10611 | diag::err_operator_new_delete_declared_in_namespace) |
| 10612 | << FnDecl->getDeclName(); |
| 10613 | } |
| 10614 | |
| 10615 | if (isa<TranslationUnitDecl>(DC) && |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 10616 | FnDecl->getStorageClass() == SC_Static) { |
| Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10617 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10618 | diag::err_operator_new_delete_declared_static) |
| 10619 | << FnDecl->getDeclName(); |
| 10620 | } |
| 10621 | |
| Anders Carlsson | 60659a8 | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 10622 | return false; |
| Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10623 | } |
| 10624 | |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10625 | static inline bool |
| 10626 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 10627 | CanQualType ExpectedResultType, |
| 10628 | CanQualType ExpectedFirstParamType, |
| 10629 | unsigned DependentParamTypeDiag, |
| 10630 | unsigned InvalidParamTypeDiag) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10631 | QualType ResultType = |
| 10632 | FnDecl->getType()->getAs<FunctionType>()->getReturnType(); |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10633 | |
| 10634 | // Check that the result type is not dependent. |
| 10635 | if (ResultType->isDependentType()) |
| 10636 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10637 | diag::err_operator_new_delete_dependent_result_type) |
| 10638 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10639 | |
| 10640 | // Check that the result type is what we expect. |
| 10641 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 10642 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10643 | diag::err_operator_new_delete_invalid_result_type) |
| 10644 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10645 | |
| 10646 | // A function template must have at least 2 parameters. |
| 10647 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 10648 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10649 | diag::err_operator_new_delete_template_too_few_parameters) |
| 10650 | << FnDecl->getDeclName(); |
| 10651 | |
| 10652 | // The function decl must have at least 1 parameter. |
| 10653 | if (FnDecl->getNumParams() == 0) |
| 10654 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10655 | diag::err_operator_new_delete_too_few_parameters) |
| 10656 | << FnDecl->getDeclName(); |
| 10657 | |
| Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 10658 | // Check the first parameter type is not dependent. |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10659 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 10660 | if (FirstParamType->isDependentType()) |
| 10661 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 10662 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10663 | |
| 10664 | // Check that the first parameter type is what we expect. |
| Douglas Gregor | 684d7bd | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 10665 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10666 | ExpectedFirstParamType) |
| 10667 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 10668 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10669 | |
| 10670 | return false; |
| 10671 | } |
| 10672 | |
| Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10673 | static bool |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10674 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10675 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10676 | // A program is ill-formed if an allocation function is declared in a |
| 10677 | // namespace scope other than global scope or declared static in global |
| 10678 | // scope. |
| 10679 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10680 | return true; |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10681 | |
| 10682 | CanQualType SizeTy = |
| 10683 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 10684 | |
| 10685 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10686 | // The return type shall be void*. The first parameter shall have type |
| 10687 | // std::size_t. |
| 10688 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 10689 | SizeTy, |
| 10690 | diag::err_operator_new_dependent_param_type, |
| 10691 | diag::err_operator_new_param_type)) |
| 10692 | return true; |
| 10693 | |
| 10694 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10695 | // The first parameter shall not have an associated default argument. |
| 10696 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
| Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10697 | return SemaRef.Diag(FnDecl->getLocation(), |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10698 | diag::err_operator_new_default_arg) |
| 10699 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 10700 | |
| 10701 | return false; |
| Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10702 | } |
| 10703 | |
| 10704 | static bool |
| Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 10705 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
| Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10706 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 10707 | // A program is ill-formed if deallocation functions are declared in a |
| 10708 | // namespace scope other than global scope or declared static in global |
| 10709 | // scope. |
| Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10710 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10711 | return true; |
| Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10712 | |
| 10713 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 10714 | // Each deallocation function shall return void and its first parameter |
| 10715 | // shall be void*. |
| Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10716 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 10717 | SemaRef.Context.VoidPtrTy, |
| 10718 | diag::err_operator_delete_dependent_param_type, |
| 10719 | diag::err_operator_delete_param_type)) |
| 10720 | return true; |
| Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10721 | |
| Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10722 | return false; |
| 10723 | } |
| 10724 | |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10725 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 10726 | /// of this overloaded operator is well-formed. If so, returns false; |
| 10727 | /// otherwise, emits appropriate diagnostics and returns true. |
| 10728 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
| Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10729 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10730 | "Expected an overloaded operator declaration"); |
| 10731 | |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10732 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 10733 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10734 | // C++ [over.oper]p5: |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10735 | // The allocation and deallocation functions, operator new, |
| 10736 | // operator new[], operator delete and operator delete[], are |
| 10737 | // described completely in 3.7.3. The attributes and restrictions |
| 10738 | // found in the rest of this subclause do not apply to them unless |
| 10739 | // explicitly stated in 3.7.3. |
| Anders Carlsson | f1f4695 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 10740 | if (Op == OO_Delete || Op == OO_Array_Delete) |
| Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10741 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
| Fariborz Jahanian | 4e08894 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 10742 | |
| Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10743 | if (Op == OO_New || Op == OO_Array_New) |
| 10744 | return CheckOperatorNewDeclaration(*this, FnDecl); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10745 | |
| 10746 | // C++ [over.oper]p6: |
| 10747 | // An operator function shall either be a non-static member |
| 10748 | // function or be a non-member function and have at least one |
| 10749 | // parameter whose type is a class, a reference to a class, an |
| 10750 | // enumeration, or a reference to an enumeration. |
| Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10751 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 10752 | if (MethodDecl->isStatic()) |
| 10753 | return Diag(FnDecl->getLocation(), |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10754 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10755 | } else { |
| 10756 | bool ClassOrEnumParam = false; |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 10757 | for (auto Param : FnDecl->params()) { |
| 10758 | QualType ParamType = Param->getType().getNonReferenceType(); |
| Eli Friedman | 173e0b7a | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 10759 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 10760 | ParamType->isEnumeralType()) { |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10761 | ClassOrEnumParam = true; |
| 10762 | break; |
| 10763 | } |
| 10764 | } |
| 10765 | |
| Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10766 | if (!ClassOrEnumParam) |
| 10767 | return Diag(FnDecl->getLocation(), |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10768 | diag::err_operator_overload_needs_class_or_enum) |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10769 | << FnDecl->getDeclName(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10770 | } |
| 10771 | |
| 10772 | // C++ [over.oper]p8: |
| 10773 | // An operator function cannot have default arguments (8.3.6), |
| 10774 | // except where explicitly stated below. |
| 10775 | // |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10776 | // Only the function-call operator allows default arguments |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10777 | // (C++ [over.call]p1). |
| 10778 | if (Op != OO_Call) { |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 10779 | for (auto Param : FnDecl->params()) { |
| 10780 | if (Param->hasDefaultArg()) |
| 10781 | return Diag(Param->getLocation(), |
| Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 10782 | diag::err_operator_overload_default_arg) |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 10783 | << FnDecl->getDeclName() << Param->getDefaultArgRange(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10784 | } |
| 10785 | } |
| 10786 | |
| Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10787 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 10788 | { false, false, false } |
| 10789 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 10790 | , { Unary, Binary, MemberOnly } |
| 10791 | #include "clang/Basic/OperatorKinds.def" |
| 10792 | }; |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10793 | |
| Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10794 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 10795 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 10796 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10797 | |
| 10798 | // C++ [over.oper]p8: |
| 10799 | // [...] Operator functions cannot have more or fewer parameters |
| 10800 | // than the number required for the corresponding operator, as |
| 10801 | // described in the rest of this subclause. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10802 | unsigned NumParams = FnDecl->getNumParams() |
| Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10803 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10804 | if (Op != OO_Call && |
| 10805 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 10806 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 10807 | (NumParams < 1) || (NumParams > 2))) { |
| 10808 | // We have the wrong number of parameters. |
| Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10809 | unsigned ErrorKind; |
| Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10810 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
| Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10811 | ErrorKind = 2; // 2 -> unary or binary. |
| Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10812 | } else if (CanBeUnaryOperator) { |
| Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10813 | ErrorKind = 0; // 0 -> unary |
| Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10814 | } else { |
| Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10815 | assert(CanBeBinaryOperator && |
| 10816 | "All non-call overloaded operators are unary or binary!"); |
| Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10817 | ErrorKind = 1; // 1 -> binary |
| Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10818 | } |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10819 | |
| Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10820 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10821 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10822 | } |
| Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 10823 | |
| Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10824 | // Overloaded operators other than operator() cannot be variadic. |
| 10825 | if (Op != OO_Call && |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10826 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10827 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10828 | << FnDecl->getDeclName(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10829 | } |
| 10830 | |
| 10831 | // Some operators must be non-static member functions. |
| Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10832 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 10833 | return Diag(FnDecl->getLocation(), |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10834 | diag::err_operator_overload_must_be_member) |
| Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10835 | << FnDecl->getDeclName(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10836 | } |
| 10837 | |
| 10838 | // C++ [over.inc]p1: |
| 10839 | // The user-defined function called operator++ implements the |
| 10840 | // prefix and postfix ++ operator. If this function is a member |
| 10841 | // function with no parameters, or a non-member function with one |
| 10842 | // parameter of class or enumeration type, it defines the prefix |
| 10843 | // increment operator ++ for objects of that type. If the function |
| 10844 | // is a member function with one parameter (which shall be of type |
| 10845 | // int) or a non-member function with two parameters (the second |
| 10846 | // of which shall be of type int), it defines the postfix |
| 10847 | // increment operator ++ for objects of that type. |
| 10848 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 10849 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| Richard Smith | 538b52a | 2014-01-30 22:24:05 +0000 | [diff] [blame] | 10850 | QualType ParamType = LastParam->getType(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10851 | |
| Richard Smith | 538b52a | 2014-01-30 22:24:05 +0000 | [diff] [blame] | 10852 | if (!ParamType->isSpecificBuiltinType(BuiltinType::Int) && |
| 10853 | !ParamType->isDependentType()) |
| Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10854 | return Diag(LastParam->getLocation(), |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10855 | diag::err_operator_overload_post_incdec_must_be_int) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 10856 | << LastParam->getType() << (Op == OO_MinusMinus); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10857 | } |
| 10858 | |
| Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10859 | return false; |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10860 | } |
| Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 10861 | |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10862 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 10863 | /// of this literal operator function is well-formed. If so, returns |
| 10864 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 10865 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| Richard Smith | 5731c75 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 10866 | if (isa<CXXMethodDecl>(FnDecl)) { |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10867 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 10868 | << FnDecl->getDeclName(); |
| 10869 | return true; |
| 10870 | } |
| 10871 | |
| Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10872 | if (FnDecl->isExternC()) { |
| 10873 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 10874 | return true; |
| 10875 | } |
| 10876 | |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10877 | bool Valid = false; |
| 10878 | |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10879 | // This might be the definition of a literal operator template. |
| 10880 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 10881 | // This might be a specialization of a literal operator template. |
| 10882 | if (!TpDecl) |
| 10883 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 10884 | |
| Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 10885 | // template <char...> type operator "" name() and |
| 10886 | // template <class T, T...> type operator "" name() are the only valid |
| 10887 | // template signatures, and the only valid signatures with no parameters. |
| Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10888 | if (TpDecl) { |
| Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10889 | if (FnDecl->param_size() == 0) { |
| Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 10890 | // Must have one or two template parameters |
| Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10891 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 10892 | if (Params->size() == 1) { |
| 10893 | NonTypeTemplateParmDecl *PmDecl = |
| Richard Smith | ed94302 | 2012-08-03 21:14:57 +0000 | [diff] [blame] | 10894 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10895 | |
| Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10896 | // The template parameter must be a char parameter pack. |
| Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10897 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 10898 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 10899 | Valid = true; |
| Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 10900 | } else if (Params->size() == 2) { |
| 10901 | TemplateTypeParmDecl *PmType = |
| 10902 | dyn_cast<TemplateTypeParmDecl>(Params->getParam(0)); |
| 10903 | NonTypeTemplateParmDecl *PmArgs = |
| 10904 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(1)); |
| 10905 | |
| 10906 | // The second template parameter must be a parameter pack with the |
| 10907 | // first template parameter as its type. |
| 10908 | if (PmType && PmArgs && |
| 10909 | !PmType->isTemplateParameterPack() && |
| 10910 | PmArgs->isTemplateParameterPack()) { |
| 10911 | const TemplateTypeParmType *TArgs = |
| 10912 | PmArgs->getType()->getAs<TemplateTypeParmType>(); |
| 10913 | if (TArgs && TArgs->getDepth() == PmType->getDepth() && |
| 10914 | TArgs->getIndex() == PmType->getIndex()) { |
| 10915 | Valid = true; |
| 10916 | if (ActiveTemplateInstantiations.empty()) |
| 10917 | Diag(FnDecl->getLocation(), |
| 10918 | diag::ext_string_literal_operator_template); |
| 10919 | } |
| 10920 | } |
| Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10921 | } |
| 10922 | } |
| Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10923 | } else if (FnDecl->param_size()) { |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10924 | // Check the first parameter |
| Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10925 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 10926 | |
| Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10927 | QualType T = (*Param)->getType().getUnqualifiedType(); |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10928 | |
| Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 10929 | // unsigned long long int, long double, and any character type are allowed |
| 10930 | // as the only parameters. |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10931 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 10932 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 10933 | Context.hasSameType(T, Context.CharTy) || |
| Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 10934 | Context.hasSameType(T, Context.WideCharTy) || |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10935 | Context.hasSameType(T, Context.Char16Ty) || |
| 10936 | Context.hasSameType(T, Context.Char32Ty)) { |
| 10937 | if (++Param == FnDecl->param_end()) |
| 10938 | Valid = true; |
| 10939 | goto FinishedParams; |
| 10940 | } |
| 10941 | |
| Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 10942 | // Otherwise it must be a pointer to const; let's strip those qualifiers. |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10943 | const PointerType *PT = T->getAs<PointerType>(); |
| 10944 | if (!PT) |
| 10945 | goto FinishedParams; |
| 10946 | T = PT->getPointeeType(); |
| Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10947 | if (!T.isConstQualified() || T.isVolatileQualified()) |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10948 | goto FinishedParams; |
| 10949 | T = T.getUnqualifiedType(); |
| 10950 | |
| 10951 | // Move on to the second parameter; |
| 10952 | ++Param; |
| 10953 | |
| 10954 | // If there is no second parameter, the first must be a const char * |
| 10955 | if (Param == FnDecl->param_end()) { |
| 10956 | if (Context.hasSameType(T, Context.CharTy)) |
| 10957 | Valid = true; |
| 10958 | goto FinishedParams; |
| 10959 | } |
| 10960 | |
| 10961 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 10962 | // are allowed as the first parameter to a two-parameter function |
| 10963 | if (!(Context.hasSameType(T, Context.CharTy) || |
| Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 10964 | Context.hasSameType(T, Context.WideCharTy) || |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10965 | Context.hasSameType(T, Context.Char16Ty) || |
| 10966 | Context.hasSameType(T, Context.Char32Ty))) |
| 10967 | goto FinishedParams; |
| 10968 | |
| 10969 | // The second and final parameter must be an std::size_t |
| 10970 | T = (*Param)->getType().getUnqualifiedType(); |
| 10971 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 10972 | ++Param == FnDecl->param_end()) |
| 10973 | Valid = true; |
| 10974 | } |
| 10975 | |
| 10976 | // FIXME: This diagnostic is absolutely terrible. |
| 10977 | FinishedParams: |
| 10978 | if (!Valid) { |
| 10979 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 10980 | << FnDecl->getDeclName(); |
| 10981 | return true; |
| 10982 | } |
| 10983 | |
| Richard Smith | 768cecc | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 10984 | // A parameter-declaration-clause containing a default argument is not |
| 10985 | // equivalent to any of the permitted forms. |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 10986 | for (auto Param : FnDecl->params()) { |
| 10987 | if (Param->hasDefaultArg()) { |
| 10988 | Diag(Param->getDefaultArgRange().getBegin(), |
| Richard Smith | 768cecc | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 10989 | diag::err_literal_operator_default_argument) |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 10990 | << Param->getDefaultArgRange(); |
| Richard Smith | 768cecc | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 10991 | break; |
| 10992 | } |
| 10993 | } |
| 10994 | |
| Richard Smith | 0df56f4 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10995 | StringRef LiteralName |
| Douglas Gregor | 86325ad | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 10996 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 10997 | if (LiteralName[0] != '_') { |
| Richard Smith | 0df56f4 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10998 | // C++11 [usrlit.suffix]p1: |
| 10999 | // Literal suffix identifiers that do not start with an underscore |
| 11000 | // are reserved for future standardization. |
| Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 11001 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved) |
| 11002 | << NumericLiteralParser::isValidUDSuffix(getLangOpts(), LiteralName); |
| Douglas Gregor | 86325ad | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 11003 | } |
| Richard Smith | 0df56f4 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 11004 | |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11005 | return false; |
| 11006 | } |
| 11007 | |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11008 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 11009 | /// linkage specification, including the language and (if present) |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11010 | /// the '{'. ExternLoc is the location of the 'extern', Lang is the |
| 11011 | /// language string literal. LBraceLoc, if valid, provides the location of |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11012 | /// the '{' brace. Otherwise, this linkage specification does not |
| 11013 | /// have any braces. |
| Chris Lattner | 8ea6442 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 11014 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11015 | Expr *LangStr, |
| Chris Lattner | 8ea6442 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 11016 | SourceLocation LBraceLoc) { |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11017 | StringLiteral *Lit = cast<StringLiteral>(LangStr); |
| 11018 | if (!Lit->isAscii()) { |
| 11019 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_not_ascii) |
| 11020 | << LangStr->getSourceRange(); |
| 11021 | return 0; |
| 11022 | } |
| 11023 | |
| 11024 | StringRef Lang = Lit->getString(); |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11025 | LinkageSpecDecl::LanguageIDs Language; |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11026 | if (Lang == "C") |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11027 | Language = LinkageSpecDecl::lang_c; |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11028 | else if (Lang == "C++") |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11029 | Language = LinkageSpecDecl::lang_cxx; |
| 11030 | else { |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11031 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_unknown) |
| 11032 | << LangStr->getSourceRange(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11033 | return 0; |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11034 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11035 | |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11036 | // FIXME: Add all the various semantics of linkage specifications |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11037 | |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11038 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, ExternLoc, |
| 11039 | LangStr->getExprLoc(), Language, |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 11040 | LBraceLoc.isValid()); |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11041 | CurContext->addDecl(D); |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11042 | PushDeclContext(S, D); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11043 | return D; |
| Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11044 | } |
| 11045 | |
| Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 11046 | /// ActOnFinishLinkageSpecification - Complete the definition of |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11047 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 11048 | /// valid, it's the position of the closing '}' brace in a linkage |
| 11049 | /// specification that uses braces. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11050 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| Abramo Bagnara | 4a8cda8 | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 11051 | Decl *LinkageSpec, |
| 11052 | SourceLocation RBraceLoc) { |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11053 | if (RBraceLoc.isValid()) { |
| 11054 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 11055 | LSDecl->setRBraceLoc(RBraceLoc); |
| Abramo Bagnara | 4a8cda8 | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 11056 | } |
| Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11057 | PopDeclContext(); |
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11058 | return LinkageSpec; |
| Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 11061 | Decl *Sema::ActOnEmptyDeclaration(Scope *S, |
| 11062 | AttributeList *AttrList, |
| 11063 | SourceLocation SemiLoc) { |
| 11064 | Decl *ED = EmptyDecl::Create(Context, CurContext, SemiLoc); |
| 11065 | // Attribute declarations appertain to empty declaration so we handle |
| 11066 | // them here. |
| 11067 | if (AttrList) |
| 11068 | ProcessDeclAttributeList(S, ED, AttrList); |
| Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 11069 | |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 11070 | CurContext->addDecl(ED); |
| 11071 | return ED; |
| Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 11072 | } |
| 11073 | |
| Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11074 | /// \brief Perform semantic analysis for the variable declaration that |
| 11075 | /// occurs within a C++ catch clause, returning the newly-created |
| 11076 | /// variable. |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11077 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11078 | TypeSourceInfo *TInfo, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11079 | SourceLocation StartLoc, |
| 11080 | SourceLocation Loc, |
| 11081 | IdentifierInfo *Name) { |
| Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11082 | bool Invalid = false; |
| Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11083 | QualType ExDeclType = TInfo->getType(); |
| 11084 | |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11085 | // Arrays and functions decay. |
| 11086 | if (ExDeclType->isArrayType()) |
| 11087 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 11088 | else if (ExDeclType->isFunctionType()) |
| 11089 | ExDeclType = Context.getPointerType(ExDeclType); |
| 11090 | |
| 11091 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 11092 | // The exception-declaration shall not denote a pointer or reference to an |
| 11093 | // incomplete type, other than [cv] void*. |
| Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11094 | // N2844 forbids rvalue references. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11095 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
| Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11096 | Diag(Loc, diag::err_catch_rvalue_ref); |
| Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11097 | Invalid = true; |
| 11098 | } |
| Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11099 | |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11100 | QualType BaseType = ExDeclType; |
| 11101 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
| Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 11102 | unsigned DK = diag::err_catch_incomplete; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11103 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11104 | BaseType = Ptr->getPointeeType(); |
| 11105 | Mode = 1; |
| Douglas Gregor | 3ecbc3d | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11106 | DK = diag::err_catch_incomplete_ptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11107 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
| Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11108 | // For the purpose of error recovery, we treat rvalue refs like lvalue refs. |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11109 | BaseType = Ref->getPointeeType(); |
| 11110 | Mode = 2; |
| Douglas Gregor | 3ecbc3d | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11111 | DK = diag::err_catch_incomplete_ref; |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11112 | } |
| Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11113 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
| Douglas Gregor | 3ecbc3d | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11114 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11115 | Invalid = true; |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11116 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11117 | if (!Invalid && !ExDeclType->isDependentType() && |
| Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11118 | RequireNonAbstractType(Loc, ExDeclType, |
| 11119 | diag::err_abstract_type_in_decl, |
| 11120 | AbstractVariableType)) |
| Sebastian Redl | 2f38ba5 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 11121 | Invalid = true; |
| 11122 | |
| John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11123 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 11124 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11125 | if (!Invalid && getLangOpts().ObjC1) { |
| John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11126 | QualType T = ExDeclType; |
| 11127 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 11128 | T = RT->getPointeeType(); |
| 11129 | |
| 11130 | if (T->isObjCObjectType()) { |
| 11131 | Diag(Loc, diag::err_objc_object_catch); |
| 11132 | Invalid = true; |
| 11133 | } else if (T->isObjCObjectPointerType()) { |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11134 | // FIXME: should this be a test for macosx-fragile specifically? |
| 11135 | if (getLangOpts().ObjCRuntime.isFragile()) |
| Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 11136 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
| John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11137 | } |
| 11138 | } |
| 11139 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11140 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 11141 | ExDeclType, TInfo, SC_None); |
| Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 11142 | ExDecl->setExceptionVariable(true); |
| 11143 | |
| Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 11144 | // In ARC, infer 'retaining' for variables of retainable type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11145 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
| Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 11146 | Invalid = true; |
| 11147 | |
| Douglas Gregor | 750734c | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 11148 | if (!Invalid && !ExDeclType->isDependentType()) { |
| John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11149 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
| John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 11150 | // Insulate this from anything else we might currently be parsing. |
| 11151 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
| 11152 | |
| Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11153 | // C++ [except.handle]p16: |
| Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11154 | // The object declared in an exception-declaration or, if the |
| 11155 | // exception-declaration does not specify a name, a temporary (12.2) is |
| Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11156 | // copy-initialized (8.5) from the exception object. [...] |
| 11157 | // The object is destroyed when the handler exits, after the destruction |
| 11158 | // of any automatic objects initialized within the handler. |
| 11159 | // |
| Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11160 | // We just pretend to initialize the object with itself, then make sure |
| Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11161 | // it can be destroyed later. |
| John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11162 | QualType initType = ExDeclType; |
| 11163 | |
| 11164 | InitializedEntity entity = |
| 11165 | InitializedEntity::InitializeVariable(ExDecl); |
| 11166 | InitializationKind initKind = |
| 11167 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 11168 | |
| 11169 | Expr *opaqueValue = |
| 11170 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 11171 | InitializationSequence sequence(*this, entity, initKind, opaqueValue); |
| 11172 | ExprResult result = sequence.Perform(*this, entity, initKind, opaqueValue); |
| John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11173 | if (result.isInvalid()) |
| Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11174 | Invalid = true; |
| John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11175 | else { |
| 11176 | // If the constructor used was non-trivial, set this as the |
| 11177 | // "initializer". |
| Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11178 | CXXConstructExpr *construct = result.takeAs<CXXConstructExpr>(); |
| John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11179 | if (!construct->getConstructor()->isTrivial()) { |
| 11180 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 11181 | ExDecl->setInit(init); |
| 11182 | } |
| 11183 | |
| 11184 | // And make sure it's destructable. |
| 11185 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 11186 | } |
| Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11187 | } |
| 11188 | } |
| 11189 | |
| Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11190 | if (Invalid) |
| 11191 | ExDecl->setInvalidDecl(); |
| 11192 | |
| 11193 | return ExDecl; |
| 11194 | } |
| 11195 | |
| 11196 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 11197 | /// handler. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11198 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11199 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| Douglas Gregor | 72772f6 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 11200 | bool Invalid = D.isInvalidType(); |
| 11201 | |
| 11202 | // Check for unexpanded parameter packs. |
| Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 11203 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 11204 | UPPC_ExceptionType)) { |
| Douglas Gregor | 72772f6 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 11205 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 11206 | D.getIdentifierLoc()); |
| 11207 | Invalid = true; |
| 11208 | } |
| 11209 | |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11210 | IdentifierInfo *II = D.getIdentifier(); |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 11211 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
| Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 11212 | LookupOrdinaryName, |
| 11213 | ForRedeclaration)) { |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11214 | // The scope should be freshly made just for us. There is just no way |
| 11215 | // it contains any previous declaration. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11216 | assert(!S->isDeclScope(PrevDecl)); |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11217 | if (PrevDecl->isTemplateParameter()) { |
| 11218 | // Maybe we will complain about the shadowed template parameter. |
| 11219 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 11220 | PrevDecl = 0; |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11221 | } |
| 11222 | } |
| 11223 | |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11224 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11225 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 11226 | << D.getCXXScopeSpec().getRange(); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11227 | Invalid = true; |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11228 | } |
| 11229 | |
| Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11230 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11231 | D.getLocStart(), |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11232 | D.getIdentifierLoc(), |
| 11233 | D.getIdentifier()); |
| Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11234 | if (Invalid) |
| 11235 | ExDecl->setInvalidDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11236 | |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11237 | // Add the exception declaration into this scope. |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11238 | if (II) |
| Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11239 | PushOnScopeChains(ExDecl, S); |
| 11240 | else |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11241 | CurContext->addDecl(ExDecl); |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11242 | |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11243 | ProcessDeclAttributes(S, ExDecl, D); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11244 | return ExDecl; |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11245 | } |
| Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11246 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11247 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11248 | Expr *AssertExpr, |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11249 | Expr *AssertMessageExpr, |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11250 | SourceLocation RParenLoc) { |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11251 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr); |
| Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11252 | |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11253 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 11254 | return 0; |
| 11255 | |
| 11256 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 11257 | AssertMessage, RParenLoc, false); |
| 11258 | } |
| 11259 | |
| 11260 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 11261 | Expr *AssertExpr, |
| 11262 | StringLiteral *AssertMessage, |
| 11263 | SourceLocation RParenLoc, |
| 11264 | bool Failed) { |
| 11265 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 11266 | !Failed) { |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11267 | // In a static_assert-declaration, the constant-expression shall be a |
| 11268 | // constant expression that can be contextually converted to bool. |
| 11269 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 11270 | if (Converted.isInvalid()) |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11271 | Failed = true; |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11272 | |
| Richard Smith | 902ca21 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 11273 | llvm::APSInt Cond; |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11274 | if (!Failed && VerifyIntegerConstantExpression(Converted.get(), &Cond, |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 11275 | diag::err_static_assert_expression_is_not_constant, |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11276 | /*AllowFold=*/false).isInvalid()) |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11277 | Failed = true; |
| Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11278 | |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11279 | if (!Failed && !Cond) { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11280 | SmallString<256> MsgBuffer; |
| Richard Smith | f506eaf | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 11281 | llvm::raw_svector_ostream Msg(MsgBuffer); |
| Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 11282 | AssertMessage->printPretty(Msg, 0, getPrintingPolicy()); |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11283 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
| Richard Smith | f506eaf | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 11284 | << Msg.str() << AssertExpr->getSourceRange(); |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11285 | Failed = true; |
| Richard Smith | f506eaf | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 11286 | } |
| Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 11287 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11288 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11289 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11290 | AssertExpr, AssertMessage, RParenLoc, |
| 11291 | Failed); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11292 | |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11293 | CurContext->addDecl(Decl); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11294 | return Decl; |
| Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11295 | } |
| Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11296 | |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11297 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 11298 | /// |
| 11299 | /// \returns A friend declaration that. |
| Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11300 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
| Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 11301 | SourceLocation FriendLoc, |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11302 | TypeSourceInfo *TSInfo) { |
| 11303 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 11304 | |
| 11305 | QualType T = TSInfo->getType(); |
| Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 11306 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11307 | |
| Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11308 | // C++03 [class.friend]p2: |
| 11309 | // An elaborated-type-specifier shall be used in a friend declaration |
| 11310 | // for a class.* |
| 11311 | // |
| 11312 | // * The class-key of the elaborated-type-specifier is required. |
| 11313 | if (!ActiveTemplateInstantiations.empty()) { |
| 11314 | // Do not complain about the form of friend template types during |
| 11315 | // template instantiation; we will already have complained when the |
| 11316 | // template was declared. |
| Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11317 | } else { |
| 11318 | if (!T->isElaboratedTypeSpecifier()) { |
| 11319 | // If we evaluated the type to a record type, suggest putting |
| 11320 | // a tag in front. |
| 11321 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 11322 | RecordDecl *RD = RT->getDecl(); |
| Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11323 | |
| Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11324 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11325 | |
| Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11326 | Diag(TypeRange.getBegin(), |
| 11327 | getLangOpts().CPlusPlus11 ? |
| 11328 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 11329 | diag::ext_unelaborated_friend_type) |
| 11330 | << (unsigned) RD->getTagKind() |
| 11331 | << T |
| 11332 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 11333 | InsertionText); |
| 11334 | } else { |
| 11335 | Diag(FriendLoc, |
| 11336 | getLangOpts().CPlusPlus11 ? |
| 11337 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 11338 | diag::ext_nonclass_type_friend) |
| 11339 | << T |
| 11340 | << TypeRange; |
| 11341 | } |
| 11342 | } else if (T->getAs<EnumType>()) { |
| Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11343 | Diag(FriendLoc, |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11344 | getLangOpts().CPlusPlus11 ? |
| Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11345 | diag::warn_cxx98_compat_enum_friend : |
| 11346 | diag::ext_enum_friend) |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11347 | << T |
| Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11348 | << TypeRange; |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11349 | } |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11350 | |
| Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11351 | // C++11 [class.friend]p3: |
| 11352 | // A friend declaration that does not declare a function shall have one |
| 11353 | // of the following forms: |
| 11354 | // friend elaborated-type-specifier ; |
| 11355 | // friend simple-type-specifier ; |
| 11356 | // friend typename-specifier ; |
| 11357 | if (getLangOpts().CPlusPlus11 && LocStart != FriendLoc) |
| 11358 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 11359 | } |
| Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11360 | |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 11361 | // If the type specifier in a friend declaration designates a (possibly |
| Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11362 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 11363 | // the friend declaration is ignored. |
| Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11364 | return FriendDecl::Create(Context, CurContext, LocStart, TSInfo, FriendLoc); |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11365 | } |
| 11366 | |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11367 | /// Handle a friend tag declaration where the scope specifier was |
| 11368 | /// templated. |
| 11369 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 11370 | unsigned TagSpec, SourceLocation TagLoc, |
| 11371 | CXXScopeSpec &SS, |
| Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11372 | IdentifierInfo *Name, |
| 11373 | SourceLocation NameLoc, |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11374 | AttributeList *Attr, |
| 11375 | MultiTemplateParamsArg TempParamLists) { |
| 11376 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 11377 | |
| 11378 | bool isExplicitSpecialization = false; |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11379 | bool Invalid = false; |
| 11380 | |
| Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 11381 | if (TemplateParameterList *TemplateParams = |
| 11382 | MatchTemplateParametersToScopeSpecifier( |
| Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 11383 | TagLoc, NameLoc, SS, 0, TempParamLists, /*friend*/ true, |
| Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 11384 | isExplicitSpecialization, Invalid)) { |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11385 | if (TemplateParams->size() > 0) { |
| 11386 | // This is a declaration of a class template. |
| 11387 | if (Invalid) |
| 11388 | return 0; |
| Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 11389 | |
| Eric Christopher | 6f228b5 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11390 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 11391 | SS, Name, NameLoc, Attr, |
| 11392 | TemplateParams, AS_public, |
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11393 | /*ModulePrivateLoc=*/SourceLocation(), |
| Eric Christopher | 6f228b5 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11394 | TempParamLists.size() - 1, |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11395 | TempParamLists.data()).take(); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11396 | } else { |
| 11397 | // The "template<>" header is extraneous. |
| 11398 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 11399 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 11400 | isExplicitSpecialization = true; |
| 11401 | } |
| 11402 | } |
| 11403 | |
| 11404 | if (Invalid) return 0; |
| 11405 | |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11406 | bool isAllExplicitSpecializations = true; |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11407 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11408 | if (TempParamLists[I]->size()) { |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11409 | isAllExplicitSpecializations = false; |
| 11410 | break; |
| 11411 | } |
| 11412 | } |
| 11413 | |
| 11414 | // FIXME: don't ignore attributes. |
| 11415 | |
| 11416 | // If it's explicit specializations all the way down, just forget |
| 11417 | // about the template header and build an appropriate non-templated |
| 11418 | // friend. TODO: for source fidelity, remember the headers. |
| 11419 | if (isAllExplicitSpecializations) { |
| Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11420 | if (SS.isEmpty()) { |
| 11421 | bool Owned = false; |
| 11422 | bool IsDependent = false; |
| 11423 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11424 | Attr, AS_public, |
| Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11425 | /*ModulePrivateLoc=*/SourceLocation(), |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11426 | MultiTemplateParamsArg(), Owned, IsDependent, |
| Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 11427 | /*ScopedEnumKWLoc=*/SourceLocation(), |
| Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11428 | /*ScopedEnumUsesClassTag=*/false, |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11429 | /*UnderlyingType=*/TypeResult(), |
| 11430 | /*IsTypeSpecifier=*/false); |
| Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11431 | } |
| Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11432 | |
| Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11433 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11434 | ElaboratedTypeKeyword Keyword |
| 11435 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11436 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 11437 | *Name, NameLoc); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11438 | if (T.isNull()) |
| 11439 | return 0; |
| 11440 | |
| 11441 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 11442 | if (isa<DependentNameType>(T)) { |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11443 | DependentNameTypeLoc TL = |
| 11444 | TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11445 | TL.setElaboratedKeywordLoc(TagLoc); |
| Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11446 | TL.setQualifierLoc(QualifierLoc); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11447 | TL.setNameLoc(NameLoc); |
| 11448 | } else { |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11449 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11450 | TL.setElaboratedKeywordLoc(TagLoc); |
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 11451 | TL.setQualifierLoc(QualifierLoc); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11452 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(NameLoc); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11453 | } |
| 11454 | |
| 11455 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11456 | TSI, FriendLoc, TempParamLists); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11457 | Friend->setAccess(AS_public); |
| 11458 | CurContext->addDecl(Friend); |
| 11459 | return Friend; |
| 11460 | } |
| Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11461 | |
| 11462 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 11463 | |
| 11464 | |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11465 | |
| 11466 | // Handle the case of a templated-scope friend class. e.g. |
| 11467 | // template <class T> class A<T>::B; |
| 11468 | // FIXME: we don't support these right now. |
| Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 11469 | Diag(NameLoc, diag::warn_template_qualified_friend_unsupported) |
| 11470 | << SS.getScopeRep() << SS.getRange() << cast<CXXRecordDecl>(CurContext); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11471 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 11472 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 11473 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11474 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11475 | TL.setElaboratedKeywordLoc(TagLoc); |
| Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11476 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11477 | TL.setNameLoc(NameLoc); |
| 11478 | |
| 11479 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11480 | TSI, FriendLoc, TempParamLists); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11481 | Friend->setAccess(AS_public); |
| 11482 | Friend->setUnsupportedFriend(true); |
| 11483 | CurContext->addDecl(Friend); |
| 11484 | return Friend; |
| 11485 | } |
| 11486 | |
| 11487 | |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11488 | /// Handle a friend type declaration. This works in tandem with |
| 11489 | /// ActOnTag. |
| 11490 | /// |
| 11491 | /// Notes on friend class templates: |
| 11492 | /// |
| 11493 | /// We generally treat friend class declarations as if they were |
| 11494 | /// declaring a class. So, for example, the elaborated type specifier |
| 11495 | /// in a friend declaration is required to obey the restrictions of a |
| 11496 | /// class-head (i.e. no typedefs in the scope chain), template |
| 11497 | /// parameters are required to match up with simple template-ids, &c. |
| 11498 | /// However, unlike when declaring a template specialization, it's |
| 11499 | /// okay to refer to a template specialization without an empty |
| 11500 | /// template parameter declaration, e.g. |
| 11501 | /// friend class A<T>::B<unsigned>; |
| 11502 | /// We permit this as a special case; if there are any template |
| 11503 | /// parameters present at all, require proper matching, i.e. |
| James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 11504 | /// template <> template \<class T> friend class A<int>::B; |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11505 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
| John McCall | c9739e3 | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 11506 | MultiTemplateParamsArg TempParams) { |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11507 | SourceLocation Loc = DS.getLocStart(); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11508 | |
| 11509 | assert(DS.isFriendSpecified()); |
| 11510 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11511 | |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11512 | // Try to convert the decl specifier to a type. This works for |
| 11513 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 11514 | // for a TUK_Friend. |
| Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11515 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11516 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 11517 | QualType T = TSI->getType(); |
| Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11518 | if (TheDeclarator.isInvalidType()) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11519 | return 0; |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11520 | |
| Douglas Gregor | 6c110f3 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11521 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 11522 | return 0; |
| 11523 | |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11524 | // This is definitely an error in C++98. It's probably meant to |
| 11525 | // be forbidden in C++0x, too, but the specification is just |
| 11526 | // poorly written. |
| 11527 | // |
| 11528 | // The problem is with declarations like the following: |
| 11529 | // template <T> friend A<T>::foo; |
| 11530 | // where deciding whether a class C is a friend or not now hinges |
| 11531 | // on whether there exists an instantiation of A that causes |
| 11532 | // 'foo' to equal C. There are restrictions on class-heads |
| 11533 | // (which we declare (by fiat) elaborated friend declarations to |
| 11534 | // be) that makes this tractable. |
| 11535 | // |
| 11536 | // FIXME: handle "template <> friend class A<T>;", which |
| 11537 | // is possibly well-formed? Who even knows? |
| Douglas Gregor | e677daf | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 11538 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11539 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 11540 | << DS.getSourceRange(); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11541 | return 0; |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11542 | } |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11543 | |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11544 | // C++98 [class.friend]p1: A friend of a class is a function |
| 11545 | // or class that is not a member of the class . . . |
| John McCall | 463e10c | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 11546 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 11547 | // deadline. It's also a very silly restriction that seriously |
| 11548 | // affects inner classes and which nobody else seems to implement; |
| 11549 | // thus we never diagnose it, not even in -pedantic. |
| John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11550 | // |
| 11551 | // But note that we could warn about it: it's always useless to |
| 11552 | // friend one of your own members (it's not, however, worthless to |
| 11553 | // friend a member of an arbitrary specialization of your template). |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11554 | |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11555 | Decl *D; |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11556 | if (unsigned NumTempParamLists = TempParams.size()) |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11557 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11558 | NumTempParamLists, |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11559 | TempParams.data(), |
| John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11560 | TSI, |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11561 | DS.getFriendSpecLoc()); |
| 11562 | else |
| Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 11563 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11564 | |
| 11565 | if (!D) |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11566 | return 0; |
| Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11567 | |
| John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11568 | D->setAccess(AS_public); |
| 11569 | CurContext->addDecl(D); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11570 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11571 | return D; |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11572 | } |
| 11573 | |
| Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 11574 | NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
| 11575 | MultiTemplateParamsArg TemplateParams) { |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11576 | const DeclSpec &DS = D.getDeclSpec(); |
| 11577 | |
| 11578 | assert(DS.isFriendSpecified()); |
| 11579 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11580 | |
| 11581 | SourceLocation Loc = D.getIdentifierLoc(); |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11582 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11583 | |
| 11584 | // C++ [class.friend]p1 |
| 11585 | // A friend of a class is a function or class.... |
| 11586 | // Note that this sees through typedefs, which is intended. |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11587 | // It *doesn't* see through dependent types, which is correct |
| 11588 | // according to [temp.arg.type]p3: |
| 11589 | // If a declaration acquires a function type through a |
| 11590 | // type dependent on a template-parameter and this causes |
| 11591 | // a declaration that does not use the syntactic form of a |
| 11592 | // function declarator to have a function type, the program |
| 11593 | // is ill-formed. |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11594 | if (!TInfo->getType()->isFunctionType()) { |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11595 | Diag(Loc, diag::err_unexpected_friend); |
| 11596 | |
| 11597 | // It might be worthwhile to try to recover by creating an |
| 11598 | // appropriate declaration. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11599 | return 0; |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11600 | } |
| 11601 | |
| 11602 | // C++ [namespace.memdef]p3 |
| 11603 | // - If a friend declaration in a non-local class first declares a |
| 11604 | // class or function, the friend class or function is a member |
| 11605 | // of the innermost enclosing namespace. |
| 11606 | // - The name of the friend is not found by simple name lookup |
| 11607 | // until a matching declaration is provided in that namespace |
| 11608 | // scope (either before or after the class declaration granting |
| 11609 | // friendship). |
| 11610 | // - If a friend function is called, its name may be found by the |
| 11611 | // name lookup that considers functions from namespaces and |
| 11612 | // classes associated with the types of the function arguments. |
| 11613 | // - When looking for a prior declaration of a class or a function |
| 11614 | // declared as a friend, scopes outside the innermost enclosing |
| 11615 | // namespace scope are not considered. |
| 11616 | |
| John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11617 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11618 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 11619 | DeclarationName Name = NameInfo.getName(); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11620 | assert(Name); |
| 11621 | |
| Douglas Gregor | 6c110f3 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11622 | // Check for unexpanded parameter packs. |
| 11623 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 11624 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 11625 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 11626 | return 0; |
| 11627 | |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11628 | // The context we found the declaration in, or in which we should |
| 11629 | // create the declaration. |
| 11630 | DeclContext *DC; |
| John McCall | ccbc032 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11631 | Scope *DCScope = S; |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11632 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11633 | ForRedeclaration); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11634 | |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11635 | // There are five cases here. |
| 11636 | // - There's no scope specifier and we're in a local class. Only look |
| 11637 | // for functions declared in the immediately-enclosing block scope. |
| 11638 | // We recover from invalid scope qualifiers as if they just weren't there. |
| 11639 | FunctionDecl *FunctionContainingLocalClass = 0; |
| 11640 | if ((SS.isInvalid() || !SS.isSet()) && |
| 11641 | (FunctionContainingLocalClass = |
| 11642 | cast<CXXRecordDecl>(CurContext)->isLocalClass())) { |
| 11643 | // C++11 [class.friend]p11: |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11644 | // If a friend declaration appears in a local class and the name |
| 11645 | // specified is an unqualified name, a prior declaration is |
| 11646 | // looked up without considering scopes that are outside the |
| 11647 | // innermost enclosing non-class scope. For a friend function |
| 11648 | // declaration, if there is no prior declaration, the program is |
| 11649 | // ill-formed. |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11650 | |
| 11651 | // Find the innermost enclosing non-class scope. This is the block |
| 11652 | // scope containing the local class definition (or for a nested class, |
| 11653 | // the outer local class). |
| 11654 | DCScope = S->getFnParent(); |
| 11655 | |
| 11656 | // Look up the function name in the scope. |
| 11657 | Previous.clear(LookupLocalFriendName); |
| 11658 | LookupName(Previous, S, /*AllowBuiltinCreation*/false); |
| 11659 | |
| 11660 | if (!Previous.empty()) { |
| 11661 | // All possible previous declarations must have the same context: |
| 11662 | // either they were declared at block scope or they are members of |
| 11663 | // one of the enclosing local classes. |
| 11664 | DC = Previous.getRepresentativeDecl()->getDeclContext(); |
| 11665 | } else { |
| 11666 | // This is ill-formed, but provide the context that we would have |
| 11667 | // declared the function in, if we were permitted to, for error recovery. |
| 11668 | DC = FunctionContainingLocalClass; |
| 11669 | } |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 11670 | adjustContextForLocalExternDecl(DC); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11671 | |
| 11672 | // C++ [class.friend]p6: |
| 11673 | // A function can be defined in a friend declaration of a class if and |
| 11674 | // only if the class is a non-local class (9.8), the function name is |
| 11675 | // unqualified, and the function has namespace scope. |
| 11676 | if (D.isFunctionDefinition()) { |
| 11677 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 11678 | } |
| 11679 | |
| 11680 | // - There's no scope specifier, in which case we just go to the |
| 11681 | // appropriate scope and look for a function or function template |
| 11682 | // there as appropriate. |
| 11683 | } else if (SS.isInvalid() || !SS.isSet()) { |
| 11684 | // C++11 [namespace.memdef]p3: |
| 11685 | // If the name in a friend declaration is neither qualified nor |
| 11686 | // a template-id and the declaration is a function or an |
| 11687 | // elaborated-type-specifier, the lookup to determine whether |
| 11688 | // the entity has been previously declared shall not consider |
| 11689 | // any scopes outside the innermost enclosing namespace. |
| John McCall | f477659 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11690 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11691 | |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11692 | // Find the appropriate context according to the above. |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11693 | DC = CurContext; |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11694 | |
| Rafael Espindola | 3626b7e | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11695 | // Skip class contexts. If someone can cite chapter and verse |
| 11696 | // for this behavior, that would be nice --- it's what GCC and |
| 11697 | // EDG do, and it seems like a reasonable intent, but the spec |
| 11698 | // really only says that checks for unqualified existing |
| 11699 | // declarations should stop at the nearest enclosing namespace, |
| 11700 | // not that they should only consider the nearest enclosing |
| 11701 | // namespace. |
| 11702 | while (DC->isRecord()) |
| 11703 | DC = DC->getParent(); |
| 11704 | |
| 11705 | DeclContext *LookupDC = DC; |
| 11706 | while (LookupDC->isTransparentContext()) |
| 11707 | LookupDC = LookupDC->getParent(); |
| 11708 | |
| 11709 | while (true) { |
| 11710 | LookupQualifiedName(Previous, LookupDC); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11711 | |
| Rafael Espindola | 3626b7e | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11712 | if (!Previous.empty()) { |
| 11713 | DC = LookupDC; |
| 11714 | break; |
| John McCall | f477659 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11715 | } |
| Rafael Espindola | 3626b7e | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11716 | |
| 11717 | if (isTemplateId) { |
| 11718 | if (isa<TranslationUnitDecl>(LookupDC)) break; |
| 11719 | } else { |
| 11720 | if (LookupDC->isFileContext()) break; |
| 11721 | } |
| 11722 | LookupDC = LookupDC->getParent(); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11723 | } |
| 11724 | |
| John McCall | ccbc032 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11725 | DCScope = getScopeForDeclContext(S, DC); |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11726 | |
| John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11727 | // - There's a non-dependent scope specifier, in which case we |
| 11728 | // compute it and do a previous lookup there for a function |
| 11729 | // or function template. |
| 11730 | } else if (!SS.getScopeRep()->isDependent()) { |
| 11731 | DC = computeDeclContext(SS); |
| 11732 | if (!DC) return 0; |
| 11733 | |
| 11734 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 11735 | |
| 11736 | LookupQualifiedName(Previous, DC); |
| 11737 | |
| 11738 | // Ignore things found implicitly in the wrong scope. |
| 11739 | // TODO: better diagnostics for this case. Suggesting the right |
| 11740 | // qualified scope would be nice... |
| 11741 | LookupResult::Filter F = Previous.makeFilter(); |
| 11742 | while (F.hasNext()) { |
| 11743 | NamedDecl *D = F.next(); |
| 11744 | if (!DC->InEnclosingNamespaceSetOf( |
| 11745 | D->getDeclContext()->getRedeclContext())) |
| 11746 | F.erase(); |
| 11747 | } |
| 11748 | F.done(); |
| 11749 | |
| 11750 | if (Previous.empty()) { |
| 11751 | D.setInvalidType(); |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11752 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 11753 | << Name << TInfo->getType(); |
| John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11754 | return 0; |
| 11755 | } |
| 11756 | |
| 11757 | // C++ [class.friend]p1: A friend of a class is a function or |
| 11758 | // class that is not a member of the class . . . |
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11759 | if (DC->Equals(CurContext)) |
| 11760 | Diag(DS.getFriendSpecLoc(), |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11761 | getLangOpts().CPlusPlus11 ? |
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11762 | diag::warn_cxx98_compat_friend_is_member : |
| 11763 | diag::err_friend_is_member); |
| Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11764 | |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11765 | if (D.isFunctionDefinition()) { |
| Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11766 | // C++ [class.friend]p6: |
| 11767 | // A function can be defined in a friend declaration of a class if and |
| 11768 | // only if the class is a non-local class (9.8), the function name is |
| 11769 | // unqualified, and the function has namespace scope. |
| 11770 | SemaDiagnosticBuilder DB |
| 11771 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 11772 | |
| 11773 | DB << SS.getScopeRep(); |
| 11774 | if (DC->isFileContext()) |
| 11775 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 11776 | SS.clear(); |
| 11777 | } |
| John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11778 | |
| 11779 | // - There's a scope specifier that does not match any template |
| 11780 | // parameter lists, in which case we use some arbitrary context, |
| 11781 | // create a method or method template, and wait for instantiation. |
| 11782 | // - There's a scope specifier that does match some template |
| 11783 | // parameter lists, which we don't handle right now. |
| 11784 | } else { |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11785 | if (D.isFunctionDefinition()) { |
| Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11786 | // C++ [class.friend]p6: |
| 11787 | // A function can be defined in a friend declaration of a class if and |
| 11788 | // only if the class is a non-local class (9.8), the function name is |
| 11789 | // unqualified, and the function has namespace scope. |
| 11790 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 11791 | << SS.getScopeRep(); |
| 11792 | } |
| 11793 | |
| John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11794 | DC = CurContext; |
| 11795 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11796 | } |
| Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11797 | |
| John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11798 | if (!DC->isRecord()) { |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11799 | // This implies that it has to be an operator or function. |
| Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11800 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 11801 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 11802 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11803 | Diag(Loc, diag::err_introducing_special_friend) << |
| Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11804 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 11805 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11806 | return 0; |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11807 | } |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11808 | } |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11809 | |
| Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 11810 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 11811 | // does not contain the declaration context, i.e., in an out-of-line |
| 11812 | // definition of a class. |
| 11813 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 11814 | if (!DCScope) { |
| 11815 | FakeDCScope.setEntity(DC); |
| 11816 | DCScope = &FakeDCScope; |
| 11817 | } |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11818 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 11819 | bool AddToScope = true; |
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11820 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11821 | TemplateParams, AddToScope); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11822 | if (!ND) return 0; |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11823 | |
| Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11824 | assert(ND->getLexicalDeclContext() == CurContext); |
| John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 11825 | |
| Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11826 | // If we performed typo correction, we might have added a scope specifier |
| 11827 | // and changed the decl context. |
| 11828 | DC = ND->getDeclContext(); |
| 11829 | |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11830 | // Add the function declaration to the appropriate lookup tables, |
| 11831 | // adjusting the redeclarations list as necessary. We don't |
| 11832 | // want to do this yet if the friending class is dependent. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11833 | // |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11834 | // Also update the scope-based lookup if the target context's |
| 11835 | // lookup context is in lexical scope. |
| 11836 | if (!CurContext->isDependentContext()) { |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11837 | DC = DC->getRedeclContext(); |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 11838 | DC->makeDeclVisibleInContext(ND); |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11839 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11840 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
| John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11841 | } |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11842 | |
| 11843 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
| Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11844 | D.getIdentifierLoc(), ND, |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11845 | DS.getFriendSpecLoc()); |
| John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 11846 | FrD->setAccess(AS_public); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11847 | CurContext->addDecl(FrD); |
| John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11848 | |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 11849 | if (ND->isInvalidDecl()) { |
| John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11850 | FrD->setInvalidDecl(); |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 11851 | } else { |
| 11852 | if (DC->isRecord()) CheckFriendAccess(ND); |
| 11853 | |
| John McCall | 2c2eb12 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 11854 | FunctionDecl *FD; |
| 11855 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 11856 | FD = FTD->getTemplatedDecl(); |
| 11857 | else |
| 11858 | FD = cast<FunctionDecl>(ND); |
| 11859 | |
| David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 11860 | // C++11 [dcl.fct.default]p4: If a friend declaration specifies a |
| 11861 | // default argument expression, that declaration shall be a definition |
| 11862 | // and shall be the only declaration of the function or function |
| 11863 | // template in the translation unit. |
| 11864 | if (functionDeclHasDefaultArgument(FD)) { |
| 11865 | if (FunctionDecl *OldFD = FD->getPreviousDecl()) { |
| 11866 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 11867 | Diag(OldFD->getLocation(), diag::note_previous_declaration); |
| 11868 | } else if (!D.isFunctionDefinition()) |
| 11869 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_must_be_def); |
| 11870 | } |
| 11871 | |
| John McCall | 2c2eb12 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 11872 | // Mark templated-scope function declarations as unsupported. |
| 11873 | if (FD->getNumTemplateParameterLists()) |
| 11874 | FrD->setUnsupportedFriend(true); |
| 11875 | } |
| John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11876 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11877 | return ND; |
| Anders Carlsson | 3881170 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 11878 | } |
| 11879 | |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11880 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 11881 | AdjustDeclIfTemplate(Dcl); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11882 | |
| Aaron Ballman | f96361e | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 11883 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(Dcl); |
| Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11884 | if (!Fn) { |
| 11885 | Diag(DelLoc, diag::err_deleted_non_function); |
| 11886 | return; |
| 11887 | } |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11888 | |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 11889 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
| David Blaikie | 3680552 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11890 | // Don't consider the implicit declaration we generate for explicit |
| 11891 | // specializations. FIXME: Do not generate these implicit declarations. |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 11892 | if ((Prev->getTemplateSpecializationKind() != TSK_ExplicitSpecialization || |
| 11893 | Prev->getPreviousDecl()) && |
| 11894 | !Prev->isDefined()) { |
| David Blaikie | 3680552 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11895 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 11896 | Diag(Prev->getLocation().isInvalid() ? DelLoc : Prev->getLocation(), |
| 11897 | Prev->isImplicit() ? diag::note_previous_implicit_declaration |
| 11898 | : diag::note_previous_declaration); |
| David Blaikie | 3680552 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11899 | } |
| Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11900 | // If the declaration wasn't the first, we delete the function anyway for |
| 11901 | // recovery. |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11902 | Fn = Fn->getCanonicalDecl(); |
| Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11903 | } |
| Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11904 | |
| 11905 | if (Fn->isDeleted()) |
| 11906 | return; |
| 11907 | |
| 11908 | // See if we're deleting a function which is already known to override a |
| 11909 | // non-deleted virtual function. |
| 11910 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11911 | bool IssuedDiagnostic = false; |
| 11912 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 11913 | E = MD->end_overridden_methods(); |
| 11914 | I != E; ++I) { |
| 11915 | if (!(*MD->begin_overridden_methods())->isDeleted()) { |
| 11916 | if (!IssuedDiagnostic) { |
| 11917 | Diag(DelLoc, diag::err_deleted_override) << MD->getDeclName(); |
| 11918 | IssuedDiagnostic = true; |
| 11919 | } |
| 11920 | Diag((*I)->getLocation(), diag::note_overridden_virtual_function); |
| 11921 | } |
| 11922 | } |
| 11923 | } |
| 11924 | |
| Richard Smith | b63b6ee | 2014-01-22 01:43:19 +0000 | [diff] [blame] | 11925 | // C++11 [basic.start.main]p3: |
| 11926 | // A program that defines main as deleted [...] is ill-formed. |
| 11927 | if (Fn->isMain()) |
| 11928 | Diag(DelLoc, diag::err_deleted_main); |
| 11929 | |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 11930 | Fn->setDeletedAsWritten(); |
| Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11931 | } |
| Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 11932 | |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11933 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
| Aaron Ballman | f96361e | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 11934 | CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Dcl); |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11935 | |
| 11936 | if (MD) { |
| Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 11937 | if (MD->getParent()->isDependentType()) { |
| 11938 | MD->setDefaulted(); |
| 11939 | MD->setExplicitlyDefaulted(); |
| 11940 | return; |
| 11941 | } |
| 11942 | |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11943 | CXXSpecialMember Member = getSpecialMember(MD); |
| 11944 | if (Member == CXXInvalid) { |
| Eli Friedman | 84c0143e | 2013-07-11 23:55:07 +0000 | [diff] [blame] | 11945 | if (!MD->isInvalidDecl()) |
| 11946 | Diag(DefaultLoc, diag::err_default_special_members); |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11947 | return; |
| 11948 | } |
| 11949 | |
| 11950 | MD->setDefaulted(); |
| 11951 | MD->setExplicitlyDefaulted(); |
| 11952 | |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11953 | // If this definition appears within the record, do the checking when |
| 11954 | // the record is complete. |
| 11955 | const FunctionDecl *Primary = MD; |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11956 | if (const FunctionDecl *Pattern = MD->getTemplateInstantiationPattern()) |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11957 | // Find the uninstantiated declaration that actually had the '= default' |
| 11958 | // on it. |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11959 | Pattern->isDefined(Primary); |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11960 | |
| Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 11961 | // If the method was defaulted on its first declaration, we will have |
| 11962 | // already performed the checking in CheckCompletedCXXClass. Such a |
| 11963 | // declaration doesn't trigger an implicit definition. |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11964 | if (Primary == Primary->getCanonicalDecl()) |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11965 | return; |
| 11966 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11967 | CheckExplicitlyDefaultedSpecialMember(MD); |
| 11968 | |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 11969 | // The exception specification is needed because we are defining the |
| 11970 | // function. |
| 11971 | ResolveExceptionSpec(DefaultLoc, |
| 11972 | MD->getType()->castAs<FunctionProtoType>()); |
| 11973 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11974 | if (MD->isInvalidDecl()) |
| 11975 | return; |
| 11976 | |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11977 | switch (Member) { |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11978 | case CXXDefaultConstructor: |
| 11979 | DefineImplicitDefaultConstructor(DefaultLoc, |
| 11980 | cast<CXXConstructorDecl>(MD)); |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 11981 | break; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11982 | case CXXCopyConstructor: |
| 11983 | DefineImplicitCopyConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11984 | break; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11985 | case CXXCopyAssignment: |
| 11986 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 11987 | break; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11988 | case CXXDestructor: |
| 11989 | DefineImplicitDestructor(DefaultLoc, cast<CXXDestructorDecl>(MD)); |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 11990 | break; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11991 | case CXXMoveConstructor: |
| 11992 | DefineImplicitMoveConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
| Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 11993 | break; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11994 | case CXXMoveAssignment: |
| 11995 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11996 | break; |
| Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11997 | case CXXInvalid: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 11998 | llvm_unreachable("Invalid special member."); |
| Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11999 | } |
| 12000 | } else { |
| 12001 | Diag(DefaultLoc, diag::err_default_special_members); |
| 12002 | } |
| 12003 | } |
| 12004 | |
| Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12005 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 12006 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
| Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12007 | Stmt *SubStmt = *CI; |
| 12008 | if (!SubStmt) |
| 12009 | continue; |
| 12010 | if (isa<ReturnStmt>(SubStmt)) |
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 12011 | Self.Diag(SubStmt->getLocStart(), |
| Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12012 | diag::err_return_in_constructor_handler); |
| 12013 | if (!isa<Expr>(SubStmt)) |
| 12014 | SearchForReturnInStmt(Self, SubStmt); |
| 12015 | } |
| 12016 | } |
| 12017 | |
| 12018 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 12019 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 12020 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 12021 | SearchForReturnInStmt(*this, Handler); |
| 12022 | } |
| 12023 | } |
| Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12024 | |
| David Blaikie | 68f71a3 | 2013-01-18 23:03:15 +0000 | [diff] [blame] | 12025 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 12026 | const CXXMethodDecl *Old) { |
| 12027 | const FunctionType *NewFT = New->getType()->getAs<FunctionType>(); |
| 12028 | const FunctionType *OldFT = Old->getType()->getAs<FunctionType>(); |
| 12029 | |
| 12030 | CallingConv NewCC = NewFT->getCallConv(), OldCC = OldFT->getCallConv(); |
| 12031 | |
| 12032 | // If the calling conventions match, everything is fine |
| 12033 | if (NewCC == OldCC) |
| 12034 | return false; |
| 12035 | |
| Hans Wennborg | 2545efe | 2013-12-11 17:42:11 +0000 | [diff] [blame] | 12036 | // If the calling conventions mismatch because the new function is static, |
| 12037 | // suppress the calling convention mismatch error; the error about static |
| 12038 | // function override (err_static_overrides_virtual from |
| 12039 | // Sema::CheckFunctionDeclaration) is more clear. |
| 12040 | if (New->getStorageClass() == SC_Static) |
| 12041 | return false; |
| 12042 | |
| Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 12043 | Diag(New->getLocation(), |
| 12044 | diag::err_conflicting_overriding_cc_attributes) |
| 12045 | << New->getDeclName() << New->getType() << Old->getType(); |
| 12046 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12047 | return true; |
| Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 12048 | } |
| 12049 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12050 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
| Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12051 | const CXXMethodDecl *Old) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12052 | QualType NewTy = New->getType()->getAs<FunctionType>()->getReturnType(); |
| 12053 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getReturnType(); |
| Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12054 | |
| Chandler Carruth | 284bb2e | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 12055 | if (Context.hasSameType(NewTy, OldTy) || |
| 12056 | NewTy->isDependentType() || OldTy->isDependentType()) |
| Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12057 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12058 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12059 | // Check if the return types are covariant |
| 12060 | QualType NewClassTy, OldClassTy; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12061 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12062 | /// Both types must be pointers or references to classes. |
| Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12063 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 12064 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12065 | NewClassTy = NewPT->getPointeeType(); |
| 12066 | OldClassTy = OldPT->getPointeeType(); |
| 12067 | } |
| Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12068 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 12069 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 12070 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 12071 | NewClassTy = NewRT->getPointeeType(); |
| 12072 | OldClassTy = OldRT->getPointeeType(); |
| 12073 | } |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12074 | } |
| 12075 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12076 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12077 | // The return types aren't either both pointers or references to a class type. |
| 12078 | if (NewClassTy.isNull()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12079 | Diag(New->getLocation(), |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12080 | diag::err_different_return_type_for_overriding_virtual_function) |
| 12081 | << New->getDeclName() << NewTy << OldTy; |
| 12082 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12083 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12084 | return true; |
| 12085 | } |
| Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12086 | |
| Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12087 | // C++ [class.virtual]p6: |
| 12088 | // If the return type of D::f differs from the return type of B::f, the |
| 12089 | // class type in the return type of D::f shall be complete at the point of |
| 12090 | // declaration of D::f or shall be the class type D. |
| Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 12091 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 12092 | if (!RT->isBeingDefined() && |
| 12093 | RequireCompleteType(New->getLocation(), NewClassTy, |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 12094 | diag::err_covariant_return_incomplete, |
| 12095 | New->getDeclName())) |
| Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12096 | return true; |
| Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 12097 | } |
| Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12098 | |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 12099 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12100 | // Check if the new class derives from the old class. |
| 12101 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 12102 | Diag(New->getLocation(), |
| 12103 | diag::err_covariant_return_not_derived) |
| 12104 | << New->getDeclName() << NewTy << OldTy; |
| 12105 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12106 | return true; |
| 12107 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12108 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12109 | // Check if we the conversion from derived to base is valid. |
| John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 12110 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
| Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 12111 | diag::err_covariant_return_inaccessible_base, |
| 12112 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 12113 | // FIXME: Should this point to the return type? |
| 12114 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
| John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 12115 | // FIXME: this note won't trigger for delayed access control |
| 12116 | // diagnostics, and it's impossible to get an undelayed error |
| 12117 | // here from access control during the original parse because |
| 12118 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12119 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12120 | return true; |
| 12121 | } |
| 12122 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12123 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12124 | // The qualifiers of the return types must be the same. |
| Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12125 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12126 | Diag(New->getLocation(), |
| 12127 | diag::err_covariant_return_type_different_qualifications) |
| Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12128 | << New->getDeclName() << NewTy << OldTy; |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12129 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12130 | return true; |
| 12131 | }; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12132 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12133 | |
| 12134 | // The new class type must have the same or less qualifiers as the old type. |
| 12135 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 12136 | Diag(New->getLocation(), |
| 12137 | diag::err_covariant_return_type_class_type_more_qualified) |
| 12138 | << New->getDeclName() << NewTy << OldTy; |
| 12139 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12140 | return true; |
| 12141 | }; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12142 | |
| Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12143 | return false; |
| Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12144 | } |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12145 | |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12146 | /// \brief Mark the given method pure. |
| 12147 | /// |
| 12148 | /// \param Method the method to be marked pure. |
| 12149 | /// |
| 12150 | /// \param InitRange the source range that covers the "0" initializer. |
| 12151 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 12152 | SourceLocation EndLoc = InitRange.getEnd(); |
| 12153 | if (EndLoc.isValid()) |
| 12154 | Method->setRangeEnd(EndLoc); |
| 12155 | |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12156 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 12157 | Method->setPure(); |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12158 | return false; |
| Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 12159 | } |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12160 | |
| 12161 | if (!Method->isInvalidDecl()) |
| 12162 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 12163 | << Method->getDeclName() << InitRange; |
| 12164 | return true; |
| 12165 | } |
| 12166 | |
| Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12167 | /// \brief Determine whether the given declaration is a static data member. |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12168 | static bool isStaticDataMember(const Decl *D) { |
| 12169 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(D)) |
| 12170 | return Var->isStaticDataMember(); |
| 12171 | |
| 12172 | return false; |
| Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12173 | } |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12174 | |
| John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 12175 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 12176 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 12177 | /// is a fresh scope pushed for just this purpose. |
| 12178 | /// |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12179 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 12180 | /// static data member of class X, names should be looked up in the scope of |
| 12181 | /// class X. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12182 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12183 | // If there is no declaration, there was an error parsing it. |
| Argyrios Kyrtzidis | 8e4be0b | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 12184 | if (D == 0 || D->isInvalidDecl()) return; |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12185 | |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 12186 | // We will always have a nested name specifier here, but this declaration |
| 12187 | // might not be out of line if the specifier names the current namespace: |
| 12188 | // extern int n; |
| 12189 | // int ::n = 0; |
| 12190 | if (D->isOutOfLine()) |
| 12191 | EnterDeclaratorContext(S, D->getDeclContext()); |
| 12192 | |
| Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12193 | // If we are parsing the initializer for a static data member, push a |
| 12194 | // new expression evaluation context that is associated with this static |
| 12195 | // data member. |
| 12196 | if (isStaticDataMember(D)) |
| 12197 | PushExpressionEvaluationContext(PotentiallyEvaluated, D); |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12198 | } |
| 12199 | |
| 12200 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12201 | /// initializer for the out-of-line declaration 'D'. |
| 12202 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12203 | // If there is no declaration, there was an error parsing it. |
| Argyrios Kyrtzidis | 8e4be0b | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 12204 | if (D == 0 || D->isInvalidDecl()) return; |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12205 | |
| Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12206 | if (isStaticDataMember(D)) |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 12207 | PopExpressionEvaluationContext(); |
| Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12208 | |
| Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 12209 | if (D->isOutOfLine()) |
| 12210 | ExitDeclaratorContext(S); |
| Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12211 | } |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12212 | |
| 12213 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 12214 | /// C++ if/switch/while/for statement. |
| 12215 | /// e.g: "if (int x = f()) {...}" |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12216 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12217 | // C++ 6.4p2: |
| 12218 | // The declarator shall not specify a function or an array. |
| 12219 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 12220 | // new class or enumeration. |
| 12221 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 12222 | "Parser allowed 'typedef' as storage class of condition decl."); |
| Argyrios Kyrtzidis | 8ea7e58 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 12223 | |
| 12224 | Decl *Dcl = ActOnDeclarator(S, D); |
| Douglas Gregor | 1fe12c9 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 12225 | if (!Dcl) |
| 12226 | return true; |
| 12227 | |
| Argyrios Kyrtzidis | 8ea7e58 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 12228 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 12229 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12230 | << D.getSourceRange(); |
| Douglas Gregor | 1fe12c9 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 12231 | return true; |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12232 | } |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12233 | |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12234 | return Dcl; |
| 12235 | } |
| Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12236 | |
| Douglas Gregor | 4daf6a3 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 12237 | void Sema::LoadExternalVTableUses() { |
| 12238 | if (!ExternalSource) |
| 12239 | return; |
| 12240 | |
| 12241 | SmallVector<ExternalVTableUse, 4> VTables; |
| 12242 | ExternalSource->ReadUsedVTables(VTables); |
| 12243 | SmallVector<VTableUse, 4> NewUses; |
| 12244 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 12245 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 12246 | = VTablesUsed.find(VTables[I].Record); |
| 12247 | // Even if a definition wasn't required before, it may be required now. |
| 12248 | if (Pos != VTablesUsed.end()) { |
| 12249 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 12250 | Pos->second = true; |
| 12251 | continue; |
| 12252 | } |
| 12253 | |
| 12254 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 12255 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 12256 | } |
| 12257 | |
| 12258 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 12259 | } |
| 12260 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12261 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 12262 | bool DefinitionRequired) { |
| 12263 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 12264 | // not have a vtable. |
| 12265 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 12266 | CurContext->isDependentContext() || isUnevaluatedContext()) |
| Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 12267 | return; |
| 12268 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12269 | // Try to insert this class into the map. |
| Douglas Gregor | 4daf6a3 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 12270 | LoadExternalVTableUses(); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12271 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 12272 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 12273 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 12274 | if (!Pos.second) { |
| Daniel Dunbar | 5321776 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 12275 | // If we already had an entry, check to see if we are promoting this vtable |
| 12276 | // to required a definition. If so, we need to reappend to the VTableUses |
| 12277 | // list, since we may have already processed the first entry. |
| 12278 | if (DefinitionRequired && !Pos.first->second) { |
| 12279 | Pos.first->second = true; |
| 12280 | } else { |
| 12281 | // Otherwise, we can early exit. |
| 12282 | return; |
| 12283 | } |
| Hans Wennborg | 3d79154 | 2014-02-24 15:58:24 +0000 | [diff] [blame] | 12284 | } else { |
| 12285 | // The Microsoft ABI requires that we perform the destructor body |
| 12286 | // checks (i.e. operator delete() lookup) when the vtable is marked used, as |
| 12287 | // the deleting destructor is emitted with the vtable, not with the |
| 12288 | // destructor definition as in the Itanium ABI. |
| 12289 | // If it has a definition, we do the check at that point instead. |
| 12290 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 12291 | Class->hasUserDeclaredDestructor() && |
| 12292 | !Class->getDestructor()->isDefined() && |
| 12293 | !Class->getDestructor()->isDeleted()) { |
| 12294 | CheckDestructor(Class->getDestructor()); |
| 12295 | } |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12296 | } |
| 12297 | |
| 12298 | // Local classes need to have their virtual members marked |
| 12299 | // immediately. For all other classes, we mark their virtual members |
| 12300 | // at the end of the translation unit. |
| 12301 | if (Class->isLocalClass()) |
| 12302 | MarkVirtualMembersReferenced(Loc, Class); |
| Daniel Dunbar | 0547ad3 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 12303 | else |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12304 | VTableUses.push_back(std::make_pair(Class, Loc)); |
| Douglas Gregor | 0c4aad1 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 12305 | } |
| 12306 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12307 | bool Sema::DefineUsedVTables() { |
| Douglas Gregor | 4daf6a3 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 12308 | LoadExternalVTableUses(); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12309 | if (VTableUses.empty()) |
| Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12310 | return false; |
| Chandler Carruth | 88bfa5e | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 12311 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12312 | // Note: The VTableUses vector could grow as a result of marking |
| 12313 | // the members of a class as "used", so we check the size each |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12314 | // time through the loop and prefer indices (which are stable) to |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12315 | // iterators (which are not). |
| Douglas Gregor | 9750969 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 12316 | bool DefinedAnything = false; |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12317 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
| Daniel Dunbar | 105ce6d | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 12318 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12319 | if (!Class) |
| 12320 | continue; |
| 12321 | |
| 12322 | SourceLocation Loc = VTableUses[I].second; |
| 12323 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12324 | bool DefineVTable = true; |
| 12325 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12326 | // If this class has a key function, but that key function is |
| 12327 | // defined in another translation unit, we don't need to emit the |
| 12328 | // vtable even though we're using it. |
| John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 12329 | const CXXMethodDecl *KeyFunction = Context.getCurrentKeyFunction(Class); |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 12330 | if (KeyFunction && !KeyFunction->hasBody()) { |
| Rafael Espindola | ef7fe1f | 2013-08-26 23:23:21 +0000 | [diff] [blame] | 12331 | // The key function is in another translation unit. |
| 12332 | DefineVTable = false; |
| 12333 | TemplateSpecializationKind TSK = |
| 12334 | KeyFunction->getTemplateSpecializationKind(); |
| 12335 | assert(TSK != TSK_ExplicitInstantiationDefinition && |
| 12336 | TSK != TSK_ImplicitInstantiation && |
| 12337 | "Instantiations don't have key functions"); |
| 12338 | (void)TSK; |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12339 | } else if (!KeyFunction) { |
| 12340 | // If we have a class with no key function that is the subject |
| 12341 | // of an explicit instantiation declaration, suppress the |
| 12342 | // vtable; it will live with the explicit instantiation |
| 12343 | // definition. |
| 12344 | bool IsExplicitInstantiationDeclaration |
| 12345 | = Class->getTemplateSpecializationKind() |
| 12346 | == TSK_ExplicitInstantiationDeclaration; |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 12347 | for (auto R : Class->redecls()) { |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12348 | TemplateSpecializationKind TSK |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 12349 | = cast<CXXRecordDecl>(R)->getTemplateSpecializationKind(); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12350 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 12351 | IsExplicitInstantiationDeclaration = true; |
| 12352 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 12353 | IsExplicitInstantiationDeclaration = false; |
| 12354 | break; |
| 12355 | } |
| 12356 | } |
| 12357 | |
| 12358 | if (IsExplicitInstantiationDeclaration) |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12359 | DefineVTable = false; |
| 12360 | } |
| 12361 | |
| 12362 | // The exception specifications for all virtual members may be needed even |
| 12363 | // if we are not providing an authoritative form of the vtable in this TU. |
| 12364 | // We may choose to emit it available_externally anyway. |
| 12365 | if (!DefineVTable) { |
| 12366 | MarkVirtualMemberExceptionSpecsNeeded(Loc, Class); |
| 12367 | continue; |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12368 | } |
| 12369 | |
| 12370 | // Mark all of the virtual members of this class as referenced, so |
| 12371 | // that we can build a vtable. Then, tell the AST consumer that a |
| 12372 | // vtable for this class is required. |
| Douglas Gregor | 9750969 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 12373 | DefinedAnything = true; |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12374 | MarkVirtualMembersReferenced(Loc, Class); |
| 12375 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 12376 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 12377 | |
| 12378 | // Optionally warn if we're emitting a weak vtable. |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 12379 | if (Class->isExternallyVisible() && |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12380 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
| Douglas Gregor | 34bc6e5 | 2011-09-23 19:04:03 +0000 | [diff] [blame] | 12381 | const FunctionDecl *KeyFunctionDef = 0; |
| 12382 | if (!KeyFunction || |
| 12383 | (KeyFunction->hasBody(KeyFunctionDef) && |
| 12384 | KeyFunctionDef->isInlined())) |
| David Blaikie | 72b6120 | 2011-12-09 18:32:50 +0000 | [diff] [blame] | 12385 | Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == |
| 12386 | TSK_ExplicitInstantiationDefinition |
| 12387 | ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) |
| 12388 | << Class; |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12389 | } |
| Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12390 | } |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12391 | VTableUses.clear(); |
| 12392 | |
| Douglas Gregor | 9750969 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 12393 | return DefinedAnything; |
| Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12394 | } |
| Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12395 | |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12396 | void Sema::MarkVirtualMemberExceptionSpecsNeeded(SourceLocation Loc, |
| 12397 | const CXXRecordDecl *RD) { |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 12398 | for (const auto *I : RD->methods()) |
| 12399 | if (I->isVirtual() && !I->isPure()) |
| 12400 | ResolveExceptionSpec(Loc, I->getType()->castAs<FunctionProtoType>()); |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12401 | } |
| 12402 | |
| Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12403 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 12404 | const CXXRecordDecl *RD) { |
| Richard Smith | 4ff9ff9 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 12405 | // Mark all functions which will appear in RD's vtable as used. |
| 12406 | CXXFinalOverriderMap FinalOverriders; |
| 12407 | RD->getFinalOverriders(FinalOverriders); |
| 12408 | for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), |
| 12409 | E = FinalOverriders.end(); |
| 12410 | I != E; ++I) { |
| 12411 | for (OverridingMethods::const_iterator OI = I->second.begin(), |
| 12412 | OE = I->second.end(); |
| 12413 | OI != OE; ++OI) { |
| 12414 | assert(OI->second.size() > 0 && "no final overrider"); |
| 12415 | CXXMethodDecl *Overrider = OI->second.front().Method; |
| Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12416 | |
| Richard Smith | 4ff9ff9 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 12417 | // C++ [basic.def.odr]p2: |
| 12418 | // [...] A virtual member function is used if it is not pure. [...] |
| 12419 | if (!Overrider->isPure()) |
| 12420 | MarkFunctionReferenced(Loc, Overrider); |
| 12421 | } |
| Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12422 | } |
| Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12423 | |
| 12424 | // Only classes that have virtual bases need a VTT. |
| 12425 | if (RD->getNumVBases() == 0) |
| 12426 | return; |
| 12427 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 12428 | for (const auto &I : RD->bases()) { |
| Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12429 | const CXXRecordDecl *Base = |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 12430 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
| Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12431 | if (Base->getNumVBases() == 0) |
| 12432 | continue; |
| 12433 | MarkVirtualMembersReferenced(Loc, Base); |
| 12434 | } |
| Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12435 | } |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12436 | |
| 12437 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 12438 | /// Objective-C implementation whose ivars need be initialized. |
| 12439 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12440 | if (!getLangOpts().CPlusPlus) |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12441 | return; |
| Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 12442 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12443 | SmallVector<ObjCIvarDecl*, 8> ivars; |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12444 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 12445 | if (ivars.empty()) |
| 12446 | return; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12447 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12448 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 12449 | FieldDecl *Field = ivars[i]; |
| Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12450 | if (Field->isInvalidDecl()) |
| 12451 | continue; |
| 12452 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 12453 | CXXCtorInitializer *Member; |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12454 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 12455 | InitializationKind InitKind = |
| 12456 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 12457 | |
| 12458 | InitializationSequence InitSeq(*this, InitEntity, InitKind, None); |
| 12459 | ExprResult MemberInit = |
| 12460 | InitSeq.Perform(*this, InitEntity, InitKind, None); |
| Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 12461 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12462 | // Note, MemberInit could actually come back empty if no initialization |
| 12463 | // is required (e.g., because it would call a trivial default constructor) |
| 12464 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 12465 | continue; |
| John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 12466 | |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12467 | Member = |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 12468 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 12469 | SourceLocation(), |
| 12470 | MemberInit.takeAs<Expr>(), |
| 12471 | SourceLocation()); |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12472 | AllToInit.push_back(Member); |
| Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12473 | |
| 12474 | // Be sure that the destructor is accessible and is marked as referenced. |
| 12475 | if (const RecordType *RecordTy |
| 12476 | = Context.getBaseElementType(Field->getType()) |
| 12477 | ->getAs<RecordType>()) { |
| 12478 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 12479 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
| Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 12480 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
| Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12481 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 12482 | PDiag(diag::err_access_dtor_ivar) |
| 12483 | << Context.getBaseElementType(Field->getType())); |
| 12484 | } |
| 12485 | } |
| Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12486 | } |
| 12487 | ObjCImplementation->setIvarInitializers(Context, |
| 12488 | AllToInit.data(), AllToInit.size()); |
| 12489 | } |
| 12490 | } |
| Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12491 | |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12492 | static |
| 12493 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 12494 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 12495 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 12496 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 12497 | Sema &S) { |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12498 | if (Ctor->isInvalidDecl()) |
| 12499 | return; |
| 12500 | |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12501 | CXXConstructorDecl *Target = Ctor->getTargetConstructor(); |
| 12502 | |
| 12503 | // Target may not be determinable yet, for instance if this is a dependent |
| 12504 | // call in an uninstantiated template. |
| 12505 | if (Target) { |
| 12506 | const FunctionDecl *FNTarget = 0; |
| 12507 | (void)Target->hasBody(FNTarget); |
| 12508 | Target = const_cast<CXXConstructorDecl*>( |
| 12509 | cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 12510 | } |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12511 | |
| 12512 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 12513 | // Avoid dereferencing a null pointer here. |
| 12514 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 12515 | |
| 12516 | if (!Current.insert(Canonical)) |
| 12517 | return; |
| 12518 | |
| 12519 | // We know that beyond here, we aren't chaining into a cycle. |
| 12520 | if (!Target || !Target->isDelegatingConstructor() || |
| 12521 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12522 | Valid.insert(Current.begin(), Current.end()); |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12523 | Current.clear(); |
| 12524 | // We've hit a cycle. |
| 12525 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 12526 | Current.count(TCanonical)) { |
| 12527 | // If we haven't diagnosed this cycle yet, do so now. |
| 12528 | if (!Invalid.count(TCanonical)) { |
| 12529 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
| Alexis Hunt | e262299 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 12530 | diag::warn_delegating_ctor_cycle) |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12531 | << Ctor; |
| 12532 | |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12533 | // Don't add a note for a function delegating directly to itself. |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12534 | if (TCanonical != Canonical) |
| 12535 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 12536 | |
| 12537 | CXXConstructorDecl *C = Target; |
| 12538 | while (C->getCanonicalDecl() != Canonical) { |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12539 | const FunctionDecl *FNTarget = 0; |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12540 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 12541 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 12542 | |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12543 | C = const_cast<CXXConstructorDecl*>( |
| 12544 | cast<CXXConstructorDecl>(FNTarget)); |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12545 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 12546 | } |
| 12547 | } |
| 12548 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12549 | Invalid.insert(Current.begin(), Current.end()); |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12550 | Current.clear(); |
| 12551 | } else { |
| 12552 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 12553 | } |
| 12554 | } |
| 12555 | |
| 12556 | |
| Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12557 | void Sema::CheckDelegatingCtorCycles() { |
| 12558 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 12559 | |
| Douglas Gregor | bae3120 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 12560 | for (DelegatingCtorDeclsType::iterator |
| 12561 | I = DelegatingCtorDecls.begin(ExternalSource), |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12562 | E = DelegatingCtorDecls.end(); |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12563 | I != E; ++I) |
| 12564 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12565 | |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12566 | for (llvm::SmallSet<CXXConstructorDecl *, 4>::iterator CI = Invalid.begin(), |
| 12567 | CE = Invalid.end(); |
| 12568 | CI != CE; ++CI) |
| Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12569 | (*CI)->setInvalidDecl(); |
| Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12570 | } |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12571 | |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12572 | namespace { |
| 12573 | /// \brief AST visitor that finds references to the 'this' expression. |
| 12574 | class FindCXXThisExpr : public RecursiveASTVisitor<FindCXXThisExpr> { |
| 12575 | Sema &S; |
| 12576 | |
| 12577 | public: |
| 12578 | explicit FindCXXThisExpr(Sema &S) : S(S) { } |
| 12579 | |
| 12580 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 12581 | S.Diag(E->getLocation(), diag::err_this_static_member_func) |
| 12582 | << E->isImplicit(); |
| 12583 | return false; |
| 12584 | } |
| 12585 | }; |
| 12586 | } |
| 12587 | |
| 12588 | bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { |
| 12589 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 12590 | if (!TSInfo) |
| 12591 | return false; |
| 12592 | |
| 12593 | TypeLoc TL = TSInfo->getTypeLoc(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12594 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12595 | if (!ProtoTL) |
| 12596 | return false; |
| 12597 | |
| 12598 | // C++11 [expr.prim.general]p3: |
| 12599 | // [The expression this] shall not appear before the optional |
| 12600 | // cv-qualifier-seq and it shall not appear within the declaration of a |
| 12601 | // static member function (although its type and value category are defined |
| 12602 | // within a static member function as they are within a non-static member |
| 12603 | // function). [ Note: this is because declaration matching does not occur |
| NAKAMURA Takumi | 40edb2a | 2012-04-21 09:40:04 +0000 | [diff] [blame] | 12604 | // until the complete declarator is known. - end note ] |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12605 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12606 | FindCXXThisExpr Finder(*this); |
| 12607 | |
| 12608 | // If the return type came after the cv-qualifier-seq, check it now. |
| 12609 | if (Proto->hasTrailingReturn() && |
| Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 12610 | !Finder.TraverseTypeLoc(ProtoTL.getReturnLoc())) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12611 | return true; |
| 12612 | |
| 12613 | // Check the exception specification. |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12614 | if (checkThisInStaticMemberFunctionExceptionSpec(Method)) |
| 12615 | return true; |
| 12616 | |
| 12617 | return checkThisInStaticMemberFunctionAttributes(Method); |
| 12618 | } |
| 12619 | |
| 12620 | bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { |
| 12621 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 12622 | if (!TSInfo) |
| 12623 | return false; |
| 12624 | |
| 12625 | TypeLoc TL = TSInfo->getTypeLoc(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12626 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12627 | if (!ProtoTL) |
| 12628 | return false; |
| 12629 | |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12630 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12631 | FindCXXThisExpr Finder(*this); |
| 12632 | |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12633 | switch (Proto->getExceptionSpecType()) { |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 12634 | case EST_Uninstantiated: |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12635 | case EST_Unevaluated: |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12636 | case EST_BasicNoexcept: |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12637 | case EST_DynamicNone: |
| 12638 | case EST_MSAny: |
| 12639 | case EST_None: |
| 12640 | break; |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12641 | |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12642 | case EST_ComputedNoexcept: |
| 12643 | if (!Finder.TraverseStmt(Proto->getNoexceptExpr())) |
| 12644 | return true; |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12645 | |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12646 | case EST_Dynamic: |
| Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 12647 | for (const auto &E : Proto->exceptions()) { |
| 12648 | if (!Finder.TraverseType(E)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12649 | return true; |
| 12650 | } |
| 12651 | break; |
| 12652 | } |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12653 | |
| 12654 | return false; |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12655 | } |
| 12656 | |
| 12657 | bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) { |
| 12658 | FindCXXThisExpr Finder(*this); |
| 12659 | |
| 12660 | // Check attributes. |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12661 | for (const auto *A : Method->attrs()) { |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12662 | // FIXME: This should be emitted by tblgen. |
| 12663 | Expr *Arg = 0; |
| 12664 | ArrayRef<Expr *> Args; |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12665 | if (const auto *G = dyn_cast<GuardedByAttr>(A)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12666 | Arg = G->getArg(); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12667 | else if (const auto *G = dyn_cast<PtGuardedByAttr>(A)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12668 | Arg = G->getArg(); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12669 | else if (const auto *AA = dyn_cast<AcquiredAfterAttr>(A)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12670 | Args = ArrayRef<Expr *>(AA->args_begin(), AA->args_size()); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12671 | else if (const auto *AB = dyn_cast<AcquiredBeforeAttr>(A)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12672 | Args = ArrayRef<Expr *>(AB->args_begin(), AB->args_size()); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12673 | else if (const auto *ETLF = dyn_cast<ExclusiveTrylockFunctionAttr>(A)) { |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12674 | Arg = ETLF->getSuccessValue(); |
| 12675 | Args = ArrayRef<Expr *>(ETLF->args_begin(), ETLF->args_size()); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12676 | } else if (const auto *STLF = dyn_cast<SharedTrylockFunctionAttr>(A)) { |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12677 | Arg = STLF->getSuccessValue(); |
| 12678 | Args = ArrayRef<Expr *>(STLF->args_begin(), STLF->args_size()); |
| Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 12679 | } else if (const auto *LR = dyn_cast<LockReturnedAttr>(A)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12680 | Arg = LR->getArg(); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12681 | else if (const auto *LE = dyn_cast<LocksExcludedAttr>(A)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12682 | Args = ArrayRef<Expr *>(LE->args_begin(), LE->args_size()); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12683 | else if (const auto *RC = dyn_cast<RequiresCapabilityAttr>(A)) |
| Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 12684 | Args = ArrayRef<Expr *>(RC->args_begin(), RC->args_size()); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12685 | else if (const auto *AC = dyn_cast<AcquireCapabilityAttr>(A)) |
| Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 12686 | Args = ArrayRef<Expr *>(AC->args_begin(), AC->args_size()); |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 12687 | else if (const auto *AC = dyn_cast<TryAcquireCapabilityAttr>(A)) |
| 12688 | Args = ArrayRef<Expr *>(AC->args_begin(), AC->args_size()); |
| 12689 | else if (const auto *RC = dyn_cast<ReleaseCapabilityAttr>(A)) |
| Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 12690 | Args = ArrayRef<Expr *>(RC->args_begin(), RC->args_size()); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12691 | |
| 12692 | if (Arg && !Finder.TraverseStmt(Arg)) |
| 12693 | return true; |
| 12694 | |
| 12695 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 12696 | if (!Finder.TraverseStmt(Args[I])) |
| 12697 | return true; |
| 12698 | } |
| 12699 | } |
| 12700 | |
| 12701 | return false; |
| 12702 | } |
| 12703 | |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12704 | void |
| 12705 | Sema::checkExceptionSpecification(ExceptionSpecificationType EST, |
| 12706 | ArrayRef<ParsedType> DynamicExceptions, |
| 12707 | ArrayRef<SourceRange> DynamicExceptionRanges, |
| 12708 | Expr *NoexceptExpr, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12709 | SmallVectorImpl<QualType> &Exceptions, |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12710 | FunctionProtoType::ExtProtoInfo &EPI) { |
| 12711 | Exceptions.clear(); |
| 12712 | EPI.ExceptionSpecType = EST; |
| 12713 | if (EST == EST_Dynamic) { |
| 12714 | Exceptions.reserve(DynamicExceptions.size()); |
| 12715 | for (unsigned ei = 0, ee = DynamicExceptions.size(); ei != ee; ++ei) { |
| 12716 | // FIXME: Preserve type source info. |
| 12717 | QualType ET = GetTypeFromParser(DynamicExceptions[ei]); |
| 12718 | |
| 12719 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 12720 | collectUnexpandedParameterPacks(ET, Unexpanded); |
| 12721 | if (!Unexpanded.empty()) { |
| 12722 | DiagnoseUnexpandedParameterPacks(DynamicExceptionRanges[ei].getBegin(), |
| 12723 | UPPC_ExceptionType, |
| 12724 | Unexpanded); |
| 12725 | continue; |
| 12726 | } |
| 12727 | |
| 12728 | // Check that the type is valid for an exception spec, and |
| 12729 | // drop it if not. |
| 12730 | if (!CheckSpecifiedExceptionType(ET, DynamicExceptionRanges[ei])) |
| 12731 | Exceptions.push_back(ET); |
| 12732 | } |
| 12733 | EPI.NumExceptions = Exceptions.size(); |
| 12734 | EPI.Exceptions = Exceptions.data(); |
| 12735 | return; |
| 12736 | } |
| 12737 | |
| 12738 | if (EST == EST_ComputedNoexcept) { |
| 12739 | // If an error occurred, there's no expression here. |
| 12740 | if (NoexceptExpr) { |
| 12741 | assert((NoexceptExpr->isTypeDependent() || |
| 12742 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 12743 | Context.BoolTy) && |
| 12744 | "Parser should have made sure that the expression is boolean"); |
| 12745 | if (NoexceptExpr && DiagnoseUnexpandedParameterPack(NoexceptExpr)) { |
| 12746 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
| 12747 | return; |
| 12748 | } |
| 12749 | |
| 12750 | if (!NoexceptExpr->isValueDependent()) |
| 12751 | NoexceptExpr = VerifyIntegerConstantExpression(NoexceptExpr, 0, |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 12752 | diag::err_noexcept_needs_constant_expression, |
| Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12753 | /*AllowFold*/ false).take(); |
| 12754 | EPI.NoexceptExpr = NoexceptExpr; |
| 12755 | } |
| 12756 | return; |
| 12757 | } |
| 12758 | } |
| 12759 | |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12760 | /// IdentifyCUDATarget - Determine the CUDA compilation target for this function |
| 12761 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D) { |
| 12762 | // Implicitly declared functions (e.g. copy constructors) are |
| 12763 | // __host__ __device__ |
| 12764 | if (D->isImplicit()) |
| 12765 | return CFT_HostDevice; |
| 12766 | |
| 12767 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 12768 | return CFT_Global; |
| 12769 | |
| 12770 | if (D->hasAttr<CUDADeviceAttr>()) { |
| 12771 | if (D->hasAttr<CUDAHostAttr>()) |
| 12772 | return CFT_HostDevice; |
| Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12773 | return CFT_Device; |
| Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12774 | } |
| 12775 | |
| 12776 | return CFT_Host; |
| 12777 | } |
| 12778 | |
| 12779 | bool Sema::CheckCUDATarget(CUDAFunctionTarget CallerTarget, |
| 12780 | CUDAFunctionTarget CalleeTarget) { |
| 12781 | // CUDA B.1.1 "The __device__ qualifier declares a function that is... |
| 12782 | // Callable from the device only." |
| 12783 | if (CallerTarget == CFT_Host && CalleeTarget == CFT_Device) |
| 12784 | return true; |
| 12785 | |
| 12786 | // CUDA B.1.2 "The __global__ qualifier declares a function that is... |
| 12787 | // Callable from the host only." |
| 12788 | // CUDA B.1.3 "The __host__ qualifier declares a function that is... |
| 12789 | // Callable from the host only." |
| 12790 | if ((CallerTarget == CFT_Device || CallerTarget == CFT_Global) && |
| 12791 | (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)) |
| 12792 | return true; |
| 12793 | |
| 12794 | if (CallerTarget == CFT_HostDevice && CalleeTarget != CFT_HostDevice) |
| 12795 | return true; |
| 12796 | |
| 12797 | return false; |
| 12798 | } |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 12799 | |
| 12800 | /// HandleMSProperty - Analyze a __delcspec(property) field of a C++ class. |
| 12801 | /// |
| 12802 | MSPropertyDecl *Sema::HandleMSProperty(Scope *S, RecordDecl *Record, |
| 12803 | SourceLocation DeclStart, |
| 12804 | Declarator &D, Expr *BitWidth, |
| 12805 | InClassInitStyle InitStyle, |
| 12806 | AccessSpecifier AS, |
| 12807 | AttributeList *MSPropertyAttr) { |
| 12808 | IdentifierInfo *II = D.getIdentifier(); |
| 12809 | if (!II) { |
| 12810 | Diag(DeclStart, diag::err_anonymous_property); |
| 12811 | return NULL; |
| 12812 | } |
| 12813 | SourceLocation Loc = D.getIdentifierLoc(); |
| 12814 | |
| 12815 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 12816 | QualType T = TInfo->getType(); |
| 12817 | if (getLangOpts().CPlusPlus) { |
| 12818 | CheckExtraCXXDefaultArguments(D); |
| 12819 | |
| 12820 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 12821 | UPPC_DataMemberType)) { |
| 12822 | D.setInvalidType(); |
| 12823 | T = Context.IntTy; |
| 12824 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 12825 | } |
| 12826 | } |
| 12827 | |
| 12828 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| 12829 | |
| 12830 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 12831 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 12832 | diag::err_invalid_thread) |
| 12833 | << DeclSpec::getSpecifierName(TSCS); |
| 12834 | |
| 12835 | // Check to see if this name was declared as a member previously |
| 12836 | NamedDecl *PrevDecl = 0; |
| 12837 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
| 12838 | LookupName(Previous, S); |
| 12839 | switch (Previous.getResultKind()) { |
| 12840 | case LookupResult::Found: |
| 12841 | case LookupResult::FoundUnresolvedValue: |
| 12842 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 12843 | break; |
| 12844 | |
| 12845 | case LookupResult::FoundOverloaded: |
| 12846 | PrevDecl = Previous.getRepresentativeDecl(); |
| 12847 | break; |
| 12848 | |
| 12849 | case LookupResult::NotFound: |
| 12850 | case LookupResult::NotFoundInCurrentInstantiation: |
| 12851 | case LookupResult::Ambiguous: |
| 12852 | break; |
| 12853 | } |
| 12854 | |
| 12855 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 12856 | // Maybe we will complain about the shadowed template parameter. |
| 12857 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 12858 | // Just pretend that we didn't see the previous declaration. |
| 12859 | PrevDecl = 0; |
| 12860 | } |
| 12861 | |
| 12862 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
| 12863 | PrevDecl = 0; |
| 12864 | |
| 12865 | SourceLocation TSSL = D.getLocStart(); |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 12866 | const AttributeList::PropertyData &Data = MSPropertyAttr->getPropertyData(); |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 12867 | MSPropertyDecl *NewPD = MSPropertyDecl::Create( |
| 12868 | Context, Record, Loc, II, T, TInfo, TSSL, Data.GetterId, Data.SetterId); |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 12869 | ProcessDeclAttributes(TUScope, NewPD, D); |
| 12870 | NewPD->setAccess(AS); |
| 12871 | |
| 12872 | if (NewPD->isInvalidDecl()) |
| 12873 | Record->setInvalidDecl(); |
| 12874 | |
| 12875 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 12876 | NewPD->setModulePrivate(); |
| 12877 | |
| 12878 | if (NewPD->isInvalidDecl() && PrevDecl) { |
| 12879 | // Don't introduce NewFD into scope; there's already something |
| 12880 | // with the same name in the same scope. |
| 12881 | } else if (II) { |
| 12882 | PushOnScopeChains(NewPD, S); |
| 12883 | } else |
| 12884 | Record->addDecl(NewPD); |
| 12885 | |
| 12886 | return NewPD; |
| 12887 | } |